36#ifndef BERTINI_LINALG_LU_SOLVER_HPP
37#define BERTINI_LINALG_LU_SOLVER_HPP
56template<
typename NumT>
59 using RealT =
typename Eigen::NumTraits<NumT>::Real;
60 static constexpr bool is_mp = !std::is_same<NumT, complex_dbl>::value;
71 if (is_mp && precision_ != 0)
93 return FactorInPlace();
102 auto code = FactorInPlace();
110 template<
typename OutDerived>
114 Eigen::MatrixBase<OutDerived>& x =
const_cast<Eigen::MatrixBase<OutDerived>&
>(x_out);
118 for (
unsigned k = 0; k < n_; ++k)
120 std::swap(y_[k], y_[piv_[k]]);
123 for (
unsigned i = 1; i < n_; ++i)
124 for (
unsigned j = 0; j < i; ++j)
132 for (
unsigned ii = n_; ii-- > 0; )
134 for (
unsigned j = ii + 1; j < n_; ++j)
141 x(ii) /= lu_(ii, ii);
153 for (
unsigned k = 0; k < n_; ++k)
157 amax_ = abs(lu_(k, k));
158 for (
unsigned i = k + 1; i < n_; ++i)
160 acur_ = abs(lu_(i, k));
161 if (acur_ > amax_) { amax_ = acur_; p = i; }
165 lu_.row(k).swap(lu_.row(p));
176 for (
unsigned i = k + 1; i < n_; ++i)
181 for (
unsigned j = k + 1; j < n_; ++j)
182 for (
unsigned i = k + 1; i < n_; ++i)
193 void ApplyPrecision()
207 unsigned precision_ = 0;
209 std::vector<unsigned> piv_;
Stateful partial-pivot LU factor/solve with preallocated, precision-adjustable banks.
Definition lu_solver.hpp:58
void ChangeSize(unsigned n)
(Re)size the workspace to hold n x n systems.
Definition lu_solver.hpp:65
MatrixSuccessCode FactorDestructive(Mat< NumT > &A)
Factor A destructively (no copy): on return A holds the L\U factors.
Definition lu_solver.hpp:99
MatrixSuccessCode Factor(Mat< NumT > const &A)
Factor a copy of A with partial pivoting; A is preserved.
Definition lu_solver.hpp:90
void ChangePrecision(unsigned prec)
Re-set the precision of every mp temporary bank (a no-op for complex_dbl).
Definition lu_solver.hpp:77
Mat< NumT > const & Factors() const
The factored matrix (L below the diagonal, U on and above), for the caller's LUPartialPivotDecomposit...
Definition lu_solver.hpp:147
unsigned precision() const
Get the current working precision (digits).
Definition lu_solver.hpp:85
void Solve(Vec< NumT > const &b, Eigen::MatrixBase< OutDerived > const &x_out) const
Solve A x = b using the stored factors.
Definition lu_solver.hpp:111
Bertini extensions of the Eigen linear algebra library.
Definition lu_solver.hpp:42
Definition algorithm_builder.hpp:36
MatrixSuccessCode
Outcome of a matrix-decomposition sanity check (e.g. LU pivot magnitude).
Definition eigen_extensions.hpp:464
Eigen::Matrix< NumT, Eigen::Dynamic, 1 > Vec
A dynamically-sized column vector of NumT.
Definition eigen_extensions.hpp:296
MatrixSuccessCode LUPartialPivotDecompositionSuccessful(Eigen::MatrixBase< Derived > const &LU)
Check the diagonal elements of an LU decomposition for small values and large ratios.
Definition eigen_extensions.hpp:480
Eigen::Matrix< NumT, Eigen::Dynamic, Eigen::Dynamic > Mat
A dynamically-sized matrix of NumT.
Definition eigen_extensions.hpp:298
unsigned Precision(Eigen::MatrixBase< Derived > const &v)
Get the precision of an Eigen object.
Definition eigen_extensions.hpp:339