SCOREC core
Parallel unstructured mesh tools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
apfDynamicMatrix.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Scientific Computation Research Center
3  *
4  * This work is open source software, licensed under the terms of the
5  * BSD license as described in the LICENSE file in the top-level directory.
6  */
7 
8 #ifndef APFDYNAMICMATRIX_H
9 #define APFDYNAMICMATRIX_H
10 
14 #include "apfDynamicVector.h"
15 #include "apfMatrix.h"
16 #include "iostream"
17 
18 namespace apf {
19 
27 {
28  public:
32  DynamicMatrix(std::size_t m, std::size_t n):
33  columns(n),
34  values(m*n)
35  {}
37  std::size_t getRows() const {return values.getSize()/columns;}
39  std::size_t getColumns() const {return columns;}
41  void setSize(std::size_t m, std::size_t n)
42  {
43  columns = n;
44  values.setSize(m*n);
45  }
47  double operator()(std::size_t i, std::size_t j) const
48  {
49  return values[i*columns + j];
50  }
52  double& operator()(std::size_t i, std::size_t j)
53  {
54  return values[i*columns + j];
55  }
58  {
59  for (std::size_t i=0; i < this->values.getSize(); ++i)
60  this->values[i] += b.values[i];
61  return *this;
62  }
65  {
66  for (std::size_t i=0; i < this->values.getSize(); ++i)
67  this->values[i] -= b.values[i];
68  return *this;
69  }
72  {
73  for (std::size_t i=0; i < this->values.getSize(); ++i)
74  this->values[i] *= s;
75  return *this;
76  }
79  {
80  for (std::size_t i=0; i < this->values.getSize(); ++i)
81  this->values[i] /= s;
82  return *this;
83  }
85  void getRow(std::size_t i, DynamicVector& r) const
86  {
87  r.setSize(columns);
88  for (std::size_t j=0; j < columns; ++j)
89  r(j) = (*this)(i,j);
90  }
92  void getColumn(std::size_t j, DynamicVector& r) const
93  {
94  std::size_t rows = getRows();
95  r.setSize(rows);
96  for (std::size_t i=0; i < rows; ++i)
97  r(i) = (*this)(i,j);
98  }
100  void setRow(std::size_t i, DynamicVector const& r)
101  {
102  for (std::size_t j=0; j < columns; ++j)
103  (*this)(i,j) = r(j);
104  }
106  void setColumn(std::size_t j, DynamicVector const& r)
107  {
108  std::size_t rows = getRows();
109  for (std::size_t i=0; i < rows; ++i)
110  (*this)(i,j) = r(i);
111  }
112  void zero()
113  {
114  std::size_t rows = getRows();
115  std::size_t columns = getColumns();
116  for(std::size_t ii = 0; ii < rows; ii++)
117  for(std::size_t jj = 0; jj < columns; jj++)
118  (*this)(ii,jj) = 0.0;
119  }
120  protected:
121  std::size_t columns;
122  DynamicArray<double> values;
123 };
124 
126 inline void multiply(DynamicMatrix const& a,
127  DynamicVector const& b,
128  DynamicVector& r)
129 {
130  std::size_t rows = a.getRows();
131  std::size_t columns = a.getColumns();
132  r.setSize(rows);
133  for (std::size_t i=0; i < rows; ++i)
134  {
135  r[i] = a(i,0)*b[0];
136  for (std::size_t j=1; j < columns; ++j)
137  r[i] += a(i,j)*b[j];
138  }
139 }
140 
142 inline void multiply(DynamicVector const& b,
143  DynamicMatrix const& a,
144  DynamicVector& r)
145 {
146  std::size_t rows = a.getRows();
147  std::size_t columns = a.getColumns();
148  r.setSize(columns);
149  for (std::size_t j=0; j < columns; ++j)
150  {
151  r[j] = a(0,j)*b[0];
152  for (std::size_t i=1; i < rows; ++i)
153  r[j] += b[i]*a(i,j);
154  }
155 }
156 
158 inline void multiply(DynamicMatrix const& a,
159  DynamicMatrix const& b,
160  DynamicMatrix& r)
161 {
162  std::size_t rows = a.getRows();
163  std::size_t columns = b.getColumns();
164  std::size_t depth = b.getRows();
165  r.setSize(rows,columns);
166  for (std::size_t i=0; i < rows; ++i)
167  for (std::size_t j=0; j < columns; ++j)
168  {
169  r(i,j) = a(i,0)*b(0,j);
170  for (std::size_t k=1; k < depth; ++k)
171  r(i,j) += a(i,k)*b(k,j);
172  }
173 }
174 
176 inline void transpose(DynamicMatrix const& a,
177  DynamicMatrix& r)
178 {
179  std::size_t rows = a.getRows();
180  std::size_t columns = a.getColumns();
181  r.setSize(columns,rows);
182  for (std::size_t i=0; i < rows; ++i)
183  for (std::size_t j=0; j < columns; ++j)
184  r(j,i) = a(i,j);
185 }
186 
188 template <std::size_t N, std::size_t M>
190 {
191  DynamicMatrix result(N,M);
192  for(std::size_t ii = 0; ii < N; ii++)
193  for(std::size_t jj = 0; jj < M; jj++)
194  result(ii,jj) = other[ii][jj];
195  return result;
196 }
197 
198 }//namespace apf
199 
201 std::ostream& operator<<(std::ostream& s, apf::DynamicMatrix const& A);
202 
203 #endif
DynamicMatrix & operator+=(DynamicMatrix const &b)
add a matrix to this matrix
DynamicMatrix & operator*=(double s)
multiply this matrix by a scalar
double operator()(std::size_t i, std::size_t j) const
immutable index operator
Matrix< N, M > transpose(Matrix< M, N > const &m)
transpose a matrix
Definition: apfMatrix.h:111
void multiply(DynamicMatrix const &a, DynamicVector const &b, DynamicVector &r)
multiply a DynamicMatrix by a DynamicVector
std::ostream & operator<<(std::ostream &s, apf::Vector3 const &v)
write apf::Vector3 to a C++ stream
The APF linear algebra matrix interface.
A runtime-sized dense matrix.
double & operator()(std::size_t i, std::size_t j)
mutable index operator
void setColumn(std::size_t j, DynamicVector const &r)
copy column data from a DynamicVector
DynamicMatrix & operator/=(double s)
divide this matrix by a scalar
void getRow(std::size_t i, DynamicVector &r) const
copy row data into a DynamicVector
DynamicMatrix(std::size_t m, std::size_t n)
construct with size m by n
DynamicMatrix fromMatrix(Matrix< N, M > other)
convert an apf::Matrix into an apf::DynamicMatrix
std::size_t getRows() const
get the number of rows (first index)
std::size_t getColumns() const
get the number of columns (second index)
DynamicMatrix & operator-=(DynamicMatrix const &b)
subtract a matrix from this matrix
template-generic matrix of M by N doubles
Definition: apfMatrix.h:31
DynamicMatrix()
defautl constructor, no allocation
A runtime-sized linear algebra vector of doubles.
void setRow(std::size_t i, DynamicVector const &r)
copy row data from a DynamicVector
Small runtime-sized vectors.
void getColumn(std::size_t j, DynamicVector &r) const
copy column data into a DynamicVector
void setSize(std::size_t m, std::size_t n)
resize to m by n