ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
dvector.h
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  *
00007  * ADModelbuilder and associated libraries and documentations are
00008  * provided under the general terms of the "New BSD" license
00009  *
00010  * License:
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions are
00014  * met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright
00017  * notice, this list of conditions and the following disclaimer.
00018  *
00019  * 2.  Redistributions in binary form must reproduce the above copyright
00020  * notice, this list of conditions and the following disclaimer in the
00021  * documentation and/or other materials provided with the distribution.
00022  *
00023  * 3.  Neither the name of the  University of California, Otter Research,
00024  * nor the ADMB Foundation nor the names of its contributors may be used
00025  * to endorse or promote products derived from this software without
00026  * specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00032  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 
00041 #ifndef __ADMB_DVECTOR_H__
00042 #define __ADMB_DVECTOR_H__
00043 
00050 class dvector
00051 {
00052  protected:
00053    double *v; 
00054    int index_min; 
00055    int index_max; 
00056 
00057 #if defined(THREAD_SAFE)
00058    ts_vector_shapex *shape;
00059 #else
00060    vector_shapex *shape;
00061 #endif
00062 
00063  public:
00064    dvector operator -();
00066    int operator!(void) const
00067    {
00068       return (shape == NULL);
00069    }
00071    int allocated(void) const
00072    {
00073       return (shape != NULL);
00074    }
00076    dvector & operator --(void)
00077    {
00078       index_min--;
00079       index_max--;
00080       v++;
00081       return *this;
00082    }
00084    dvector & operator ++(void)
00085    {
00086       index_min++;
00087       index_max++;
00088       v--;
00089       return *this;
00090    }
00091    void reallocate(double size);
00098    dvector sub(int lb, int ub)
00099    {
00100       return predvector(this, lb, ub);
00101    }
00108    dvector operator () (int lb, int ub)
00109    {
00110       return predvector(this, lb, ub);
00111    }
00112 
00113    dvector(const predvector & pd);
00114 
00115    void fill_randpoisson(double lambda, const random_number_generator & rng);
00116    void fill_randnegbinomial(double lambda, double tau,
00117      const random_number_generator & rng);
00118    void shallow_copy(const dvector &);
00119 
00120    friend class banded_symmetric_dvar_matrix;
00121    friend class banded_lower_triangular_dvar_matrix;
00122    friend class banded_symmetric_dmatrix;
00123    friend class banded_lower_triangular_dmatrix;
00124 
00125    void allocate(int ncl, int ncu);
00126    void allocate(const dvector & dv);
00127    void allocatec(const dvector & dv);
00128    void allocate(const dvar_vector &);
00129    void allocate(const char *);
00130    void allocate(void);
00131    void deallocate(void);
00132    void safe_deallocate(void);
00133    void safe_allocate(int, int);
00134 
00135    const double *address() const
00136    {
00137       return v;
00138    }
00139    double *&get_v(void)
00140    {
00141       return (v);
00142    }
00143    double &elem(int i)
00144    {
00145       return (v[i]);
00146    }
00147    double *initpointer(void)
00148    {
00149       return (v + indexmin());
00150    }
00151    const double *initpointer(void) const
00152    {
00153       return (v + indexmin());
00154    }
00155    double *get_v(void) const
00156    {
00157       return v;
00158    }
00159    const double &elem(int i) const
00160    {
00161       return v[i];
00162    }
00163 
00164    void fill(const char *s);
00165    void fill_randu_ni(long int &n);
00166    void fill_randn_ni(long int &n);
00167    void fill_randbi_ni(long int &n, double);
00168 
00169    void fill_randu(long int &n);
00170    void fill_randn(long int &n);
00171    void fill_randbi(long int &n, double);
00172 
00173 
00174    void fill_randbi(double p, const random_number_generator & rng);
00175    void fill_randu(const random_number_generator & rng);
00176    void fill_randn(const random_number_generator & rng);
00177    void fill_randcau(const random_number_generator & rng);
00178 
00179    void fill_seqadd(double, double);
00180    void fill_multinomial(const int &seed, const dvector & p);
00181    void fill_multinomial(const random_number_generator& rng, const dvector& p);
00182    void initialize(void);
00183 
00184    // returns the minimum allowable index
00185    int &testmin()
00186    {
00187       return shape->index_min;
00188    }
00189 
00190    // returns the minimum allowable index
00191    int &testmax()
00192    {
00193       return shape->index_max;
00194    }
00195 
00196    unsigned int get_ncopies() const
00197    {
00198      return shape ? shape->get_ncopies() : 0;
00199    }
00200 
00202    int indexmin() const
00203    {
00204       return index_min;
00205    }
00206 
00208    int indexmax() const
00209    {
00210       return index_max;
00211    }
00212 
00214    int size() const
00215    {
00216       return (index_max - index_min + 1);
00217    }
00218 
00219    dvector & shift(int min);
00220 
00221    dvector(const dvar_vector_position & dvp, const kkludge_object &);
00222 
00223    dvector(const ad_integer &, const index_type &);
00224    void allocate(const ad_integer &, const index_type &);
00225    dvector(void);
00226    dvector(const dvector &);
00227    //dvector(const dvector&,int lb,int ub);
00228 
00229    dvector(const ivector &);
00230 
00231    dvector(const lvector &);
00232 
00233    dvector(const char *);
00234 
00235    dvector(int ncl, int ncu);
00236    // makes an array [ncl..ncu]
00237 
00238    dvector(unsigned int sz, double *x);
00239 
00240    dvector(char *filename, const int &column);
00241 
00242    //operator double* () { return v;}
00243 
00244    ~dvector();
00245 
00246    void save_dvector_position(void) const;
00247    void save_dvector_derivatives(const dvar_vector_position & pos) const;
00248    void save_dvector_derivatives_na(const dvar_vector_position & pos)
00249       const;
00250    void save_dvector_derivatives(void) const;
00251    void save_dvector_value(void) const;
00252 
00253    //dvector operator()(int,int);
00254    dvector operator() (const lvector &);
00255    dvector operator() (const ivector & u);
00256 
00257    dvector & operator+=(const dvector & v1);
00258    dvector & operator-=(const dvector & v1);
00259    dvector & operator +=(double v1);
00260    dvector & operator /=(double v1);
00261    dvector & operator *=(double v1);
00262    dvector & operator -=(double v1);
00263 
00264    void read_from(const uistream &);
00265 
00266    friend class sdmatrix;
00267    friend double norm(const dvector &);
00268    friend double norm2(const dvector &);
00269    friend double sumsq(const dvector &);
00270    friend class dvar_vector;
00271    friend class dmatrix;
00272    friend class d3_array;
00273    friend char *fform(const char *, const dvector &);
00274 
00275    void write_on(const ostream &) const;
00276    void write_on(const uostream &) const;
00277    void read_from(const istream &);
00278    friend double operator*(const dvector &, const dvector &);
00279 
00280    friend dvariable operator*(const dvector &, const dvar_vector &);
00281 
00282    friend dvariable operator*(const dvar_vector &, const dvector &);
00283 
00284    friend dvar_vector operator*(const prevariable &, const dvector &);
00285 
00286    friend dvector operator*(double, const dvector &);
00287 
00288    friend dvector operator+(const dvector &, const dvector &);
00289 
00290    friend dvector elem_prod(const dvector &, const dvector &);
00291 
00292    friend dvector first_difference(const dvector &);
00293    friend dvector second_difference(const dvector &);
00294 
00295    friend dvector elem_div(const dvector &, const dvector &);
00296 
00297    friend dvar_vector elem_prod(const dvector &, const dvar_vector &);
00298 
00299    friend dvar_vector elem_div(const dvector &, const dvar_vector &);
00300 
00301    friend dvar_vector elem_prod(const dvar_vector &, const dvector &);
00302 
00303    friend dvar_vector elem_div(const dvar_vector &, const dvector &);
00304 
00305    friend dvar_vector operator+(const dvar_vector &, const dvector &);
00306 
00307    friend dvar_vector operator+(const dvector &, const dvar_vector &);
00308 
00309    friend dvector operator-(const dvector &, const dvector &);
00310 
00311    friend dvar_vector operator-(const dvar_vector &, const dvector &);
00312 
00313    friend dvar_vector operator-(const dvector &, const dvar_vector &);
00314 
00315    friend dvector operator*(const dvector & x, const dmatrix & m);
00316 
00317    friend dvector operator*(const dmatrix & x, const dvector & m);
00318 
00319    friend dvar_vector operator*(const dvector & x, const dvar_matrix & m);
00320 
00321    friend dvar_vector operator*(const dvar_matrix & x, const dvector & m);
00322 
00323    double& operator[](int i);
00324    double& operator()(int i);
00325    const double& operator[](int i) const;
00326    const double& operator()(int i) const;
00327 
00328    dvector & operator=(const dvector & t);
00329 
00330    dvector & operator =(double x);
00331 
00332   bool is_valid_index(const int i) const;
00333 
00334    friend dvector exp(const dvector &);
00335 
00336    friend dvector log(const dvector &);
00337 
00338    friend dvector fabs(const dvector &);
00339 
00340    friend double max(const dvector &);
00341 
00342    friend double min(const dvector &);
00343 };
00344 
00345 #ifdef OPT_LIB
00346 inline double& dvector::operator[](int i)
00347 {
00348   return *(v + i);
00349 }
00350 inline double& dvector::operator()(int i)
00351 {
00352   return *(v + i);
00353 }
00354 inline const double& dvector::operator[](int i) const
00355 {
00356   return *(v + i);
00357 }
00358 inline const double& dvector::operator()(int i) const
00359 {
00360   return *(v + i);
00361 }
00362 #endif
00363 
00364 #endif