10 #ifndef EIGEN_SPARSETRIANGULARSOLVER_H
11 #define EIGEN_SPARSETRIANGULARSOLVER_H
19 template<
typename Lhs,
typename Rhs,
int Mode,
20 int UpLo = (Mode &
Lower)
25 int StorageOrder = int(traits<Lhs>::Flags) &
RowMajorBit>
26 struct sparse_solve_triangular_selector;
29 template<
typename Lhs,
typename Rhs,
int Mode>
30 struct sparse_solve_triangular_selector<Lhs,Rhs,Mode,
Lower,
RowMajor>
32 typedef typename Rhs::Scalar Scalar;
33 typedef evaluator<Lhs> LhsEval;
34 typedef typename evaluator<Lhs>::InnerIterator
LhsIterator;
35 static void run(
const Lhs& lhs, Rhs& other)
42 Scalar tmp = other.coeff(
i,
col);
48 lastIndex = it.index();
51 tmp -= lastVal * other.coeff(lastIndex,
col);
54 other.coeffRef(
i,
col) = tmp;
58 other.coeffRef(
i,
col) = tmp/lastVal;
66 template<
typename Lhs,
typename Rhs,
int Mode>
67 struct sparse_solve_triangular_selector<Lhs,Rhs,Mode,
Upper,
RowMajor>
69 typedef typename Rhs::Scalar Scalar;
70 typedef evaluator<Lhs> LhsEval;
71 typedef typename evaluator<Lhs>::InnerIterator
LhsIterator;
72 static void run(
const Lhs& lhs, Rhs& other)
77 for(
Index i=lhs.rows()-1 ;
i>=0 ; --
i)
79 Scalar tmp = other.coeff(
i,
col);
82 while(it && it.index()<
i)
90 else if (it && it.index() ==
i)
94 tmp -= it.value() * other.coeff(it.index(),
col);
98 else other.coeffRef(
i,
col) = tmp/l_ii;
105 template<
typename Lhs,
typename Rhs,
int Mode>
106 struct sparse_solve_triangular_selector<Lhs,Rhs,Mode,
Lower,
ColMajor>
108 typedef typename Rhs::Scalar Scalar;
109 typedef evaluator<Lhs> LhsEval;
110 typedef typename evaluator<Lhs>::InnerIterator
LhsIterator;
111 static void run(
const Lhs& lhs, Rhs& other)
113 LhsEval lhsEval(lhs);
118 Scalar& tmp = other.coeffRef(
i,
col);
122 while(it && it.index()<
i)
129 if (it && it.index()==
i)
132 other.coeffRef(it.index(),
col) -= tmp * it.value();
140 template<
typename Lhs,
typename Rhs,
int Mode>
141 struct sparse_solve_triangular_selector<Lhs,Rhs,Mode,
Upper,
ColMajor>
143 typedef typename Rhs::Scalar Scalar;
144 typedef evaluator<Lhs> LhsEval;
145 typedef typename evaluator<Lhs>::InnerIterator
LhsIterator;
146 static void run(
const Lhs& lhs, Rhs& other)
148 LhsEval lhsEval(lhs);
151 for(
Index i=lhs.cols()-1;
i>=0; --
i)
153 Scalar& tmp = other.coeffRef(
i,
col);
160 while(it && it.index()!=
i)
163 other.coeffRef(
i,
col) /= it.value();
166 for(; it && it.index()<
i; ++it)
167 other.coeffRef(it.index(),
col) -= tmp * it.value();
176 #ifndef EIGEN_PARSED_BY_DOXYGEN
178 template<
typename ExpressionType,
unsigned int Mode>
179 template<
typename OtherDerived>
180 void TriangularViewImpl<ExpressionType,Mode,Sparse>::solveInPlace(MatrixBase<OtherDerived>& other)
const
185 enum { copy = internal::traits<OtherDerived>::Flags &
RowMajorBit };
187 typedef std::conditional_t<copy,
188 typename internal::plain_matrix_type_column_major<OtherDerived>::type, OtherDerived&> OtherCopy;
189 OtherCopy otherCopy(other.derived());
191 internal::sparse_solve_triangular_selector<ExpressionType, std::remove_reference_t<OtherCopy>, Mode>::run(derived().nestedExpression(), otherCopy);
202 template<
typename Lhs,
typename Rhs,
int Mode,
203 int UpLo = (Mode &
Lower)
208 int StorageOrder = int(Lhs::Flags) & (
RowMajorBit)>
209 struct sparse_solve_triangular_sparse_selector;
212 template<
typename Lhs,
typename Rhs,
int Mode,
int UpLo>
213 struct sparse_solve_triangular_sparse_selector<Lhs,Rhs,Mode,UpLo,ColMajor>
215 typedef typename Rhs::Scalar Scalar;
216 typedef typename promote_index_type<typename traits<Lhs>::StorageIndex,
217 typename traits<Rhs>::StorageIndex>::type StorageIndex;
218 static void run(
const Lhs& lhs, Rhs& other)
220 const bool IsLower = (UpLo==
Lower);
221 AmbiVector<Scalar,StorageIndex> tempVector(other.rows()*2);
222 tempVector.setBounds(0,other.rows());
224 Rhs
res(other.rows(), other.cols());
225 res.reserve(other.nonZeros());
230 tempVector.init(.99);
231 tempVector.setZero();
232 tempVector.restart();
233 for (
typename Rhs::InnerIterator rhsIt(other,
col); rhsIt; ++rhsIt)
235 tempVector.coeffRef(rhsIt.index()) = rhsIt.value();
238 for(
Index i=IsLower?0:lhs.cols()-1;
239 IsLower?
i<lhs.cols():
i>=0;
242 tempVector.restart();
243 Scalar& ci = tempVector.coeffRef(
i);
247 typename Lhs::InnerIterator it(lhs,
i);
256 ci /= lhs.coeff(
i,
i);
258 tempVector.restart();
264 tempVector.coeffRef(it.index()) -= ci * it.value();
268 for(; it && it.index()<
i; ++it)
269 tempVector.coeffRef(it.index()) -= ci * it.value();
277 for (
typename AmbiVector<Scalar,StorageIndex>::Iterator it(tempVector); it; ++it)
283 res.insert(it.index(),
col) = it.value();
288 other =
res.markAsRValue();
294 #ifndef EIGEN_PARSED_BY_DOXYGEN
295 template<
typename ExpressionType,
unsigned int Mode>
296 template<
typename OtherDerived>
297 void TriangularViewImpl<ExpressionType,Mode,Sparse>::solveInPlace(SparseMatrixBase<OtherDerived>& other)
const
308 internal::sparse_solve_triangular_sparse_selector<ExpressionType, OtherDerived, Mode>::run(derived().nestedExpression(), other.derived());
ColXpr col(Index i)
This is the const version of col().
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
const unsigned int RowMajorBit
bool is_exactly_zero(const X &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.