2009-11-18 21:52:52 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
2014-07-30 17:39:52 +08:00
|
|
|
// Copyright (C) 2009-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2013-06-11 20:42:29 +08:00
|
|
|
// Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
|
2009-11-18 21:52:52 +08:00
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-11-18 21:52:52 +08:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_SPARSE_TRIANGULARVIEW_H
|
|
|
|
|
#define EIGEN_SPARSE_TRIANGULARVIEW_H
|
|
|
|
|
|
2015-05-02 04:10:41 +08:00
|
|
|
#ifndef EIGEN_PARSED_BY_DOXYGEN
|
|
|
|
|
// Doxygen gets confused with template specialization:
|
|
|
|
|
// https://bugzilla.gnome.org/show_bug.cgi?id=406027
|
|
|
|
|
namespace Eigen {
|
2012-04-15 18:06:28 +08:00
|
|
|
|
2014-07-22 17:35:56 +08:00
|
|
|
template<typename MatrixType, unsigned int Mode> class TriangularViewImpl<MatrixType,Mode,Sparse>
|
|
|
|
|
: public SparseMatrixBase<TriangularView<MatrixType,Mode> >
|
2009-11-18 21:52:52 +08:00
|
|
|
{
|
2011-12-04 21:39:24 +08:00
|
|
|
enum { SkipFirst = ((Mode&Lower) && !(MatrixType::Flags&RowMajorBit))
|
|
|
|
|
|| ((Mode&Upper) && (MatrixType::Flags&RowMajorBit)),
|
|
|
|
|
SkipLast = !SkipFirst,
|
2013-06-11 20:42:29 +08:00
|
|
|
SkipDiag = (Mode&ZeroDiag) ? 1 : 0,
|
2011-12-04 21:39:24 +08:00
|
|
|
HasUnitDiag = (Mode&UnitDiag) ? 1 : 0
|
2011-12-04 06:49:37 +08:00
|
|
|
};
|
2014-07-22 17:35:56 +08:00
|
|
|
|
|
|
|
|
typedef TriangularView<MatrixType,Mode> TriangularViewType;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// dummy solve function to make TriangularView happy.
|
|
|
|
|
void solve() const;
|
2011-12-04 06:49:37 +08:00
|
|
|
|
2009-11-18 21:52:52 +08:00
|
|
|
public:
|
2010-10-27 21:13:03 +08:00
|
|
|
|
2014-07-22 17:35:56 +08:00
|
|
|
EIGEN_SPARSE_PUBLIC_INTERFACE(TriangularViewType)
|
|
|
|
|
|
2009-11-18 21:52:52 +08:00
|
|
|
class InnerIterator;
|
2011-12-04 06:49:37 +08:00
|
|
|
class ReverseInnerIterator;
|
2009-11-18 21:52:52 +08:00
|
|
|
|
2012-01-26 01:16:48 +08:00
|
|
|
typedef typename MatrixType::Nested MatrixTypeNested;
|
2011-10-10 04:19:01 +08:00
|
|
|
typedef typename internal::remove_reference<MatrixTypeNested>::type MatrixTypeNestedNonRef;
|
|
|
|
|
typedef typename internal::remove_all<MatrixTypeNested>::type MatrixTypeNestedCleaned;
|
2009-11-18 21:52:52 +08:00
|
|
|
|
2014-07-22 17:35:56 +08:00
|
|
|
template<typename RhsType, typename DstType>
|
|
|
|
|
EIGEN_DEVICE_FUNC
|
|
|
|
|
EIGEN_STRONG_INLINE void _solve_impl(const RhsType &rhs, DstType &dst) const {
|
|
|
|
|
if(!(internal::is_same<RhsType,DstType>::value && internal::extract_data(dst) == internal::extract_data(rhs)))
|
|
|
|
|
dst = rhs;
|
2014-09-17 15:55:44 +08:00
|
|
|
this->solveInPlace(dst);
|
2014-07-22 17:35:56 +08:00
|
|
|
}
|
2009-11-18 21:52:52 +08:00
|
|
|
|
|
|
|
|
template<typename OtherDerived> void solveInPlace(MatrixBase<OtherDerived>& other) const;
|
|
|
|
|
template<typename OtherDerived> void solveInPlace(SparseMatrixBase<OtherDerived>& other) const;
|
2014-07-22 17:35:56 +08:00
|
|
|
|
2009-11-18 21:52:52 +08:00
|
|
|
};
|
|
|
|
|
|
2014-07-22 17:35:56 +08:00
|
|
|
template<typename MatrixType, unsigned int Mode>
|
|
|
|
|
class TriangularViewImpl<MatrixType,Mode,Sparse>::InnerIterator : public MatrixTypeNestedCleaned::InnerIterator
|
2009-11-18 21:52:52 +08:00
|
|
|
{
|
2012-01-26 15:52:20 +08:00
|
|
|
typedef typename MatrixTypeNestedCleaned::InnerIterator Base;
|
2009-11-18 21:52:52 +08:00
|
|
|
public:
|
|
|
|
|
|
2014-07-22 17:35:56 +08:00
|
|
|
EIGEN_STRONG_INLINE InnerIterator(const TriangularViewImpl& view, Index outer)
|
2014-07-30 17:39:52 +08:00
|
|
|
: Base(view.derived().nestedExpression(), outer), m_returnOne(false)
|
2009-11-18 21:52:52 +08:00
|
|
|
{
|
|
|
|
|
if(SkipFirst)
|
2011-12-04 21:39:24 +08:00
|
|
|
{
|
2013-06-11 20:42:29 +08:00
|
|
|
while((*this) && ((HasUnitDiag||SkipDiag) ? this->index()<=outer : this->index()<outer))
|
2011-12-04 21:39:24 +08:00
|
|
|
Base::operator++();
|
|
|
|
|
if(HasUnitDiag)
|
|
|
|
|
m_returnOne = true;
|
|
|
|
|
}
|
|
|
|
|
else if(HasUnitDiag && ((!Base::operator bool()) || Base::index()>=Base::outer()))
|
|
|
|
|
{
|
|
|
|
|
if((!SkipFirst) && Base::operator bool())
|
|
|
|
|
Base::operator++();
|
|
|
|
|
m_returnOne = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EIGEN_STRONG_INLINE InnerIterator& operator++()
|
|
|
|
|
{
|
|
|
|
|
if(HasUnitDiag && m_returnOne)
|
|
|
|
|
m_returnOne = false;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Base::operator++();
|
|
|
|
|
if(HasUnitDiag && (!SkipFirst) && ((!Base::operator bool()) || Base::index()>=Base::outer()))
|
|
|
|
|
{
|
|
|
|
|
if((!SkipFirst) && Base::operator bool())
|
|
|
|
|
Base::operator++();
|
|
|
|
|
m_returnOne = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
2009-11-18 21:52:52 +08:00
|
|
|
}
|
2011-12-04 21:39:24 +08:00
|
|
|
|
2015-02-14 01:57:41 +08:00
|
|
|
inline Index row() const { return (MatrixType::Flags&RowMajorBit ? Base::outer() : this->index()); }
|
|
|
|
|
inline Index col() const { return (MatrixType::Flags&RowMajorBit ? this->index() : Base::outer()); }
|
|
|
|
|
inline Index index() const
|
2011-12-04 21:39:24 +08:00
|
|
|
{
|
|
|
|
|
if(HasUnitDiag && m_returnOne) return Base::outer();
|
|
|
|
|
else return Base::index();
|
|
|
|
|
}
|
|
|
|
|
inline Scalar value() const
|
|
|
|
|
{
|
|
|
|
|
if(HasUnitDiag && m_returnOne) return Scalar(1);
|
|
|
|
|
else return Base::value();
|
|
|
|
|
}
|
2009-11-18 21:52:52 +08:00
|
|
|
|
|
|
|
|
EIGEN_STRONG_INLINE operator bool() const
|
|
|
|
|
{
|
2011-12-04 21:39:24 +08:00
|
|
|
if(HasUnitDiag && m_returnOne)
|
|
|
|
|
return true;
|
2013-06-11 20:42:29 +08:00
|
|
|
if(SkipFirst) return Base::operator bool();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (SkipDiag) return (Base::operator bool() && this->index() < this->outer());
|
|
|
|
|
else return (Base::operator bool() && this->index() <= this->outer());
|
|
|
|
|
}
|
2009-11-18 21:52:52 +08:00
|
|
|
}
|
2011-12-04 21:39:24 +08:00
|
|
|
protected:
|
|
|
|
|
bool m_returnOne;
|
2009-11-18 21:52:52 +08:00
|
|
|
};
|
|
|
|
|
|
2014-07-22 17:35:56 +08:00
|
|
|
template<typename MatrixType, unsigned int Mode>
|
|
|
|
|
class TriangularViewImpl<MatrixType,Mode,Sparse>::ReverseInnerIterator : public MatrixTypeNestedCleaned::ReverseInnerIterator
|
2011-12-04 06:49:37 +08:00
|
|
|
{
|
2012-01-26 15:52:20 +08:00
|
|
|
typedef typename MatrixTypeNestedCleaned::ReverseInnerIterator Base;
|
2011-12-04 06:49:37 +08:00
|
|
|
public:
|
|
|
|
|
|
2014-07-22 17:35:56 +08:00
|
|
|
EIGEN_STRONG_INLINE ReverseInnerIterator(const TriangularViewType& view, Index outer)
|
2014-07-30 17:39:52 +08:00
|
|
|
: Base(view.derived().nestedExpression(), outer)
|
2011-12-04 06:49:37 +08:00
|
|
|
{
|
2011-12-04 21:39:24 +08:00
|
|
|
eigen_assert((!HasUnitDiag) && "ReverseInnerIterator does not support yet triangular views with a unit diagonal");
|
2013-06-11 20:42:29 +08:00
|
|
|
if(SkipLast) {
|
|
|
|
|
while((*this) && (SkipDiag ? this->index()>=outer : this->index()>outer))
|
2011-12-04 06:49:37 +08:00
|
|
|
--(*this);
|
2013-06-11 20:42:29 +08:00
|
|
|
}
|
2011-12-04 06:49:37 +08:00
|
|
|
}
|
2011-12-04 21:39:24 +08:00
|
|
|
|
2013-05-17 20:40:32 +08:00
|
|
|
EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
|
2011-12-04 21:39:24 +08:00
|
|
|
{ Base::operator--(); return *this; }
|
|
|
|
|
|
2015-02-14 01:57:41 +08:00
|
|
|
inline Index row() const { return Base::row(); }
|
|
|
|
|
inline Index col() const { return Base::col(); }
|
2011-12-04 06:49:37 +08:00
|
|
|
|
|
|
|
|
EIGEN_STRONG_INLINE operator bool() const
|
|
|
|
|
{
|
2013-06-11 20:42:29 +08:00
|
|
|
if (SkipLast) return Base::operator bool() ;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(SkipDiag) return (Base::operator bool() && this->index() > this->outer());
|
|
|
|
|
else return (Base::operator bool() && this->index() >= this->outer());
|
|
|
|
|
}
|
2011-12-04 06:49:37 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-30 17:39:52 +08:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
template<typename ArgType, unsigned int Mode>
|
|
|
|
|
struct unary_evaluator<TriangularView<ArgType,Mode>, IteratorBased>
|
|
|
|
|
: evaluator_base<TriangularView<ArgType,Mode> >
|
|
|
|
|
{
|
|
|
|
|
typedef TriangularView<ArgType,Mode> XprType;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
typedef typename XprType::Scalar Scalar;
|
|
|
|
|
typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
|
|
|
|
|
|
|
|
|
|
enum { SkipFirst = ((Mode&Lower) && !(ArgType::Flags&RowMajorBit))
|
|
|
|
|
|| ((Mode&Upper) && (ArgType::Flags&RowMajorBit)),
|
|
|
|
|
SkipLast = !SkipFirst,
|
|
|
|
|
SkipDiag = (Mode&ZeroDiag) ? 1 : 0,
|
|
|
|
|
HasUnitDiag = (Mode&UnitDiag) ? 1 : 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
|
|
|
|
|
Flags = XprType::Flags
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-23 20:28:23 +08:00
|
|
|
explicit unary_evaluator(const XprType &xpr) : m_argImpl(xpr.nestedExpression()) {}
|
2014-07-30 17:39:52 +08:00
|
|
|
|
2015-04-02 04:27:34 +08:00
|
|
|
inline Index nonZerosEstimate() const {
|
|
|
|
|
return m_argImpl.nonZerosEstimate();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 17:39:52 +08:00
|
|
|
class InnerIterator : public EvalIterator
|
|
|
|
|
{
|
|
|
|
|
typedef EvalIterator Base;
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& xprEval, Index outer)
|
2014-08-01 22:24:23 +08:00
|
|
|
: Base(xprEval.m_argImpl,outer), m_returnOne(false)
|
2014-07-30 17:39:52 +08:00
|
|
|
{
|
|
|
|
|
if(SkipFirst)
|
|
|
|
|
{
|
|
|
|
|
while((*this) && ((HasUnitDiag||SkipDiag) ? this->index()<=outer : this->index()<outer))
|
|
|
|
|
Base::operator++();
|
|
|
|
|
if(HasUnitDiag)
|
|
|
|
|
m_returnOne = true;
|
|
|
|
|
}
|
|
|
|
|
else if(HasUnitDiag && ((!Base::operator bool()) || Base::index()>=Base::outer()))
|
|
|
|
|
{
|
|
|
|
|
if((!SkipFirst) && Base::operator bool())
|
|
|
|
|
Base::operator++();
|
2014-11-01 00:12:13 +08:00
|
|
|
m_returnOne = true; // FIXME check innerSize()>outer();
|
2014-07-30 17:39:52 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EIGEN_STRONG_INLINE InnerIterator& operator++()
|
|
|
|
|
{
|
|
|
|
|
if(HasUnitDiag && m_returnOne)
|
|
|
|
|
m_returnOne = false;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Base::operator++();
|
|
|
|
|
if(HasUnitDiag && (!SkipFirst) && ((!Base::operator bool()) || Base::index()>=Base::outer()))
|
|
|
|
|
{
|
|
|
|
|
if((!SkipFirst) && Base::operator bool())
|
|
|
|
|
Base::operator++();
|
2014-11-01 00:12:13 +08:00
|
|
|
m_returnOne = true; // FIXME check innerSize()>outer();
|
2014-07-30 17:39:52 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EIGEN_STRONG_INLINE operator bool() const
|
|
|
|
|
{
|
|
|
|
|
if(HasUnitDiag && m_returnOne)
|
|
|
|
|
return true;
|
|
|
|
|
if(SkipFirst) return Base::operator bool();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (SkipDiag) return (Base::operator bool() && this->index() < this->outer());
|
|
|
|
|
else return (Base::operator bool() && this->index() <= this->outer());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-14 01:57:41 +08:00
|
|
|
// inline Index row() const { return (ArgType::Flags&RowMajorBit ? Base::outer() : this->index()); }
|
|
|
|
|
// inline Index col() const { return (ArgType::Flags&RowMajorBit ? this->index() : Base::outer()); }
|
|
|
|
|
inline Index index() const
|
2014-07-30 17:39:52 +08:00
|
|
|
{
|
|
|
|
|
if(HasUnitDiag && m_returnOne) return Base::outer();
|
|
|
|
|
else return Base::index();
|
|
|
|
|
}
|
|
|
|
|
inline Scalar value() const
|
|
|
|
|
{
|
|
|
|
|
if(HasUnitDiag && m_returnOne) return Scalar(1);
|
|
|
|
|
else return Base::value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
bool m_returnOne;
|
|
|
|
|
private:
|
|
|
|
|
Scalar& valueRef();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
typename evaluator<ArgType>::type m_argImpl;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end namespace internal
|
|
|
|
|
|
2009-11-18 21:52:52 +08:00
|
|
|
template<typename Derived>
|
|
|
|
|
template<int Mode>
|
2014-10-08 00:29:28 +08:00
|
|
|
inline const TriangularView<const Derived, Mode>
|
2009-11-18 21:52:52 +08:00
|
|
|
SparseMatrixBase<Derived>::triangularView() const
|
|
|
|
|
{
|
2014-10-08 00:29:28 +08:00
|
|
|
return TriangularView<const Derived, Mode>(derived());
|
2009-11-18 21:52:52 +08:00
|
|
|
}
|
|
|
|
|
|
2012-04-15 18:06:28 +08:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
2015-05-02 04:10:41 +08:00
|
|
|
#endif // not EIGEN_PARSED_BY_DOXYGEN
|
|
|
|
|
|
2009-11-18 21:52:52 +08:00
|
|
|
#endif // EIGEN_SPARSE_TRIANGULARVIEW_H
|