ADMB Documentation  11.1x.2711
 All Classes Files Functions Variables Typedefs Friends Defines
dvector.h
Go to the documentation of this file.
00001 /*
00002  * $Id: dvector.h 1973 2014-05-02 21:07:36Z johnoel $
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 
00197    int indexmin() const
00198    {
00199       return index_min;
00200    }
00201 
00203    int indexmax() const
00204    {
00205       return index_max;
00206    }
00207 
00209    int size() const
00210    {
00211       return (index_max - index_min + 1);
00212    }
00213 
00214    dvector & shift(int min);
00215 
00216    dvector(const dvar_vector_position & dvp, const kkludge_object &);
00217 
00218    dvector(const ad_integer &, const index_type &);
00219    void allocate(const ad_integer &, const index_type &);
00220    dvector(void);
00221    dvector(const dvector &);
00222    //dvector(const dvector&,int lb,int ub);
00223 
00224    dvector(const ivector &);
00225 
00226    dvector(const lvector &);
00227 
00228    dvector(const char *);
00229 
00230    dvector(int ncl, int ncu);
00231    // makes an array [ncl..ncu]
00232 
00233    dvector(unsigned int sz, double *x);
00234 
00235    dvector(char *filename, const int &column);
00236 
00237    //operator double* () { return v;}
00238 
00239    ~dvector();
00240 
00241    void save_dvector_position(void) const;
00242    void save_dvector_derivatives(const dvar_vector_position & pos) const;
00243    void save_dvector_derivatives_na(const dvar_vector_position & pos)
00244       const;
00245    void save_dvector_derivatives(void) const;
00246    void save_dvector_value(void) const;
00247 
00248    //dvector operator()(int,int);
00249    dvector operator() (const lvector &);
00250    dvector operator() (const ivector & u);
00251 
00252    dvector & operator+=(const dvector & v1);
00253    dvector & operator-=(const dvector & v1);
00254    dvector & operator +=(double v1);
00255    dvector & operator /=(double v1);
00256    dvector & operator *=(double v1);
00257    dvector & operator -=(double v1);
00258 
00259    void read_from(const uistream &);
00260 
00261    friend class sdmatrix;
00262    friend double norm(const dvector &);
00263    friend double norm2(const dvector &);
00264    friend double sumsq(const dvector &);
00265    friend class dvar_vector;
00266    friend class dmatrix;
00267    friend class d3_array;
00268    friend char *fform(const char *, const dvector &);
00269 
00270    void write_on(const ostream &) const;
00271    void write_on(const uostream &) const;
00272    void read_from(const istream &);
00273    friend double operator*(const dvector &, const dvector &);
00274 
00275    friend dvariable operator*(const dvector &, const dvar_vector &);
00276 
00277    friend dvariable operator*(const dvar_vector &, const dvector &);
00278 
00279    friend dvar_vector operator*(const prevariable &, const dvector &);
00280 
00281    friend dvector operator*(double, const dvector &);
00282 
00283    friend dvector operator+(const dvector &, const dvector &);
00284 
00285    friend dvector elem_prod(const dvector &, const dvector &);
00286 
00287    friend dvector first_difference(const dvector &);
00288    friend dvector second_difference(const dvector &);
00289 
00290    friend dvector elem_div(const dvector &, const dvector &);
00291 
00292    friend dvar_vector elem_prod(const dvector &, const dvar_vector &);
00293 
00294    friend dvar_vector elem_div(const dvector &, const dvar_vector &);
00295 
00296    friend dvar_vector elem_prod(const dvar_vector &, const dvector &);
00297 
00298    friend dvar_vector elem_div(const dvar_vector &, const dvector &);
00299 
00300    friend dvar_vector operator+(const dvar_vector &, const dvector &);
00301 
00302    friend dvar_vector operator+(const dvector &, const dvar_vector &);
00303 
00304    friend dvector operator-(const dvector &, const dvector &);
00305 
00306    friend dvar_vector operator-(const dvar_vector &, const dvector &);
00307 
00308    friend dvar_vector operator-(const dvector &, const dvar_vector &);
00309 
00310    friend dvector operator*(const dvector & x, const dmatrix & m);
00311 
00312    friend dvector operator*(const dmatrix & x, const dvector & m);
00313 
00314    friend dvar_vector operator*(const dvector & x, const dvar_matrix & m);
00315 
00316    friend dvar_vector operator*(const dvar_matrix & x, const dvector & m);
00317 
00318    double& operator[](int i);
00319    double& operator()(int i);
00320    const double& operator[](int i) const;
00321    const double& operator()(int i) const;
00322 
00323    dvector & operator=(const dvector & t);
00324 
00325    dvector & operator =(double x);
00326 
00327    friend dvector exp(const dvector &);
00328 
00329    friend dvector log(const dvector &);
00330 
00331    friend dvector fabs(const dvector &);
00332 
00333    friend double max(const dvector &);
00334 
00335    friend double min(const dvector &);
00336 };
00337 
00338 #ifdef OPT_LIB
00339 inline double& dvector::operator[](int i)
00340 {
00341   return *(v + i);
00342 }
00343 inline double& dvector::operator()(int i)
00344 {
00345   return *(v + i);
00346 }
00347 inline const double& dvector::operator[](int i) const
00348 {
00349   return *(v + i);
00350 }
00351 inline const double& dvector::operator()(int i) const
00352 {
00353   return *(v + i);
00354 }
00355 #endif
00356 
00357 #endif