Cantera  3.1.0a1
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 "ct_defs.h"
12 #include <iostream>
13 
14 namespace Cantera
15 {
16 
17 //! A class for 2D arrays stored in column-major (Fortran-compatible) form.
18 /*!
19  * In this form, the data entry for an n row, m col matrix is
20  *
21  * index = i + (n-1) * j
22  *
23  * where
24  *
25  * J(i,j) = data_start + index
26  * i = row
27  * j = column
28  *
29  * @ingroup matrices
30  */
31 class Array2D
32 {
33 public:
34  /**
35  * Default constructor. Create an empty array.
36  */
37  Array2D() = default;
38 
39  //! Constructor.
40  /*!
41  * Create an @c m by @c n array, and initialize all elements to @c v.
42  *
43  * @param m Number of rows
44  * @param n Number of columns
45  * @param v Default fill value. The default is 0.0
46  */
47  Array2D(const size_t m, const size_t n, const double v=0.0);
48 
49  //! Constructor.
50  /*!
51  * Create an @c m by @c n array, initialized with the contents of the array
52  * @c values.
53  *
54  * @param m Number of rows
55  * @param n Number of columns
56  * @param values Initial values of the array. Must be of length m*n, and
57  * stored in column-major order.
58  */
59  Array2D(const size_t m, const size_t n, const double* values);
60 
61  Array2D(const Array2D& y);
62 
63  virtual ~Array2D() = default;
64 
65  Array2D& operator=(const Array2D& y);
66 
67  //! Resize the array, and fill the new entries with 'v'
68  /*!
69  * @param n This is the number of rows
70  * @param m This is the number of columns in the new matrix
71  * @param v Default fill value -> defaults to zero.
72  */
73  virtual void resize(size_t n, size_t m, double v=0.0);
74 
75  //! Append a column to the existing matrix using a std vector
76  /*!
77  * This operation will add a column onto the existing matrix.
78  *
79  * @param c This vector is the entries in the column to be added. It must
80  * have a length equal to m_nrows or greater.
81  */
82  void appendColumn(const vector<double>& c);
83 
84  //! Append a column to the existing matrix
85  /*!
86  * This operation will add a column onto the existing matrix.
87  *
88  * @param c This vector of doubles is the entries in the column to be
89  * added. It must have a length equal to m_nrows or greater.
90  */
91  void appendColumn(const double* const c);
92 
93  //! Set the nth row to array rw
94  /*!
95  * @param n Index of the row to be changed
96  * @param rw Vector for the row. Must have a length of m_ncols.
97  */
98  void setRow(size_t n, const double* const rw);
99 
100  //! Get the nth row and return it in a vector
101  /*!
102  * @param n Index of the row to be returned.
103  * @param rw Return Vector for the operation. Must have a length of
104  * m_ncols.
105  */
106  void getRow(size_t n, double* const rw);
107 
108  //! Set the values in column m to those in array col
109  /*!
110  * A(i,m) = col(i)
111  *
112  * @param m Column to set
113  * @param col pointer to a col vector. Vector must have a length of m_nrows.
114  */
115  void setColumn(size_t m, double* const col);
116 
117  //! Get the values in column m
118  /*!
119  * col(i) = A(i,m)
120  *
121  * @param m Column to set
122  * @param col pointer to a col vector that will be returned
123  */
124  void getColumn(size_t m, double* const col);
125 
126  //! Set all of the entries to zero
127  void zero() {
128  m_data.assign(m_data.size(), 0.0);
129  }
130 
131  //! Allows setting elements using the syntax A(i,j) = x.
132  /*!
133  * @param i row index
134  * @param j column index.
135  * @returns a reference to A(i,j) which may be assigned.
136  */
137  double& operator()(size_t i, size_t j) {
138  return value(i,j);
139  }
140 
141  //! Allows retrieving elements using the syntax x = A(i,j).
142  /*!
143  * @param i Index for the row to be retrieved
144  * @param j Index for the column to be retrieved.
145  * @returns the value of the matrix entry
146  */
147  double operator()(size_t i, size_t j) const {
148  return value(i,j);
149  }
150 
151  //! Returns a changeable reference to position in the matrix
152  /*!
153  * Returns a reference to the matrix's (i,j) element. This may be used as an
154  * L value.
155  *
156  * @param i The row index
157  * @param j The column index
158  * @returns a changeable reference to the matrix entry
159  */
160  double& value(size_t i, size_t j) {
161  return m_data[m_nrows*j + i];
162  }
163 
164  //! Returns the value of a single matrix entry
165  /*!
166  * Returns the value of the matrix position (i,j) element.
167  *
168  * @param i The row index
169  * @param j The column index
170  */
171  double value(size_t i, size_t j) const {
172  return m_data[m_nrows*j + i];
173  }
174 
175  //! Number of rows
176  size_t nRows() const {
177  return m_nrows;
178  }
179 
180  //! Number of columns
181  size_t nColumns() const {
182  return m_ncols;
183  }
184 
185  //! Return a reference to the data vector
186  vector<double>& data() {
187  return m_data;
188  }
189 
190  //! Return a const reference to the data vector
191  const vector<double>& data() const {
192  return m_data;
193  }
194 
195  void operator*=(double a);
196 
197  //! Return a pointer to the top of column j, columns are contiguous
198  //! in memory
199  /*!
200  * @param j Value of the column
201  * @returns a pointer to the top of the column
202  */
203  double* ptrColumn(size_t j) {
204  return &m_data[m_nrows*j];
205  }
206 
207  //! Return a const pointer to the top of column j, columns are contiguous
208  //! in memory
209  /*!
210  * @param j Value of the column
211  * @returns a const pointer to the top of the column
212  */
213  const double* ptrColumn(size_t j) const {
214  return &m_data[m_nrows*j];
215  }
216 
217 protected:
218  //! Data stored in a single array
219  vector<double> m_data;
220 
221  //! Number of rows
222  size_t m_nrows = 0;
223 
224  //! Number of columns
225  size_t m_ncols = 0;
226 };
227 
228 //! Output the current contents of the Array2D object
229 /*!
230  * Example of usage:
231  * s << m << endl;
232  *
233  * @param s Reference to the ostream to write to
234  * @param m Object of type Array2D that you are querying
235  * @returns a reference to the ostream.
236  */
237 std::ostream& operator<<(std::ostream& s, const Array2D& m);
238 
239 //! Overload the times equals operator for multiplication of a matrix and a
240 //! scalar.
241 /*!
242  * Scaled every element of the matrix by the scalar input
243  *
244  * @param m Matrix
245  * @param a scalar
246  */
247 void operator*=(Array2D& m, double a);
248 
249 }
250 
251 #endif
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:32
vector< double > m_data
Data stored in a single array.
Definition: Array.h:219
const vector< double > & data() const
Return a const reference to the data vector.
Definition: Array.h:191
void zero()
Set all of the entries to zero.
Definition: Array.h:127
Array2D()=default
Default constructor.
size_t m_nrows
Number of rows.
Definition: Array.h:222
void getColumn(size_t m, double *const col)
Get the values in column m.
Definition: Array.cpp:93
void setColumn(size_t m, double *const col)
Set the values in column m to those in array col.
Definition: Array.cpp:86
vector< double > & data()
Return a reference to the data vector.
Definition: Array.h:186
double & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
Definition: Array.h:160
double & operator()(size_t i, size_t j)
Allows setting elements using the syntax A(i,j) = x.
Definition: Array.h:137
size_t nRows() const
Number of rows.
Definition: Array.h:176
size_t m_ncols
Number of columns.
Definition: Array.h:225
double value(size_t i, size_t j) const
Returns the value of a single matrix entry.
Definition: Array.h:171
size_t nColumns() const
Number of columns.
Definition: Array.h:181
void appendColumn(const vector< double > &c)
Append a column to the existing matrix using a std vector.
Definition: Array.cpp:54
void setRow(size_t n, const double *const rw)
Set the nth row to array rw.
Definition: Array.cpp:72
void getRow(size_t n, double *const rw)
Get the nth row and return it in a vector.
Definition: Array.cpp:79
const double * ptrColumn(size_t j) const
Return a const pointer to the top of column j, columns are contiguous in memory.
Definition: Array.h:213
double operator()(size_t i, size_t j) const
Allows retrieving elements using the syntax x = A(i,j).
Definition: Array.h:147
virtual void resize(size_t n, size_t m, double v=0.0)
Resize the array, and fill the new entries with 'v'.
Definition: Array.cpp:47
double * ptrColumn(size_t j)
Return a pointer to the top of column j, columns are contiguous in memory.
Definition: Array.h:203
This file contains definitions of constants, types and terms that are used in internal routines and a...
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:564
void operator*=(Array2D &m, double a)
Overload the times equals operator for multiplication of a matrix and a scalar.
Definition: Array.cpp:114
std::ostream & operator<<(std::ostream &s, const Array2D &m)
Output the current contents of the Array2D object.
Definition: Array.cpp:100