diff options
Diffstat (limited to 'extern/Eigen2/Eigen/src/LU')
-rw-r--r-- | extern/Eigen2/Eigen/src/LU/Determinant.h | 122 | ||||
-rw-r--r-- | extern/Eigen2/Eigen/src/LU/Inverse.h | 258 | ||||
-rw-r--r-- | extern/Eigen2/Eigen/src/LU/LU.h | 541 |
3 files changed, 0 insertions, 921 deletions
diff --git a/extern/Eigen2/Eigen/src/LU/Determinant.h b/extern/Eigen2/Eigen/src/LU/Determinant.h deleted file mode 100644 index 4f435054ac6..00000000000 --- a/extern/Eigen2/Eigen/src/LU/Determinant.h +++ /dev/null @@ -1,122 +0,0 @@ -// This file is part of Eigen, a lightweight C++ template library -// for linear algebra. Eigen itself is part of the KDE project. -// -// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com> -// -// Eigen is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 3 of the License, or (at your option) any later version. -// -// Alternatively, you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of -// the License, or (at your option) any later version. -// -// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License and a copy of the GNU General Public License along with -// Eigen. If not, see <http://www.gnu.org/licenses/>. - -#ifndef EIGEN_DETERMINANT_H -#define EIGEN_DETERMINANT_H - -template<typename Derived> -inline const typename Derived::Scalar ei_bruteforce_det3_helper -(const MatrixBase<Derived>& matrix, int a, int b, int c) -{ - return matrix.coeff(0,a) - * (matrix.coeff(1,b) * matrix.coeff(2,c) - matrix.coeff(1,c) * matrix.coeff(2,b)); -} - -template<typename Derived> -const typename Derived::Scalar ei_bruteforce_det4_helper -(const MatrixBase<Derived>& matrix, int j, int k, int m, int n) -{ - return (matrix.coeff(j,0) * matrix.coeff(k,1) - matrix.coeff(k,0) * matrix.coeff(j,1)) - * (matrix.coeff(m,2) * matrix.coeff(n,3) - matrix.coeff(n,2) * matrix.coeff(m,3)); -} - -const int TriangularDeterminant = 0; - -template<typename Derived, - int DeterminantType = - (Derived::Flags & (UpperTriangularBit | LowerTriangularBit)) - ? TriangularDeterminant : Derived::RowsAtCompileTime -> struct ei_determinant_impl -{ - static inline typename ei_traits<Derived>::Scalar run(const Derived& m) - { - return m.lu().determinant(); - } -}; - -template<typename Derived> struct ei_determinant_impl<Derived, TriangularDeterminant> -{ - static inline typename ei_traits<Derived>::Scalar run(const Derived& m) - { - if (Derived::Flags & UnitDiagBit) - return 1; - else if (Derived::Flags & ZeroDiagBit) - return 0; - else - return m.diagonal().redux(ei_scalar_product_op<typename ei_traits<Derived>::Scalar>()); - } -}; - -template<typename Derived> struct ei_determinant_impl<Derived, 1> -{ - static inline typename ei_traits<Derived>::Scalar run(const Derived& m) - { - return m.coeff(0,0); - } -}; - -template<typename Derived> struct ei_determinant_impl<Derived, 2> -{ - static inline typename ei_traits<Derived>::Scalar run(const Derived& m) - { - return m.coeff(0,0) * m.coeff(1,1) - m.coeff(1,0) * m.coeff(0,1); - } -}; - -template<typename Derived> struct ei_determinant_impl<Derived, 3> -{ - static typename ei_traits<Derived>::Scalar run(const Derived& m) - { - return ei_bruteforce_det3_helper(m,0,1,2) - - ei_bruteforce_det3_helper(m,1,0,2) - + ei_bruteforce_det3_helper(m,2,0,1); - } -}; - -template<typename Derived> struct ei_determinant_impl<Derived, 4> -{ - static typename ei_traits<Derived>::Scalar run(const Derived& m) - { - // trick by Martin Costabel to compute 4x4 det with only 30 muls - return ei_bruteforce_det4_helper(m,0,1,2,3) - - ei_bruteforce_det4_helper(m,0,2,1,3) - + ei_bruteforce_det4_helper(m,0,3,1,2) - + ei_bruteforce_det4_helper(m,1,2,0,3) - - ei_bruteforce_det4_helper(m,1,3,0,2) - + ei_bruteforce_det4_helper(m,2,3,0,1); - } -}; - -/** \lu_module - * - * \returns the determinant of this matrix - */ -template<typename Derived> -inline typename ei_traits<Derived>::Scalar MatrixBase<Derived>::determinant() const -{ - assert(rows() == cols()); - return ei_determinant_impl<Derived>::run(derived()); -} - -#endif // EIGEN_DETERMINANT_H diff --git a/extern/Eigen2/Eigen/src/LU/Inverse.h b/extern/Eigen2/Eigen/src/LU/Inverse.h deleted file mode 100644 index 3d4d6348949..00000000000 --- a/extern/Eigen2/Eigen/src/LU/Inverse.h +++ /dev/null @@ -1,258 +0,0 @@ -// This file is part of Eigen, a lightweight C++ template library -// for linear algebra. Eigen itself is part of the KDE project. -// -// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com> -// -// Eigen is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 3 of the License, or (at your option) any later version. -// -// Alternatively, you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of -// the License, or (at your option) any later version. -// -// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License and a copy of the GNU General Public License along with -// Eigen. If not, see <http://www.gnu.org/licenses/>. - -#ifndef EIGEN_INVERSE_H -#define EIGEN_INVERSE_H - -/******************************************************************** -*** Part 1 : optimized implementations for fixed-size 2,3,4 cases *** -********************************************************************/ - -template<typename MatrixType> -void ei_compute_inverse_in_size2_case(const MatrixType& matrix, MatrixType* result) -{ - typedef typename MatrixType::Scalar Scalar; - const Scalar invdet = Scalar(1) / matrix.determinant(); - result->coeffRef(0,0) = matrix.coeff(1,1) * invdet; - result->coeffRef(1,0) = -matrix.coeff(1,0) * invdet; - result->coeffRef(0,1) = -matrix.coeff(0,1) * invdet; - result->coeffRef(1,1) = matrix.coeff(0,0) * invdet; -} - -template<typename XprType, typename MatrixType> -bool ei_compute_inverse_in_size2_case_with_check(const XprType& matrix, MatrixType* result) -{ - typedef typename MatrixType::Scalar Scalar; - const Scalar det = matrix.determinant(); - if(ei_isMuchSmallerThan(det, matrix.cwise().abs().maxCoeff())) return false; - const Scalar invdet = Scalar(1) / det; - result->coeffRef(0,0) = matrix.coeff(1,1) * invdet; - result->coeffRef(1,0) = -matrix.coeff(1,0) * invdet; - result->coeffRef(0,1) = -matrix.coeff(0,1) * invdet; - result->coeffRef(1,1) = matrix.coeff(0,0) * invdet; - return true; -} - -template<typename MatrixType> -void ei_compute_inverse_in_size3_case(const MatrixType& matrix, MatrixType* result) -{ - typedef typename MatrixType::Scalar Scalar; - const Scalar det_minor00 = matrix.minor(0,0).determinant(); - const Scalar det_minor10 = matrix.minor(1,0).determinant(); - const Scalar det_minor20 = matrix.minor(2,0).determinant(); - const Scalar invdet = Scalar(1) / ( det_minor00 * matrix.coeff(0,0) - - det_minor10 * matrix.coeff(1,0) - + det_minor20 * matrix.coeff(2,0) ); - result->coeffRef(0, 0) = det_minor00 * invdet; - result->coeffRef(0, 1) = -det_minor10 * invdet; - result->coeffRef(0, 2) = det_minor20 * invdet; - result->coeffRef(1, 0) = -matrix.minor(0,1).determinant() * invdet; - result->coeffRef(1, 1) = matrix.minor(1,1).determinant() * invdet; - result->coeffRef(1, 2) = -matrix.minor(2,1).determinant() * invdet; - result->coeffRef(2, 0) = matrix.minor(0,2).determinant() * invdet; - result->coeffRef(2, 1) = -matrix.minor(1,2).determinant() * invdet; - result->coeffRef(2, 2) = matrix.minor(2,2).determinant() * invdet; -} - -template<typename MatrixType> -bool ei_compute_inverse_in_size4_case_helper(const MatrixType& matrix, MatrixType* result) -{ - /* Let's split M into four 2x2 blocks: - * (P Q) - * (R S) - * If P is invertible, with inverse denoted by P_inverse, and if - * (S - R*P_inverse*Q) is also invertible, then the inverse of M is - * (P' Q') - * (R' S') - * where - * S' = (S - R*P_inverse*Q)^(-1) - * P' = P1 + (P1*Q) * S' *(R*P_inverse) - * Q' = -(P_inverse*Q) * S' - * R' = -S' * (R*P_inverse) - */ - typedef Block<MatrixType,2,2> XprBlock22; - typedef typename MatrixBase<XprBlock22>::PlainMatrixType Block22; - Block22 P_inverse; - if(ei_compute_inverse_in_size2_case_with_check(matrix.template block<2,2>(0,0), &P_inverse)) - { - const Block22 Q = matrix.template block<2,2>(0,2); - const Block22 P_inverse_times_Q = P_inverse * Q; - const XprBlock22 R = matrix.template block<2,2>(2,0); - const Block22 R_times_P_inverse = R * P_inverse; - const Block22 R_times_P_inverse_times_Q = R_times_P_inverse * Q; - const XprBlock22 S = matrix.template block<2,2>(2,2); - const Block22 X = S - R_times_P_inverse_times_Q; - Block22 Y; - ei_compute_inverse_in_size2_case(X, &Y); - result->template block<2,2>(2,2) = Y; - result->template block<2,2>(2,0) = - Y * R_times_P_inverse; - const Block22 Z = P_inverse_times_Q * Y; - result->template block<2,2>(0,2) = - Z; - result->template block<2,2>(0,0) = P_inverse + Z * R_times_P_inverse; - return true; - } - else - { - return false; - } -} - -template<typename MatrixType> -void ei_compute_inverse_in_size4_case(const MatrixType& matrix, MatrixType* result) -{ - if(ei_compute_inverse_in_size4_case_helper(matrix, result)) - { - // good ! The topleft 2x2 block was invertible, so the 2x2 blocks approach is successful. - return; - } - else - { - // rare case: the topleft 2x2 block is not invertible (but the matrix itself is assumed to be). - // since this is a rare case, we don't need to optimize it. We just want to handle it with little - // additional code. - MatrixType m(matrix); - m.row(0).swap(m.row(2)); - m.row(1).swap(m.row(3)); - if(ei_compute_inverse_in_size4_case_helper(m, result)) - { - // good, the topleft 2x2 block of m is invertible. Since m is different from matrix in that some - // rows were permuted, the actual inverse of matrix is derived from the inverse of m by permuting - // the corresponding columns. - result->col(0).swap(result->col(2)); - result->col(1).swap(result->col(3)); - } - else - { - // last possible case. Since matrix is assumed to be invertible, this last case has to work. - // first, undo the swaps previously made - m.row(0).swap(m.row(2)); - m.row(1).swap(m.row(3)); - // swap row 0 with the the row among 0 and 1 that has the biggest 2 first coeffs - int swap0with = ei_abs(m.coeff(0,0))+ei_abs(m.coeff(0,1))>ei_abs(m.coeff(1,0))+ei_abs(m.coeff(1,1)) ? 0 : 1; - m.row(0).swap(m.row(swap0with)); - // swap row 1 with the the row among 2 and 3 that has the biggest 2 first coeffs - int swap1with = ei_abs(m.coeff(2,0))+ei_abs(m.coeff(2,1))>ei_abs(m.coeff(3,0))+ei_abs(m.coeff(3,1)) ? 2 : 3; - m.row(1).swap(m.row(swap1with)); - ei_compute_inverse_in_size4_case_helper(m, result); - result->col(1).swap(result->col(swap1with)); - result->col(0).swap(result->col(swap0with)); - } - } -} - -/*********************************************** -*** Part 2 : selector and MatrixBase methods *** -***********************************************/ - -template<typename MatrixType, int Size = MatrixType::RowsAtCompileTime> -struct ei_compute_inverse -{ - static inline void run(const MatrixType& matrix, MatrixType* result) - { - LU<MatrixType> lu(matrix); - lu.computeInverse(result); - } -}; - -template<typename MatrixType> -struct ei_compute_inverse<MatrixType, 1> -{ - static inline void run(const MatrixType& matrix, MatrixType* result) - { - typedef typename MatrixType::Scalar Scalar; - result->coeffRef(0,0) = Scalar(1) / matrix.coeff(0,0); - } -}; - -template<typename MatrixType> -struct ei_compute_inverse<MatrixType, 2> -{ - static inline void run(const MatrixType& matrix, MatrixType* result) - { - ei_compute_inverse_in_size2_case(matrix, result); - } -}; - -template<typename MatrixType> -struct ei_compute_inverse<MatrixType, 3> -{ - static inline void run(const MatrixType& matrix, MatrixType* result) - { - ei_compute_inverse_in_size3_case(matrix, result); - } -}; - -template<typename MatrixType> -struct ei_compute_inverse<MatrixType, 4> -{ - static inline void run(const MatrixType& matrix, MatrixType* result) - { - ei_compute_inverse_in_size4_case(matrix, result); - } -}; - -/** \lu_module - * - * Computes the matrix inverse of this matrix. - * - * \note This matrix must be invertible, otherwise the result is undefined. - * - * \param result Pointer to the matrix in which to store the result. - * - * Example: \include MatrixBase_computeInverse.cpp - * Output: \verbinclude MatrixBase_computeInverse.out - * - * \sa inverse() - */ -template<typename Derived> -inline void MatrixBase<Derived>::computeInverse(PlainMatrixType *result) const -{ - ei_assert(rows() == cols()); - EIGEN_STATIC_ASSERT(NumTraits<Scalar>::HasFloatingPoint,NUMERIC_TYPE_MUST_BE_FLOATING_POINT) - ei_compute_inverse<PlainMatrixType>::run(eval(), result); -} - -/** \lu_module - * - * \returns the matrix inverse of this matrix. - * - * \note This matrix must be invertible, otherwise the result is undefined. - * - * \note This method returns a matrix by value, which can be inefficient. To avoid that overhead, - * use computeInverse() instead. - * - * Example: \include MatrixBase_inverse.cpp - * Output: \verbinclude MatrixBase_inverse.out - * - * \sa computeInverse() - */ -template<typename Derived> -inline const typename MatrixBase<Derived>::PlainMatrixType MatrixBase<Derived>::inverse() const -{ - PlainMatrixType result(rows(), cols()); - computeInverse(&result); - return result; -} - -#endif // EIGEN_INVERSE_H diff --git a/extern/Eigen2/Eigen/src/LU/LU.h b/extern/Eigen2/Eigen/src/LU/LU.h deleted file mode 100644 index 176e76a91a3..00000000000 --- a/extern/Eigen2/Eigen/src/LU/LU.h +++ /dev/null @@ -1,541 +0,0 @@ -// This file is part of Eigen, a lightweight C++ template library -// for linear algebra. Eigen itself is part of the KDE project. -// -// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com> -// -// Eigen is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 3 of the License, or (at your option) any later version. -// -// Alternatively, you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of -// the License, or (at your option) any later version. -// -// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License and a copy of the GNU General Public License along with -// Eigen. If not, see <http://www.gnu.org/licenses/>. - -#ifndef EIGEN_LU_H -#define EIGEN_LU_H - -/** \ingroup LU_Module - * - * \class LU - * - * \brief LU decomposition of a matrix with complete pivoting, and related features - * - * \param MatrixType the type of the matrix of which we are computing the LU decomposition - * - * This class represents a LU decomposition of any matrix, with complete pivoting: the matrix A - * is decomposed as A = PLUQ where L is unit-lower-triangular, U is upper-triangular, and P and Q - * are permutation matrices. This is a rank-revealing LU decomposition. The eigenvalues (diagonal - * coefficients) of U are sorted in such a way that any zeros are at the end, so that the rank - * of A is the index of the first zero on the diagonal of U (with indices starting at 0) if any. - * - * This decomposition provides the generic approach to solving systems of linear equations, computing - * the rank, invertibility, inverse, kernel, and determinant. - * - * This LU decomposition is very stable and well tested with large matrices. Even exact rank computation - * works at sizes larger than 1000x1000. However there are use cases where the SVD decomposition is inherently - * more stable when dealing with numerically damaged input. For example, computing the kernel is more stable with - * SVD because the SVD can determine which singular values are negligible while LU has to work at the level of matrix - * coefficients that are less meaningful in this respect. - * - * The data of the LU decomposition can be directly accessed through the methods matrixLU(), - * permutationP(), permutationQ(). - * - * As an exemple, here is how the original matrix can be retrieved: - * \include class_LU.cpp - * Output: \verbinclude class_LU.out - * - * \sa MatrixBase::lu(), MatrixBase::determinant(), MatrixBase::inverse(), MatrixBase::computeInverse() - */ -template<typename MatrixType> class LU -{ - public: - - typedef typename MatrixType::Scalar Scalar; - typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar; - typedef Matrix<int, 1, MatrixType::ColsAtCompileTime> IntRowVectorType; - typedef Matrix<int, MatrixType::RowsAtCompileTime, 1> IntColVectorType; - typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType; - typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> ColVectorType; - - enum { MaxSmallDimAtCompileTime = EIGEN_ENUM_MIN( - MatrixType::MaxColsAtCompileTime, - MatrixType::MaxRowsAtCompileTime) - }; - - typedef Matrix<typename MatrixType::Scalar, - MatrixType::ColsAtCompileTime, // the number of rows in the "kernel matrix" is the number of cols of the original matrix - // so that the product "matrix * kernel = zero" makes sense - Dynamic, // we don't know at compile-time the dimension of the kernel - MatrixType::Options, - MatrixType::MaxColsAtCompileTime, // see explanation for 2nd template parameter - MatrixType::MaxColsAtCompileTime // the kernel is a subspace of the domain space, whose dimension is the number - // of columns of the original matrix - > KernelResultType; - - typedef Matrix<typename MatrixType::Scalar, - MatrixType::RowsAtCompileTime, // the image is a subspace of the destination space, whose dimension is the number - // of rows of the original matrix - Dynamic, // we don't know at compile time the dimension of the image (the rank) - MatrixType::Options, - MatrixType::MaxRowsAtCompileTime, // the image matrix will consist of columns from the original matrix, - MatrixType::MaxColsAtCompileTime // so it has the same number of rows and at most as many columns. - > ImageResultType; - - /** Constructor. - * - * \param matrix the matrix of which to compute the LU decomposition. - */ - LU(const MatrixType& matrix); - - /** \returns the LU decomposition matrix: the upper-triangular part is U, the - * unit-lower-triangular part is L (at least for square matrices; in the non-square - * case, special care is needed, see the documentation of class LU). - * - * \sa matrixL(), matrixU() - */ - inline const MatrixType& matrixLU() const - { - return m_lu; - } - - /** \returns a vector of integers, whose size is the number of rows of the matrix being decomposed, - * representing the P permutation i.e. the permutation of the rows. For its precise meaning, - * see the examples given in the documentation of class LU. - * - * \sa permutationQ() - */ - inline const IntColVectorType& permutationP() const - { - return m_p; - } - - /** \returns a vector of integers, whose size is the number of columns of the matrix being - * decomposed, representing the Q permutation i.e. the permutation of the columns. - * For its precise meaning, see the examples given in the documentation of class LU. - * - * \sa permutationP() - */ - inline const IntRowVectorType& permutationQ() const - { - return m_q; - } - - /** Computes a basis of the kernel of the matrix, also called the null-space of the matrix. - * - * \note This method is only allowed on non-invertible matrices, as determined by - * isInvertible(). Calling it on an invertible matrix will make an assertion fail. - * - * \param result a pointer to the matrix in which to store the kernel. The columns of this - * matrix will be set to form a basis of the kernel (it will be resized - * if necessary). - * - * Example: \include LU_computeKernel.cpp - * Output: \verbinclude LU_computeKernel.out - * - * \sa kernel(), computeImage(), image() - */ - template<typename KernelMatrixType> - void computeKernel(KernelMatrixType *result) const; - - /** Computes a basis of the image of the matrix, also called the column-space or range of he matrix. - * - * \note Calling this method on the zero matrix will make an assertion fail. - * - * \param result a pointer to the matrix in which to store the image. The columns of this - * matrix will be set to form a basis of the image (it will be resized - * if necessary). - * - * Example: \include LU_computeImage.cpp - * Output: \verbinclude LU_computeImage.out - * - * \sa image(), computeKernel(), kernel() - */ - template<typename ImageMatrixType> - void computeImage(ImageMatrixType *result) const; - - /** \returns the kernel of the matrix, also called its null-space. The columns of the returned matrix - * will form a basis of the kernel. - * - * \note: this method is only allowed on non-invertible matrices, as determined by - * isInvertible(). Calling it on an invertible matrix will make an assertion fail. - * - * \note: this method returns a matrix by value, which induces some inefficiency. - * If you prefer to avoid this overhead, use computeKernel() instead. - * - * Example: \include LU_kernel.cpp - * Output: \verbinclude LU_kernel.out - * - * \sa computeKernel(), image() - */ - const KernelResultType kernel() const; - - /** \returns the image of the matrix, also called its column-space. The columns of the returned matrix - * will form a basis of the kernel. - * - * \note: Calling this method on the zero matrix will make an assertion fail. - * - * \note: this method returns a matrix by value, which induces some inefficiency. - * If you prefer to avoid this overhead, use computeImage() instead. - * - * Example: \include LU_image.cpp - * Output: \verbinclude LU_image.out - * - * \sa computeImage(), kernel() - */ - const ImageResultType image() const; - - /** This method finds a solution x to the equation Ax=b, where A is the matrix of which - * *this is the LU decomposition, if any exists. - * - * \param b the right-hand-side of the equation to solve. Can be a vector or a matrix, - * the only requirement in order for the equation to make sense is that - * b.rows()==A.rows(), where A is the matrix of which *this is the LU decomposition. - * \param result a pointer to the vector or matrix in which to store the solution, if any exists. - * Resized if necessary, so that result->rows()==A.cols() and result->cols()==b.cols(). - * If no solution exists, *result is left with undefined coefficients. - * - * \returns true if any solution exists, false if no solution exists. - * - * \note If there exist more than one solution, this method will arbitrarily choose one. - * If you need a complete analysis of the space of solutions, take the one solution obtained - * by this method and add to it elements of the kernel, as determined by kernel(). - * - * Example: \include LU_solve.cpp - * Output: \verbinclude LU_solve.out - * - * \sa MatrixBase::solveTriangular(), kernel(), computeKernel(), inverse(), computeInverse() - */ - template<typename OtherDerived, typename ResultType> - bool solve(const MatrixBase<OtherDerived>& b, ResultType *result) const; - - /** \returns the determinant of the matrix of which - * *this is the LU decomposition. It has only linear complexity - * (that is, O(n) where n is the dimension of the square matrix) - * as the LU decomposition has already been computed. - * - * \note This is only for square matrices. - * - * \note For fixed-size matrices of size up to 4, MatrixBase::determinant() offers - * optimized paths. - * - * \warning a determinant can be very big or small, so for matrices - * of large enough dimension, there is a risk of overflow/underflow. - * - * \sa MatrixBase::determinant() - */ - typename ei_traits<MatrixType>::Scalar determinant() const; - - /** \returns the rank of the matrix of which *this is the LU decomposition. - * - * \note This is computed at the time of the construction of the LU decomposition. This - * method does not perform any further computation. - */ - inline int rank() const - { - return m_rank; - } - - /** \returns the dimension of the kernel of the matrix of which *this is the LU decomposition. - * - * \note Since the rank is computed at the time of the construction of the LU decomposition, this - * method almost does not perform any further computation. - */ - inline int dimensionOfKernel() const - { - return m_lu.cols() - m_rank; - } - - /** \returns true if the matrix of which *this is the LU decomposition represents an injective - * linear map, i.e. has trivial kernel; false otherwise. - * - * \note Since the rank is computed at the time of the construction of the LU decomposition, this - * method almost does not perform any further computation. - */ - inline bool isInjective() const - { - return m_rank == m_lu.cols(); - } - - /** \returns true if the matrix of which *this is the LU decomposition represents a surjective - * linear map; false otherwise. - * - * \note Since the rank is computed at the time of the construction of the LU decomposition, this - * method almost does not perform any further computation. - */ - inline bool isSurjective() const - { - return m_rank == m_lu.rows(); - } - - /** \returns true if the matrix of which *this is the LU decomposition is invertible. - * - * \note Since the rank is computed at the time of the construction of the LU decomposition, this - * method almost does not perform any further computation. - */ - inline bool isInvertible() const - { - return isInjective() && isSurjective(); - } - - /** Computes the inverse of the matrix of which *this is the LU decomposition. - * - * \param result a pointer to the matrix into which to store the inverse. Resized if needed. - * - * \note If this matrix is not invertible, *result is left with undefined coefficients. - * Use isInvertible() to first determine whether this matrix is invertible. - * - * \sa MatrixBase::computeInverse(), inverse() - */ - inline void computeInverse(MatrixType *result) const - { - solve(MatrixType::Identity(m_lu.rows(), m_lu.cols()), result); - } - - /** \returns the inverse of the matrix of which *this is the LU decomposition. - * - * \note If this matrix is not invertible, the returned matrix has undefined coefficients. - * Use isInvertible() to first determine whether this matrix is invertible. - * - * \sa computeInverse(), MatrixBase::inverse() - */ - inline MatrixType inverse() const - { - MatrixType result; - computeInverse(&result); - return result; - } - - protected: - const MatrixType& m_originalMatrix; - MatrixType m_lu; - IntColVectorType m_p; - IntRowVectorType m_q; - int m_det_pq; - int m_rank; - RealScalar m_precision; -}; - -template<typename MatrixType> -LU<MatrixType>::LU(const MatrixType& matrix) - : m_originalMatrix(matrix), - m_lu(matrix), - m_p(matrix.rows()), - m_q(matrix.cols()) -{ - const int size = matrix.diagonal().size(); - const int rows = matrix.rows(); - const int cols = matrix.cols(); - - // this formula comes from experimenting (see "LU precision tuning" thread on the list) - // and turns out to be identical to Higham's formula used already in LDLt. - m_precision = machine_epsilon<Scalar>() * size; - - IntColVectorType rows_transpositions(matrix.rows()); - IntRowVectorType cols_transpositions(matrix.cols()); - int number_of_transpositions = 0; - - RealScalar biggest = RealScalar(0); - m_rank = size; - for(int k = 0; k < size; ++k) - { - int row_of_biggest_in_corner, col_of_biggest_in_corner; - RealScalar biggest_in_corner; - - biggest_in_corner = m_lu.corner(Eigen::BottomRight, rows-k, cols-k) - .cwise().abs() - .maxCoeff(&row_of_biggest_in_corner, &col_of_biggest_in_corner); - row_of_biggest_in_corner += k; - col_of_biggest_in_corner += k; - if(k==0) biggest = biggest_in_corner; - - // if the corner is negligible, then we have less than full rank, and we can finish early - if(ei_isMuchSmallerThan(biggest_in_corner, biggest, m_precision)) - { - m_rank = k; - for(int i = k; i < size; i++) - { - rows_transpositions.coeffRef(i) = i; - cols_transpositions.coeffRef(i) = i; - } - break; - } - - rows_transpositions.coeffRef(k) = row_of_biggest_in_corner; - cols_transpositions.coeffRef(k) = col_of_biggest_in_corner; - if(k != row_of_biggest_in_corner) { - m_lu.row(k).swap(m_lu.row(row_of_biggest_in_corner)); - ++number_of_transpositions; - } - if(k != col_of_biggest_in_corner) { - m_lu.col(k).swap(m_lu.col(col_of_biggest_in_corner)); - ++number_of_transpositions; - } - if(k<rows-1) - m_lu.col(k).end(rows-k-1) /= m_lu.coeff(k,k); - if(k<size-1) - for(int col = k + 1; col < cols; ++col) - m_lu.col(col).end(rows-k-1) -= m_lu.col(k).end(rows-k-1) * m_lu.coeff(k,col); - } - - for(int k = 0; k < matrix.rows(); ++k) m_p.coeffRef(k) = k; - for(int k = size-1; k >= 0; --k) - std::swap(m_p.coeffRef(k), m_p.coeffRef(rows_transpositions.coeff(k))); - - for(int k = 0; k < matrix.cols(); ++k) m_q.coeffRef(k) = k; - for(int k = 0; k < size; ++k) - std::swap(m_q.coeffRef(k), m_q.coeffRef(cols_transpositions.coeff(k))); - - m_det_pq = (number_of_transpositions%2) ? -1 : 1; -} - -template<typename MatrixType> -typename ei_traits<MatrixType>::Scalar LU<MatrixType>::determinant() const -{ - return Scalar(m_det_pq) * m_lu.diagonal().redux(ei_scalar_product_op<Scalar>()); -} - -template<typename MatrixType> -template<typename KernelMatrixType> -void LU<MatrixType>::computeKernel(KernelMatrixType *result) const -{ - ei_assert(!isInvertible()); - const int dimker = dimensionOfKernel(), cols = m_lu.cols(); - result->resize(cols, dimker); - - /* Let us use the following lemma: - * - * Lemma: If the matrix A has the LU decomposition PAQ = LU, - * then Ker A = Q(Ker U). - * - * Proof: trivial: just keep in mind that P, Q, L are invertible. - */ - - /* Thus, all we need to do is to compute Ker U, and then apply Q. - * - * U is upper triangular, with eigenvalues sorted so that any zeros appear at the end. - * Thus, the diagonal of U ends with exactly - * m_dimKer zero's. Let us use that to construct m_dimKer linearly - * independent vectors in Ker U. - */ - - Matrix<Scalar, Dynamic, Dynamic, MatrixType::Options, - MatrixType::MaxColsAtCompileTime, MatrixType::MaxColsAtCompileTime> - y(-m_lu.corner(TopRight, m_rank, dimker)); - - m_lu.corner(TopLeft, m_rank, m_rank) - .template marked<UpperTriangular>() - .solveTriangularInPlace(y); - - for(int i = 0; i < m_rank; ++i) result->row(m_q.coeff(i)) = y.row(i); - for(int i = m_rank; i < cols; ++i) result->row(m_q.coeff(i)).setZero(); - for(int k = 0; k < dimker; ++k) result->coeffRef(m_q.coeff(m_rank+k), k) = Scalar(1); -} - -template<typename MatrixType> -const typename LU<MatrixType>::KernelResultType -LU<MatrixType>::kernel() const -{ - KernelResultType result(m_lu.cols(), dimensionOfKernel()); - computeKernel(&result); - return result; -} - -template<typename MatrixType> -template<typename ImageMatrixType> -void LU<MatrixType>::computeImage(ImageMatrixType *result) const -{ - ei_assert(m_rank > 0); - result->resize(m_originalMatrix.rows(), m_rank); - for(int i = 0; i < m_rank; ++i) - result->col(i) = m_originalMatrix.col(m_q.coeff(i)); -} - -template<typename MatrixType> -const typename LU<MatrixType>::ImageResultType -LU<MatrixType>::image() const -{ - ImageResultType result(m_originalMatrix.rows(), m_rank); - computeImage(&result); - return result; -} - -template<typename MatrixType> -template<typename OtherDerived, typename ResultType> -bool LU<MatrixType>::solve( - const MatrixBase<OtherDerived>& b, - ResultType *result -) const -{ - /* The decomposition PAQ = LU can be rewritten as A = P^{-1} L U Q^{-1}. - * So we proceed as follows: - * Step 1: compute c = Pb. - * Step 2: replace c by the solution x to Lx = c. Exists because L is invertible. - * Step 3: replace c by the solution x to Ux = c. Check if a solution really exists. - * Step 4: result = Qc; - */ - - const int rows = m_lu.rows(), cols = m_lu.cols(); - ei_assert(b.rows() == rows); - const int smalldim = std::min(rows, cols); - - typename OtherDerived::PlainMatrixType c(b.rows(), b.cols()); - - // Step 1 - for(int i = 0; i < rows; ++i) c.row(m_p.coeff(i)) = b.row(i); - - // Step 2 - m_lu.corner(Eigen::TopLeft,smalldim,smalldim).template marked<UnitLowerTriangular>() - .solveTriangularInPlace( - c.corner(Eigen::TopLeft, smalldim, c.cols())); - if(rows>cols) - { - c.corner(Eigen::BottomLeft, rows-cols, c.cols()) - -= m_lu.corner(Eigen::BottomLeft, rows-cols, cols) * c.corner(Eigen::TopLeft, cols, c.cols()); - } - - // Step 3 - if(!isSurjective()) - { - // is c is in the image of U ? - RealScalar biggest_in_c = m_rank>0 ? c.corner(TopLeft, m_rank, c.cols()).cwise().abs().maxCoeff() : 0; - for(int col = 0; col < c.cols(); ++col) - for(int row = m_rank; row < c.rows(); ++row) - if(!ei_isMuchSmallerThan(c.coeff(row,col), biggest_in_c, m_precision)) - return false; - } - m_lu.corner(TopLeft, m_rank, m_rank) - .template marked<UpperTriangular>() - .solveTriangularInPlace(c.corner(TopLeft, m_rank, c.cols())); - - // Step 4 - result->resize(m_lu.cols(), b.cols()); - for(int i = 0; i < m_rank; ++i) result->row(m_q.coeff(i)) = c.row(i); - for(int i = m_rank; i < m_lu.cols(); ++i) result->row(m_q.coeff(i)).setZero(); - return true; -} - -/** \lu_module - * - * \return the LU decomposition of \c *this. - * - * \sa class LU - */ -template<typename Derived> -inline const LU<typename MatrixBase<Derived>::PlainMatrixType> -MatrixBase<Derived>::lu() const -{ - return LU<PlainMatrixType>(eval()); -} - -#endif // EIGEN_LU_H |