Cantera  2.3.0
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 http://www.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  //! Evaluate z = a*x + y.
198  /*!
199  * This function evaluates the AXPY operation, and stores the result in the
200  * object's Array2D object. It's assumed that all 3 objects have the same
201  * dimensions, but no error checking is done.
202  *
203  * @param a scalar to multiply x with
204  * @param x First Array2D object to be used
205  * @param y Second Array2D object to be used
206  * @deprecated Unused. To be removed after Cantera 2.3.
207  */
208  void axpy(doublereal a, const Array2D& x, const Array2D& y) {
209  warn_deprecated("Array2D::axpy",
210  "Unused. To be removed after Cantera 2.3.");
211  auto b = begin();
212  auto xb = x.begin();
213  auto yb = y.begin();
214  for (; b != end(); ++b, ++xb, ++yb) {
215  *b = a*(*xb) + *yb;
216  }
217  }
218 
219  //! Set all of the entries to zero
220  void zero() {
221  m_data.assign(m_data.size(), 0.0);
222  }
223 
224  //! Allows setting elements using the syntax A(i,j) = x.
225  /*!
226  * @param i row index
227  * @param j column index.
228  * @returns a reference to A(i,j) which may be assigned.
229  */
230  doublereal& operator()(size_t i, size_t j) {
231  return value(i,j);
232  }
233 
234  //! Allows retrieving elements using the syntax x = A(i,j).
235  /*!
236  * @param i Index for the row to be retrieved
237  * @param j Index for the column to be retrieved.
238  * @returns the value of the matrix entry
239  */
240  doublereal operator()(size_t i, size_t j) const {
241  return value(i,j);
242  }
243 
244  //! Returns a changeable reference to position in the matrix
245  /*!
246  * Returns a reference to the matrix's (i,j) element. This may be used as an
247  * L value.
248  *
249  * @param i The row index
250  * @param j The column index
251  * @returns a changeable reference to the matrix entry
252  */
253  doublereal& value(size_t i, size_t j) {
254  return m_data[m_nrows*j + i];
255  }
256 
257  //! Returns the value of a single matrix entry
258  /*!
259  * Returns the value of the matrix position (i,j) element.
260  *
261  * @param i The row index
262  * @param j The column index
263  */
264  doublereal value(size_t i, size_t j) const {
265  return m_data[m_nrows*j + i];
266  }
267 
268  /// Number of rows
269  size_t nRows() const {
270  return m_nrows;
271  }
272 
273  /// Number of columns
274  size_t nColumns() const {
275  return m_ncols;
276  }
277 
278  /// Return an iterator pointing to the first element
280  return m_data.begin();
281  }
282 
283  /// Return an iterator pointing past the last element
285  return m_data.end();
286  }
287 
288  /// Return a const iterator pointing to the first element
290  return m_data.begin();
291  }
292 
293  /// Return a const iterator pointing to past the last element
294  const_iterator end() const {
295  return m_data.end();
296  }
297 
298  /// Return a reference to the data vector
300  return m_data;
301  }
302 
303  /// Return a const reference to the data vector
304  const vector_fp& data() const {
305  return m_data;
306  }
307 
308  //! Return a pointer to the top of column j, columns are contiguous
309  //! in memory
310  /*!
311  * @param j Value of the column
312  * @returns a pointer to the top of the column
313  */
314  doublereal* ptrColumn(size_t j) {
315  return &m_data[m_nrows*j];
316  }
317 
318  //! Return a const pointer to the top of column j, columns are contiguous
319  //! in memory
320  /*!
321  * @param j Value of the column
322  * @returns a const pointer to the top of the column
323  */
324  const doublereal* ptrColumn(size_t j) const {
325  return &m_data[m_nrows*j];
326  }
327 
328 protected:
329  //! Data stored in a single array
331 
332  //! Number of rows
333  size_t m_nrows;
334 
335  //! Number of columns
336  size_t m_ncols;
337 };
338 
339 //! Output the current contents of the Array2D object
340 /*!
341  * Example of usage:
342  * s << m << endl;
343  *
344  * @param s Reference to the ostream to write to
345  * @param m Object of type Array2D that you are querying
346  * @returns a reference to the ostream.
347  */
348 inline std::ostream& operator<<(std::ostream& s, const Array2D& m)
349 {
350  size_t nr = m.nRows();
351  size_t nc = m.nColumns();
352  for (size_t i = 0; i < nr; i++) {
353  s << m(i,0);
354  for (size_t j = 1; j < nc; j++) {
355  s << ", " << m(i,j);
356  }
357  s << std::endl;
358  }
359  return s;
360 }
361 
362 //! Overload the times equals operator for multiplication of a matrix and a
363 //! scalar.
364 /*!
365  * Scaled every element of the matrix by the scalar input
366  *
367  * @param m Matrix
368  * @param a scalar
369  */
370 inline void operator*=(Array2D& m, doublereal a)
371 {
372  scale(m.begin(), m.end(), m.begin(), a);
373 }
374 
375 //! Overload the plus equals operator for addition of one matrix with another
376 /*!
377  * Adds each element of the second matrix into the first matrix
378  *
379  * @param x First matrix
380  * @param y Second matrix, which is a const
381  */
382 inline void operator+=(Array2D& x, const Array2D& y)
383 {
384  sum_each(x.begin(), x.end(), y.begin());
385 }
386 
387 }
388 
389 #endif
size_t nRows() const
Number of rows.
Definition: Array.h:269
vector_fp m_data
Data stored in a single array.
Definition: Array.h:330
Array2D()
Default constructor.
Definition: Array.h:51
void resize(size_t n, size_t m, doublereal v=0.0)
Resize the array, and fill the new entries with &#39;v&#39;.
Definition: Array.h:112
Various templated functions that carry out common vector operations (see Templated Utility Functions)...
void appendColumn(const vector_fp &c)
Append a column to the existing matrix using a std vector.
Definition: Array.h:125
doublereal value(size_t i, size_t j) const
Returns the value of a single matrix entry.
Definition: Array.h:264
void operator*=(Array2D &m, doublereal a)
Overload the times equals operator for multiplication of a matrix and a scalar.
Definition: Array.h:370
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:54
iterator end()
Return an iterator pointing past the last element.
Definition: Array.h:284
doublereal * ptrColumn(size_t j)
Return a pointer to the top of column j, columns are contiguous in memory.
Definition: Array.h:314
const_iterator end() const
Return a const iterator pointing to past the last element.
Definition: Array.h:294
void setRow(size_t n, const doublereal *const rw)
Set the nth row to array rw.
Definition: Array.h:153
void axpy(doublereal a, const Array2D &x, const Array2D &y)
Evaluate z = a*x + y.
Definition: Array.h:208
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:31
void appendColumn(const doublereal *const c)
Append a column to the existing matrix.
Definition: Array.h:140
const_iterator begin() const
Return a const iterator pointing to the first element.
Definition: Array.h:289
void getColumn(size_t m, doublereal *const col)
Get the values in column m.
Definition: Array.h:191
vector_fp::iterator iterator
Type definition for the iterator class that is can be used by Array2D types.
Definition: Array.h:39
Array2D(const size_t m, const size_t n, const doublereal v=0.0)
Constructor.
Definition: Array.h:65
size_t m_ncols
Number of columns.
Definition: Array.h:336
size_t m_nrows
Number of rows.
Definition: Array.h:333
Array2D(const size_t m, const size_t n, const doublereal *values)
Constructor.
Definition: Array.h:80
const vector_fp & data() const
Return a const reference to the data vector.
Definition: Array.h:304
vector_fp & data()
Return a reference to the data vector.
Definition: Array.h:299
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
doublereal & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
Definition: Array.h:253
void operator+=(Array2D &x, const Array2D &y)
Overload the plus equals operator for addition of one matrix with another.
Definition: Array.h:382
doublereal operator()(size_t i, size_t j) const
Allows retrieving elements using the syntax x = A(i,j).
Definition: Array.h:240
doublereal & operator()(size_t i, size_t j)
Allows setting elements using the syntax A(i,j) = x.
Definition: Array.h:230
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:324
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:290
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:157
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:130
iterator begin()
Return an iterator pointing to the first element.
Definition: Array.h:279
void getRow(size_t n, doublereal *const rw)
Get the nth row and return it in a vector.
Definition: Array.h:165
void zero()
Set all of the entries to zero.
Definition: Array.h:220
Namespace for the Cantera kernel.
Definition: application.cpp:29
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 nColumns() const
Number of columns.
Definition: Array.h:274