52#ifndef MATH_RECTMATRIX_HPP
53#define MATH_RECTMATRIX_HPP
65 template<
typename Real,
unsigned int Row,
unsigned int Col>
68 using ElemType = Real;
69 using ElemPoinerType = Real*;
73 for (
unsigned int i = 0; i < Row; i++)
74 for (
unsigned int j = 0; j < Col; j++)
75 this->data_[i][j] = 0.0;
80 for (
unsigned int i = 0; i < Row; i++)
81 for (
unsigned int j = 0; j < Col; j++)
82 this->data_[i][j] = s;
86 for (
unsigned int i = 0; i < Row; i++)
87 for (
unsigned int j = 0; j < Col; j++)
88 this->data_[i][j] = array[i * Row + j];
99 if (
this == &m)
return *
this;
101 for (
unsigned int i = 0; i < Row; i++)
102 for (
unsigned int j = 0; j < Col; j++)
103 this->data_[i][j] = m.data_[i][j];
115 return this->data_[i][j];
125 return this->data_[i][j];
133 for (
unsigned int i = 0; i < Row; i++) {
134 for (
unsigned int j = 0; j < Col; j++) {
135 array[i * Row + j] = this->data_[i][j];
151 for (
unsigned int i = 0; i < Col; i++)
152 v[i] = this->data_[row][i];
163 for (
unsigned int i = 0; i < Col; i++)
164 this->data_[row][i] = v[i];
175 for (
unsigned int j = 0; j < Row; j++)
176 v[j] = this->data_[j][col];
187 for (
unsigned int j = 0; j < Row; j++)
188 this->data_[j][col] = v[j];
196 void swapRow(
unsigned int i,
unsigned int j) {
197 assert(i < Row && j < Row);
199 for (
unsigned int k = 0; k < Col; k++)
200 std::swap(this->data_[i][k], this->data_[j][k]);
209 assert(i < Col && j < Col);
211 for (
unsigned int k = 0; k < Row; k++)
212 std::swap(this->data_[k][i], this->data_[k][j]);
224 for (
unsigned int i = 0; i < Row; i++)
225 for (
unsigned int j = 0; j < Col; j++)
226 if (!
equal(this->data_[i][j], m.data_[i][j]))
return false;
238 return !(*
this == m);
243 for (
unsigned int i = 0; i < Row; i++)
244 for (
unsigned int j = 0; j < Col; j++)
245 this->data_[i][j] = -this->data_[i][j];
253 for (
unsigned int i = 0; i < Row; i++)
254 for (
unsigned int j = 0; j < Col; j++)
255 this->data_[i][j] = -m.data_[i][j];
263 for (
unsigned int i = 0; i < Row; i++)
264 for (
unsigned int j = 0; j < Col; j++)
265 this->data_[i][j] += m.data_[i][j];
275 for (
unsigned int i = 0; i < Row; i++)
276 for (
unsigned int j = 0; j < Col; j++)
277 this->data_[i][j] = m1.data_[i][j] + m2.data_[i][j];
286 for (
unsigned int i = 0; i < Row; i++)
287 for (
unsigned int j = 0; j < Col; j++)
288 this->data_[i][j] -= m.data_[i][j];
299 for (
unsigned int i = 0; i < Row; i++)
300 for (
unsigned int j = 0; j < Col; j++)
301 this->data_[i][j] = m1.data_[i][j] - m2.data_[i][j];
310 for (
unsigned int i = 0; i < Row; i++)
311 for (
unsigned int j = 0; j < Col; j++)
312 this->data_[i][j] *= s;
322 for (
unsigned int i = 0; i < Row; i++)
323 for (
unsigned int j = 0; j < Col; j++)
324 this->data_[i][j] = s * m.data_[i][j];
333 for (
unsigned int i = 0; i < Row; i++)
334 for (
unsigned int j = 0; j < Col; j++)
335 this->data_[i][j] /= s;
345 for (
unsigned int i = 0; i < Row; i++)
346 for (
unsigned int j = 0; j < Col; j++)
347 this->data_[i][j] = m.data_[i][j] / s;
394 for (
unsigned int i = 0; i < Row; i++)
395 for (
unsigned int j = 0; j < Col; j++)
396 result(j, i) = this->data_[i][j];
401 template<
class MatrixType>
402 void setSubMatrix(
unsigned int beginRow,
unsigned int beginCol,
403 const MatrixType& m) {
404 assert(beginRow + m.getNRow() - 1 <= getNRow());
405 assert(beginCol + m.getNCol() - 1 <= getNCol());
407 for (
unsigned int i = 0; i < m.getNRow(); ++i)
408 for (
unsigned int j = 0; j < m.getNCol(); ++j)
409 this->data_[beginRow + i][beginCol + j] = m(i, j);
412 template<
class MatrixType>
413 void getSubMatrix(
unsigned int beginRow,
unsigned int beginCol,
415 assert(beginRow + m.getNRow() - 1 <= getNRow());
416 assert(beginCol + m.getNCol() - 1 <= getNCol());
418 for (
unsigned int i = 0; i < m.getNRow(); ++i)
419 for (
unsigned int j = 0; j < m.getNCol(); ++j)
420 m(i, j) = this->data_[beginRow + i][beginCol + j];
423 unsigned int getNRow()
const {
return Row; }
424 unsigned int getNCol()
const {
return Col; }
426 Real frobeniusNorm() {
428 for (
unsigned int i = 0; i < Row; i++) {
429 for (
unsigned int j = 0; j < Col; j++) {
430 norm += pow(abs(this->data_[i][j]), 2);
437 Real data_[Row][Col];
441 template<
typename Real,
unsigned int Row,
unsigned int Col>
457 template<
typename Real,
unsigned int Row,
unsigned int Col>
474 template<
typename Real,
unsigned int Row,
unsigned int Col>
491 template<
typename Real,
unsigned int Row,
unsigned int Col>
507 template<
typename Real,
unsigned int Row,
unsigned int Col>
523 template<
typename Real,
unsigned int Row,
unsigned int Col,
524 unsigned int SameDim>
530 for (
unsigned int i = 0; i < Row; i++)
531 for (
unsigned int j = 0; j < Col; j++)
532 for (
unsigned int k = 0; k < SameDim; k++)
533 result(i, j) += m1(i, k) * m2(k, j);
544 template<
typename Real,
unsigned int Row,
unsigned int Col>
549 for (
unsigned int i = 0; i < Row; i++)
550 for (
unsigned int j = 0; j < Col; j++)
551 result[i] += m(i, j) * v[j];
562 template<
typename Real,
unsigned int Row,
unsigned int Col>
567 for (
unsigned int i = 0; i < Col; i++)
568 for (
unsigned int j = 0; j < Row; j++)
569 result[i] += v[j] * m(j, i);
580 template<
typename Real,
unsigned int Row,
unsigned int Col>
601 template<
typename Real,
unsigned int Row,
unsigned int Col>
607 for (
unsigned int i = 0; i < Row; i++)
608 for (
unsigned int j = 0; j < Col; j++)
609 tmp += t1(i, j) * t2(i, j);
635 template<
typename Real,
unsigned int Row,
unsigned int Col>
642 for (
unsigned int i = 0; i < Row; i++) {
645 for (
unsigned int j = 0; j < Col; j++) {
646 result[i] += t1(i1, j) * t2(i2, j) - t1(i2, j) * t2(i1, j);
655 template<
typename Real,
unsigned int Row,
unsigned int Col>
656 std::ostream& operator<<(std::ostream& o,
658 for (
unsigned int i = 0; i < Row; i++) {
660 for (
unsigned int j = 0; j < Col; j++) {
662 if (j != Col - 1) o <<
"\t";
664 o <<
")" << std::endl;
RectMatrix< Real, Col, Row > transpose() const
Return the transpose of this matrix.
void negate(const RectMatrix< Real, Row, Col > &m)
Sets the value of this matrix to the negation of matrix m.
RectMatrix(const RectMatrix< Real, Row, Col > &m)
copy constructor
void sub(const RectMatrix< Real, Row, Col > &m1, const RectMatrix< Real, Row, Col > &m2)
Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2).
RectMatrix< Real, Row, Col > & operator-=(const RectMatrix< Real, Row, Col > &m)
Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m)
void sub(const RectMatrix< Real, Row, Col > &m)
Sets the value of this matrix to the difference of itself and m (*this -= m).
void add(const RectMatrix< Real, Row, Col > &m1, const RectMatrix< Real, Row, Col > &m2)
Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2).
Vector< Real, Row > getRow(unsigned int row)
Returns a row of this matrix as a vector.
void setRow(unsigned int row, const Vector< Real, Row > &v)
Sets a row of this matrix.
void mul(Real s, const RectMatrix< Real, Row, Col > &m)
Sets the value of this matrix to the scalar multiplication of matrix m (*this = s * m).
void div(Real s)
Sets the value of this matrix to the scalar division of itself (*this /= s ).
void setColumn(unsigned int col, const Vector< Real, Col > &v)
Sets a column of this matrix.
RectMatrix< Real, Row, Col > & operator/=(const Real s)
Divides every element of this matrix by a scalar.
RectMatrix< Real, Row, Col > & operator+=(const RectMatrix< Real, Row, Col > &m)
Sets the value of this matrix to the sum of the other matrix and itself (*this += m).
void swapColumn(unsigned int i, unsigned int j)
swap two Columns of this matrix
Vector< Real, Col > getColumn(unsigned int col)
Returns a column of this matrix as a vector.
RectMatrix()
default constructor
bool operator!=(const RectMatrix< Real, Row, Col > &m)
Tests if this matrix is not equal to matrix m.
void div(Real s, const RectMatrix< Real, Row, Col > &m)
Sets the value of this matrix to the scalar division of matrix m (*this = m /s).
void getArray(Real *array)
Copy the internal data to an array.
bool operator==(const RectMatrix< Real, Row, Col > &m)
Tests if this matrix is identical to matrix m.
void mul(Real s)
Sets the value of this matrix to the scalar multiplication of itself (*this *= s).
Real * getArrayPointer()
Returns the pointer of internal array.
Real & operator()(unsigned int i, unsigned int j)
Return the reference of a single element of this matrix.
void negate()
Negates the value of this matrix in place.
void swapRow(unsigned int i, unsigned int j)
swap two rows of this matrix
RectMatrix< Real, Row, Col > & operator*=(const Real s)
Multiples a scalar into every element of this matrix.
void add(const RectMatrix< Real, Row, Col > &m)
Sets the value of this matrix to the sum of itself and m (*this += m).
RectMatrix(Real s)
Constructs and initializes every element of this matrix to a scalar.
RectMatrix< Real, Row, Col > & operator=(const RectMatrix< Real, Row, Col > &m)
copy assignment operator
Real operator()(unsigned int i, unsigned int j) const
Return the value of a single element of this matrix.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
Real doubleDot(const RectMatrix< Real, Row, Col > &t1, const RectMatrix< Real, Row, Col > &t2)
Returns the tensor contraction (double dot product) of two rank 2 tensors (or Matrices)
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).
Vector< Real, Row > mCross(const RectMatrix< Real, Row, Col > &t1, const RectMatrix< Real, Row, Col > &t2)
Returns the vector (cross) product of two matrices.
DynamicRectMatrix< Real > operator/(const DynamicRectMatrix< Real > &m, Real s)
Return the scalar division of matrix (m / s).