Cantera  3.1.0a1
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"
10 #include "cantera/base/global.h"
11 
12 namespace Cantera
13 {
14 
15 Array2D::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 
22 Array2D::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 
29 Array2D::Array2D(const Array2D& y)
30  : m_data(y.m_data)
31  , m_nrows(y.m_nrows)
32  , m_ncols(y.m_ncols)
33 {
34 }
35 
36 Array2D& 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 
47 void 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 
54 void 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 
63 void 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 
72 void 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 
79 void 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 
86 void 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 
93 void 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 
100 std::ostream& operator<<(std::ostream& s, const Array2D& m)
101 {
102  size_t nr = m.nRows();
103  size_t nc = m.nColumns();
104  for (size_t i = 0; i < nr; i++) {
105  s << m(i,0);
106  for (size_t j = 1; j < nc; j++) {
107  s << ", " << m(i,j);
108  }
109  s << std::endl;
110  }
111  return s;
112 }
113 
114 void operator*=(Array2D& m, double a)
115 {
116  scale(m.data().begin(), m.data().end(), m.data().begin(), a);
117 }
118 
119 }
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:219
Array2D()=default
Default constructor.
size_t m_nrows
Number of rows.
Definition: Array.h:222
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
vector< double > & data()
Return a reference to the data vector.
Definition: Array.h:186
double & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
Definition: Array.h:160
size_t nRows() const
Number of rows.
Definition: Array.h:176
size_t m_ncols
Number of columns.
Definition: Array.h:225
size_t nColumns() const
Number of columns.
Definition: Array.h:181
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
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
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:114
std::ostream & operator<<(std::ostream &s, const Array2D &m)
Output the current contents of the Array2D object.
Definition: Array.cpp:100
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...