ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmpdif6.cpp
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  */
11 #include <fvar.hpp>
12 
13 #ifdef __TURBOC__
14  #pragma hdrstop
15  #include <iostream.h>
16 #endif
17 
18 #ifdef __ZTC__
19  #include <iostream.hpp>
20 #endif
21 
22 #include <string.h>
23 
28 {
30 }
32 {
33  constexpr size_t wsize=sizeof(int);
34  constexpr size_t wsize1=sizeof(void*);
35 
36  dmatrix_position tmp(m);
37 
38  int min=m.rowmin();
39  int max=m.rowmax();
40  int* plbi = tmp.lb.get_v() + min;
41  int* pubi = tmp.ub.get_v() + min;
42  for (int i=min;i<=max;i++)
43  {
44  fwrite(plbi, wsize);
45  fwrite(pubi, wsize);
46  fwrite(&(tmp.ptr(i)),wsize1);
47 
48  ++plbi;
49  ++pubi;
50  }
51  fwrite(&(tmp.row_min),wsize);
52  fwrite(&(tmp.row_max),wsize);
53 }
54 
60 {
62 }
64 {
65  // saves the size and address information for a dvar_vector
66  int mmin=a.indexmin();
67  int mmax=a.indexmax();
68  constexpr size_t wsize = sizeof(int);
69 /*
70  dmatrix_position tmp(*this);
71  const int wsize=sizeof(int);
72  const int wsize1=sizeof(void*);
73 
74  for (int i=min;i<=max;i++)
75  {
76  gradient_structure::get_fp()->fwrite(&(tmp.lb(i)),wsize);
77  gradient_structure::get_fp()->fwrite(&(tmp.ub(i)),wsize);
78  gradient_structure::get_fp()->fwrite(&(tmp.ptr(i)),wsize1);
79  }
80 */
81  fwrite(&(mmin),wsize);
82  fwrite(&(mmax),wsize);
83 }
84 
90 {
92 }
94 {
95  constexpr size_t wsize = sizeof(int);
96 
97  // saves the size and address information for a dvar_vector
98  int mmin;
99  int mmax;
100  fread(&mmax,wsize);
101  fread(&mmin,wsize);
102  d3_array_position tmp(mmin,mmax);
103  return tmp;
104 }
105 
111 {
113 }
115 {
116  constexpr size_t wsize = sizeof(int);
117  constexpr size_t wsize2 = sizeof(void*);
118 
119  int min;
120  int max;
121  fread(&max, wsize);
122  fread(&min, wsize);
123  dvar_matrix_position tmp(min,max);
124  // cout << "tmp.ptr= " << tmp.ptr ;
125  int* plbi = tmp.lb.get_v() + max;
126  int* pubi = tmp.ub.get_v() + max;
127  for (int i=max;i>=min;i--)
128  {
129  fread(&(tmp.ptr(i)), wsize2);
130  fread(pubi, wsize);
131  fread(plbi, wsize);
132 
133  --pubi;
134  --plbi;
135  }
136  return tmp;
137 }
138 
144 {
146 }
148 {
149  constexpr size_t wsize = sizeof(int);
150  constexpr size_t wsize2 = sizeof(void*);
151 
152  // reads back the size and address information for a dvar_matrix
153  // restores the size, address, and value information for a dvar_vector
154  int min;
155  int max;
156  fread(&max, wsize);
157  fread(&min, wsize);
158  dmatrix_position tmp(min,max);
159  // cout << "tmp.ptr= " << tmp.ptr ;
160  int* plbi = tmp.lb.get_v() + max;
161  int* pubi = tmp.ub.get_v() + max;
162  for (int i=max;i>=min;i--)
163  {
164  fread(&(tmp.ptr(i)), wsize2);
165  fread(pubi, wsize);
166  fread(plbi, wsize);
167 
168  --pubi;
169  --plbi;
170  }
171  return tmp;
172 }
173 
179 {
181  dmatrix tmp(pos);
182  int min = pos.row_min;
183  int max = pos.row_max;
184  dvector* ptmpi = &tmp(max);
185  for (int i = max; i >= min; --i)
186  {
188  --ptmpi;
189  }
190  return tmp;
191 }
192 
198 {
200  dmatrix tmp(pos);
201  int min = pos.row_min;
202  int max = pos.row_max;
203  dvector* ptmpi = &tmp(max);
204  for (int i = max; i >= min; --i)
205  {
206  *ptmpi = restore_dvar_vector_der_nozero(pos(i));
207  --ptmpi;
208  }
209  return tmp;
210 }
211 
217  const int& ii)
218 {
221  return tmp;
222 }
223 
229  const int& ii)
230 {
232 
233  int min = pos.rowmin();
234  int max = pos.rowmax();
235  dvector tmpvec(min, max);
236 
237  double* ptmpveci = tmpvec.get_v() + min;
238  for (int i = min; i <= max; ++i)
239  {
240  *ptmpveci = ((pos(i)).va)[ii].xvalue();
241  ((pos(i)).va)[ii].xvalue() = 0.0;
242 
243  ++ptmpveci;
244  }
245  return tmpvec;
246 }
247 
253 {
254  kkludge_object kg;
255 
256  int min = tmp.indexmin();
257  int max = tmp.indexmax();
258 
259  // dvar_vector constructor that doesn't do any initialization
260  dvar_vector out(min, max, kg);
261 
262  double_and_int* pouti = out.va + min;
263  double* ptmpi = tmp.get_v() + min;
264  for (int i = min; i <= max; ++i)
265  {
266  pouti->x = *ptmpi;
267 
268  ++pouti;
269  ++ptmpi;
270  }
271  return out;
272 }
273 
279 {
280  // cout << "Entering nograd assign"<<endl;
281  //kkludge_object kg;
282  int nrl = m.rowmin();
283  int nrh = m.rowmax();
284  ivector ncl(nrl, nrh);
285  ivector nch(nrl, nrh);
286 
287  int* pncli = ncl.get_v() + nrl;
288  int* pnchi = nch.get_v() + nrl;
289  const dvector* pmi = &m(nrl);
290  for (int i = nrl; i <= nrh; ++i)
291  {
292  *pncli = pmi->indexmin();
293  *pnchi = pmi->indexmax();
294 
295  ++pncli;
296  ++pnchi;
297  ++pmi;
298  }
299 
300  // dvar_matrix constructor that doesn't do any initialization
301  dvar_matrix out(nrl,nrh,ncl,nch);
302 
303  pmi = &m(nrl);
304  pncli = ncl.get_v() + nrl;
305  pnchi = nch.get_v() + nrl;
306  dvar_vector* pouti = &out(nrl);
307  for (int i = nrl; i <= nrh; ++i)
308  {
309  int jmin = *pncli;
310  int jmax = *pnchi;
311  double* pmij = pmi->get_v() + jmin;
312  double_and_int* poutij = pouti->va + jmin;
313  for (int j = jmin; j <= jmax; ++j)
314  {
315  poutij->x = *pmij;
316 
317  ++pmij;
318  ++poutij;
319  }
320  ++pmi;
321  ++pncli;
322  ++pnchi;
323  ++pouti;
324  // out(i)=nograd_assign(m(i));
325  }
326  // cout << "Leaving nograd assign"<<endl;
327  return out;
328 }
329 
335 {
336  // cout << "Entering nograd assign"<<endl;
337  //kkludge_object kg;
338  int nrl=m.rowmin();
339  int nrh=m.rowmax();
340  ivector ncl(nrl,nrh);
341  ivector nch(nrl,nrh);
342 
343  int* pncli = ncl.get_v() + nrl;
344  int* pnchi = nch.get_v() + nrl;
345  const dvector* pmi = &m(nrl);
346  for (int i = nrl; i <= nrh; ++i)
347  {
348  *pncli = pmi->indexmin();
349  *pnchi = pmi->indexmax();
350 
351  ++pncli;
352  ++pnchi;
353  ++pmi;
354  }
355 
356  // dvar_matrix constructor that doesn't do any initialization
357  dvar_matrix out(nrl,nrh,ncl,nch);
358 
359  pncli = ncl.get_v() + nrl;
360  pnchi = nch.get_v() + nrl;
361  pmi = &m(nrl);
362  for (int i = nrl; i <= nrh; ++i)
363  {
364  int jmin = *pncli;
365  int jmax = *pnchi;
366  double* pmij = pmi->get_v() + jmin;
367  dvar_vector* poutj = &out(jmin);
368  for (int j = jmin; j <= jmax; ++j)
369  {
370  (poutj->va + i)->x = *pmij;
371 
372  ++poutj;
373  ++pmij;
374  }
375 
376  ++pmi;
377  ++pncli;
378  ++pnchi;
379  // out(i)=nograd_assign(m(i));
380  }
381  // cout << "Leaving nograd assign"<<endl;
382  return out;
383 }
384 
389 void nograd_assign_column(const dvar_matrix& m, const dvector& v, const int& ii)
390 {
391  // cout << "Entering nograd assign"<<endl;
392  //kkludge_object kg;
393 
394 #ifndef OPT_LIB
395  if (ii < m.colmin() || ii > m.colmax()
396  || (v.indexmin() != m.rowmin())
397  || (v.indexmax() != m.rowmax()))
398  {
399  cerr << "Error -- Index out of bounds in\n"
400  "void nograd_assign(const dvar_matrix& m,const dvector& v, const int& ii)"
401  << endl;
402  ad_exit(1);
403  }
404 #endif
405 
406  int min = v.indexmin();
407  int max = v.indexmax();
408  double* pvj = v.get_v() + min;
409  const dvar_vector* pmj = &m(min);
410  for (int j = min; j <= max; ++j)
411  {
412  (pmj->va + ii)->x = *pvj;
413 
414  ++pvj;
415  ++pmj;
416  }
417  // out(i)=nograd_assign(m(i));
418 }
419 
425  min(mmin), max(mmax)
426  {}
Description not yet available.
Definition: fvar.hpp:4883
d3_array_position(int mmin, int mmax)
Description not yet available.
Definition: cmpdif6.cpp:424
int rowmax(void) const
Definition: fvar.hpp:2564
int & rowmin(void)
Definition: fvar.hpp:4855
dvector restore_dvar_matrix_derivative_column(const dvar_matrix_position &_pos, const int &ii)
Description not yet available.
Definition: cmpdif6.cpp:228
ptr_vector ptr
Definition: fvar.hpp:4890
dmatrix restore_dvar_matrix_der_nozero(const dvar_matrix_position &_pos)
Description not yet available.
Definition: cmpdif6.cpp:197
int indexmax() const
Definition: fvar.hpp:3822
int colmin(void) const
Definition: fvar.hpp:2552
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void save_dmatrix_position(const dmatrix &m)
Definition: cmpdif6.cpp:31
dmatrix restore_dvar_matrix_derivatives(const dvar_matrix_position &_pos)
Description not yet available.
Definition: cmpdif6.cpp:178
dvar_matrix nograd_assign_trans(const dmatrix &m)
Description not yet available.
Definition: cmpdif6.cpp:334
dvar_vector nograd_assign(dvector tmp)
Description not yet available.
Definition: cmpdif6.cpp:252
dvar_matrix_position restore_dvar_matrix_position()
Reads back the size and address information for a dvar_matrix, restores the size, address...
Definition: cmpdif6.cpp:110
dmatrix_position restore_dmatrix_position()
Description not yet available.
Definition: cmpdif6.cpp:143
exitptr ad_exit
Definition: gradstrc.cpp:53
dmatrix_position restore_dmatrix_position()
Definition: cmpdif6.cpp:147
Null class to allow specialized function overloads.
Definition: fvar.hpp:469
ADMB variable vector.
Definition: fvar.hpp:2172
Holds the data for the prevariable class.
Definition: fvar.hpp:191
int * get_v() const
Definition: ivector.h:114
dvector restore_dvar_matrix_derivative_row(const dvar_matrix_position &_pos, const int &ii)
Description not yet available.
Definition: cmpdif6.cpp:216
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
Description not yet available.
Definition: fvar.hpp:4902
int rowmax() const
Definition: fvar.hpp:2929
#define min(a, b)
Definition: cbivnorm.cpp:188
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
ptr_vector ptr
Definition: fvar.hpp:4850
int rowmin(void) const
Definition: fvar.hpp:2560
Description not yet available.
Definition: fvar.hpp:2819
ivector lb
Definition: fvar.hpp:4888
void save_d3_array_position(const d3_array &a)
Definition: cmpdif6.cpp:63
dvector restore_dvar_vector_der_nozero(const dvar_vector_position &tmp)
Description not yet available.
Definition: cmpdif5.cpp:185
dvar_matrix_position restore_dvar_matrix_position()
Definition: cmpdif6.cpp:114
static _THREAD DF_FILE * fp
void save_d3_array_position() const
Description not yet available.
Definition: cmpdif6.cpp:59
ivector ub
Definition: fvar.hpp:4889
void save_dmatrix_position() const
Saves size and address information for a dmatrix to adjoint data file.
Definition: cmpdif6.cpp:27
size_t pos(const adstring &substr, const adstring &s)
Definition: string3.cpp:56
int & rowmax(void)
Definition: fvar.hpp:4859
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
dvector restore_dvar_vector_derivatives(const dvar_vector_position &tmp)
Description not yet available.
Definition: cmpdif5.cpp:150
d3_array_position restore_d3_array_position()
Definition: cmpdif6.cpp:93
void fwrite(const void *s, const size_t num_bytes)
Reads num_bytes from s and writes to buffer.
Definition: df_file.cpp:296
void fread(void *s, const size_t num_bytes)
Reads num_bytes from buffer and copies to s.
Definition: df_file.cpp:270
int indexmin() const
Definition: fvar.hpp:3818
double_and_int * va
Definition: fvar.hpp:2175
void nograd_assign_column(const dvar_matrix &m, const dvector &v, const int &ii)
Description not yet available.
Definition: cmpdif6.cpp:389
#define max(a, b)
Definition: cbivnorm.cpp:189
d3_array_position restore_d3_array_position()
Description not yet available.
Definition: cmpdif6.cpp:89
Description not yet available.
Definition: fvar.hpp:3727
double *& get_v(void)
Definition: dvector.h:148
int rowmin() const
Definition: fvar.hpp:2925
double x
&lt; value of the variable
Definition: fvar.hpp:195
int colmax(void) const
Definition: fvar.hpp:2556
Description not yet available.
Definition: fvar.hpp:4843