Cantera  2.5.1
Array.h
Go to the documentation of this file.
1 /**
2  * @file Array.h Header file for class Cantera::Array2D
3  */
4 
5 // This file is part of Cantera. See License.txt in the top-level directory or
6 // at https://cantera.org/license.txt for license and copyright information.
7 
8 #ifndef CT_ARRAY_H
9 #define CT_ARRAY_H
10 
11 #include "utilities.h"
12 
13 #include <iostream>
14 #include <cstring>
15 
16 namespace Cantera
17 {
18 
19 //! A class for 2D arrays stored in column-major (Fortran-compatible) form.
20 /*!
21  * In this form, the data entry for an n row, m col matrix is
22  *
23  * index = i + (n-1) * j
24  *
25  * where
26  *
27  * J(i,j) = data_start + index
28  * i = row
29  * j = column
30  */
31 class Array2D
32 {
33 public:
34  //! Type definition for the iterator class that is can be used by Array2D
35  //! types.
36  /*!
37  * This is just equal to vector_fp::iterator.
38  */
39  typedef vector_fp::iterator iterator;
40 
41  //! Type definition for the const_iterator class that is can be used by
42  //! Array2D types.
43  /*!
44  * This is just equal to vector_fp::const_iterator.
45  */
46  typedef vector_fp::const_iterator const_iterator;
47 
48  /**
49  * Default constructor. Create an empty array.
50  */
51  Array2D() :
52  m_data(0),
53  m_nrows(0),
54  m_ncols(0) {
55  }
56 
57  //! Constructor.
58  /*!
59  * Create an \c m by \c n array, and initialize all elements to \c v.
60  *
61  * @param m Number of rows
62  * @param n Number of columns
63  * @param v Default fill value. The default is 0.0
64  */
65  Array2D(const size_t m, const size_t n, const doublereal v = 0.0)
66  : m_data(0), m_nrows(m), m_ncols(n) {
67  m_data.assign(n*m, v);
68  }
69 
70  //! Constructor.
71  /*!
72  * Create an \c m by \c n array, initialized with the contents of the array
73  * \c values.
74  *
75  * @param m Number of rows
76  * @param n Number of columns
77  * @param values Initial values of the array. Must be of length m*n, and
78  * stored in column-major order.
79  */
80  Array2D(const size_t m, const size_t n, const doublereal* values)
81  : m_data(0), m_nrows(m), m_ncols(n) {
82  m_data.assign(values, values + n*m);
83  }
84 
85  Array2D(const Array2D& y) :
86  m_data(0),
87  m_nrows(0),
88  m_ncols(0) {
89  m_nrows = y.m_nrows;
90  m_ncols = y.m_ncols;
91  m_data = y.m_data;
92  }
93 
94  virtual ~Array2D() {}
95 
96  Array2D& operator=(const Array2D& y) {
97  if (&y == this) {
98  return *this;
99  }
100  m_nrows = y.m_nrows;
101  m_ncols = y.m_ncols;
102  m_data = y.m_data;
103  return *this;
104  }
105 
106  //! Resize the array, and fill the new entries with 'v'
107  /*!
108  * @param n This is the number of rows
109  * @param m This is the number of columns in the new matrix
110  * @param v Default fill value -> defaults to zero.
111  */
112  void resize(size_t n, size_t m, doublereal v = 0.0) {
113  m_nrows = n;
114  m_ncols = m;
115  m_data.resize(n*m, v);
116  }
117 
118  //! Append a column to the existing matrix using a std vector
119  /*!
120  * This operation will add a column onto the existing matrix.
121  *
122  * @param c This vector is the entries in the column to be added. It must
123  * have a length equal to m_nrows or greater.
124  */
125  void appendColumn(const vector_fp& c) {
126  m_ncols++;
127  m_data.resize(m_nrows*m_ncols);
128  for (size_t m = 0; m < m_nrows; m++) {
129  value(m_ncols, m) = c[m];
130  }
131  }
132 
133  //! Append a column to the existing matrix
134  /*!
135  * This operation will add a column onto the existing matrix.
136  *
137  * @param c This vector of doubles is the entries in the column to be
138  * added. It must have a length equal to m_nrows or greater.
139  */
140  void appendColumn(const doublereal* const c) {
141  m_ncols++;
142  m_data.resize(m_nrows*m_ncols);
143  for (size_t m = 0; m < m_nrows; m++) {
144  value(m_ncols, m) = c[m];
145  }
146  }
147 
148  //! Set the nth row to array rw
149  /*!
150  * @param n Index of the row to be changed
151  * @param rw Vector for the row. Must have a length of m_ncols.
152  */
153  void setRow(size_t n, const doublereal* const rw) {
154  for (size_t j = 0; j < m_ncols; j++) {
155  m_data[m_nrows*j + n] = rw[j];
156  }
157  }
158 
159  //! Get the nth row and return it in a vector
160  /*!
161  * @param n Index of the row to be returned.
162  * @param rw Return Vector for the operation. Must have a length of
163  * m_ncols.
164  */
165  void getRow(size_t n, doublereal* const rw) {
166  for (size_t j = 0; j < m_ncols; j++) {
167  rw[j] = m_data[m_nrows*j + n];
168  }
169  }
170 
171  //! Set the values in column m to those in array col
172  /*!
173  * A(i,m) = col(i)
174  *
175  * @param m Column to set
176  * @param col pointer to a col vector. Vector must have a length of m_nrows.
177  */
178  void setColumn(size_t m, doublereal* const col) {
179  for (size_t i = 0; i < m_nrows; i++) {
180  m_data[m_nrows*m + i] = col[i];
181  }
182  }
183 
184  //! Get the values in column m
185  /*!
186  * col(i) = A(i,m)
187  *
188  * @param m Column to set
189  * @param col pointer to a col vector that will be returned
190  */
191  void getColumn(size_t m, doublereal* const col) {
192  for (size_t i = 0; i < m_nrows; i++) {
193  col[i] = m_data[m_nrows*m + i];
194  }
195  }
196 
197  //! Set all of the entries to zero
198  void zero() {
199  m_data.assign(m_data.size(), 0.0);
200  }
201 
202  //! Allows setting elements using the syntax A(i,j) = x.
203  /*!
204  * @param i row index
205  * @param j column index.
206  * @returns a reference to A(i,j) which may be assigned.
207  */
208  doublereal& operator()(size_t i, size_t j) {
209  return value(i,j);
210  }
211 
212  //! Allows retrieving elements using the syntax x = A(i,j).
213  /*!
214  * @param i Index for the row to be retrieved
215  * @param j Index for the column to be retrieved.
216  * @returns the value of the matrix entry
217  */
218  doublereal operator()(size_t i, size_t j) const {
219  return value(i,j);
220  }
221 
222  //! Returns a changeable reference to position in the matrix
223  /*!
224  * Returns a reference to the matrix's (i,j) element. This may be used as an
225  * L value.
226  *
227  * @param i The row index
228  * @param j The column index
229  * @returns a changeable reference to the matrix entry
230  */
231  doublereal& value(size_t i, size_t j) {
232  return m_data[m_nrows*j + i];
233  }
234 
235  //! Returns the value of a single matrix entry
236  /*!
237  * Returns the value of the matrix position (i,j) element.
238  *
239  * @param i The row index
240  * @param j The column index
241  */
242  doublereal value(size_t i, size_t j) const {
243  return m_data[m_nrows*j + i];
244  }
245 
246  /// Number of rows
247  size_t nRows() const {
248  return m_nrows;
249  }
250 
251  /// Number of columns
252  size_t nColumns() const {
253  return m_ncols;
254  }
255 
256  /// Return an iterator pointing to the first element
258  return m_data.begin();
259  }
260 
261  /// Return an iterator pointing past the last element
263  return m_data.end();
264  }
265 
266  /// Return a const iterator pointing to the first element
268  return m_data.begin();
269  }
270 
271  /// Return a const iterator pointing to past the last element
272  const_iterator end() const {
273  return m_data.end();
274  }
275 
276  /// Return a reference to the data vector
278  return m_data;
279  }
280 
281  /// Return a const reference to the data vector
282  const vector_fp& data() const {
283  return m_data;
284  }
285 
286  //! Return a pointer to the top of column j, columns are contiguous
287  //! in memory
288  /*!
289  * @param j Value of the column
290  * @returns a pointer to the top of the column
291  */
292  doublereal* ptrColumn(size_t j) {
293  return &m_data[m_nrows*j];
294  }
295 
296  //! Return a const pointer to the top of column j, columns are contiguous
297  //! in memory
298  /*!
299  * @param j Value of the column
300  * @returns a const pointer to the top of the column
301  */
302  const doublereal* ptrColumn(size_t j) const {
303  return &m_data[m_nrows*j];
304  }
305 
306 protected:
307  //! Data stored in a single array
309 
310  //! Number of rows
311  size_t m_nrows;
312 
313  //! Number of columns
314  size_t m_ncols;
315 };
316 
317 //! Output the current contents of the Array2D object
318 /*!
319  * Example of usage:
320  * s << m << endl;
321  *
322  * @param s Reference to the ostream to write to
323  * @param m Object of type Array2D that you are querying
324  * @returns a reference to the ostream.
325  */
326 inline std::ostream& operator<<(std::ostream& s, const Array2D& m)
327 {
328  size_t nr = m.nRows();
329  size_t nc = m.nColumns();
330  for (size_t i = 0; i < nr; i++) {
331  s << m(i,0);
332  for (size_t j = 1; j < nc; j++) {
333  s << ", " << m(i,j);
334  }
335  s << std::endl;
336  }
337  return s;
338 }
339 
340 //! Overload the times equals operator for multiplication of a matrix and a
341 //! scalar.
342 /*!
343  * Scaled every element of the matrix by the scalar input
344  *
345  * @param m Matrix
346  * @param a scalar
347  */
348 inline void operator*=(Array2D& m, doublereal a)
349 {
350  scale(m.begin(), m.end(), m.begin(), a);
351 }
352 
353 //! Overload the plus equals operator for addition of one matrix with another
354 /*!
355  * Adds each element of the second matrix into the first matrix
356  *
357  * @param x First matrix
358  * @param y Second matrix, which is a const
359  */
360 inline void operator+=(Array2D& x, const Array2D& y)
361 {
362  sum_each(x.begin(), x.end(), y.begin());
363 }
364 
365 }
366 
367 #endif
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:32
void setRow(size_t n, const doublereal *const rw)
Set the nth row to array rw.
Definition: Array.h:153
Array2D(const size_t m, const size_t n, const doublereal v=0.0)
Constructor.
Definition: Array.h:65
void resize(size_t n, size_t m, doublereal v=0.0)
Resize the array, and fill the new entries with 'v'.
Definition: Array.h:112
void zero()
Set all of the entries to zero.
Definition: Array.h:198
size_t m_nrows
Number of rows.
Definition: Array.h:311
vector_fp m_data
Data stored in a single array.
Definition: Array.h:308
doublereal & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
Definition: Array.h:231
vector_fp & data()
Return a reference to the data vector.
Definition: Array.h:277
const_iterator begin() const
Return a const iterator pointing to the first element.
Definition: Array.h:267
void appendColumn(const doublereal *const c)
Append a column to the existing matrix.
Definition: Array.h:140
const vector_fp & data() const
Return a const reference to the data vector.
Definition: Array.h:282
Array2D(const size_t m, const size_t n, const doublereal *values)
Constructor.
Definition: Array.h:80
void setColumn(size_t m, doublereal *const col)
Set the values in column m to those in array col.
Definition: Array.h:178
size_t nRows() const
Number of rows.
Definition: Array.h:247
void getColumn(size_t m, doublereal *const col)
Get the values in column m.
Definition: Array.h:191
size_t m_ncols
Number of columns.
Definition: Array.h:314
const doublereal * ptrColumn(size_t j) const
Return a const pointer to the top of column j, columns are contiguous in memory.
Definition: Array.h:302
doublereal & operator()(size_t i, size_t j)
Allows setting elements using the syntax A(i,j) = x.
Definition: Array.h:208
doublereal * ptrColumn(size_t j)
Return a pointer to the top of column j, columns are contiguous in memory.
Definition: Array.h:292
size_t nColumns() const
Number of columns.
Definition: Array.h:252
void getRow(size_t n, doublereal *const rw)
Get the nth row and return it in a vector.
Definition: Array.h:165
doublereal value(size_t i, size_t j) const
Returns the value of a single matrix entry.
Definition: Array.h:242
doublereal operator()(size_t i, size_t j) const
Allows retrieving elements using the syntax x = A(i,j).
Definition: Array.h:218
iterator end()
Return an iterator pointing past the last element.
Definition: Array.h:262
const_iterator end() const
Return a const iterator pointing to past the last element.
Definition: Array.h:272
iterator begin()
Return an iterator pointing to the first element.
Definition: Array.h:257
void appendColumn(const vector_fp &c)
Append a column to the existing matrix using a std vector.
Definition: Array.h:125
vector_fp::const_iterator const_iterator
Type definition for the const_iterator class that is can be used by Array2D types.
Definition: Array.h:46
Array2D()
Default constructor.
Definition: Array.h:51
vector_fp::iterator iterator
Type definition for the iterator class that is can be used by Array2D types.
Definition: Array.h:39
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:180
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
void operator*=(Array2D &m, doublereal a)
Overload the times equals operator for multiplication of a matrix and a scalar.
Definition: Array.h:348
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:135
void operator+=(Array2D &x, const Array2D &y)
Overload the plus equals operator for addition of one matrix with another.
Definition: Array.h:360
void sum_each(OutputIter x_begin, OutputIter x_end, InputIter y_begin)
Increment each entry in x by the corresponding entry in y.
Definition: utilities.h:305
Various templated functions that carry out common vector operations (see Templated Utility Functions)...