11 #ifndef EIGEN_PARTIALLU_H
12 #define EIGEN_PARTIALLU_H
22 typedef int StorageIndex;
30 template<
typename T,
typename Derived>
36 template<
typename T,
typename Derived>
77 :
public SolverBase<PartialPivLU<_MatrixType> >
81 typedef _MatrixType MatrixType;
87 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
88 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
92 typedef typename MatrixType::PlainObject PlainObject;
117 template<
typename InputType>
127 template<
typename InputType>
130 template<
typename InputType>
145 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
153 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
157 #ifdef EIGEN_PARSED_BY_DOXYGEN
175 template<
typename Rhs>
185 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
186 return internal::rcond_estimate_helper(m_l1_norm, *
this);
198 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
219 inline Index rows()
const {
return m_lu.rows(); }
220 inline Index cols()
const {
return m_lu.cols(); }
222 #ifndef EIGEN_PARSED_BY_DOXYGEN
223 template<
typename RhsType,
typename DstType>
225 void _solve_impl(
const RhsType &rhs, DstType &dst)
const {
237 m_lu.template triangularView<UnitLower>().solveInPlace(dst);
240 m_lu.template triangularView<Upper>().solveInPlace(dst);
243 template<
bool Conjugate,
typename RhsType,
typename DstType>
245 void _solve_impl_transposed(
const RhsType &rhs, DstType &dst)
const {
253 eigen_assert(rhs.rows() == m_lu.cols());
256 dst = m_lu.template triangularView<Upper>().transpose()
257 .template conjugateIf<Conjugate>().solve(rhs);
259 m_lu.template triangularView<UnitLower>().transpose()
260 .template conjugateIf<Conjugate>().solveInPlace(dst);
268 static void check_template_parameters()
270 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
277 TranspositionType m_rowsTranspositions;
278 RealScalar m_l1_norm;
280 bool m_isInitialized;
283 template<
typename MatrixType>
287 m_rowsTranspositions(),
290 m_isInitialized(false)
294 template<
typename MatrixType>
298 m_rowsTranspositions(size),
301 m_isInitialized(false)
305 template<
typename MatrixType>
306 template<
typename InputType>
308 : m_lu(matrix.rows(),matrix.cols()),
310 m_rowsTranspositions(matrix.rows()),
313 m_isInitialized(false)
318 template<
typename MatrixType>
319 template<
typename InputType>
321 : m_lu(matrix.derived()),
323 m_rowsTranspositions(matrix.rows()),
326 m_isInitialized(false)
334 template<
typename Scalar,
int StorageOrder,
typename PivIndex,
int SizeAtCompileTime=Dynamic>
337 static const int UnBlockedBound = 16;
338 static const bool UnBlockedAtCompileTime = SizeAtCompileTime!=
Dynamic && SizeAtCompileTime<=UnBlockedBound;
339 static const int ActualSizeAtCompileTime = UnBlockedAtCompileTime ? SizeAtCompileTime :
Dynamic;
341 static const int RRows = SizeAtCompileTime==2 ? 1 :
Dynamic;
342 static const int RCols = SizeAtCompileTime==2 ? 1 :
Dynamic;
346 typedef typename MatrixType::RealScalar RealScalar;
358 static Index unblocked_lu(
MatrixTypeRef& lu, PivIndex* row_transpositions, PivIndex& nb_transpositions)
361 typedef typename Scoring::result_type Score;
362 const Index rows = lu.rows();
363 const Index cols = lu.cols();
364 const Index size = (std::min)(rows,cols);
367 const Index endk = UnBlockedAtCompileTime ? size-1 : size;
368 nb_transpositions = 0;
369 Index first_zero_pivot = -1;
370 for(
Index k = 0; k < endk; ++k)
372 int rrows = internal::convert_index<int>(rows-k-1);
373 int rcols = internal::convert_index<int>(cols-k-1);
375 Index row_of_biggest_in_col;
376 Score biggest_in_corner
377 = lu.col(k).tail(rows-k).unaryExpr(Scoring()).maxCoeff(&row_of_biggest_in_col);
378 row_of_biggest_in_col += k;
380 row_transpositions[k] = PivIndex(row_of_biggest_in_col);
382 if(biggest_in_corner != Score(0))
384 if(k != row_of_biggest_in_col)
386 lu.row(k).swap(lu.row(row_of_biggest_in_col));
390 lu.col(k).tail(fix<RRows>(rrows)) /= lu.coeff(k,k);
392 else if(first_zero_pivot==-1)
396 first_zero_pivot = k;
400 lu.bottomRightCorner(fix<RRows>(rrows),fix<RCols>(rcols)).noalias() -= lu.col(k).tail(fix<RRows>(rrows)) * lu.row(k).tail(fix<RCols>(rcols));
404 if(UnBlockedAtCompileTime)
407 row_transpositions[k] = PivIndex(k);
408 if (Scoring()(lu(k, k)) == Score(0) && first_zero_pivot == -1)
409 first_zero_pivot = k;
412 return first_zero_pivot;
430 static Index blocked_lu(
Index rows,
Index cols, Scalar* lu_data,
Index luStride, PivIndex* row_transpositions, PivIndex& nb_transpositions,
Index maxBlockSize=256)
434 const Index size = (std::min)(rows,cols);
437 if(UnBlockedAtCompileTime || size<=UnBlockedBound)
439 return unblocked_lu(lu, row_transpositions, nb_transpositions);
447 blockSize = (blockSize/16)*16;
448 blockSize = (std::min)((std::max)(blockSize,
Index(8)), maxBlockSize);
451 nb_transpositions = 0;
452 Index first_zero_pivot = -1;
453 for(
Index k = 0; k < size; k+=blockSize)
455 Index bs = (std::min)(size-k,blockSize);
456 Index trows = rows - k - bs;
457 Index tsize = size - k - bs;
464 BlockType A_2 = lu.block(0,k+bs,rows,tsize);
466 BlockType A12 = lu.block(k,k+bs,bs,tsize);
467 BlockType A21 = lu.block(k+bs,k,trows,bs);
468 BlockType A22 = lu.block(k+bs,k+bs,trows,tsize);
470 PivIndex nb_transpositions_in_panel;
473 Index ret = blocked_lu(trows+bs, bs, &lu.coeffRef(k,k), luStride,
474 row_transpositions+k, nb_transpositions_in_panel, 16);
475 if(ret>=0 && first_zero_pivot==-1)
476 first_zero_pivot = k+ret;
478 nb_transpositions += nb_transpositions_in_panel;
480 for(
Index i=k; i<k+bs; ++i)
482 Index piv = (row_transpositions[i] += internal::convert_index<PivIndex>(k));
483 A_0.row(i).swap(A_0.row(piv));
489 for(
Index i=k;i<k+bs; ++i)
490 A_2.row(i).swap(A_2.row(row_transpositions[i]));
493 A11.template triangularView<UnitLower>().solveInPlace(A12);
495 A22.noalias() -= A21 * A12;
498 return first_zero_pivot;
504 template<
typename MatrixType,
typename TranspositionType>
505 void partial_lu_inplace(MatrixType& lu, TranspositionType& row_transpositions,
typename TranspositionType::StorageIndex& nb_transpositions)
507 eigen_assert(lu.cols() == row_transpositions.size());
508 eigen_assert((&row_transpositions.coeffRef(1)-&row_transpositions.coeffRef(0)) == 1);
512 typename TranspositionType::StorageIndex,
513 EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime)>
514 ::blocked_lu(lu.rows(), lu.cols(), &lu.coeffRef(0,0), lu.outerStride(), &row_transpositions.coeffRef(0), nb_transpositions);
519 template<
typename MatrixType>
520 void PartialPivLU<MatrixType>::compute()
522 check_template_parameters();
525 eigen_assert(m_lu.rows()<NumTraits<int>::highest());
528 m_l1_norm = m_lu.cwiseAbs().colwise().sum().maxCoeff();
530 m_l1_norm = RealScalar(0);
532 eigen_assert(m_lu.rows() == m_lu.cols() &&
"PartialPivLU is only for square (and moreover invertible) matrices");
533 const Index size = m_lu.rows();
535 m_rowsTranspositions.resize(size);
537 typename TranspositionType::StorageIndex nb_transpositions;
538 internal::partial_lu_inplace(m_lu, m_rowsTranspositions, nb_transpositions);
539 m_det_p = (nb_transpositions%2) ? -1 : 1;
541 m_p = m_rowsTranspositions;
543 m_isInitialized =
true;
546 template<
typename MatrixType>
549 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
550 return Scalar(m_det_p) * m_lu.diagonal().prod();
556 template<
typename MatrixType>
559 eigen_assert(m_isInitialized &&
"LU is not initialized.");
561 MatrixType res = m_lu.template triangularView<UnitLower>().toDenseMatrix()
562 * m_lu.template triangularView<Upper>();
565 res = m_p.inverse() * res;
575 template<
typename DstXprType,
typename MatrixType>
582 dst = src.nestedExpression().solve(MatrixType::Identity(src.rows(), src.cols()));
595 template<
typename Derived>
610 template<
typename Derived>
619 #endif // EIGEN_PARTIALLU_H