Cantera 2.6.0
Array.cpp
Go to the documentation of this file.
1/**
2 * @file Array.cpp Implementation 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
10
11namespace Cantera
12{
13
15 : m_nrows(0)
16 , m_ncols(0)
17{
18}
19
20Array2D::Array2D(const size_t m, const size_t n, const double v)
21 : m_nrows(m)
22 , m_ncols(n)
23{
24 m_data.assign(n*m, v);
25}
26
27Array2D::Array2D(const size_t m, const size_t n, const double* values)
28 : m_nrows(m)
29 , m_ncols(n)
30{
31 m_data.assign(values, values + n*m);
32}
33
35 : m_data(y.m_data)
36 , m_nrows(y.m_nrows)
37 , m_ncols(y.m_ncols)
38{
39}
40
41Array2D& Array2D::operator=(const Array2D& y)
42{
43 if (&y == this) {
44 return *this;
45 }
46 m_nrows = y.m_nrows;
47 m_ncols = y.m_ncols;
48 m_data = y.m_data;
49 return *this;
50}
51
52void Array2D::resize(size_t n, size_t m, double v)
53{
54 m_nrows = n;
55 m_ncols = m;
56 m_data.resize(n*m, v);
57}
58
60{
61 m_ncols++;
62 m_data.resize(m_nrows * m_ncols);
63 for (size_t m = 0; m < m_nrows; m++) {
64 value(m_ncols, m) = c[m];
65 }
66}
67
68void Array2D::appendColumn(const double* const c)
69{
70 m_ncols++;
71 m_data.resize(m_nrows * m_ncols);
72 for (size_t m = 0; m < m_nrows; m++) {
73 value(m_ncols, m) = c[m];
74 }
75}
76
77void Array2D::setRow(size_t n, const double* const rw)
78{
79 for (size_t j = 0; j < m_ncols; j++) {
80 m_data[m_nrows*j + n] = rw[j];
81 }
82}
83
84void Array2D::getRow(size_t n, double* const rw)
85{
86 for (size_t j = 0; j < m_ncols; j++) {
87 rw[j] = m_data[m_nrows*j + n];
88 }
89}
90
91void Array2D::setColumn(size_t m, double* const col)
92{
93 for (size_t i = 0; i < m_nrows; i++) {
94 m_data[m_nrows*m + i] = col[i];
95 }
96}
97
98void Array2D::getColumn(size_t m, double* const col)
99{
100 for (size_t i = 0; i < m_nrows; i++) {
101 col[i] = m_data[m_nrows*m + i];
102 }
103}
104
105
106std::ostream& operator<<(std::ostream& s, const Array2D& m)
107{
108 size_t nr = m.nRows();
109 size_t nc = m.nColumns();
110 for (size_t i = 0; i < nr; i++) {
111 s << m(i,0);
112 for (size_t j = 1; j < nc; j++) {
113 s << ", " << m(i,j);
114 }
115 s << std::endl;
116 }
117 return s;
118}
119
120void operator*=(Array2D& m, double a)
121{
122 scale(m.begin(), m.end(), m.begin(), a);
123}
124
125}
Header file for class Cantera::Array2D.
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:30
size_t m_nrows
Number of rows.
Definition: Array.h:252
vector_fp m_data
Data stored in a single array.
Definition: Array.h:249
void getColumn(size_t m, double *const col)
Get the values in column m.
Definition: Array.cpp:98
void setColumn(size_t m, double *const col)
Set the values in column m to those in array col.
Definition: Array.cpp:91
size_t nRows() const
Number of rows.
Definition: Array.h:188
size_t m_ncols
Number of columns.
Definition: Array.h:255
size_t nColumns() const
Number of columns.
Definition: Array.h:193
void setRow(size_t n, const double *const rw)
Set the nth row to array rw.
Definition: Array.cpp:77
doublereal & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
Definition: Array.h:172
iterator end()
Return an iterator pointing past the last element.
Definition: Array.h:203
void getRow(size_t n, double *const rw)
Get the nth row and return it in a vector.
Definition: Array.cpp:84
iterator begin()
Return an iterator pointing to the first element.
Definition: Array.h:198
void appendColumn(const vector_fp &c)
Append a column to the existing matrix using a std vector.
Definition: Array.cpp:59
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
Array2D()
Default constructor.
Definition: Array.cpp:14
Namespace for the Cantera kernel.
Definition: AnyMap.h:29
void operator*=(Array2D &m, double a)
Overload the times equals operator for multiplication of a matrix and a scalar.
Definition: Array.cpp:120
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:100
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:184
std::ostream & operator<<(std::ostream &s, const Array2D &m)
Output the current contents of the Array2D object.
Definition: Array.cpp:106
Various templated functions that carry out common vector operations (see Templated Utility Functions)...