ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dvector.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  *
7  * ADModelbuilder and associated libraries and documentations are
8  * provided under the general terms of the "New BSD" license
9  *
10  * License:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are
14  * met:
15  *
16  * 1. Redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer.
18  *
19  * 2. Redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in the
21  * documentation and/or other materials provided with the distribution.
22  *
23  * 3. Neither the name of the University of California, Otter Research,
24  * nor the ADMB Foundation nor the names of its contributors may be used
25  * to endorse or promote products derived from this software without
26  * specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #ifndef __ADMB_DVECTOR_H__
42 #define __ADMB_DVECTOR_H__
43 
50 class dvector
51 {
52  protected:
53  double *v;
54  int index_min;
55  int index_max;
56 #if defined(THREAD_SAFE)
58  ts_vector_shapex *shape;
59 #else
61 #endif
62 
63  public:
66  int operator!(void) const
67  {
68  return (shape == NULL);
69  }
71  int allocated(void) const
72  {
73  return (shape != NULL);
74  }
77  {
78  index_min--;
79  index_max--;
80  v++;
81  return *this;
82  }
85  {
86  index_min++;
87  index_max++;
88  v--;
89  return *this;
90  }
91  void reallocate(double size);
98  dvector sub(int lb, int ub)
99  {
100  return predvector(this, lb, ub);
101  }
108  dvector operator () (int lb, int ub)
109  {
110  return predvector(this, lb, ub);
111  }
112 
113  dvector(const predvector & pd);
114 
115  double* begin() const
116  {
117  return v ? &v[index_min] : nullptr;
118  }
119  double* end() const
120  {
121  return v ? begin() + size() : nullptr;
122  }
123 
124  void fill_randpoisson(double lambda, const random_number_generator & rng);
125  void fill_randnegbinomial(double lambda, double tau,
126  const random_number_generator & rng);
127  void shallow_copy(const dvector &);
128 
133 
134  void allocate(int ncl, int ncu);
135  void allocate(const dvector & dv);
136  void allocatec(const dvector & dv);
137  void allocate(const dvar_vector &);
138  void allocate(const char *);
139  void allocate(void);
140  void deallocate(void);
141  void safe_deallocate(void);
142  void safe_allocate(int, int);
143 
144  const double *address() const
145  {
146  return v;
147  }
148  double *&get_v(void)
149  {
150  return (v);
151  }
152  double &elem(int i)
153  {
154  return (v[i]);
155  }
156  double *initpointer(void)
157  {
158  return (v + indexmin());
159  }
160  const double *initpointer(void) const
161  {
162  return (v + indexmin());
163  }
164  double *get_v(void) const
165  {
166  return v;
167  }
168  const double &elem(int i) const
169  {
170  return v[i];
171  }
172 
173  void fill(const char *s);
174  void fill_randu_ni(long int &n);
175  void fill_randn_ni(long int &n);
176  void fill_randbi_ni(long int &n, double);
177 
178  void fill_randu(long int &n);
179  void fill_randn(long int &n);
180  void fill_randbi(long int &n, double);
181 
182 
183  void fill_randbi(double p, const random_number_generator & rng);
184  void fill_randu(const random_number_generator & rng);
185  void fill_randn(const random_number_generator & rng);
186  void fill_randcau(const random_number_generator & rng);
187 
188  void fill_seqadd(double, double);
189  void fill_multinomial(const int &seed, const dvector & p);
190  void fill_multinomial(const random_number_generator& rng, const dvector& p);
191  void initialize(void);
192 
193  unsigned int get_ncopies() const
194  {
195  return shape ? shape->get_ncopies() : 0;
196  }
197 
199  int indexmin() const
200  {
201  return index_min;
202  }
204  int indexmax() const
205  {
206  return index_max;
207  }
209  unsigned int size() const
210  {
211  return static_cast<unsigned int>(index_max - index_min + 1);
212  }
213 
214  dvector & shift(int min);
215 
216  dvector(const dvar_vector_position & dvp, const kkludge_object &);
217 
218  dvector(const ad_integer &, const index_type &);
219  void allocate(const ad_integer &, const index_type &);
220  dvector();
221  dvector(const dvector &);
222  //dvector(const dvector&,int lb,int ub);
223 
224  dvector(const ivector &);
225 
226  dvector(const lvector &);
227 
228  dvector(const char *);
229 
230  dvector(int ncl, int ncu);
231  // makes an array [ncl..ncu]
232 
233  dvector(unsigned int sz, double *x);
234 
235  dvector(char *filename, const int &column);
236 
237  //operator double* () { return v;}
238 
239  ~dvector();
240 
241  void save_dvector_position() const;
244  const;
245  void save_dvector_derivatives(void) const;
246  void save_dvector_value() const;
247 
248  //dvector operator()(int,int);
249  dvector operator() (const lvector &);
250  dvector operator() (const ivector & u);
251 
252  dvector & operator+=(const dvector & v1);
253  dvector & operator-=(const dvector & v1);
254  dvector & operator +=(double v1);
255  dvector & operator /=(double v1);
256  dvector & operator *=(double v1);
257  dvector & operator -=(double v1);
258 
259  void read_from(const uistream &);
260 
261  friend class sdmatrix;
262  friend double norm(const dvector &);
263  friend double norm2(const dvector &);
264  friend double sumsq(const dvector &);
265  friend class dvar_vector;
266  friend class dmatrix;
267  friend class d3_array;
268  friend char *fform(const char *, const dvector &);
269 
270  void write_on(const ostream &) const;
271  void write_on(const uostream &) const;
272  void read_from(const istream &);
273  friend double operator*(const dvector &, const dvector &);
274 
275  friend dvariable operator*(const dvector &, const dvar_vector &);
276 
277  friend dvariable operator*(const dvar_vector &, const dvector &);
278 
279  friend dvar_vector operator*(const prevariable &, const dvector &);
280 
281  friend dvector operator*(double, const dvector &);
282 
283  friend dvector operator+(const dvector &, const dvector &);
284 
285  friend dvector elem_prod(const dvector &, const dvector &);
286 
287  friend dvector first_difference(const dvector &);
288  friend dvector second_difference(const dvector &);
289 
290  friend dvector elem_div(const dvector &, const dvector &);
291 
292  friend dvar_vector elem_prod(const dvector &, const dvar_vector &);
293 
294  friend dvar_vector elem_div(const dvector &, const dvar_vector &);
295 
296  friend dvar_vector elem_prod(const dvar_vector &, const dvector &);
297 
298  friend dvar_vector elem_div(const dvar_vector &, const dvector &);
299 
300  friend dvar_vector operator+(const dvar_vector &, const dvector &);
301 
302  friend dvar_vector operator+(const dvector &, const dvar_vector &);
303 
304  friend dvector operator-(const dvector &, const dvector &);
305 
306  friend dvar_vector operator-(const dvar_vector &, const dvector &);
307 
308  friend dvar_vector operator-(const dvector &, const dvar_vector &);
309 
310  friend dvector operator*(const dvector & x, const dmatrix & m);
311 
312  friend dvector operator*(const dmatrix & x, const dvector & m);
313 
314  friend dvar_vector operator*(const dvector & x, const dvar_matrix & m);
315 
316  friend dvar_vector operator*(const dvar_matrix & x, const dvector & m);
317 
318  double& operator[](int i);
319  double& operator()(int i);
320  const double& operator[](int i) const;
321  const double& operator()(int i) const;
322 
323  dvector & operator=(const dvector & t);
324 
325  dvector & operator =(double x);
326 
327  bool is_valid_index(const int i) const;
328 
329  friend dvector exp(const dvector &);
330 
331  friend dvector log(const dvector &);
332 
333  friend dvector fabs(const dvector &);
334 
335  friend double max(const dvector &);
336 
337  friend double min(const dvector &);
338 
339  istream& parse(istream& input_json);
340 };
341 
342 #ifdef OPT_LIB
343 inline double& dvector::operator[](int i)
344 {
345  return *(v + i);
346 }
347 inline double& dvector::operator()(int i)
348 {
349  return *(v + i);
350 }
351 inline const double& dvector::operator[](int i) const
352 {
353  return *(v + i);
354 }
355 inline const double& dvector::operator()(int i) const
356 {
357  return *(v + i);
358 }
359 #endif
360 
361 #endif
Description not yet available.
Definition: fvar.hpp:7981
friend dvector elem_div(const dvector &, const dvector &)
Element-wise division of two vectors; constant objects.
Definition: dvect2.cpp:58
dvector & operator*=(double v1)
Multiply elements of a vector by a constant.
Definition: dvect20.cpp:34
dvector sub(int lb, int ub)
Get subvector.
Definition: dvector.h:98
Base class for dvariable.
Definition: fvar.hpp:1315
double * v
pointer to the data
Definition: dvector.h:53
void safe_deallocate(void)
Safely deallocates memory by reporting if shallow copies are still in scope.
Definition: dvector.cpp:110
friend double norm2(const dvector &)
Squared norm of a vector; constant objects.
Definition: dvect3.cpp:28
unsigned int get_ncopies(void)
Definition: vector_shapex.h:89
Description not yet available.
Definition: fvar.hpp:3212
Uses polymorphism to get index information from various data types to be used in constructing and all...
Definition: fvar.hpp:7731
friend double operator*(const dvector &, const dvector &)
Compute the dot product of two vectors.
Definition: dvector.cpp:512
dvector()
Construct a dvector without allocating memory.
Definition: dvector.cpp:378
double & elem(int i)
Definition: dvector.h:152
int allocated(void) const
Returns 1 (TRUE) if memory is allocated.
Definition: dvector.h:71
double * get_v(void) const
Definition: dvector.h:164
friend dvector operator+(const dvector &, const dvector &)
Add two dvectors.
Definition: dvector.cpp:554
void save_dvector_derivatives_na(const dvar_vector_position &pos) const
Description not yet available.
Definition: cmpdif5.cpp:236
dvector operator-()
Description not yet available.
Definition: dvect22.cpp:17
double * initpointer(void)
Definition: dvector.h:156
#define x
Vector of double precision numbers.
Definition: dvector.h:50
Description not yet available.
Definition: fvar.hpp:8190
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
friend dvector log(const dvector &)
Description not yet available.
Definition: dvect7.cpp:17
void fill_seqadd(double, double)
Fills dvector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:58
dvector & operator=(const dvector &t)
Assignment operator for dvector argument.
Definition: dvector.cpp:244
double * end() const
Definition: dvector.h:119
friend double sumsq(const dvector &)
Definition: dvect3.cpp:33
Description not yet available.
Definition: fvar.hpp:656
void allocatec(const dvector &dv)
Make shallow copy of dvector shape.
Definition: dvector.cpp:472
int operator!(void) const
Returns 1 (TRUE) if memory is NOT allocated.
Definition: dvector.h:66
Description not yet available.
Definition: fvar.hpp:4814
friend double min(const dvector &)
Returns the minimum value of vector vec.
Definition: dvect7.cpp:107
void allocate(void)
Allocate dvector without allocating memory.
Definition: dvector.cpp:495
dmatrix * t
Definition: fvar.hpp:3729
Null class to allow specialized function overloads.
Definition: fvar.hpp:469
void save_dvector_derivatives(void) const
ADMB variable vector.
Definition: fvar.hpp:2172
void write_on(const ostream &) const
Description not yet available.
Definition: dvect_io.cpp:49
void fill_randu(long int &n)
Fill vector with random numbers.
Definition: ranfill.cpp:190
void fill_randpoisson(double lambda, const random_number_generator &rng)
Fill vector with Poisson pseudorandom numbers.
Definition: dranpois.cpp:85
double * begin() const
Definition: dvector.h:115
friend dvector second_difference(const dvector &)
friend dvector elem_prod(const dvector &, const dvector &)
Element-wise multiplication of two vectors; constant objects.
Definition: dvect2.cpp:20
friend double max(const dvector &)
Returns the maximum value of vector vec.
Definition: dvect7.cpp:85
void fill_randn(long int &n)
Fill vector with random numbers.
Definition: ranfill.cpp:231
Description not yet available.
Definition: fvar.hpp:7951
const double * address() const
Definition: dvector.h:144
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
double & operator[](int i)
Definition: dvector.h:343
friend dvector exp(const dvector &)
Description not yet available.
Definition: dvect7.cpp:39
Description not yet available.
Definition: fvar.hpp:3398
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
void safe_allocate(int, int)
Safely allocate memory for a dvector.
Definition: dvector.cpp:390
void fill_randbi(long int &n, double)
Fill vector with binary random numbers.
Definition: ranfill.cpp:164
Description not yet available.
Definition: fvar.hpp:8120
friend dvector first_difference(const dvector &)
Returns dvector containing the differences of an values(i) and values(i + 1) for i = 1 to values...
Definition: dvec_dif.cpp:17
dvector & operator++(void)
Increment vector indices array pointer.
Definition: dvector.h:84
void fill_randnegbinomial(double lambda, double tau, const random_number_generator &rng)
Description not yet available.
Definition: drannegbin.cpp:17
void shallow_copy(const dvector &)
Explicit shallow copy.
Definition: dvector.cpp:157
Description not yet available.
Definition: fvar.hpp:1901
friend double norm(const dvector &)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect3.cpp:13
Description not yet available.
Definition: fvar.hpp:2819
void fill_randcau(const random_number_generator &rng)
Description not yet available.
Definition: drancau.cpp:27
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
dvector & shift(int min)
Shift valid range of subscripts.
Definition: dvector.cpp:52
unsigned int get_ncopies() const
Definition: dvector.h:193
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
int index_min
minimum valid subscript
Definition: dvector.h:54
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
dvector & operator--(void)
Decrement vector indices array pointer.
Definition: dvector.h:76
~dvector()
Default destructor.
Definition: dvector.cpp:65
const double & elem(int i) const
Definition: dvector.h:168
friend dvector fabs(const dvector &)
Description not yet available.
Definition: dvect7.cpp:61
istream & parse(istream &input_json)
Definition: adjson.cpp:125
vector_shapex * shape
pointer to vector &quot;shape&quot;
Definition: dvector.h:60
size_t pos(const adstring &substr, const adstring &s)
Definition: string3.cpp:56
friend char * fform(const char *, const dvector &)
dvector & operator-=(const dvector &v1)
Subtract values from dvector element-wise.
Definition: dvect16.cpp:40
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
void fill_randn_ni(long int &n)
Description not yet available.
Definition: ranf_ni.cpp:102
void fill(const char *s)
Fill vectors from values in string s.
Definition: dvect10.cpp:43
dvector & operator/=(double v1)
Divide elements of a vector by a constant.
Definition: dvect20.cpp:17
const double * initpointer(void) const
Definition: dvector.h:160
Description not yet available.
Definition: fvar.hpp:3516
dvector column(const dmatrix &matrix, int j)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat6.cpp:13
Stores integer.
Definition: fvar.hpp:7654
Description not yet available.
Definition: fvar.hpp:3727
int index_max
maximum valid subscript
Definition: dvector.h:55
void fill_multinomial(const int &seed, const dvector &p)
Description not yet available.
Definition: dvect11.cpp:17
dvector operator()(int lb, int ub)
Get subvector.
Definition: dvector.h:108
double *& get_v(void)
Definition: dvector.h:148
void read_from(const uistream &)
Description not yet available.
Definition: dvec_io2.cpp:77
void reallocate(double size)
Reallocate size of array.
Definition: dvect26.cpp:18
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
dvector & operator+=(const dvector &v1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect16.cpp:12
bool is_valid_index(const int i) const
Check index i is in dvector bounds [index_min, index_max].
Definition: dvec_acc.cpp:17
Description not yet available.
Definition: fvar.hpp:8065
void deallocate(void)
Called by destructor to deallocate memory for a dvector object.
Definition: dvector.cpp:92
void fill_randbi_ni(long int &n, double)
Description not yet available.
Definition: ranf_ni.cpp:33
void save_dvector_position() const
Description not yet available.
Definition: cmpdif4.cpp:28
void save_dvector_value() const
Saves the size, address, and value information for a dvector.
Definition: cmpdif4.cpp:126
void fill_randu_ni(long int &n)
Description not yet available.
Definition: ranf_ni.cpp:60