| Cantera 3.0.0

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