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
8#include "cantera/base/Array.h"
9#include "cantera/base/utilities.h"
10
11 namespace Cantera
12{
13
14 Array2D::Array2D()
15 : m_nrows(0)
16 , m_ncols(0)
17{
18}
19
20 Array2D::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
27 Array2D::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
34 Array2D::Array2D(const Array2D& y)
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
52 void 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
59 void Array2D::appendColumn(const vector_fp& c)
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
68 void 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
77 void 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
84 void 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
91 void 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
98 void 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
120 void operator*=(Array2D& m, double a)
121{
122 scale(m.begin(), m.end(), m.begin(), a);
123}
124
125}
Array.h
Header file for class Cantera::Array2D.
Cantera::Array2D
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:30
Cantera::Array2D::m_nrows
size_t m_nrows
Number of rows.
Definition: Array.h:252
Cantera::Array2D::m_data
vector_fp m_data
Data stored in a single array.
Definition: Array.h:249
Cantera::Array2D::getColumn
void getColumn(size_t m, double *const col)
Get the values in column m.
Definition: Array.cpp:98
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:91
Cantera::Array2D::nRows
size_t nRows() const
Number of rows.
Definition: Array.h:188
Cantera::Array2D::m_ncols
size_t m_ncols
Number of columns.
Definition: Array.h:255
Cantera::Array2D::nColumns
size_t nColumns() const
Number of columns.
Definition: Array.h:193
Cantera::Array2D::setRow
void setRow(size_t n, const double *const rw)
Set the nth row to array rw.
Definition: Array.cpp:77
Cantera::Array2D::value
doublereal & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
Definition: Array.h:172
Cantera::Array2D::end
iterator end()
Return an iterator pointing past the last element.
Definition: Array.h:203
Cantera::Array2D::getRow
void getRow(size_t n, double *const rw)
Get the nth row and return it in a vector.
Definition: Array.cpp:84
Cantera::Array2D::begin
iterator begin()
Return an iterator pointing to the first element.
Definition: Array.h:198
Cantera::Array2D::appendColumn
void appendColumn(const vector_fp &c)
Append a column to the existing matrix using a std vector.
Definition: Array.cpp:59
Cantera::Array2D::resize
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
Cantera::Array2D::Array2D
Array2D()
Default constructor.
Definition: Array.cpp:14
Cantera
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:557
Cantera::operator*=
void operator*=(Array2D &m, double a)
Overload the times equals operator for multiplication of a matrix and a scalar.
Definition: Array.cpp:120
Cantera::vector_fp
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
Cantera::operator<<
std::ostream & operator<<(std::ostream &s, const Array2D &m)
Output the current contents of the Array2D object.
Definition: Array.cpp:106
Cantera::scale
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:100
utilities.h
Various templated functions that carry out common vector operations (see Templated Utility Functions)...