11 #ifndef EIGEN_TRIANGULARMATRIX_H
12 #define EIGEN_TRIANGULARMATRIX_H
18 template<
int S
ide,
typename TriangularType,
typename Rhs>
struct triangular_solve_retval;
52 typedef DenseMatrixType DenseType;
53 typedef Derived
const& Nested;
59 inline Index rows()
const {
return derived().rows(); }
61 inline Index cols()
const {
return derived().cols(); }
63 inline Index outerStride()
const {
return derived().outerStride(); }
65 inline Index innerStride()
const {
return derived().innerStride(); }
71 EIGEN_UNUSED_VARIABLE(rows);
72 EIGEN_UNUSED_VARIABLE(cols);
73 eigen_assert(rows==this->rows() && cols==this->cols());
77 inline Scalar coeff(
Index row,
Index col)
const {
return derived().coeff(row,col); }
79 inline Scalar& coeffRef(
Index row,
Index col) {
return derived().coeffRef(row,col); }
83 template<
typename Other>
87 derived().coeffRef(row, col) = other.coeff(row, col);
91 inline Scalar operator()(
Index row,
Index col)
const
93 check_coordinates(row, col);
94 return coeff(row,col);
97 inline Scalar& operator()(
Index row,
Index col)
99 check_coordinates(row, col);
100 return coeffRef(row,col);
103 #ifndef EIGEN_PARSED_BY_DOXYGEN
105 inline const Derived& derived()
const {
return *
static_cast<const Derived*
>(
this); }
107 inline Derived& derived() {
return *
static_cast<Derived*
>(
this); }
108 #endif // not EIGEN_PARSED_BY_DOXYGEN
110 template<
typename DenseDerived>
113 template<
typename DenseDerived>
118 DenseMatrixType toDenseMatrix()
const
120 DenseMatrixType res(rows(), cols());
127 void check_coordinates(
Index row,
Index col)
const
129 EIGEN_ONLY_USED_FOR_DEBUG(row);
130 EIGEN_ONLY_USED_FOR_DEBUG(col);
131 eigen_assert(col>=0 && col<cols() && row>=0 && row<rows());
133 EIGEN_ONLY_USED_FOR_DEBUG(mode);
134 eigen_assert((mode==
Upper && col>=row)
135 || (mode==
Lower && col<=row)
140 #ifdef EIGEN_INTERNAL_DEBUGGING
141 void check_coordinates_internal(
Index row,
Index col)
const
143 check_coordinates(row, col);
146 void check_coordinates_internal(
Index ,
Index )
const {}
169 template<
typename MatrixType,
unsigned int _Mode>
173 typedef typename remove_reference<MatrixTypeNested>::type MatrixTypeNestedNonRef;
174 typedef typename remove_all<MatrixTypeNested>::type MatrixTypeNestedCleaned;
175 typedef typename MatrixType::PlainObject FullMatrixType;
176 typedef MatrixType ExpressionType;
188 :
public TriangularViewImpl<_MatrixType, _Mode, typename internal::traits<_MatrixType>::StorageKind >
194 typedef _MatrixType MatrixType;
200 typedef typename internal::remove_all<typename MatrixType::ConjugateReturnType>::type MatrixConjugateReturnType;
215 IsVectorAtCompileTime =
false
219 explicit inline TriangularView(MatrixType& matrix) : m_matrix(matrix)
254 return ReturnType(m_matrix.template conjugateIf<Cond>());
268 EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
269 typename MatrixType::TransposeReturnType tmp(m_matrix);
281 template<
typename Other>
289 template<
int S
ide,
typename Other>
291 inline const internal::triangular_solve_retval<Side,TriangularView, Other>
292 solve(
const MatrixBase<Other>& other)
const
293 {
return Base::template solve<Side>(other); }
328 return m_matrix.diagonal().prod();
333 MatrixTypeNested m_matrix;
354 typedef _MatrixType MatrixType;
355 typedef typename MatrixType::PlainObject DenseMatrixType;
356 typedef DenseMatrixType PlainObject;
359 using Base::evalToLazy;
379 template<
typename Other>
386 template<
typename Other>
402 void fill(
const Scalar& value) { setConstant(value); }
406 {
return *
this = MatrixType::Constant(derived().rows(), derived().cols(), value); }
420 Base::check_coordinates_internal(row, col);
421 return derived().nestedExpression().coeff(row, col);
431 Base::check_coordinates_internal(row, col);
432 return derived().nestedExpression().coeffRef(row, col);
436 template<
typename OtherDerived>
441 template<
typename OtherDerived>
445 #ifndef EIGEN_PARSED_BY_DOXYGEN
448 {
return *
this = other.derived().nestedExpression(); }
450 template<
typename OtherDerived>
452 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC
455 template<
typename OtherDerived>
457 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC
462 template<
typename OtherDerived>
471 template<
typename OtherDerived>
friend
502 template<
int S
ide,
typename Other>
515 template<
int S
ide,
typename OtherDerived>
519 template<
typename OtherDerived>
522 {
return solveInPlace<OnTheLeft>(other); }
525 template<
typename OtherDerived>
527 #ifdef EIGEN_PARSED_BY_DOXYGEN
533 EIGEN_STATIC_ASSERT_LVALUE(OtherDerived);
538 template<
typename OtherDerived>
540 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC
543 EIGEN_STATIC_ASSERT_LVALUE(OtherDerived);
547 template<
typename RhsType,
typename DstType>
549 EIGEN_STRONG_INLINE
void _solve_impl(
const RhsType &rhs, DstType &dst)
const {
550 if(!internal::is_same_dense(dst,rhs))
552 this->solveInPlace(dst);
555 template<
typename ProductType>
557 EIGEN_STRONG_INLINE TriangularViewType& _assignProduct(
const ProductType& prod,
const Scalar& alpha,
bool beta);
559 EIGEN_DEFAULT_COPY_CONSTRUCTOR(TriangularViewImpl)
560 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(TriangularViewImpl)
568 #ifndef EIGEN_PARSED_BY_DOXYGEN
570 template<
typename MatrixType,
unsigned int Mode>
571 template<
typename OtherDerived>
572 EIGEN_DEVICE_FUNC
inline TriangularView<MatrixType, Mode>&
573 TriangularViewImpl<MatrixType, Mode, Dense>::operator=(
const MatrixBase<OtherDerived>& other)
575 internal::call_assignment_no_alias(derived(), other.derived(), internal::assign_op<Scalar,typename OtherDerived::Scalar>());
580 template<
typename MatrixType,
unsigned int Mode>
581 template<
typename OtherDerived>
582 EIGEN_DEVICE_FUNC
void TriangularViewImpl<MatrixType, Mode, Dense>::lazyAssign(
const MatrixBase<OtherDerived>& other)
584 internal::call_assignment_no_alias(derived(), other.template triangularView<Mode>());
589 template<
typename MatrixType,
unsigned int Mode>
590 template<
typename OtherDerived>
591 EIGEN_DEVICE_FUNC
inline TriangularView<MatrixType, Mode>&
592 TriangularViewImpl<MatrixType, Mode, Dense>::operator=(
const TriangularBase<OtherDerived>& other)
594 eigen_assert(Mode ==
int(OtherDerived::Mode));
595 internal::call_assignment(derived(), other.derived());
599 template<
typename MatrixType,
unsigned int Mode>
600 template<
typename OtherDerived>
601 EIGEN_DEVICE_FUNC
void TriangularViewImpl<MatrixType, Mode, Dense>::lazyAssign(
const TriangularBase<OtherDerived>& other)
603 eigen_assert(Mode ==
int(OtherDerived::Mode));
604 internal::call_assignment_no_alias(derived(), other.derived());
614 template<
typename Derived>
615 template<
typename DenseDerived>
618 evalToLazy(other.derived());
640 template<
typename Derived>
641 template<
unsigned int Mode>
646 return typename TriangularViewReturnType<Mode>::Type(derived());
650 template<
typename Derived>
651 template<
unsigned int Mode>
656 return typename ConstTriangularViewReturnType<Mode>::Type(derived());
664 template<
typename Derived>
667 RealScalar maxAbsOnUpperPart =
static_cast<RealScalar
>(-1);
668 for(
Index j = 0; j < cols(); ++j)
670 Index maxi = numext::mini(j, rows()-1);
671 for(
Index i = 0; i <= maxi; ++i)
673 RealScalar absValue = numext::abs(coeff(i,j));
674 if(absValue > maxAbsOnUpperPart) maxAbsOnUpperPart = absValue;
677 RealScalar threshold = maxAbsOnUpperPart * prec;
678 for(
Index j = 0; j < cols(); ++j)
679 for(
Index i = j+1; i < rows(); ++i)
680 if(numext::abs(coeff(i, j)) > threshold)
return false;
689 template<
typename Derived>
692 RealScalar maxAbsOnLowerPart =
static_cast<RealScalar
>(-1);
693 for(
Index j = 0; j < cols(); ++j)
694 for(
Index i = j; i < rows(); ++i)
696 RealScalar absValue = numext::abs(coeff(i,j));
697 if(absValue > maxAbsOnLowerPart) maxAbsOnLowerPart = absValue;
699 RealScalar threshold = maxAbsOnLowerPart * prec;
700 for(
Index j = 1; j < cols(); ++j)
702 Index maxi = numext::mini(j, rows()-1);
703 for(
Index i = 0; i < maxi; ++i)
704 if(numext::abs(coeff(i, j)) > threshold)
return false;
722 template<
typename MatrixType,
unsigned int Mode>
725 typedef typename storage_kind_to_evaluator_kind<typename MatrixType::StorageKind>::Kind Kind;
726 typedef typename glue_shapes<typename evaluator_traits<MatrixType>::Shape,
TriangularShape>::type Shape;
729 template<
typename MatrixType,
unsigned int Mode>
731 :
evaluator<typename internal::remove_all<MatrixType>::type>
740 struct Triangular2Triangular {};
741 struct Triangular2Dense {};
742 struct Dense2Triangular {};
753 template<
int UpLo,
int Mode,
int SetOpposite,
typename DstEvaluatorTypeT,
typename SrcEvaluatorTypeT,
typename Functor,
int Version = Specialized>
758 typedef typename Base::DstXprType DstXprType;
759 typedef typename Base::SrcXprType SrcXprType;
762 using Base::m_functor;
765 typedef typename Base::DstEvaluatorType DstEvaluatorType;
766 typedef typename Base::SrcEvaluatorType SrcEvaluatorType;
767 typedef typename Base::Scalar Scalar;
772 :
Base(dst, src, func, dstExpr)
775 #ifdef EIGEN_INTERNAL_DEBUGGING
778 eigen_internal_assert(row!=col);
785 EIGEN_DEVICE_FUNC
void assignDiagonalCoeff(
Index id)
787 if(Mode==
UnitDiag && SetOpposite) m_functor.assignCoeff(m_dst.coeffRef(
id,
id), Scalar(1));
788 else if(Mode==
ZeroDiag && SetOpposite) m_functor.assignCoeff(m_dst.coeffRef(
id,
id), Scalar(0));
792 EIGEN_DEVICE_FUNC
void assignOppositeCoeff(
Index row,
Index col)
794 eigen_internal_assert(row!=col);
796 m_functor.assignCoeff(m_dst.coeffRef(row,col), Scalar(0));
800 template<
int Mode,
bool SetOpposite,
typename DstXprType,
typename SrcXprType,
typename Functor>
801 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
802 void call_triangular_assignment_loop(DstXprType& dst,
const SrcXprType& src,
const Functor &func)
804 typedef evaluator<DstXprType> DstEvaluatorType;
805 typedef evaluator<SrcXprType> SrcEvaluatorType;
807 SrcEvaluatorType srcEvaluator(src);
809 Index dstRows = src.rows();
810 Index dstCols = src.cols();
811 if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
812 dst.resize(dstRows, dstCols);
813 DstEvaluatorType dstEvaluator(dst);
816 DstEvaluatorType,SrcEvaluatorType,Functor> Kernel;
817 Kernel kernel(dstEvaluator, srcEvaluator, func, dst.const_cast_derived());
820 unroll = DstXprType::SizeAtCompileTime !=
Dynamic
821 && SrcEvaluatorType::CoeffReadCost <
HugeCost
822 && DstXprType::SizeAtCompileTime * (DstEvaluatorType::CoeffReadCost+SrcEvaluatorType::CoeffReadCost) / 2 <= EIGEN_UNROLLING_LIMIT
825 triangular_assignment_loop<Kernel, Mode, unroll ? int(DstXprType::SizeAtCompileTime) :
Dynamic, SetOpposite>::run(kernel);
828 template<
int Mode,
bool SetOpposite,
typename DstXprType,
typename SrcXprType>
829 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
830 void call_triangular_assignment_loop(DstXprType& dst,
const SrcXprType& src)
832 call_triangular_assignment_loop<Mode,SetOpposite>(dst, src, internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar>());
835 template<>
struct AssignmentKind<TriangularShape,TriangularShape> {
typedef Triangular2Triangular Kind; };
836 template<>
struct AssignmentKind<DenseShape,TriangularShape> {
typedef Triangular2Dense Kind; };
837 template<>
struct AssignmentKind<TriangularShape,DenseShape> {
typedef Dense2Triangular Kind; };
840 template<
typename DstXprType,
typename SrcXprType,
typename Functor>
843 EIGEN_DEVICE_FUNC
static void run(DstXprType &dst,
const SrcXprType &src,
const Functor &func)
845 eigen_assert(
int(DstXprType::Mode) ==
int(SrcXprType::Mode));
847 call_triangular_assignment_loop<DstXprType::Mode, false>(dst, src, func);
851 template<
typename DstXprType,
typename SrcXprType,
typename Functor>
852 struct Assignment<DstXprType, SrcXprType, Functor, Triangular2Dense>
854 EIGEN_DEVICE_FUNC
static void run(DstXprType &dst,
const SrcXprType &src,
const Functor &func)
856 call_triangular_assignment_loop<SrcXprType::Mode, (SrcXprType::Mode&SelfAdjoint)==0>(dst, src, func);
860 template<
typename DstXprType,
typename SrcXprType,
typename Functor>
861 struct Assignment<DstXprType, SrcXprType, Functor, Dense2Triangular>
863 EIGEN_DEVICE_FUNC
static void run(DstXprType &dst,
const SrcXprType &src,
const Functor &func)
865 call_triangular_assignment_loop<DstXprType::Mode, false>(dst, src, func);
870 template<
typename Kernel,
unsigned int Mode,
int UnrollCount,
bool SetOpposite>
871 struct triangular_assignment_loop
874 typedef typename Kernel::DstEvaluatorType DstEvaluatorType;
875 typedef typename DstEvaluatorType::XprType DstXprType;
878 col = (UnrollCount-1) / DstXprType::RowsAtCompileTime,
879 row = (UnrollCount-1) % DstXprType::RowsAtCompileTime
882 typedef typename Kernel::Scalar Scalar;
885 static inline void run(Kernel &kernel)
890 kernel.assignDiagonalCoeff(row);
891 else if( ((Mode&
Lower) && row>col) || ((Mode&
Upper) && row<col) )
892 kernel.assignCoeff(row,col);
894 kernel.assignOppositeCoeff(row,col);
899 template<
typename Kernel,
unsigned int Mode,
bool SetOpposite>
900 struct triangular_assignment_loop<Kernel, Mode, 0, SetOpposite>
903 static inline void run(Kernel &) {}
912 template<
typename Kernel,
unsigned int Mode,
bool SetOpposite>
915 typedef typename Kernel::Scalar Scalar;
917 static inline void run(Kernel &kernel)
919 for(
Index j = 0; j < kernel.cols(); ++j)
921 Index maxi = numext::mini(j, kernel.rows());
923 if (((Mode&
Lower) && SetOpposite) || (Mode&
Upper))
926 if(Mode&
Upper) kernel.assignCoeff(i, j);
927 else kernel.assignOppositeCoeff(i, j);
933 kernel.assignDiagonalCoeff(i++);
935 if (((Mode&
Upper) && SetOpposite) || (Mode&
Lower))
937 for(; i < kernel.rows(); ++i)
938 if(Mode&
Lower) kernel.assignCoeff(i, j);
939 else kernel.assignOppositeCoeff(i, j);
949 template<
typename Derived>
950 template<
typename DenseDerived>
953 other.derived().
resize(this->rows(), this->cols());
954 internal::call_triangular_assignment_loop<Derived::Mode,(Derived::Mode&
SelfAdjoint)==0 >(other.derived(), derived().nestedExpression());
960 template<
typename DstXprType,
typename Lhs,
typename Rhs,
typename Scalar>
966 Index dstRows = src.rows();
967 Index dstCols = src.cols();
968 if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
969 dst.resize(dstRows, dstCols);
971 dst._assignProduct(src, 1, 0);
976 template<
typename DstXprType,
typename Lhs,
typename Rhs,
typename Scalar>
982 dst._assignProduct(src, 1, 1);
987 template<
typename DstXprType,
typename Lhs,
typename Rhs,
typename Scalar>
993 dst._assignProduct(src, -1, 1);
1001 #endif // EIGEN_TRIANGULARMATRIX_H