// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008-2009 Gael Guennebaud // // 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/. #ifndef EIGEN_SOLVETRIANGULAR_H #define EIGEN_SOLVETRIANGULAR_H namespace Eigen { namespace internal { // Forward declarations: // The following two routines are implemented in the products/TriangularSolver*.h files template struct triangular_solve_vector; template struct triangular_solve_matrix; // small helper struct extracting some traits on the underlying solver operation template class trsolve_traits { private: enum { RhsIsVectorAtCompileTime = (Side==OnTheLeft ? Rhs::ColsAtCompileTime : Rhs::RowsAtCompileTime)==1 }; public: enum { Unrolling = (RhsIsVectorAtCompileTime && Rhs::SizeAtCompileTime != Dynamic && Rhs::SizeAtCompileTime <= 8) ? CompleteUnrolling : NoUnrolling, RhsVectors = RhsIsVectorAtCompileTime ? 1 : Dynamic }; }; template::Unrolling, int RhsVectors = trsolve_traits::RhsVectors > struct triangular_solver_selector; template struct triangular_solver_selector { typedef typename Lhs::Scalar LhsScalar; typedef typename Rhs::Scalar RhsScalar; typedef blas_traits LhsProductTraits; typedef typename LhsProductTraits::ExtractType ActualLhsType; typedef Map, Aligned> MappedRhs; static void run(const Lhs& lhs, Rhs& rhs) { ActualLhsType actualLhs = LhsProductTraits::extract(lhs); // FIXME find a way to allow an inner stride if packet_traits::size==1 bool useRhsDirectly = Rhs::InnerStrideAtCompileTime==1 || rhs.innerStride()==1; ei_declare_aligned_stack_constructed_variable(RhsScalar,actualRhs,rhs.size(), (useRhsDirectly ? rhs.data() : 0)); if(!useRhsDirectly) MappedRhs(actualRhs,rhs.size()) = rhs; triangular_solve_vector ::run(actualLhs.cols(), actualLhs.data(), actualLhs.outerStride(), actualRhs); if(!useRhsDirectly) rhs = MappedRhs(actualRhs, rhs.size()); } }; // the rhs is a matrix template struct triangular_solver_selector { typedef typename Rhs::Scalar Scalar; typedef typename Rhs::Index Index; typedef blas_traits LhsProductTraits; typedef typename LhsProductTraits::DirectLinearAccessType ActualLhsType; static void run(const Lhs& lhs, Rhs& rhs) { typename internal::add_const_on_value_type::type actualLhs = LhsProductTraits::extract(lhs); const Index size = lhs.rows(); const Index othersize = Side==OnTheLeft? rhs.cols() : rhs.rows(); typedef internal::gemm_blocking_space<(Rhs::Flags&RowMajorBit) ? RowMajor : ColMajor,Scalar,Scalar, Rhs::MaxRowsAtCompileTime, Rhs::MaxColsAtCompileTime, Lhs::MaxRowsAtCompileTime,4> BlockingType; BlockingType blocking(rhs.rows(), rhs.cols(), size); triangular_solve_matrix ::run(size, othersize, &actualLhs.coeffRef(0,0), actualLhs.outerStride(), &rhs.coeffRef(0,0), rhs.outerStride(), blocking); } }; /*************************************************************************** * meta-unrolling implementation ***************************************************************************/ template struct triangular_solver_unroller; template struct triangular_solver_unroller { enum { IsLower = ((Mode&Lower)==Lower), I = IsLower ? Index : Size - Index - 1, S = IsLower ? 0 : I+1 }; static void run(const Lhs& lhs, Rhs& rhs) { if (Index>0) rhs.coeffRef(I) -= lhs.row(I).template segment(S).transpose() .cwiseProduct(rhs.template segment(S)).sum(); if(!(Mode & UnitDiag)) rhs.coeffRef(I) /= lhs.coeff(I,I); triangular_solver_unroller::run(lhs,rhs); } }; template struct triangular_solver_unroller { static void run(const Lhs&, Rhs&) {} }; template struct triangular_solver_selector { static void run(const Lhs& lhs, Rhs& rhs) { triangular_solver_unroller::run(lhs,rhs); } }; template struct triangular_solver_selector { static void run(const Lhs& lhs, Rhs& rhs) { Transpose trLhs(lhs); Transpose trRhs(rhs); triangular_solver_unroller,Transpose, ((Mode&Upper)==Upper ? Lower : Upper) | (Mode&UnitDiag), 0,Rhs::SizeAtCompileTime>::run(trLhs,trRhs); } }; } // end namespace internal /*************************************************************************** * TriangularView methods ***************************************************************************/ /** "in-place" version of TriangularView::solve() where the result is written in \a other * * \warning The parameter is only marked 'const' to make the C++ compiler accept a temporary expression here. * This function will const_cast it, so constness isn't honored here. * * See TriangularView:solve() for the details. */ template template void TriangularView::solveInPlace(const MatrixBase& _other) const { OtherDerived& other = _other.const_cast_derived(); eigen_assert( cols() == rows() && ((Side==OnTheLeft && cols() == other.rows()) || (Side==OnTheRight && cols() == other.cols())) ); eigen_assert((!(Mode & ZeroDiag)) && bool(Mode & (Upper|Lower))); enum { copy = internal::traits::Flags & RowMajorBit && OtherDerived::IsVectorAtCompileTime }; typedef typename internal::conditional::type, OtherDerived&>::type OtherCopy; OtherCopy otherCopy(other); internal::triangular_solver_selector::type, Side, Mode>::run(nestedExpression(), otherCopy); if (copy) other = otherCopy; } /** \returns the product of the inverse of \c *this with \a other, \a *this being triangular. * * This function computes the inverse-matrix matrix product inverse(\c *this) * \a other if * \a Side==OnTheLeft (the default), or the right-inverse-multiply \a other * inverse(\c *this) if * \a Side==OnTheRight. * * The matrix \c *this must be triangular and invertible (i.e., all the coefficients of the * diagonal must be non zero). It works as a forward (resp. backward) substitution if \c *this * is an upper (resp. lower) triangular matrix. * * Example: \include MatrixBase_marked.cpp * Output: \verbinclude MatrixBase_marked.out * * This function returns an expression of the inverse-multiply and can works in-place if it is assigned * to the same matrix or vector \a other. * * For users coming from BLAS, this function (and more specifically solveInPlace()) offer * all the operations supported by the \c *TRSV and \c *TRSM BLAS routines. * * \sa TriangularView::solveInPlace() */ template template const internal::triangular_solve_retval,Other> TriangularView::solve(const MatrixBase& other) const { return internal::triangular_solve_retval(*this, other.derived()); } namespace internal { template struct traits > { typedef typename internal::plain_matrix_type_column_major::type ReturnType; }; template struct triangular_solve_retval : public ReturnByValue > { typedef typename remove_all::type RhsNestedCleaned; typedef ReturnByValue Base; typedef typename Base::Index Index; triangular_solve_retval(const TriangularType& tri, const Rhs& rhs) : m_triangularMatrix(tri), m_rhs(rhs) {} inline Index rows() const { return m_rhs.rows(); } inline Index cols() const { return m_rhs.cols(); } template inline void evalTo(Dest& dst) const { if(!(is_same::value && extract_data(dst) == extract_data(m_rhs))) dst = m_rhs; m_triangularMatrix.template solveInPlace(dst); } protected: const TriangularType& m_triangularMatrix; typename Rhs::Nested m_rhs; }; } // namespace internal } // end namespace Eigen #endif // EIGEN_SOLVETRIANGULAR_H