void IdentityMatrix<TYPE>::Solve(const BaseMatrix<TYPE>& in, BaseMatrix<TYPE>& out) const { int n = GeneralMatrix<TYPE>::nrows; assert(n == in.Nrows()); assert(in.Ncols() == out.Ncols() && in.Nrows() == out.Nrows()); std::shared_ptr<LinearEquationSolver<TYPE> > solver = this->MakeSolver(); solver->Solve(in, out); }
void CholeskySolver<TYPE>::Solve(const BaseMatrix<TYPE> &in, BaseMatrix<TYPE> &out) const { assert(in.Nrows() == lm.Ncols()); assert(in.Nrows() == out.Nrows() && in.Ncols() == out.Ncols()); if (LinearEquationSolver<TYPE>::fail) { Singleton<Tracer>::Instance()->AddMessage("CholeskySolver::Solve(in, out)"); throw NPDException(SimpleSolver<TYPE>::mat); } Matrix<TYPE> temp(out.Nrows(), out.Ncols()); lm.Solve(in, temp); t(lm).Solve(temp, out); }
void LUsolverNoPivot<TYPE>::Solve(const BaseMatrix<TYPE> &in, BaseMatrix<TYPE> &out) const { assert(in.Nrows() == combine.Ncols()); assert(in.Nrows() == out.Nrows() && in.Ncols() == out.Ncols()); if (LinearEquationSolver<TYPE>::IsFailed()) { Singleton<Tracer>::Instance()->AddMessage("LUsolverNoPivot::Solve"); throw LogicError("LUsolverNoPivot: LU decomposition is failed"); } Matrix<TYPE> t(in.Nrows(), in.Ncols()); LUsolver<TYPE>::lm.Solve(in, t); LUsolver<TYPE>::um.Solve(t, out); }
void BandLUsolverPartialPivot<TYPE>::Solve(const BaseMatrix<TYPE> &in, BaseMatrix<TYPE> &out) const { assert(in.Nrows() == combine.Ncols()); assert(in.Nrows() == out.Nrows() && in.Ncols() == out.Ncols()); if (LinearEquationSolver<TYPE>::IsFailed()) { Singleton<Tracer>::Instance()->AddMessage("BandLUsolverPartialPivot::Solve"); throw SingularException(BandLUsolver<TYPE>::mat); } const PermuteMatrix<TYPE> &lp = BandLUsolver<TYPE>::left; Matrix<TYPE> t(in.Nrows(), in.Ncols()); lm.Solve(c_perm(lp, in), t); um.Solve(t, out); }
BandLUsolverPartialPivot<TYPE>::BandLUsolverPartialPivot(const BaseMatrix<TYPE> &bm, const TYPE &e) : BandLUsolver<TYPE>(bm, bm.BandWidth().Lower(), std::min(bm.BandWidth().Lower() + bm.BandWidth().Upper(), bm.Nrows() - 1), e), lm(bm.Nrows()), um(bm.Nrows(), BandLUsolver<TYPE>::ubw), combine(lm, um) { lm << bm; um << bm; static const TYPE one(1); for (int i = 1; i <= bm.Nrows(); ++i) { lm(i, i) = one; } BandLUdecomposion(); }
void SymmetricBandMatrix<TYPE>::operator<<(const BaseMatrix<TYPE>& bm) { if (&bm == this) { return; } assert(bm.Nrows() == GeneralMatrix<TYPE>::nrows && bm.Ncols() == GeneralMatrix<TYPE>::ncols); int n = GeneralMatrix<TYPE>::nrows, lb = this->BandWidth().Lower(); if (bm.Search(*this) == 0) { for (int i = 0; i <= lb; ++i) { for (int j = 1; j <= n - i; ++j) { operator()(j + i, j) = bm(j + i, j); } } } else { SymmetricBandMatrix<TYPE> t(n, lb); t << bm; this->Swap(t); } }
void IdentityMatrix<TYPE>::operator<<(const BaseMatrix<TYPE> &bm) { if (&bm == this) { return; } assert(bm.Nrows() == GeneralMatrix<TYPE>::nrows && bm.Ncols() == GeneralMatrix<TYPE>::ncols); operator()(1, 1) = bm(1, 1); }
void ConstantSolver<TYPE>::Solve(const BaseMatrix<TYPE> &in, BaseMatrix<TYPE> &out) const { if (LinearEquationSolver<TYPE>::IsFailed()) { Singleton<Tracer>::Instance()->AddMessage("ConstantSolver::Solve"); throw SingularException(SimpleSolver<TYPE>::mat); } int r = SimpleSolver<TYPE>::mat.Nrows(); int c = SimpleSolver<TYPE>::mat.Ncols(); assert(r == 1 && c == 1 && c == in.Nrows()); assert(in.Ncols() == out.Ncols() && in.Nrows() == out.Nrows()); const BaseMatrix<TYPE> &m = SimpleSolver<TYPE>::mat; for (int i = 1; i <= c; ++i) { for (int j = r; j >= 1; --j) { out(j, i) = in(j, i) / m(j, j); } } }
void IdentitySolver<TYPE>::Solve(const BaseMatrix<TYPE> &in, BaseMatrix<TYPE> &out) const { if (LinearEquationSolver<TYPE>::IsFailed()) { Singleton<Tracer>::Instance()->AddMessage("IdentitySolver::Solve"); throw SingularException(SimpleSolver<TYPE>::mat); } int n = SimpleSolver<TYPE>::mat.Nrows(); assert(n == in.Nrows()); assert(in.Ncols() == out.Ncols() && in.Nrows() == out.Nrows()); TYPE t = SimpleSolver<TYPE>::mat(1, 1); int c = in.Ncols(); for (int i = 1; i <= c; ++i) { for (int j = n; j >= 1; --j) { out(j, i) = in(j, i) / t; } } }
void CholeskySolver<TYPE>::CholeskyDecomposition(const BaseMatrix<TYPE> &bm) { assert(bm.Nrows() == bm.Ncols()); int n = lm.Nrows(); const TYPE &e = SimpleSolver<TYPE>::epsilon; TYPE temp; for (int i = 1; i <= n; ++i) { if (i == 1) { temp = bm(i, i); } else { temp = bm(i, i) - (c_sub(lm, i, i, 1, i - 1) * t(c_sub(lm, i, i, 1, i - 1)))(1, 1); } if (temp <= e) { LinearEquationSolver<TYPE>::fail = true; return; } else { lm(i, i) = std::sqrt(temp); for (int j = i + 1; j <= n; ++j) { if (i == 1) { lm(j, i) = bm(j, i) / lm(i, i); } else { lm(j, i) = (bm(j, i) - (c_sub(lm, i, i, 1, i - 1) * t(c_sub(lm, j, j, 1, i - 1)))(1, 1)) / lm(i, i); } } } } }