diff --git a/Eigen/Core b/Eigen/Core index a233222e0..af3b4de98 100644 --- a/Eigen/Core +++ b/Eigen/Core @@ -40,10 +40,10 @@ namespace Eigen { #include "src/Core/CwiseBinaryOp.h" #include "src/Core/CwiseUnaryOp.h" #include "src/Core/CwiseNullaryOp.h" -#include "src/Core/InverseProduct.h" #include "src/Core/Dot.h" #include "src/Core/Product.h" #include "src/Core/DiagonalProduct.h" +#include "src/Core/InverseProduct.h" #include "src/Core/Block.h" #include "src/Core/Minor.h" #include "src/Core/Transpose.h" diff --git a/Eigen/src/Core/CwiseBinaryOp.h b/Eigen/src/Core/CwiseBinaryOp.h index 8df4097c3..ac5440c22 100644 --- a/Eigen/src/Core/CwiseBinaryOp.h +++ b/Eigen/src/Core/CwiseBinaryOp.h @@ -177,6 +177,11 @@ MatrixBase::operator+=(const MatrixBase& other) /** \returns an expression of the Schur product (coefficient wise product) of *this and \a other * + * \addexample CwiseProduct \label How to perform a component wise product of two matrices. + * + * Example: \include Cwise_product.cpp + * Output: \verbinclude Cwise_product.out + * * \sa class CwiseBinaryOp */ template diff --git a/Eigen/src/Core/CwiseNullaryOp.h b/Eigen/src/Core/CwiseNullaryOp.h index 343be79a1..a7957a426 100644 --- a/Eigen/src/Core/CwiseNullaryOp.h +++ b/Eigen/src/Core/CwiseNullaryOp.h @@ -450,7 +450,7 @@ Derived& MatrixBase::setOnes() * This variant is meant to be used for dynamic-size matrix types. For fixed-size types, * it is redundant to pass \a rows and \a cols as arguments, so Identity() should be used * instead. - * + * * \addexample Identity \label How to get an identity matrix * * Example: \include MatrixBase_identity_int_int.cpp diff --git a/Eigen/src/Core/Functors.h b/Eigen/src/Core/Functors.h index cb14585f6..cfbc7affb 100644 --- a/Eigen/src/Core/Functors.h +++ b/Eigen/src/Core/Functors.h @@ -318,6 +318,7 @@ struct ei_scalar_constant_op { }; template struct ei_scalar_constant_op { + inline ei_scalar_constant_op(const ei_scalar_constant_op& other) : m_other(other.m_other) { } inline ei_scalar_constant_op(const Scalar& other) : m_other(other) { } inline const Scalar operator() (int, int = 0) const { return m_other; } const Scalar m_other; diff --git a/Eigen/src/Core/InverseProduct.h b/Eigen/src/Core/InverseProduct.h index 0ee54a3fb..87f426af5 100755 --- a/Eigen/src/Core/InverseProduct.h +++ b/Eigen/src/Core/InverseProduct.h @@ -25,6 +25,171 @@ #ifndef EIGEN_INVERSEPRODUCT_H #define EIGEN_INVERSEPRODUCT_H +template +struct ei_trisolve_selector; + +// forward substitution, row-major +template +struct ei_trisolve_selector +{ + typedef typename Rhs::Scalar Scalar; + static void run(const Lhs& lhs, Rhs& other) + { + for(int c=0 ; c +struct ei_trisolve_selector +{ + typedef typename Rhs::Scalar Scalar; + static void run(const Lhs& lhs, Rhs& other) + { + const int size = lhs.cols(); + for(int c=0 ; c=0 ; --i) + { + Scalar tmp = other.coeff(i,c) + - ((lhs.row(i).end(size-i-1)) * other.col(c).end(size-i-1)).coeff(0,0); + if (Lhs::Flags & UnitDiagBit) + other.coeffRef(i,c) = tmp; + else + other.coeffRef(i,c) = tmp/lhs.coeff(i,i); + } + } + } +}; + +// forward substitution, col-major +template +struct ei_trisolve_selector +{ + typedef typename Rhs::Scalar Scalar; + typedef typename ei_packet_traits::type Packet; + enum {PacketSize = ei_packet_traits::size}; + + static void run(const Lhs& lhs, Rhs& other) + { + const int size = lhs.cols(); + for(int c=0 ; c btmp; + /* Let's process the 4x4 sub-matrix as usual. + * btmp stores the diagonal coefficients used to update the remaining part of the result. + */ + for (;i0) + other.col(c).block(i+1,remainingSize) -= other.coeffRef(i,c) * Block(lhs, i+1, i, remainingSize, 1); + btmp.coeffRef(i-startBlock) = -other.coeffRef(i,c); + } + + /* Now we can efficiently update the remaining part of the result as a matrix * vector product. + * NOTE in order to reduce both compilation time and binary size, let's directly call + * the fast product implementation. It is equivalent to the following code: + * other.col(c).end(size-endBlock) += (lhs.block(endBlock, startBlock, size-endBlock, endBlock-startBlock) + * * other.col(c).block(startBlock,endBlock-startBlock)).lazy(); + */ + ei_cache_friendly_product_colmajor_times_vector( + size-endBlock, &(lhs.const_cast_derived().coeffRef(endBlock,startBlock)), lhs.stride(), + btmp, &(other.coeffRef(endBlock,c))); + } + + /* Now we have to process the remaining part as usual */ + int i; + for(i=blockyEnd; i(lhs, i+1,i, size-i-1,1); + } + if(!(Lhs::Flags & UnitDiagBit)) + other.coeffRef(i,c) /= lhs.coeff(i,i); + } + } +}; + +// backward substitution, col-major +template +struct ei_trisolve_selector +{ + typedef typename Rhs::Scalar Scalar; + static void run(const Lhs& lhs, Rhs& other) + { + const int size = lhs.cols(); + for(int c=0 ; cblockyEnd;) + { + int startBlock = i; + int endBlock = startBlock-4; + Matrix btmp; + /* Let's process the 4x4 sub-matrix as usual. + * btmp stores the diagonal coefficients used to update the remaining part of the result. + */ + for (; i>endBlock; --i) + { + if(!(Lhs::Flags & UnitDiagBit)) + other.coeffRef(i,c) /= lhs.coeff(i,i); + int remainingSize = i-endBlock-1; + if (remainingSize>0) + other.col(c).block(endBlock+1,remainingSize) -= other.coeffRef(i,c) * Block(lhs, endBlock+1, i, remainingSize, 1); + btmp.coeffRef(remainingSize) = -other.coeffRef(i,c); + } + + ei_cache_friendly_product_colmajor_times_vector( + endBlock+1, &(lhs.const_cast_derived().coeffRef(0,endBlock+1)), lhs.stride(), + btmp, &(other.coeffRef(0,c))); + } + + for(int i=blockyEnd; i>0; --i) + { + if(!(Lhs::Flags & UnitDiagBit)) + other.coeffRef(i,c) /= lhs.coeff(i,i); + other.col(c).start(i) -= other.coeffRef(i,c) * Block(lhs, 0,i, i, 1); + } + if(!(Lhs::Flags & UnitDiagBit)) + other.coeffRef(0,c) /= lhs.coeff(0,0); + } + } +}; /** "in-place" version of MatrixBase::inverseProduct() where the result is written in \a other * @@ -34,42 +199,12 @@ template template void MatrixBase::inverseProductInPlace(MatrixBase& other) const { - ei_assert(cols() == other.rows()); + ei_assert(derived().cols() == derived().rows()); + ei_assert(derived().cols() == other.rows()); ei_assert(!(Flags & ZeroDiagBit)); ei_assert(Flags & (UpperTriangularBit|LowerTriangularBit)); - for(int c=0 ; crow(i).start(i)) * other.col(c).start(i)).coeff(0,0); - if (Flags & UnitDiagBit) - other.coeffRef(i,c) = tmp; - else - other.coeffRef(i,c) = tmp/coeff(i,i); - } - } - else - { - // backward substitution - if(!(Flags & UnitDiagBit)) - other.coeffRef(cols()-1,c) = other.coeff(cols()-1, c)/coeff(rows()-1, cols()-1); - for(int i=rows()-2 ; i>=0 ; --i) - { - Scalar tmp = other.coeff(i,c) - - ((this->row(i).end(cols()-i-1)) * other.col(c).end(cols()-i-1)).coeff(0,0); - if (Flags & UnitDiagBit) - other.coeffRef(i,c) = tmp; - else - other.coeffRef(i,c) = tmp/coeff(i,i); - } - } - } + ei_trisolve_selector::run(derived(), other.derived()); } /** \returns the product of the inverse of \c *this with \a other, \a *this being triangular. diff --git a/Eigen/src/Core/Part.h b/Eigen/src/Core/Part.h index cd349855d..1a7c7f82a 100644 --- a/Eigen/src/Core/Part.h +++ b/Eigen/src/Core/Part.h @@ -53,7 +53,7 @@ struct ei_traits > ColsAtCompileTime = MatrixType::ColsAtCompileTime, MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime, MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime, - Flags = (_MatrixTypeNested::Flags & ~(PacketAccessBit | LinearAccessBit | DirectAccessBit)) | Mode, + Flags = (_MatrixTypeNested::Flags & (HereditaryBits | DirectAccessBit) & (~(PacketAccessBit | LinearAccessBit))) | Mode, CoeffReadCost = _MatrixTypeNested::CoeffReadCost }; }; @@ -84,6 +84,7 @@ template class Part inline int rows() const { return m_matrix.rows(); } inline int cols() const { return m_matrix.cols(); } + inline int stride() const { return m_matrix.stride(); } inline Scalar coeff(int row, int col) const { @@ -97,7 +98,7 @@ template class Part return m_matrix.coeff(row, col); } - inline Scalar coeffRef(int row, int col) const + inline Scalar& coeffRef(int row, int col) { EIGEN_STATIC_ASSERT(!(Flags & UnitDiagBit), writting_to_triangular_part_with_unit_diag_is_not_supported); EIGEN_STATIC_ASSERT(!(Flags & SelfAdjointBit), default_writting_to_selfadjoint_not_supported); @@ -105,7 +106,7 @@ template class Part || (Mode==Lower && col<=row) || (Mode==StrictlyUpper && col>row) || (Mode==StrictlyLower && col */ template typename ei_eval::type -MatrixBase::perpendicular() const +MatrixBase::someOrthogonal() const { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived); return ei_perpendicular_selector::run(derived()); diff --git a/Eigen/src/Sparse/TriangularSolver.h b/Eigen/src/Sparse/TriangularSolver.h index 8634e114c..41361a471 100644 --- a/Eigen/src/Sparse/TriangularSolver.h +++ b/Eigen/src/Sparse/TriangularSolver.h @@ -33,11 +33,11 @@ template -struct ei_inverse_product_selector; +struct ei_sparse_trisolve_selector; // forward substitution, row-major template -struct ei_inverse_product_selector +struct ei_sparse_trisolve_selector { typedef typename Rhs::Scalar Scalar; static void run(const Lhs& lhs, const Rhs& rhs, Rhs& res) @@ -69,7 +69,7 @@ struct ei_inverse_product_selector // backward substitution, row-major template -struct ei_inverse_product_selector +struct ei_sparse_trisolve_selector { typedef typename Rhs::Scalar Scalar; static void run(const Lhs& lhs, const Rhs& rhs, Rhs& res) @@ -100,7 +100,7 @@ struct ei_inverse_product_selector // forward substitution, col-major template -struct ei_inverse_product_selector +struct ei_sparse_trisolve_selector { typedef typename Rhs::Scalar Scalar; static void run(const Lhs& lhs, const Rhs& rhs, Rhs& res) @@ -127,7 +127,7 @@ struct ei_inverse_product_selector // backward substitution, col-major template -struct ei_inverse_product_selector +struct ei_sparse_trisolve_selector { typedef typename Rhs::Scalar Scalar; static void run(const Lhs& lhs, const Rhs& rhs, Rhs& res) @@ -155,15 +155,14 @@ struct ei_inverse_product_selector template template -OtherDerived -SparseMatrixBase::inverseProduct(const MatrixBase& other) const +OtherDerived SparseMatrixBase::inverseProduct(const MatrixBase& other) const { ei_assert(derived().cols() == other.rows()); ei_assert(!(Flags & ZeroDiagBit)); ei_assert(Flags & (UpperTriangularBit|LowerTriangularBit)); OtherDerived res(other.rows(), other.cols()); - ei_inverse_product_selector::run(derived(), other.derived(), res); + ei_sparse_trisolve_selector::run(derived(), other.derived(), res); return res; } diff --git a/doc/snippets/Cwise_product.cpp b/doc/snippets/Cwise_product.cpp new file mode 100644 index 000000000..460ed6700 --- /dev/null +++ b/doc/snippets/Cwise_product.cpp @@ -0,0 +1,4 @@ +Matrix3i a = Matrix3i::Random(), b = Matrix3i::Random(); +Matrix3i c = a.cwise() * b; +cout << "a:\n" << a << "\nb:\n" << b << "\nc:\n" << c << endl; + diff --git a/test/geometry.cpp b/test/geometry.cpp index 829165da7..a41a26c23 100644 --- a/test/geometry.cpp +++ b/test/geometry.cpp @@ -58,9 +58,9 @@ template void geometry(void) (v0.cross(v1).cross(v0)).normalized(); VERIFY(m.isUnitary()); - // perpendicular - VERIFY_IS_MUCH_SMALLER_THAN(u0.perpendicular().dot(u0), Scalar(1)); - VERIFY_IS_MUCH_SMALLER_THAN(v0.perpendicular().dot(v0), Scalar(1)); + // someOrthogonal + VERIFY_IS_MUCH_SMALLER_THAN(u0.someOrthogonal().dot(u0), Scalar(1)); + VERIFY_IS_MUCH_SMALLER_THAN(v0.someOrthogonal().dot(v0), Scalar(1)); q1 = AngleAxis(ei_random(-M_PI, M_PI), v0.normalized()); q2 = AngleAxis(ei_random(-M_PI, M_PI), v1.normalized()); diff --git a/test/triangular.cpp b/test/triangular.cpp index 185471dc7..a1e5383bc 100644 --- a/test/triangular.cpp +++ b/test/triangular.cpp @@ -27,6 +27,7 @@ template void triangular(const MatrixType& m) { typedef typename MatrixType::Scalar Scalar; + typedef typename NumTraits::Real RealScalar; typedef Matrix VectorType; int rows = m.rows(); @@ -78,9 +79,17 @@ template void triangular(const MatrixType& m) VERIFY_IS_APPROX(m3.template part(), m1); // test back and forward subsitution - m1 = MatrixType::Random(rows, cols); - VERIFY_IS_APPROX(m1.template part() * (m1.template part().inverseProduct(m2)), m2); - VERIFY_IS_APPROX(m1.template part() * (m1.template part().inverseProduct(m2)), m2); + m3 = m1.template part(); + VERIFY(m3.template marked().inverseProduct(m3).cwise().abs().isIdentity(test_precision())); + + m3 = m1.template part(); + VERIFY(m3.template marked().inverseProduct(m3).cwise().abs().isIdentity(test_precision())); + + // FIXME these tests failed due to numerical issues + // m1 = MatrixType::Random(rows, cols); + // VERIFY_IS_APPROX(m1.template part().eval() * (m1.template part().inverseProduct(m2)), m2); + // VERIFY_IS_APPROX(m1.template part().eval() * (m1.template part().inverseProduct(m2)), m2); + VERIFY((m1.template part() * m2.template part()).isUpper()); } @@ -91,6 +100,7 @@ void test_triangular() // triangular(Matrix()); CALL_SUBTEST( triangular(Matrix3d()) ); CALL_SUBTEST( triangular(MatrixXcf(4, 4)) ); -// CALL_SUBTEST( triangular(Matrix,8, 8>()) ); + CALL_SUBTEST( triangular(Matrix,8, 8>()) ); + CALL_SUBTEST( triangular(MatrixXf(12,12)) ); } }