Cantera  3.0.0
Loading...
Searching...
No Matches
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#include "cantera/base/global.h"
11
12namespace Cantera
13{
14
15Array2D::Array2D(const size_t m, const size_t n, const double v)
16 : m_nrows(m)
17 , m_ncols(n)
18{
19 m_data.assign(n*m, v);
20}
21
22Array2D::Array2D(const size_t m, const size_t n, const double* values)
23 : m_nrows(m)
24 , m_ncols(n)
25{
26 m_data.assign(values, values + n*m);
27}
28
30 : m_data(y.m_data)
31 , m_nrows(y.m_nrows)
32 , m_ncols(y.m_ncols)
33{
34}
35
36Array2D& Array2D::operator=(const Array2D& y)
37{
38 if (&y == this) {
39 return *this;
40 }
41 m_nrows = y.m_nrows;
42 m_ncols = y.m_ncols;
43 m_data = y.m_data;
44 return *this;
45}
46
47void Array2D::resize(size_t n, size_t m, double v)
48{
49 m_nrows = n;
50 m_ncols = m;
51 m_data.resize(n*m, v);
52}
53
54void Array2D::appendColumn(const vector<double>& c)
55{
56 m_ncols++;
57 m_data.resize(m_nrows * m_ncols);
58 for (size_t m = 0; m < m_nrows; m++) {
59 value(m_ncols, m) = c[m];
60 }
61}
62
63void Array2D::appendColumn(const double* const c)
64{
65 m_ncols++;
66 m_data.resize(m_nrows * m_ncols);
67 for (size_t m = 0; m < m_nrows; m++) {
68 value(m_ncols, m) = c[m];
69 }
70}
71
72void Array2D::setRow(size_t n, const double* const rw)
73{
74 for (size_t j = 0; j < m_ncols; j++) {
75 m_data[m_nrows*j + n] = rw[j];
76 }
77}
78
79void Array2D::getRow(size_t n, double* const rw)
80{
81 for (size_t j = 0; j < m_ncols; j++) {
82 rw[j] = m_data[m_nrows*j + n];
83 }
84}
85
86void Array2D::setColumn(size_t m, double* const col)
87{
88 for (size_t i = 0; i < m_nrows; i++) {
89 m_data[m_nrows*m + i] = col[i];
90 }
91}
92
93void Array2D::getColumn(size_t m, double* const col)
94{
95 for (size_t i = 0; i < m_nrows; i++) {
96 col[i] = m_data[m_nrows*m + i];
97 }
98}
99
101 warn_deprecated("Array2D::begin", "To be removed after Cantera 3.0.");
102 return m_data.begin();
103}
104
106 warn_deprecated("Array2D::end", "To be removed after Cantera 3.0.");
107 return m_data.end();
108}
109
111 warn_deprecated("Array2D::begin", "To be removed after Cantera 3.0.");
112 return m_data.begin();
113}
114
116 warn_deprecated("Array2D::end", "To be removed after Cantera 3.0.");
117 return m_data.end();
118}
119
120std::ostream& operator<<(std::ostream& s, const Array2D& m)
121{
122 size_t nr = m.nRows();
123 size_t nc = m.nColumns();
124 for (size_t i = 0; i < nr; i++) {
125 s << m(i,0);
126 for (size_t j = 1; j < nc; j++) {
127 s << ", " << m(i,j);
128 }
129 s << std::endl;
130 }
131 return s;
132}
133
134void operator*=(Array2D& m, double a)
135{
136 scale(m.begin(), m.end(), m.begin(), a);
137}
138
139}
Header file for class Cantera::Array2D.
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:251
Array2D()=default
Default constructor.
size_t m_nrows
Number of rows.
Definition Array.h:254
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
iterator begin()
Return an iterator pointing to the first element.
Definition Array.cpp:100
iterator end()
Return an iterator pointing past the last element.
Definition Array.cpp:105
size_t nRows() const
Number of rows.
Definition Array.h:192
vector< double >::iterator iterator
Type definition for the iterator class that is can be used by Array2D types.
Definition Array.h:40
size_t m_ncols
Number of columns.
Definition Array.h:257
size_t nColumns() const
Number of columns.
Definition Array.h:197
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
double & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
Definition Array.h:176
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
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
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition utilities.h:104
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:134
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.
Definition AnyMap.cpp:1926
std::ostream & operator<<(std::ostream &s, const Array2D &m)
Output the current contents of the Array2D object.
Definition Array.cpp:120
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...