Cantera  4.0.0a1
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
14namespace 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 */
32{
33public:
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, span<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(span<const double> c);
83
84 //! Set the nth row to array rw
85 /*!
86 * @param n Index of the row to be changed
87 * @param rw Vector for the row. Must have a length of m_ncols.
88 */
89 void setRow(size_t n, span<const double> rw);
90
91 //! Get the nth row and return it in a vector
92 /*!
93 * @param n Index of the row to be returned.
94 * @param rw Return Vector for the operation. Must have a length of
95 * m_ncols.
96 */
97 void getRow(size_t n, span<double> rw) const;
98
99 //! Set the values in column m to those in array col
100 /*!
101 * A(i,m) = col(i)
102 *
103 * @param m Column to set
104 * @param col pointer to a col vector. Vector must have a length of m_nrows.
105 */
106 void setColumn(size_t m, span<const double> col);
107
108 //! Get the values in column m
109 /*!
110 * col(i) = A(i,m)
111 *
112 * @param m Column to set
113 * @param col pointer to a col vector that will be returned
114 */
115 void getColumn(size_t m, span<double> col) const;
116
117 //! Set all of the entries to zero
118 void zero() {
119 m_data.assign(m_data.size(), 0.0);
120 }
121
122 //! Allows setting elements using the syntax A(i,j) = x.
123 /*!
124 * @param i row index
125 * @param j column index.
126 * @returns a reference to A(i,j) which may be assigned.
127 */
128 double& operator()(size_t i, size_t j) {
129 return value(i,j);
130 }
131
132 //! Allows retrieving elements using the syntax x = A(i,j).
133 /*!
134 * @param i Index for the row to be retrieved
135 * @param j Index for the column to be retrieved.
136 * @returns the value of the matrix entry
137 */
138 double operator()(size_t i, size_t j) const {
139 return value(i,j);
140 }
141
142 //! Returns a changeable reference to position in the matrix
143 /*!
144 * Returns a reference to the matrix's (i,j) element. This may be used as an
145 * L value.
146 *
147 * @param i The row index
148 * @param j The column index
149 * @returns a changeable reference to the matrix entry
150 */
151 double& value(size_t i, size_t j) {
152 return m_data[m_nrows*j + i];
153 }
154
155 //! Returns the value of a single matrix entry
156 /*!
157 * Returns the value of the matrix position (i,j) element.
158 *
159 * @param i The row index
160 * @param j The column index
161 */
162 double value(size_t i, size_t j) const {
163 return m_data[m_nrows*j + i];
164 }
165
166 //! Number of rows
167 size_t nRows() const {
168 return m_nrows;
169 }
170
171 //! Number of columns
172 size_t nColumns() const {
173 return m_ncols;
174 }
175
176 //! Return a reference to the data vector
177 vector<double>& data() {
178 return m_data;
179 }
180
181 //! Return a const reference to the data vector
182 const vector<double>& data() const {
183 return m_data;
184 }
185
186 void operator*=(double a);
187
188 //! Return a writable span over column `j`.
189 span<double> col(size_t j) {
190 return span<double>(m_data.data() + m_nrows * j, m_nrows);
191 }
192
193 //! Return a read-only span over column `j`.
194 span<const double> col(size_t j) const {
195 return span<const double>(m_data.data() + m_nrows * j, m_nrows);
196 }
197
198protected:
199 //! Data stored in a single array
200 vector<double> m_data;
201
202 //! Number of rows
203 size_t m_nrows = 0;
204
205 //! Number of columns
206 size_t m_ncols = 0;
207};
208
209//! Output the current contents of the Array2D object
210/*!
211 * Example of usage:
212 * s << m << endl;
213 *
214 * @param s Reference to the ostream to write to
215 * @param m Object of type Array2D that you are querying
216 * @returns a reference to the ostream.
217 */
218std::ostream& operator<<(std::ostream& s, const Array2D& m);
219
220//! Overload the times equals operator for multiplication of a matrix and a
221//! scalar.
222/*!
223 * Scaled every element of the matrix by the scalar input
224 *
225 * @param m Matrix
226 * @param a scalar
227 */
228void operator*=(Array2D& m, double a);
229
230}
231
232#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:200
span< const double > col(size_t j) const
Return a read-only span over column j.
Definition Array.h:194
void zero()
Set all of the entries to zero.
Definition Array.h:118
Array2D()=default
Default constructor.
size_t m_nrows
Number of rows.
Definition Array.h:203
void getColumn(size_t m, span< double > col) const
Get the values in column m.
Definition Array.cpp:93
void setRow(size_t n, span< const double > rw)
Set the nth row to array rw.
Definition Array.cpp:69
void appendColumn(span< const double > c)
Append a column to the existing matrix using a std vector.
Definition Array.cpp:59
void getRow(size_t n, span< double > rw) const
Get the nth row and return it in a vector.
Definition Array.cpp:77
size_t nRows() const
Number of rows.
Definition Array.h:167
size_t m_ncols
Number of columns.
Definition Array.h:206
double value(size_t i, size_t j) const
Returns the value of a single matrix entry.
Definition Array.h:162
double & operator()(size_t i, size_t j)
Allows setting elements using the syntax A(i,j) = x.
Definition Array.h:128
size_t nColumns() const
Number of columns.
Definition Array.h:172
const vector< double > & data() const
Return a const reference to the data vector.
Definition Array.h:182
void setColumn(size_t m, span< const double > col)
Set the values in column m to those in array col.
Definition Array.cpp:85
span< double > col(size_t j)
Return a writable span over column j.
Definition Array.h:189
vector< double > & data()
Return a reference to the data vector.
Definition Array.h:177
double & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
Definition Array.h:151
double operator()(size_t i, size_t j) const
Allows retrieving elements using the syntax x = A(i,j).
Definition Array.h:138
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:52
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:595
void operator*=(Array2D &m, double a)
Overload the times equals operator for multiplication of a matrix and a scalar.
Definition Array.cpp:115
std::ostream & operator<<(std::ostream &s, const Array2D &m)
Output the current contents of the Array2D object.
Definition Array.cpp:101