55#ifndef MATH_DYNAMICRECTMATRIX_HPP
56#define MATH_DYNAMICRECTMATRIX_HPP
77 template<
typename Real>
80 using ElemType = Real;
81 using ElemPoinerType = Real*;
88 nrow_(nrow), ncol_(ncol), data_(nrow * ncol, Real(0)) {}
92 nrow_(nrow), ncol_(ncol), data_(nrow * ncol, s) {}
95 nrow_(nrow), ncol_(ncol), data_(array, array + nrow * ncol) {}
118 return data_[i * ncol_ + j];
127 return data_[i * ncol_ + j];
135 for (
unsigned int i = 0; i < nrow_ * ncol_; i++)
149 for (
unsigned int i = 0; i < ncol_; i++)
150 v[i] = data_[row * ncol_ + i];
160 assert(v.size() == ncol_);
161 for (
unsigned int i = 0; i < ncol_; i++)
162 data_[row * ncol_ + i] = v[i];
171 for (
unsigned int j = 0; j < nrow_; j++)
172 v[j] = data_[j * ncol_ + col];
182 for (
unsigned int j = 0; j < nrow_; j++)
183 data_[j * ncol_ + col] = v[j];
191 void swapRow(
unsigned int i,
unsigned int j) {
192 assert(i < nrow_ && j < nrow_);
193 for (
unsigned int k = 0; k < ncol_; k++)
194 std::swap(data_[i * ncol_ + k], data_[j * ncol_ + k]);
203 assert(i < ncol_ && j < ncol_);
204 for (
unsigned int k = 0; k < nrow_; k++)
205 std::swap(data_[k * ncol_ + i], data_[k * ncol_ + j]);
210 unsigned int dim = (nrow_ < ncol_) ? nrow_ : ncol_;
212 for (
unsigned int i = 0; i < dim; ++i)
213 result(i) = data_[i * ncol_ + i];
222 assert(nrow_ == m.nrow_ && ncol_ == m.ncol_);
223 for (
unsigned int i = 0; i < nrow_ * ncol_; i++)
224 if (!
equal(data_[i], m.data_[i]))
return false;
233 return !(*
this == m);
238 for (
auto& v : data_) v = -v;
246 assert(nrow_ == m.nrow_ && ncol_ == m.ncol_);
247 for (
unsigned int i = 0; i < nrow_ * ncol_; i++)
248 data_[i] = -m.data_[i];
256 assert(nrow_ == m.nrow_ && ncol_ == m.ncol_);
257 for (
unsigned int i = 0; i < nrow_ * ncol_; i++)
258 data_[i] += m.data_[i];
268 assert(m1.nrow_ == m2.nrow_ && m1.ncol_ == m2.ncol_);
269 for (
unsigned int i = 0; i < nrow_ * ncol_; i++)
270 data_[i] = m1.data_[i] + m2.data_[i];
279 assert(nrow_ == m.nrow_ && ncol_ == m.ncol_);
280 for (
unsigned int i = 0; i < nrow_ * ncol_; i++)
281 data_[i] -= m.data_[i];
292 assert(m1.nrow_ == m2.nrow_ && m1.ncol_ == m2.ncol_);
293 for (
unsigned int i = 0; i < nrow_ * ncol_; i++)
294 data_[i] = m1.data_[i] - m2.data_[i];
303 for (
auto& v : data_) v *= s;
313 assert(nrow_ == m.nrow_ && ncol_ == m.ncol_);
314 for (
unsigned int i = 0; i < nrow_ * ncol_; i++)
315 data_[i] = s * m.data_[i];
324 for (
auto& v : data_) v /= s;
334 assert(nrow_ == m.nrow_ && ncol_ == m.ncol_);
335 for (
unsigned int i = 0; i < nrow_ * ncol_; i++)
336 data_[i] = m.data_[i] / s;
380 for (
unsigned int i = 0; i < nrow_; i++)
381 for (
unsigned int j = 0; j < ncol_; j++)
382 result(j, i) = data_[i * ncol_ + j];
386 unsigned int getNRow()
const {
return nrow_; }
387 unsigned int getNCol()
const {
return ncol_; }
389 template<
class MatrixType>
390 void setSubMatrix(
unsigned int beginRow,
unsigned int beginCol,
391 const MatrixType& m) {
392 assert(beginRow + m.getNRow() - 1 <= nrow_);
393 assert(beginCol + m.getNCol() - 1 <= ncol_);
394 for (
unsigned int i = 0; i < m.getNRow(); ++i)
395 for (
unsigned int j = 0; j < m.getNCol(); ++j)
396 data_[(beginRow + i) * ncol_ + (beginCol + j)] = m(i, j);
399 template<
class MatrixType>
400 void getSubMatrix(
unsigned int beginRow,
unsigned int beginCol,
402 assert(beginRow + m.getNRow() - 1 <= nrow_);
403 assert(beginCol + m.getNCol() - 1 <= ncol_);
404 for (
unsigned int i = 0; i < m.getNRow(); ++i)
405 for (
unsigned int j = 0; j < m.getNCol(); ++j)
406 m(i, j) = data_[(beginRow + i) * ncol_ + (beginCol + j)];
412 std::vector<Real> data_;
416 template<
typename Real>
428 template<
typename Real>
441 template<
typename Real>
454 template<
typename Real>
467 template<
typename Real>
480 template<
typename Real>
483 assert(m1.getNCol() == m2.getNRow());
484 unsigned int sameDim = m1.getNCol();
485 unsigned int nrow = m1.getNRow();
486 unsigned int ncol = m2.getNCol();
488 for (
unsigned int i = 0; i < nrow; i++)
489 for (
unsigned int j = 0; j < ncol; j++)
490 for (
unsigned int k = 0; k < sameDim; k++)
491 result(i, j) += m1(i, k) * m2(k, j);
500 template<
typename Real>
503 unsigned int nrow = m.getNRow();
504 unsigned int ncol = m.getNCol();
505 assert(ncol == v.size());
507 for (
unsigned int i = 0; i < nrow; i++)
508 for (
unsigned int j = 0; j < ncol; j++)
509 result[i] += m(i, j) * v[j];
518 template<
typename Real>
529 template<
typename Real>
531 for (
unsigned int i = 0; i < m.getNRow(); i++) {
533 for (
unsigned int j = 0; j < m.getNCol(); j++) {
535 if (j != m.getNCol() - 1) o <<
"\t";
537 o <<
")" << std::endl;
DynamicRectMatrix()
default constructor
Rectangular matrix class with contiguous flat storage.
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< Real > & operator-=(const DynamicRectMatrix< Real > &m)
Sets the value of this matrix to the difference of itself and the other matrix (*this -= m).
DynamicRectMatrix< Real > & operator=(DynamicRectMatrix< Real > &&m) noexcept=default
move assignment operator
void swapColumn(unsigned int i, unsigned int j)
swap two columns of this matrix
void negate()
Negates the value of this matrix in place.
DynamicVector< Real > diagonals() const
Returns a DynamicVector of the diagonal elements.
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).
Real & operator()(unsigned int i, unsigned int j)
Returns the reference of a single element of this matrix.
bool operator==(const DynamicRectMatrix< Real > &m) const
Tests if this matrix is identical to matrix m.
Real operator()(unsigned int i, unsigned int j) const
Returns the value of a single element of this matrix.
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).
~DynamicRectMatrix()=default
destructor
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.
DynamicRectMatrix< Real > & operator+=(const DynamicRectMatrix< Real > &m)
Sets the value of this matrix to the sum of the other matrix and itself (*this += m).
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.
void getArray(Real *array) const
Copies the internal data to an array (row-major order).
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).
DynamicRectMatrix()
default constructor
DynamicRectMatrix< Real > & operator/=(const Real s)
Divides every element of this matrix by a scalar.
DynamicRectMatrix(unsigned int nrow, unsigned int ncol, Real s)
Constructs and initializes every element of this matrix to a scalar.
bool operator!=(const DynamicRectMatrix< Real > &m) const
Tests if this matrix is not equal to matrix m.
DynamicRectMatrix(SelfType &&m) noexcept=default
move constructor
DynamicRectMatrix< Real > & operator=(const DynamicRectMatrix< Real > &m)=default
copy assignment operator
Real * getArrayPointer()
Returns a pointer to the contiguous internal storage (row-major).
DynamicRectMatrix(const SelfType &m)=default
copy constructor
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).