55#ifndef MATH_RECTMATRIX_HPP
56#define MATH_RECTMATRIX_HPP
68 template<
typename Real,
unsigned int Row,
unsigned int Col>
71 using ElemType = Real;
72 using ElemPoinerType = Real*;
76 for (
unsigned int i = 0; i < Row; i++)
77 for (
unsigned int j = 0; j < Col; j++)
78 this->data_[i][j] = 0.0;
83 for (
unsigned int i = 0; i < Row; i++)
84 for (
unsigned int j = 0; j < Col; j++)
85 this->data_[i][j] = s;
89 for (
unsigned int i = 0; i < Row; i++)
90 for (
unsigned int j = 0; j < Col; j++)
91 this->data_[i][j] = array[i * Row + j];
102 if (
this == &m)
return *
this;
104 for (
unsigned int i = 0; i < Row; i++)
105 for (
unsigned int j = 0; j < Col; j++)
106 this->data_[i][j] = m.data_[i][j];
118 return this->data_[i][j];
128 return this->data_[i][j];
136 for (
unsigned int i = 0; i < Row; i++) {
137 for (
unsigned int j = 0; j < Col; j++) {
138 array[i * Row + j] = this->data_[i][j];
154 for (
unsigned int i = 0; i < Col; i++)
155 v[i] = this->data_[row][i];
166 for (
unsigned int i = 0; i < Col; i++)
167 this->data_[row][i] = v[i];
178 for (
unsigned int j = 0; j < Row; j++)
179 v[j] = this->data_[j][col];
190 for (
unsigned int j = 0; j < Row; j++)
191 this->data_[j][col] = v[j];
199 void swapRow(
unsigned int i,
unsigned int j) {
200 assert(i < Row && j < Row);
202 for (
unsigned int k = 0; k < Col; k++)
203 std::swap(this->data_[i][k], this->data_[j][k]);
212 assert(i < Col && j < Col);
214 for (
unsigned int k = 0; k < Row; k++)
215 std::swap(this->data_[k][i], this->data_[k][j]);
227 for (
unsigned int i = 0; i < Row; i++)
228 for (
unsigned int j = 0; j < Col; j++)
229 if (!
equal(this->data_[i][j], m.data_[i][j]))
return false;
241 return !(*
this == m);
246 for (
unsigned int i = 0; i < Row; i++)
247 for (
unsigned int j = 0; j < Col; j++)
248 this->data_[i][j] = -this->data_[i][j];
256 for (
unsigned int i = 0; i < Row; i++)
257 for (
unsigned int j = 0; j < Col; j++)
258 this->data_[i][j] = -m.data_[i][j];
266 for (
unsigned int i = 0; i < Row; i++)
267 for (
unsigned int j = 0; j < Col; j++)
268 this->data_[i][j] += m.data_[i][j];
278 for (
unsigned int i = 0; i < Row; i++)
279 for (
unsigned int j = 0; j < Col; j++)
280 this->data_[i][j] = m1.data_[i][j] + m2.data_[i][j];
289 for (
unsigned int i = 0; i < Row; i++)
290 for (
unsigned int j = 0; j < Col; j++)
291 this->data_[i][j] -= m.data_[i][j];
302 for (
unsigned int i = 0; i < Row; i++)
303 for (
unsigned int j = 0; j < Col; j++)
304 this->data_[i][j] = m1.data_[i][j] - m2.data_[i][j];
313 for (
unsigned int i = 0; i < Row; i++)
314 for (
unsigned int j = 0; j < Col; j++)
315 this->data_[i][j] *= s;
325 for (
unsigned int i = 0; i < Row; i++)
326 for (
unsigned int j = 0; j < Col; j++)
327 this->data_[i][j] = s * m.data_[i][j];
336 for (
unsigned int i = 0; i < Row; i++)
337 for (
unsigned int j = 0; j < Col; j++)
338 this->data_[i][j] /= s;
348 for (
unsigned int i = 0; i < Row; i++)
349 for (
unsigned int j = 0; j < Col; j++)
350 this->data_[i][j] = m.data_[i][j] / s;
397 for (
unsigned int i = 0; i < Row; i++)
398 for (
unsigned int j = 0; j < Col; j++)
399 result(j, i) = this->data_[i][j];
404 template<
class MatrixType>
405 void setSubMatrix(
unsigned int beginRow,
unsigned int beginCol,
406 const MatrixType& m) {
407 assert(beginRow + m.getNRow() - 1 <= getNRow());
408 assert(beginCol + m.getNCol() - 1 <= getNCol());
410 for (
unsigned int i = 0; i < m.getNRow(); ++i)
411 for (
unsigned int j = 0; j < m.getNCol(); ++j)
412 this->data_[beginRow + i][beginCol + j] = m(i, j);
415 template<
class MatrixType>
416 void getSubMatrix(
unsigned int beginRow,
unsigned int beginCol,
418 assert(beginRow + m.getNRow() - 1 <= getNRow());
419 assert(beginCol + m.getNCol() - 1 <= getNCol());
421 for (
unsigned int i = 0; i < m.getNRow(); ++i)
422 for (
unsigned int j = 0; j < m.getNCol(); ++j)
423 m(i, j) = this->data_[beginRow + i][beginCol + j];
426 unsigned int getNRow()
const {
return Row; }
427 unsigned int getNCol()
const {
return Col; }
429 Real frobeniusNorm() {
431 for (
unsigned int i = 0; i < Row; i++) {
432 for (
unsigned int j = 0; j < Col; j++) {
433 norm += pow(abs(this->data_[i][j]), 2);
440 Real data_[Row][Col];
444 template<
typename Real,
unsigned int Row,
unsigned int Col>
460 template<
typename Real,
unsigned int Row,
unsigned int Col>
477 template<
typename Real,
unsigned int Row,
unsigned int Col>
494 template<
typename Real,
unsigned int Row,
unsigned int Col>
510 template<
typename Real,
unsigned int Row,
unsigned int Col>
526 template<
typename Real,
unsigned int Row,
unsigned int Col,
527 unsigned int SameDim>
533 for (
unsigned int i = 0; i < Row; i++)
534 for (
unsigned int j = 0; j < Col; j++)
535 for (
unsigned int k = 0; k < SameDim; k++)
536 result(i, j) += m1(i, k) * m2(k, j);
547 template<
typename Real,
unsigned int Row,
unsigned int Col>
552 for (
unsigned int i = 0; i < Row; i++)
553 for (
unsigned int j = 0; j < Col; j++)
554 result[i] += m(i, j) * v[j];
565 template<
typename Real,
unsigned int Row,
unsigned int Col>
570 for (
unsigned int i = 0; i < Col; i++)
571 for (
unsigned int j = 0; j < Row; j++)
572 result[i] += v[j] * m(j, i);
583 template<
typename Real,
unsigned int Row,
unsigned int Col>
604 template<
typename Real,
unsigned int Row,
unsigned int Col>
610 for (
unsigned int i = 0; i < Row; i++)
611 for (
unsigned int j = 0; j < Col; j++)
612 tmp += t1(i, j) * t2(i, j);
638 template<
typename Real,
unsigned int Row,
unsigned int Col>
645 for (
unsigned int i = 0; i < Row; i++) {
648 for (
unsigned int j = 0; j < Col; j++) {
649 result[i] += t1(i1, j) * t2(i2, j) - t1(i2, j) * t2(i1, j);
658 template<
typename Real,
unsigned int Row,
unsigned int Col>
659 std::ostream& operator<<(std::ostream& o,
661 for (
unsigned int i = 0; i < Row; i++) {
663 for (
unsigned int j = 0; j < Col; j++) {
665 if (j != Col - 1) o <<
"\t";
667 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).