52#ifndef MATH_DYNAMICRECTMATRIX_HPP
53#define MATH_DYNAMICRECTMATRIX_HPP
66 template<
typename Real>
69 using ElemType = Real;
70 using ElemPoinerType = Real*;
83 for (
unsigned int i = 0; i < nrow_; i++)
84 for (
unsigned int j = 0; j < ncol_; j++)
85 this->data_[i][j] = 0.0;
91 for (
unsigned int i = 0; i < nrow_; i++)
92 for (
unsigned int j = 0; j < ncol_; j++)
93 this->data_[i][j] = s;
98 for (
unsigned int i = 0; i < nrow_; i++)
99 for (
unsigned int j = 0; j < ncol_; j++)
100 this->data_[i][j] = array[i * ncol_ + j];
105 allocate(m.getNRow(), m.getNCol());
107 for (
unsigned int i = 0; i < nrow_; i++)
108 for (
unsigned int j = 0; j < ncol_; j++)
109 this->data_[i][j] = m.data_[i][j];
117 if (
this == &m)
return *
this;
118 if (nrow_ != m.getNRow() || ncol_ != m.getNCol()) {
120 allocate(m.getNRow(), m.getNCol());
123 for (
unsigned int i = 0; i < nrow_; i++)
124 for (
unsigned int j = 0; j < ncol_; j++)
125 this->data_[i][j] = m.data_[i][j];
136 return this->data_[i][j];
146 return this->data_[i][j];
154 for (
unsigned int i = 0; i < nrow_; i++) {
155 for (
unsigned int j = 0; j < ncol_; j++) {
156 array[i * nrow_ + j] = this->data_[i][j];
169 for (
unsigned int i = 0; i < ncol_; i++)
170 v[i] = this->data_[row][i];
181 assert(v.size() == nrow_);
182 for (
unsigned int i = 0; i < ncol_; i++)
183 this->data_[row][i] = v[i];
194 for (
unsigned int j = 0; j < nrow_; j++)
195 v[j] = this->data_[j][col];
206 for (
unsigned int j = 0; j < nrow_; j++)
207 this->data_[j][col] = v[j];
215 void swapRow(
unsigned int i,
unsigned int j) {
216 assert(i < nrow_ && j < nrow_);
218 for (
unsigned int k = 0; k < ncol_; k++)
219 std::swap(this->data_[i][k], this->data_[j][k]);
228 assert(i < ncol_ && j < ncol_);
230 for (
unsigned int k = 0; k < nrow_; k++)
231 std::swap(this->data_[k][i], this->data_[k][j]);
243 assert(nrow_ == m.getNRow() && ncol_ == m.getNCol());
244 for (
unsigned int i = 0; i < nrow_; i++)
245 for (
unsigned int j = 0; j < ncol_; j++)
246 if (!
equal(this->data_[i][j], m.data_[i][j]))
return false;
261 for (
unsigned int i = 0; i < nrow_; i++)
262 for (
unsigned int j = 0; j < ncol_; j++)
263 this->data_[i][j] = -this->data_[i][j];
271 for (
unsigned int i = 0; i < nrow_; i++)
272 for (
unsigned int j = 0; j < ncol_; j++)
273 this->data_[i][j] = -m.data_[i][j];
281 assert(nrow_ == m.getNRow() && ncol_ == m.getNCol());
282 for (
unsigned int i = 0; i < nrow_; i++)
283 for (
unsigned int j = 0; j < ncol_; j++)
284 this->data_[i][j] += m.data_[i][j];
294 assert(m1.getNRow() == m2.getNRow() && m1.getNCol() == m2.getNCol());
295 for (
unsigned int i = 0; i < nrow_; i++)
296 for (
unsigned int j = 0; j < ncol_; j++)
297 this->data_[i][j] = m1.data_[i][j] + m2.data_[i][j];
306 assert(nrow_ == m.getNRow() && ncol_ == m.getNCol());
307 for (
unsigned int i = 0; i < nrow_; i++)
308 for (
unsigned int j = 0; j < ncol_; j++)
309 this->data_[i][j] -= m.data_[i][j];
320 assert(m1.getNRow() == m2.getNRow() && m1.getNCol() == m2.getNCol());
321 for (
unsigned int i = 0; i < nrow_; i++)
322 for (
unsigned int j = 0; j < ncol_; j++)
323 this->data_[i][j] = m1.data_[i][j] - m2.data_[i][j];
332 for (
unsigned int i = 0; i < nrow_; i++)
333 for (
unsigned int j = 0; j < ncol_; j++)
334 this->data_[i][j] *= s;
344 assert(nrow_ == m.getNRow() && ncol_ == m.getNCol());
345 for (
unsigned int i = 0; i < nrow_; i++)
346 for (
unsigned int j = 0; j < ncol_; j++)
347 this->data_[i][j] = s * m.data_[i][j];
356 for (
unsigned int i = 0; i < nrow_; i++)
357 for (
unsigned int j = 0; j < ncol_; j++)
358 this->data_[i][j] /= s;
368 assert(nrow_ == m.getNRow() && ncol_ == m.getNCol());
369 for (
unsigned int i = 0; i < nrow_; i++)
370 for (
unsigned int j = 0; j < ncol_; j++)
371 this->data_[i][j] = m.data_[i][j] / s;
398 assert(nrow_ == m.getNRow() && ncol_ == m.getNCol());
409 assert(nrow_ == m.getNRow() && ncol_ == m.getNCol());
418 for (
unsigned int i = 0; i < nrow_; i++)
419 for (
unsigned int j = 0; j < ncol_; j++)
420 result(j, i) = this->data_[i][j];
425 unsigned int getNRow()
const {
return nrow_; }
426 unsigned int getNCol()
const {
return ncol_; }
428 template<
class MatrixType>
429 void setSubMatrix(
unsigned int beginRow,
unsigned int beginCol,
430 const MatrixType& m) {
431 assert(beginRow + m.getNRow() - 1 <= nrow_);
432 assert(beginCol + m.getNCol() - 1 <= ncol_);
434 for (
unsigned int i = 0; i < m.getNRow(); ++i)
435 for (
unsigned int j = 0; j < m.getNCol(); ++j)
436 this->data_[beginRow + i][beginCol + j] = m(i, j);
439 template<
class MatrixType>
440 void getSubMatrix(
unsigned int beginRow,
unsigned int beginCol,
442 assert(beginRow + m.getNRow() - 1 <= nrow_);
443 assert(beginCol + m.getNCol() - 1 <= ncol_);
445 for (
unsigned int i = 0; i < m.getNRow(); ++i)
446 for (
unsigned int j = 0; j < m.getNCol(); ++j)
447 m(i, j) = this->data_[beginRow + i][beginCol + j];
456 void allocate(
unsigned int nrow,
unsigned int ncol) {
457 nrow_ = (
unsigned int)nrow;
458 ncol_ = (
unsigned int)ncol;
459 data_ =
new Real*[nrow_];
460 for (
unsigned int i = 0; i < nrow_; ++i)
461 data_[i] =
new Real[ncol_];
465 for (
unsigned int i = 0; i < nrow_; ++i)
476 template<
typename Real>
491 template<
typename Real>
507 template<
typename Real>
523 template<
typename Real>
539 template<
typename Real>
555 template<
typename Real>
558 assert(m1.getNCol() == m2.getNRow());
559 unsigned int sameDim = m1.getNCol();
560 unsigned int nrow = m1.getNRow();
561 unsigned int ncol = m2.getNCol();
563 for (
unsigned int i = 0; i < nrow; i++)
564 for (
unsigned int j = 0; j < ncol; j++)
565 for (
unsigned int k = 0; k < sameDim; k++)
566 result(i, j) += m1(i, k) * m2(k, j);
577 template<
typename Real>
580 unsigned int nrow = m.getNRow();
581 unsigned int ncol = m.getNCol();
582 assert(ncol == v.size());
585 for (
unsigned int i = 0; i < nrow; i++)
586 for (
unsigned int j = 0; j < ncol; j++)
587 result[i] += m(i, j) * v[j];
598 template<
typename Real>
611 template<
typename Real>
613 for (
unsigned int i = 0; i < m.getNRow(); i++) {
615 for (
unsigned int j = 0; j < m.getNCol(); j++) {
617 if (j != m.getNCol() - 1) o <<
"\t";
619 o <<
")" << std::endl;
void add(const DynamicRectMatrix< Real > &m)
Sets the value of this matrix to the sum of itself and m (*this += m).
void mul(Real s, const DynamicRectMatrix< Real > &m)
Sets the value of this matrix to the scalar multiplication of matrix m (*this = s * m).
~DynamicRectMatrix()
destructor
void swapColumn(unsigned int i, unsigned int j)
swap two Columns of this matrix
void negate()
Negates the value of this matrix in place.
DynamicRectMatrix< Real > operator-=(const DynamicRectMatrix< Real > m)
Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m)
DynamicVector< Real > getRow(unsigned int row)
Returns a row of this matrix as a vector.
void swapRow(unsigned int i, unsigned int j)
swap two rows of this matrix
void div(Real s)
Sets the value of this matrix to the scalar division of itself (*this /= s ).
DynamicRectMatrix< Real > operator+=(const DynamicRectMatrix< Real > m)
Sets the value of this matrix to the sum of the other matrix and itself (*this += m).
Real & operator()(unsigned int i, unsigned int j)
Returns the reference of a single element of this matrix.
Real operator()(unsigned int i, unsigned int j) const
Returns the value of a single element of this matrix.
bool operator!=(const DynamicRectMatrix< Real > &m)
Tests if this matrix is not equal to matrix m.
void div(Real s, const DynamicRectMatrix< Real > &m)
Sets the value of this matrix to the scalar division of matrix m (*this = m /s).
void negate(const DynamicRectMatrix< Real > &m)
Sets the value of this matrix to the negation of matrix m.
void mul(Real s)
Sets the value of this matrix to the scalar multiplication of itself (*this *= s).
void setColumn(unsigned int col, const DynamicVector< Real > &v)
Sets a column of this matrix.
DynamicRectMatrix< Real > operator*=(const Real s)
Multiples a scalar onto every element of this matrix.
void sub(const DynamicRectMatrix< Real > &m)
Sets the value of this matrix to the difference of itself and m (*this -= m).
DynamicRectMatrix< Real > transpose() const
Return the transpose of this matrix.
bool operator==(const DynamicRectMatrix< Real > &m)
Tests if this matrix is identical to matrix m.
DynamicRectMatrix< Real > operator/=(const Real s)
Divides every element of this matrix by a scalar.
void setRow(unsigned int row, const DynamicVector< Real > &v)
Sets a row of this matrix.
void add(const DynamicRectMatrix< Real > &m1, const DynamicRectMatrix< Real > &m2)
Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2).
DynamicVector< Real > getColumn(unsigned int col)
Returns a column of this matrix as a vector.
void sub(const DynamicRectMatrix< Real > &m1, const DynamicRectMatrix< Real > &m2)
Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2).
void getArray(Real *array)
Copies the internal data to an array.
DynamicRectMatrix()
default constructor
DynamicRectMatrix(const SelfType &m)
copy constructor
DynamicRectMatrix< Real > operator=(const DynamicRectMatrix< Real > &m)
copy assignment operator
DynamicRectMatrix(unsigned int nrow, unsigned int ncol, Real s)
Constructs and initializes every element of this matrix to a scalar.
Dynamically-sized vector class.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
DynamicRectMatrix< Real > operator-(const DynamicRectMatrix< Real > &m)
Negate the value of every element of this matrix.
bool equal(const Polynomial< Real > &p1, const Polynomial< Real > &p2)
Tests if two polynomial have the same exponents.
DynamicRectMatrix< Real > operator*(const DynamicRectMatrix< Real > &m, Real s)
Return the multiplication of scalar and matrix (m * s).
DynamicRectMatrix< Real > operator/(const DynamicRectMatrix< Real > &m, Real s)
Return the scalar division of matrix (m / s).