#pragma once #include #include "RowTypeMatrix.h" namespace MbD { template class FullColumn; template class FullRow; template class FullMatrix : public RowTypeMatrix>> { public: FullMatrix() {} FullMatrix(int m) : RowTypeMatrix>>(m) { } FullMatrix(int m, int n) { for (int i = 0; i < m; i++) { auto row = std::make_shared>(n); this->push_back(row); } } FullMatrix(std::initializer_list>> listOfRows) { for (auto& row : listOfRows) { this->push_back(row); } } FullMatrix(std::initializer_list> list2D) { for (auto& rowList : list2D) { auto row = std::make_shared>(rowList); this->push_back(row); } } void identity(); std::shared_ptr> column(int j); std::shared_ptr> timesFullColumn(std::shared_ptr> fullCol); std::shared_ptr> timesFullMatrix(std::shared_ptr> fullMat); std::shared_ptr> timesTransposeFullMatrix(std::shared_ptr> fullMat); std::shared_ptr> times(double a); std::shared_ptr> transposeTimesFullMatrix(std::shared_ptr> fullMat); std::shared_ptr> plusFullMatrix(std::shared_ptr> fullMat); std::shared_ptr> transpose(); std::shared_ptr> negated(); void symLowerWithUpper(); void atiput(int i, std::shared_ptr> fullRow); void atijput(int i, int j, T value); void atijputFullColumn(int i, int j, std::shared_ptr> fullCol); void atijplusFullRow(int i, int j, std::shared_ptr> fullRow); void atijplusNumber(int i, int j, double value); void atijminusNumber(int i, int j, double value); double sumOfSquares() override; void zeroSelf() override; std::shared_ptr> copy(); FullMatrix operator+(const FullMatrix fullMat); std::shared_ptr> transposeTimesFullColumn(const std::shared_ptr> fullCol); }; template<> inline void FullMatrix::identity() { this->zeroSelf(); for (int i = 0; i < this->size(); i++) { this->at(i)->at(i) = 1.0; } } template inline std::shared_ptr> FullMatrix::column(int j) { int n = (int)this->size(); auto answer = std::make_shared>(n); for (int i = 0; i < n; i++) { answer->at(i) = this->at(i)->at(j); } return answer; } template inline std::shared_ptr> FullMatrix::timesFullMatrix(std::shared_ptr> fullMat) { int m = this->nrow(); auto answer = std::make_shared>(m); for (int i = 0; i < m; i++) { answer->at(i) = this->at(i)->timesFullMatrix(fullMat); } return answer; } template inline std::shared_ptr> FullMatrix::timesTransposeFullMatrix(std::shared_ptr> fullMat) { int nrow = this->nrow(); auto answer = std::make_shared>(nrow); for (int i = 0; i < nrow; i++) { answer->at(i) = this->at(i)->timesTransposeFullMatrix(fullMat); } return answer; } template inline std::shared_ptr> FullMatrix::times(double a) { int m = this->nrow(); auto answer = std::make_shared>(m); for (int i = 0; i < m; i++) { answer->at(i) = this->at(i)->times(a); } return answer; } template inline std::shared_ptr> FullMatrix::transposeTimesFullMatrix(std::shared_ptr> fullMat) { return this->transpose()->timesFullMatrix(fullMat); } template inline std::shared_ptr> FullMatrix::plusFullMatrix(std::shared_ptr> fullMat) { int n = (int)this->size(); auto answer = std::make_shared>(n); for (int i = 0; i < n; i++) { answer->at(i) = this->at(i)->plusFullRow(fullMat->at(i)); } return answer; } template inline std::shared_ptr> FullMatrix::transpose() { int nrow = this->nrow(); auto ncol = this->ncol(); auto answer = std::make_shared>(ncol, nrow); for (int i = 0; i < nrow; i++) { auto& row = this->at(i); for (int j = 0; j < ncol; j++) { answer->at(j)->at(i) = row->at(j); } } return answer; } template inline std::shared_ptr> FullMatrix::negated() { return this->times(-1.0); } template inline void FullMatrix::symLowerWithUpper() { int n = (int)this->size(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { this->at(j)->at(i) = this->at(i)->at(j); } } } template inline void FullMatrix::atiput(int i, std::shared_ptr> fullRow) { this->at(i) = fullRow; } template inline void FullMatrix::atijput(int i, int j, T value) { this->at(i)->atiput(j, value); } template inline void FullMatrix::atijputFullColumn(int i1, int j1, std::shared_ptr> fullCol) { for (int ii = 0; ii < fullCol->size(); ii++) { this->at(i1 + ii)->at(j1) = fullCol->at(ii); } } template inline void FullMatrix::atijplusFullRow(int i, int j, std::shared_ptr> fullRow) { this->at(i)->atiplusFullRow(j, fullRow); } template inline void FullMatrix::atijplusNumber(int i, int j, double value) { auto rowi = this->at(i); rowi->at(j) += value; } template inline void FullMatrix::atijminusNumber(int i, int j, double value) { auto rowi = this->at(i); rowi->at(j) -= value; } template<> inline double FullMatrix::sumOfSquares() { double sum = 0.0; for (int i = 0; i < this->size(); i++) { sum += this->at(i)->sumOfSquares(); } return sum; } template inline double FullMatrix::sumOfSquares() { assert(false); return 0.0; } template<> inline void FullMatrix::zeroSelf() { for (int i = 0; i < this->size(); i++) { this->at(i)->zeroSelf(); } } template inline void FullMatrix::zeroSelf() { assert(false); } template inline std::shared_ptr> FullMatrix::copy() { auto m = (int)this->size(); auto answer = std::make_shared>(m); for (int i = 0; i < m; i++) { answer->at(i) = this->at(i)->copy(); } return answer; } template inline FullMatrix FullMatrix::operator+(const FullMatrix fullMat) { int n = (int)this->size(); auto answer = FullMatrix(n); for (int i = 0; i < n; i++) { answer.at(i) = this->at(i)->plusFullRow(fullMat.at(i)); } return answer; } template inline std::shared_ptr> FullMatrix::transposeTimesFullColumn(std::shared_ptr> fullCol) { auto sptr = std::make_shared>(*this); return fullCol->transpose()->timesFullMatrix(sptr)->transpose(); } template inline std::shared_ptr> FullMatrix::timesFullColumn(std::shared_ptr> fullCol) { //"a*b = a(i,j)b(j) sum j." auto nrow = this->nrow(); auto answer = std::make_shared>(nrow); for (int i = 0; i < nrow; i++) { answer->at(i) = this->at(i)->timesFullColumn(fullCol); } return answer; } using FMatDsptr = std::shared_ptr>; using FMatDsptr = std::shared_ptr>; using FMatFColDsptr = std::shared_ptr>>>; using FMatFMatDsptr = std::shared_ptr>>>; using FColFMatDsptr = std::shared_ptr>>>; }