Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef APFDYNAMICMATRIX_H
00009 #define APFDYNAMICMATRIX_H
00010
00014 #include "apfDynamicVector.h"
00015 #include "apfMatrix.h"
00016 #include "iostream"
00017
00018 namespace apf {
00019
00026 class DynamicMatrix
00027 {
00028 public:
00030 DynamicMatrix() {}
00032 DynamicMatrix(std::size_t m, std::size_t n):
00033 columns(n),
00034 values(m*n)
00035 {}
00037 std::size_t getRows() const {return values.getSize()/columns;}
00039 std::size_t getColumns() const {return columns;}
00041 void setSize(std::size_t m, std::size_t n)
00042 {
00043 columns = n;
00044 values.setSize(m*n);
00045 }
00047 double operator()(std::size_t i, std::size_t j) const
00048 {
00049 return values[i*columns + j];
00050 }
00052 double& operator()(std::size_t i, std::size_t j)
00053 {
00054 return values[i*columns + j];
00055 }
00057 DynamicMatrix& operator+=(DynamicMatrix const& b)
00058 {
00059 for (std::size_t i=0; i < this->values.getSize(); ++i)
00060 this->values[i] += b.values[i];
00061 return *this;
00062 }
00064 DynamicMatrix& operator-=(DynamicMatrix const& b)
00065 {
00066 for (std::size_t i=0; i < this->values.getSize(); ++i)
00067 this->values[i] += b.values[i];
00068 return *this;
00069 }
00071 DynamicMatrix& operator*=(double s)
00072 {
00073 for (std::size_t i=0; i < this->values.getSize(); ++i)
00074 this->values[i] *= s;
00075 return *this;
00076 }
00078 DynamicMatrix& operator/=(double s)
00079 {
00080 for (std::size_t i=0; i < this->values.getSize(); ++i)
00081 this->values[i] *= s;
00082 return *this;
00083 }
00085 void getRow(std::size_t i, DynamicVector& r) const
00086 {
00087 r.setSize(columns);
00088 for (std::size_t j=0; j < columns; ++j)
00089 r(j) = (*this)(i,j);
00090 }
00092 void getColumn(std::size_t j, DynamicVector& r) const
00093 {
00094 std::size_t rows = getRows();
00095 r.setSize(rows);
00096 for (std::size_t i=0; i < rows; ++i)
00097 r(i) = (*this)(i,j);
00098 }
00100 void setRow(std::size_t i, DynamicVector const& r)
00101 {
00102 for (std::size_t j=0; j < columns; ++j)
00103 (*this)(i,j) = r(j);
00104 }
00106 void setColumn(std::size_t j, DynamicVector const& r)
00107 {
00108 std::size_t rows = getRows();
00109 for (std::size_t i=0; i < rows; ++i)
00110 (*this)(i,j) = r(i);
00111 }
00112 void zero()
00113 {
00114 std::size_t rows = getRows();
00115 std::size_t columns = getColumns();
00116 for(std::size_t ii = 0; ii < rows; ii++)
00117 for(std::size_t jj = 0; jj < columns; jj++)
00118 (*this)(ii,jj) = 0.0;
00119 }
00120 protected:
00121 std::size_t columns;
00122 DynamicArray<double> values;
00123 };
00124
00126 inline void multiply(DynamicMatrix const& a,
00127 DynamicVector const& b,
00128 DynamicVector& r)
00129 {
00130 std::size_t rows = a.getRows();
00131 std::size_t columns = a.getColumns();
00132 r.setSize(rows);
00133 for (std::size_t i=0; i < rows; ++i)
00134 {
00135 r[i] = a(i,0)*b[0];
00136 for (std::size_t j=1; j < columns; ++j)
00137 r[i] += a(i,j)*b[j];
00138 }
00139 }
00140
00142 inline void multiply(DynamicVector const& b,
00143 DynamicMatrix const& a,
00144 DynamicVector& r)
00145 {
00146 std::size_t rows = a.getRows();
00147 std::size_t columns = a.getColumns();
00148 r.setSize(columns);
00149 for (std::size_t j=0; j < columns; ++j)
00150 {
00151 r[j] = a(0,j)*b[0];
00152 for (std::size_t i=1; i < rows; ++i)
00153 r[j] += b[i]*a(i,j);
00154 }
00155 }
00156
00158 inline void multiply(DynamicMatrix const& a,
00159 DynamicMatrix const& b,
00160 DynamicMatrix& r)
00161 {
00162 std::size_t rows = a.getRows();
00163 std::size_t columns = b.getColumns();
00164 std::size_t depth = b.getRows();
00165 r.setSize(rows,columns);
00166 for (std::size_t i=0; i < rows; ++i)
00167 for (std::size_t j=0; j < columns; ++j)
00168 {
00169 r(i,j) = a(i,0)*b(0,j);
00170 for (std::size_t k=1; k < depth; ++k)
00171 r(i,j) += a(i,k)*b(k,j);
00172 }
00173 }
00174
00176 inline void transpose(DynamicMatrix const& a,
00177 DynamicMatrix& r)
00178 {
00179 std::size_t rows = a.getRows();
00180 std::size_t columns = a.getColumns();
00181 r.setSize(columns,rows);
00182 for (std::size_t i=0; i < rows; ++i)
00183 for (std::size_t j=0; j < columns; ++j)
00184 r(j,i) = a(i,j);
00185 }
00186
00188 template <std::size_t N, std::size_t M>
00189 inline DynamicMatrix fromMatrix(Matrix<N,M> other)
00190 {
00191 DynamicMatrix result(N,M);
00192 for(std::size_t ii = 0; ii < N; ii++)
00193 for(std::size_t jj = 0; jj < M; jj++)
00194 result(ii,jj) = other[ii][jj];
00195 return result;
00196 }
00197
00198 }
00199
00201 std::ostream& operator<<(std::ostream& s, apf::DynamicMatrix const& A);
00202
00203 #endif