/*************************************************************************** * Copyright (c) 2023 Ondsel, Inc. * * * * This file is part of OndselSolver. * * * * See LICENSE file for details about copyright. * ***************************************************************************/ #pragma once #include "FullVector.h" //#include "FullColumn.h" namespace MbD { template class FullRow; template using FRowsptr = std::shared_ptr>; using FRowDsptr = std::shared_ptr>; template class FullMatrix; template using FMatsptr = std::shared_ptr>; template class FullColumn; template using FColsptr = std::shared_ptr>; using ListFRD = std::initializer_list; template class FullRow : public FullVector { public: FullRow() : FullVector() {} FullRow(std::vector vec) : FullVector(vec) {} FullRow(size_t count) : FullVector(count) {} FullRow(size_t count, const T& value) : FullVector(count, value) {} FullRow(typename std::vector::const_iterator begin, typename std::vector::const_iterator end) : FullVector(begin, end) {} FullRow(std::initializer_list list) : FullVector{ list } {} FRowsptr times(T a); FRowsptr negated(); FRowsptr plusFullRow(FRowsptr fullRow); FRowsptr minusFullRow(FRowsptr fullRow); T timesFullColumn(FColsptr fullCol); T timesFullColumn(FullColumn* fullCol); FRowsptr timesFullMatrix(FMatsptr fullMat); FRowsptr timesTransposeFullMatrix(FMatsptr fullMat); void equalSelfPlusFullRowTimes(FRowsptr fullRow, double factor); void equalFullRow(FRowsptr fullRow); FColsptr transpose(); FRowsptr copy(); void atiplusFullRow(size_t j, FRowsptr fullRow); FMatsptr transposeTimesFullRow(FRowsptr fullRow); double dot(std::shared_ptr> vec); std::shared_ptr> dot(std::shared_ptr>>> vecvec); std::ostream& printOn(std::ostream& s) const override; }; template<> inline FRowDsptr FullRow::times(double a) { auto n = this->size(); auto answer = std::make_shared>(n); for (size_t i = 0; i < n; i++) { answer->at(i) = this->at(i) * a; } return answer; } template inline FRowsptr FullRow::times(T) { throw SimulationStoppingError("To be implemented."); } template inline FRowsptr FullRow::negated() { return this->times(-1.0); } template inline FRowsptr FullRow::plusFullRow(FRowsptr fullRow) { auto n = this->size(); auto answer = std::make_shared>(n); for (size_t i = 0; i < n; i++) { answer->at(i) = this->at(i) + fullRow->at(i); } return answer; } template inline FRowsptr FullRow::minusFullRow(FRowsptr fullRow) { auto n = this->size(); auto answer = std::make_shared>(n); for (size_t i = 0; i < n; i++) { answer->at(i) = this->at(i) - fullRow->at(i); } return answer; } template inline T FullRow::timesFullColumn(FColsptr fullCol) { return this->timesFullColumn(fullCol.get()); } template inline T FullRow::timesFullColumn(FullColumn* fullCol) { auto answer = this->at(0) * fullCol->at(0); for (size_t i = 1; i < this->size(); i++) { answer += this->at(i) * fullCol->at(i); } return answer; } template inline FRowsptr FullRow::timesTransposeFullMatrix(FMatsptr fullMat) { //"a*bT = a(1,j)b(k,j)" size_t ncol = fullMat->nrow(); auto answer = std::make_shared>(ncol); for (size_t k = 0; k < ncol; k++) { answer->at(k) = this->dot(fullMat->at(k)); } return answer; } template inline void FullRow::equalSelfPlusFullRowTimes(FRowsptr fullRow, double factor) { this->equalSelfPlusFullVectortimes(fullRow, factor); } template inline void FullRow::equalFullRow(FRowsptr fullRow) { this->equalArrayAt(fullRow, 0); } template inline FColsptr FullRow::transpose() { return std::make_shared>(*this); } template<> inline FRowDsptr FullRow::copy() { auto n = this->size(); auto answer = std::make_shared>(n); for (size_t i = 0; i < n; i++) { answer->at(i) = this->at(i); } return answer; } template inline void FullRow::atiplusFullRow(size_t j1, FRowsptr fullRow) { for (size_t jj = 0; jj < fullRow->size(); jj++) { auto j = j1 + jj; this->at(j) += fullRow->at(jj); } } template inline FMatsptr FullRow::transposeTimesFullRow(FRowsptr fullRow) { //"a*b = a(i)b(j)" auto nrow = this->size(); auto answer = std::make_shared>(nrow); for (size_t i = 0; i < nrow; i++) { answer->atiput(i, fullRow->times(this->at(i))); } return answer; } template inline double FullRow::dot(std::shared_ptr> vec) { auto n = this->size(); double answer = 0.0; for (size_t i = 0; i < n; i++) { answer += this->at(i) * vec->at(i); } return answer; } template inline std::shared_ptr> FullRow::dot(std::shared_ptr>>> vecvec) { auto ncol = this->size(); auto nelem = vecvec->at(0)->size(); auto answer = std::make_shared>(nelem); for (size_t k = 0; k < nelem; k++) { auto sum = 0.0; for (size_t i = 0; i < ncol; i++) { sum += this->at(i) * vecvec->at(i)->at(k); } answer->at(k) = sum; } return answer; } template inline std::ostream& FullRow::printOn(std::ostream& s) const { s << "FullRow{"; s << this->at(0); for (size_t i = 1; i < this->size(); i++) { s << ", " << this->at(i); } s << "}"; return s; } template inline FRowsptr FullRow::timesFullMatrix(FMatsptr fullMat) { FRowsptr answer = fullMat->at(0)->times(this->at(0)); for (size_t j = 1; j < this->size(); j++) { answer->equalSelfPlusFullRowTimes(fullMat->at(j), this->at(j)); } return answer; //return FRowsptr(); } }