ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
imatrix.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2014 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_IMATRIX_H__
42 #define __ADMB_IMATRIX_H__
43 
49 {
50 public:
51  int row_min;
52  int row_max;
56  imatrix_position(const imatrix &);
57  imatrix_position(int min, int max);
60 };
61 
64 
69 class imatrix
70 {
71 protected:
72  int index_min;
73  int index_max;
76 
77 public:
78  int operator!(void) const
79  {
80  return (shape == NULL);
81  }
82 
83  imatrix(int, int);
84  // makes a matrix [0..nr][0..nc]
85 
86  imatrix(int nrl, int nrh, const ivector & iv);
87 
88  imatrix(int, int, int, int);
89  // makes a matrix [nrl..nrh][ncl..nch]
90  imatrix(int, int, int, const ivector &);
91  imatrix sub(int, int);
92  imatrix(int, int, const ivector &, const ivector &);
93  imatrix(const ad_integer & nrl, const ad_integer & nrh,
94  const index_type & ncl, const index_type & nch);
95 
96  imatrix& operator=(const imatrix & t);
97  imatrix& operator=(const int);
98  imatrix(const imatrix &);
99  // copy initializer
100  imatrix(const imatrix_position &);
101  imatrix(void);
102 
103  ~imatrix();
104  void shallow_copy(const imatrix &);
105 
106  void save_imatrix_value();
107  void save_imatrix_position();
110 
111  void allocate(void);
112  void allocate(int nrl, int nrh, const ivector & iv);
113  void allocate(const imatrix& dm);
114  void allocate(int nrl, int nrh, int ncl, int nch);
115  void allocate(int nrl, int nrh);
116  void allocate(int nrl, int nrh, int ncl, const ivector& nch);
117  void allocate(int nrl, int nrh, const ivector & ncl, const ivector & nch);
118  void allocate(const ad_integer & nrl, const ad_integer & nrh,
119  const index_type & ncl, const index_type & nch);
120  void deallocate();
121 
122  ivector& operator[](int);
123  ivector& operator()(int);
124  int& operator()(int, int);
125  const ivector& operator[](int) const;
126  const ivector& operator()(int) const;
127  const int& operator()(int, int) const;
128 
129 
130  ivector* begin() const
131  {
132  return m ? &m[indexmin()] : nullptr;
133  }
134  ivector* end() const
135  {
136  return m ? &m[indexmin()] + rowsize(): nullptr;
137  }
138  int indexmin() const
139  {
140  return rowmin();
141  }
142  int indexmax() const
143  {
144  return rowmax();
145  }
146  int rowmin() const
147  {
148  return index_min;
149  }
150  int rowmax() const
151  {
152  return index_max;
153  }
154 
155  int colmin(void) const
156  {
157  return ((*this) (indexmin()).indexmin());
158  }
159  int colmax(void) const
160  {
161  return ((*this) (indexmin()).indexmax());
162  }
163 
165  unsigned int rowsize() const
166  {
167  return static_cast<unsigned int>(rowmax() - rowmin() + 1);
168  }
170  unsigned int colsize() const
171  {
172  return static_cast<unsigned int>(colmax() - colmin() + 1);
173  }
174 
175  void rowshift(int min);
176 ivector& elem(int i);
177 const ivector& elem(int i) const;
178 
179  inline int &elem(int i, int j)
180  {
181  return (*((*(m + i)).v + j));
182  }
183  inline const int &elem(int i, int j) const
184  {
185  return (*((*(m + i)).v + j));
186  }
187 
188  void write_on(const ostream &) const;
189  void write_on(const uostream &) const;
190  void read_from(const istream &);
191  void read_from(const uistream &);
192  void initialize(void);
193  void fill_seqadd(int, int);
194  void colfill_seqadd(int, int, int);
195 
196  bool is_valid_row(const int i) const;
197 
198  friend char* fform(const char*, const dmatrix&);
199  friend class i3_array;
200 
201  unsigned int get_ncopies() const { return shape ? shape->ncopies : 0; }
202 };
203 
204 #ifdef OPT_LIB
206 {
207  return m[i];
208 }
209 inline int& imatrix::operator()(int i, int j)
210 {
211  return (*((*(m + i)).v + j));
212 }
214 {
215  if (!m)
216  throw std::bad_alloc();
217  else
218  return m[i];
219 }
220 inline const ivector& imatrix::operator()(int i) const
221 {
222  return m[i];
223 }
224 inline const int& imatrix::operator()(int i, int j) const
225 {
226  return (*((*(m + i)).v + j));
227 }
228 inline const ivector& imatrix::operator[](int i) const
229 {
230  return m[i];
231 }
232 #endif
233 inline ivector& imatrix::elem(int i)
234 {
235  if (i < rowmin())
236  {
237  cerr << "matrix bound exceeded -- row index too low in "
238  "imatrix::operator[i] value was \"" << i << "\".\n";
239  ad_exit(1);
240  }
241  if (i > rowmax())
242  {
243  cerr << "matrix bound exceeded -- row index too high in "
244  "imatrix::operator[i] value was \"" << i << "\".\n";
245  ad_exit(1);
246  }
247  return *(m + i);
248 }
249 inline const ivector& imatrix::elem(int i) const
250 {
251  if (i < rowmin())
252  {
253  cerr << "matrix bound exceeded -- row index too low in "
254  "imatrix::operator[i] value was \"" << i << "\".\n";
255  ad_exit(1);
256  }
257  if (i > rowmax())
258  {
259  cerr << "matrix bound exceeded -- row index too high in "
260  "imatrix::operator[i] value was \"" << i << "\".\n";
261  ad_exit(1);
262  }
263  return *(m + i);
264 }
265 #endif
ivector * m
Definition: imatrix.h:74
ptr_vector ptr
Definition: imatrix.h:55
ivector & elem(int i)
Definition: imatrix.h:233
Uses polymorphism to get index information from various data types to be used in constructing and all...
Definition: fvar.hpp:7731
void allocate(void)
Does not allocate, but initializes imatrix members.
Definition: imat.cpp:138
int indexmax() const
Definition: imatrix.h:142
void colfill_seqadd(int, int, int)
Fills column of a matrix with a sequence of the form base, base+offset, base+2*offset,...
Definition: ranfill1.cpp:55
Description not yet available.
Definition: fvar.hpp:2030
mat_shapex * shape
Definition: imatrix.h:75
Description not yet available.
Definition: imatrix.h:69
ivector ub
Definition: imatrix.h:54
int operator!(void) const
Definition: imatrix.h:78
Description not yet available.
Definition: imatrix.h:48
int indexmin() const
Definition: imatrix.h:138
void deallocate()
Deallocate imatrix memory.
Definition: imat.cpp:248
ivector_position operator()(int i)
ivector * end() const
Definition: imatrix.h:134
int index_max
Definition: imatrix.h:73
Description not yet available.
Definition: fvar.hpp:568
int & elem(int i, int j)
Definition: imatrix.h:179
~imatrix()
Destructor.
Definition: imat.cpp:243
ivector & operator[](int)
Definition: imatrix.h:213
imatrix restore_imatrix_value(const imatrix_position &mpos)
Description not yet available.
Definition: cmpdif9.cpp:85
int rowmin() const
Definition: imatrix.h:146
exitptr ad_exit
Definition: gradstrc.cpp:53
imatrix_position(const imatrix &)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: cmpdif10.cpp:15
imatrix_position restore_imatrix_position(void)
unsigned int colsize() const
Returns the number of columns.
Definition: imatrix.h:170
ivector lb
Definition: imatrix.h:53
friend char * fform(const char *, const dmatrix &)
void shallow_copy(const imatrix &)
Shallow copy other data structure pointers.
Definition: imat.cpp:200
ivector & operator()(int)
Definition: imatrix.h:205
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
#define min(a, b)
Definition: cbivnorm.cpp:188
void initialize(void)
Description not yet available.
Definition: imat3.cpp:20
Description not yet available.
Definition: fvar.hpp:3398
int colmin(void) const
Definition: imatrix.h:155
unsigned int rowsize() const
Returns the number of rows.
Definition: imatrix.h:165
Description not yet available.
Definition: fvar.hpp:4947
ivector * begin() const
Definition: imatrix.h:130
imatrix restore_imatrix_value(const imatrix_position &mpos)
void fill_seqadd(int, int)
Fill imatrix with sequence of integers from start and adding with increment.
Definition: imat8.cpp:69
Description not yet available.
Definition: fvar.hpp:2819
int colmax(void) const
Definition: imatrix.h:159
unsigned int ncopies
Definition: fvar.hpp:2034
imatrix_position restore_imatrix_position()
Reads and restores back the size and address information for a imatrix.
Definition: cmpdif9.cpp:59
void write_on(const ostream &) const
imatrix(void)
Default constructor.
Definition: imat.cpp:238
void save_imatrix_position()
Saves the size and address information for a imatrix.
Definition: cmpdif9.cpp:35
Description not yet available.
Definition: fvar.hpp:3516
const int & elem(int i, int j) const
Definition: imatrix.h:183
void save_imatrix_value()
Saves the size, address, and value information for a imatrix.
Definition: cmpdif9.cpp:20
Stores integer.
Definition: fvar.hpp:7654
Description not yet available.
Definition: fvar.hpp:3944
void read_from(const istream &)
#define max(a, b)
Definition: cbivnorm.cpp:189
imatrix sub(int, int)
Description not yet available.
Definition: imat.cpp:45
int rowmax() const
Definition: imatrix.h:150
imatrix & operator=(const imatrix &t)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: imat5.cpp:17
int index_min
Definition: imatrix.h:72
bool is_valid_row(const int i) const
Check index i is in matrix row bounds [index_min, index_max].
Definition: imat1.cpp:19
unsigned int get_ncopies() const
Definition: imatrix.h:201
void rowshift(int min)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: imat.cpp:17