mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
PR 567: makes all dense solvers inherit SoverBase (LU,Cholesky,QR,SVD).
This changeset also includes: * add HouseholderSequence::conjugateIf * define int as the StorageIndex type for all dense solvers * dedicated unit tests, including assertion checking * _check_solve_assertion(): this method can be implemented in derived solver classes to implement custom checks * CompleteOrthogonalDecompositions: add applyZOnTheLeftInPlace, fix scalar type in applyZAdjointOnTheLeftInPlace(), add missing assertions * Cholesky: add missing assertions * FullPivHouseholderQR: Corrected Scalar type in _solve_impl() * BDCSVD: Unambiguous return type for ternary operator * SVDBase: Corrected Scalar type in _solve_impl()
This commit is contained in:
@@ -46,6 +46,8 @@ void bdcsvd_method()
|
||||
VERIFY_RAISES_ASSERT(m.bdcSvd().matrixU());
|
||||
VERIFY_RAISES_ASSERT(m.bdcSvd().matrixV());
|
||||
VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).solve(m), m);
|
||||
VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m);
|
||||
VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m);
|
||||
}
|
||||
|
||||
// compare the Singular values returned with Jacobi and Bdc
|
||||
|
||||
@@ -7,15 +7,12 @@
|
||||
// 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_NO_ASSERTION_CHECKING
|
||||
#define EIGEN_NO_ASSERTION_CHECKING
|
||||
#endif
|
||||
|
||||
#define TEST_ENABLE_TEMPORARY_TRACKING
|
||||
|
||||
#include "main.h"
|
||||
#include <Eigen/Cholesky>
|
||||
#include <Eigen/QR>
|
||||
#include "solverbase.h"
|
||||
|
||||
template<typename MatrixType, int UpLo>
|
||||
typename MatrixType::RealScalar matrix_l1_norm(const MatrixType& m) {
|
||||
@@ -81,15 +78,17 @@ template<typename MatrixType> void cholesky(const MatrixType& m)
|
||||
}
|
||||
|
||||
{
|
||||
STATIC_CHECK(( internal::is_same<typename LLT<MatrixType,Lower>::StorageIndex,int>::value ));
|
||||
STATIC_CHECK(( internal::is_same<typename LLT<MatrixType,Upper>::StorageIndex,int>::value ));
|
||||
|
||||
SquareMatrixType symmUp = symm.template triangularView<Upper>();
|
||||
SquareMatrixType symmLo = symm.template triangularView<Lower>();
|
||||
|
||||
LLT<SquareMatrixType,Lower> chollo(symmLo);
|
||||
VERIFY_IS_APPROX(symm, chollo.reconstructedMatrix());
|
||||
vecX = chollo.solve(vecB);
|
||||
VERIFY_IS_APPROX(symm * vecX, vecB);
|
||||
matX = chollo.solve(matB);
|
||||
VERIFY_IS_APPROX(symm * matX, matB);
|
||||
|
||||
check_solverbase<VectorType, VectorType>(symm, chollo, rows, rows, 1);
|
||||
check_solverbase<MatrixType, MatrixType>(symm, chollo, rows, cols, rows);
|
||||
|
||||
const MatrixType symmLo_inverse = chollo.solve(MatrixType::Identity(rows,cols));
|
||||
RealScalar rcond = (RealScalar(1) / matrix_l1_norm<MatrixType, Lower>(symmLo)) /
|
||||
@@ -143,6 +142,9 @@ template<typename MatrixType> void cholesky(const MatrixType& m)
|
||||
|
||||
// LDLT
|
||||
{
|
||||
STATIC_CHECK(( internal::is_same<typename LDLT<MatrixType,Lower>::StorageIndex,int>::value ));
|
||||
STATIC_CHECK(( internal::is_same<typename LDLT<MatrixType,Upper>::StorageIndex,int>::value ));
|
||||
|
||||
int sign = internal::random<int>()%2 ? 1 : -1;
|
||||
|
||||
if(sign == -1)
|
||||
@@ -156,10 +158,9 @@ template<typename MatrixType> void cholesky(const MatrixType& m)
|
||||
LDLT<SquareMatrixType,Lower> ldltlo(symmLo);
|
||||
VERIFY(ldltlo.info()==Success);
|
||||
VERIFY_IS_APPROX(symm, ldltlo.reconstructedMatrix());
|
||||
vecX = ldltlo.solve(vecB);
|
||||
VERIFY_IS_APPROX(symm * vecX, vecB);
|
||||
matX = ldltlo.solve(matB);
|
||||
VERIFY_IS_APPROX(symm * matX, matB);
|
||||
|
||||
check_solverbase<VectorType, VectorType>(symm, ldltlo, rows, rows, 1);
|
||||
check_solverbase<MatrixType, MatrixType>(symm, ldltlo, rows, cols, rows);
|
||||
|
||||
const MatrixType symmLo_inverse = ldltlo.solve(MatrixType::Identity(rows,cols));
|
||||
RealScalar rcond = (RealScalar(1) / matrix_l1_norm<MatrixType, Lower>(symmLo)) /
|
||||
@@ -313,10 +314,9 @@ template<typename MatrixType> void cholesky_cplx(const MatrixType& m)
|
||||
|
||||
LLT<RealMatrixType,Lower> chollo(symmLo);
|
||||
VERIFY_IS_APPROX(symm, chollo.reconstructedMatrix());
|
||||
vecX = chollo.solve(vecB);
|
||||
VERIFY_IS_APPROX(symm * vecX, vecB);
|
||||
// matX = chollo.solve(matB);
|
||||
// VERIFY_IS_APPROX(symm * matX, matB);
|
||||
|
||||
check_solverbase<VectorType, VectorType>(symm, chollo, rows, rows, 1);
|
||||
//check_solverbase<MatrixType, MatrixType>(symm, chollo, rows, cols, rows);
|
||||
}
|
||||
|
||||
// LDLT
|
||||
@@ -333,10 +333,9 @@ template<typename MatrixType> void cholesky_cplx(const MatrixType& m)
|
||||
LDLT<RealMatrixType,Lower> ldltlo(symmLo);
|
||||
VERIFY(ldltlo.info()==Success);
|
||||
VERIFY_IS_APPROX(symm, ldltlo.reconstructedMatrix());
|
||||
vecX = ldltlo.solve(vecB);
|
||||
VERIFY_IS_APPROX(symm * vecX, vecB);
|
||||
// matX = ldltlo.solve(matB);
|
||||
// VERIFY_IS_APPROX(symm * matX, matB);
|
||||
|
||||
check_solverbase<VectorType, VectorType>(symm, ldltlo, rows, rows, 1);
|
||||
//check_solverbase<MatrixType, MatrixType>(symm, ldltlo, rows, cols, rows);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -477,16 +476,20 @@ template<typename MatrixType> void cholesky_verify_assert()
|
||||
VERIFY_RAISES_ASSERT(llt.matrixL())
|
||||
VERIFY_RAISES_ASSERT(llt.matrixU())
|
||||
VERIFY_RAISES_ASSERT(llt.solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(llt.solveInPlace(&tmp))
|
||||
VERIFY_RAISES_ASSERT(llt.transpose().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(llt.adjoint().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(llt.solveInPlace(tmp))
|
||||
|
||||
LDLT<MatrixType> ldlt;
|
||||
VERIFY_RAISES_ASSERT(ldlt.matrixL())
|
||||
VERIFY_RAISES_ASSERT(ldlt.permutationP())
|
||||
VERIFY_RAISES_ASSERT(ldlt.transpositionsP())
|
||||
VERIFY_RAISES_ASSERT(ldlt.vectorD())
|
||||
VERIFY_RAISES_ASSERT(ldlt.isPositive())
|
||||
VERIFY_RAISES_ASSERT(ldlt.isNegative())
|
||||
VERIFY_RAISES_ASSERT(ldlt.solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(ldlt.solveInPlace(&tmp))
|
||||
VERIFY_RAISES_ASSERT(ldlt.transpose().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(ldlt.adjoint().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(ldlt.solveInPlace(tmp))
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(cholesky)
|
||||
|
||||
@@ -67,6 +67,8 @@ void jacobisvd_method()
|
||||
VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixU());
|
||||
VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixV());
|
||||
VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).solve(m), m);
|
||||
VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m);
|
||||
VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m);
|
||||
}
|
||||
|
||||
namespace Foo {
|
||||
|
||||
66
test/lu.cpp
66
test/lu.cpp
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "main.h"
|
||||
#include <Eigen/LU>
|
||||
#include "solverbase.h"
|
||||
using namespace std;
|
||||
|
||||
template<typename MatrixType>
|
||||
@@ -96,32 +97,14 @@ template<typename MatrixType> void lu_non_invertible()
|
||||
VERIFY(m1image.fullPivLu().rank() == rank);
|
||||
VERIFY_IS_APPROX(m1 * m1.adjoint() * m1image, m1image);
|
||||
|
||||
check_solverbase<CMatrixType, MatrixType>(m1, lu, rows, cols, cols2);
|
||||
|
||||
m2 = CMatrixType::Random(cols,cols2);
|
||||
m3 = m1*m2;
|
||||
m2 = CMatrixType::Random(cols,cols2);
|
||||
// test that the code, which does resize(), may be applied to an xpr
|
||||
m2.block(0,0,m2.rows(),m2.cols()) = lu.solve(m3);
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
|
||||
// test solve with transposed
|
||||
m3 = MatrixType::Random(rows,cols2);
|
||||
m2 = m1.transpose()*m3;
|
||||
m3 = MatrixType::Random(rows,cols2);
|
||||
lu.template _solve_impl_transposed<false>(m2, m3);
|
||||
VERIFY_IS_APPROX(m2, m1.transpose()*m3);
|
||||
m3 = MatrixType::Random(rows,cols2);
|
||||
m3 = lu.transpose().solve(m2);
|
||||
VERIFY_IS_APPROX(m2, m1.transpose()*m3);
|
||||
|
||||
// test solve with conjugate transposed
|
||||
m3 = MatrixType::Random(rows,cols2);
|
||||
m2 = m1.adjoint()*m3;
|
||||
m3 = MatrixType::Random(rows,cols2);
|
||||
lu.template _solve_impl_transposed<true>(m2, m3);
|
||||
VERIFY_IS_APPROX(m2, m1.adjoint()*m3);
|
||||
m3 = MatrixType::Random(rows,cols2);
|
||||
m3 = lu.adjoint().solve(m2);
|
||||
VERIFY_IS_APPROX(m2, m1.adjoint()*m3);
|
||||
}
|
||||
|
||||
template<typename MatrixType> void lu_invertible()
|
||||
@@ -150,10 +133,12 @@ template<typename MatrixType> void lu_invertible()
|
||||
VERIFY(lu.isSurjective());
|
||||
VERIFY(lu.isInvertible());
|
||||
VERIFY(lu.image(m1).fullPivLu().isInvertible());
|
||||
|
||||
check_solverbase<MatrixType, MatrixType>(m1, lu, size, size, size);
|
||||
|
||||
MatrixType m1_inverse = lu.inverse();
|
||||
m3 = MatrixType::Random(size,size);
|
||||
m2 = lu.solve(m3);
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
MatrixType m1_inverse = lu.inverse();
|
||||
VERIFY_IS_APPROX(m2, m1_inverse*m3);
|
||||
|
||||
RealScalar rcond = (RealScalar(1) / matrix_l1_norm(m1)) / matrix_l1_norm(m1_inverse);
|
||||
@@ -162,20 +147,6 @@ template<typename MatrixType> void lu_invertible()
|
||||
// truth.
|
||||
VERIFY(rcond_est > rcond / 10 && rcond_est < rcond * 10);
|
||||
|
||||
// test solve with transposed
|
||||
lu.template _solve_impl_transposed<false>(m3, m2);
|
||||
VERIFY_IS_APPROX(m3, m1.transpose()*m2);
|
||||
m3 = MatrixType::Random(size,size);
|
||||
m3 = lu.transpose().solve(m2);
|
||||
VERIFY_IS_APPROX(m2, m1.transpose()*m3);
|
||||
|
||||
// test solve with conjugate transposed
|
||||
lu.template _solve_impl_transposed<true>(m3, m2);
|
||||
VERIFY_IS_APPROX(m3, m1.adjoint()*m2);
|
||||
m3 = MatrixType::Random(size,size);
|
||||
m3 = lu.adjoint().solve(m2);
|
||||
VERIFY_IS_APPROX(m2, m1.adjoint()*m3);
|
||||
|
||||
// Regression test for Bug 302
|
||||
MatrixType m4 = MatrixType::Random(size,size);
|
||||
VERIFY_IS_APPROX(lu.solve(m3*m4), lu.solve(m3)*m4);
|
||||
@@ -197,30 +168,17 @@ template<typename MatrixType> void lu_partial_piv()
|
||||
|
||||
VERIFY_IS_APPROX(m1, plu.reconstructedMatrix());
|
||||
|
||||
check_solverbase<MatrixType, MatrixType>(m1, plu, size, size, size);
|
||||
|
||||
MatrixType m1_inverse = plu.inverse();
|
||||
m3 = MatrixType::Random(size,size);
|
||||
m2 = plu.solve(m3);
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
MatrixType m1_inverse = plu.inverse();
|
||||
VERIFY_IS_APPROX(m2, m1_inverse*m3);
|
||||
|
||||
RealScalar rcond = (RealScalar(1) / matrix_l1_norm(m1)) / matrix_l1_norm(m1_inverse);
|
||||
const RealScalar rcond_est = plu.rcond();
|
||||
// Verify that the estimate is within a factor of 10 of the truth.
|
||||
VERIFY(rcond_est > rcond / 10 && rcond_est < rcond * 10);
|
||||
|
||||
// test solve with transposed
|
||||
plu.template _solve_impl_transposed<false>(m3, m2);
|
||||
VERIFY_IS_APPROX(m3, m1.transpose()*m2);
|
||||
m3 = MatrixType::Random(size,size);
|
||||
m3 = plu.transpose().solve(m2);
|
||||
VERIFY_IS_APPROX(m2, m1.transpose()*m3);
|
||||
|
||||
// test solve with conjugate transposed
|
||||
plu.template _solve_impl_transposed<true>(m3, m2);
|
||||
VERIFY_IS_APPROX(m3, m1.adjoint()*m2);
|
||||
m3 = MatrixType::Random(size,size);
|
||||
m3 = plu.adjoint().solve(m2);
|
||||
VERIFY_IS_APPROX(m2, m1.adjoint()*m3);
|
||||
}
|
||||
|
||||
template<typename MatrixType> void lu_verify_assert()
|
||||
@@ -234,6 +192,8 @@ template<typename MatrixType> void lu_verify_assert()
|
||||
VERIFY_RAISES_ASSERT(lu.kernel())
|
||||
VERIFY_RAISES_ASSERT(lu.image(tmp))
|
||||
VERIFY_RAISES_ASSERT(lu.solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(lu.transpose().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(lu.adjoint().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(lu.determinant())
|
||||
VERIFY_RAISES_ASSERT(lu.rank())
|
||||
VERIFY_RAISES_ASSERT(lu.dimensionOfKernel())
|
||||
@@ -246,6 +206,8 @@ template<typename MatrixType> void lu_verify_assert()
|
||||
VERIFY_RAISES_ASSERT(plu.matrixLU())
|
||||
VERIFY_RAISES_ASSERT(plu.permutationP())
|
||||
VERIFY_RAISES_ASSERT(plu.solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(plu.transpose().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(plu.adjoint().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(plu.determinant())
|
||||
VERIFY_RAISES_ASSERT(plu.inverse())
|
||||
}
|
||||
|
||||
16
test/qr.cpp
16
test/qr.cpp
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "main.h"
|
||||
#include <Eigen/QR>
|
||||
#include "solverbase.h"
|
||||
|
||||
template<typename MatrixType> void qr(const MatrixType& m)
|
||||
{
|
||||
@@ -41,11 +42,7 @@ template<typename MatrixType, int Cols2> void qr_fixedsize()
|
||||
|
||||
VERIFY_IS_APPROX(m1, qr.householderQ() * r);
|
||||
|
||||
Matrix<Scalar,Cols,Cols2> m2 = Matrix<Scalar,Cols,Cols2>::Random(Cols,Cols2);
|
||||
Matrix<Scalar,Rows,Cols2> m3 = m1*m2;
|
||||
m2 = Matrix<Scalar,Cols,Cols2>::Random(Cols,Cols2);
|
||||
m2 = qr.solve(m3);
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
check_solverbase<Matrix<Scalar,Cols,Cols2>, Matrix<Scalar,Rows,Cols2> >(m1, qr, Rows, Cols, Cols2);
|
||||
}
|
||||
|
||||
template<typename MatrixType> void qr_invertible()
|
||||
@@ -57,6 +54,8 @@ template<typename MatrixType> void qr_invertible()
|
||||
typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
|
||||
STATIC_CHECK(( internal::is_same<typename HouseholderQR<MatrixType>::StorageIndex,int>::value ));
|
||||
|
||||
int size = internal::random<int>(10,50);
|
||||
|
||||
MatrixType m1(size, size), m2(size, size), m3(size, size);
|
||||
@@ -70,9 +69,8 @@ template<typename MatrixType> void qr_invertible()
|
||||
}
|
||||
|
||||
HouseholderQR<MatrixType> qr(m1);
|
||||
m3 = MatrixType::Random(size,size);
|
||||
m2 = qr.solve(m3);
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
|
||||
check_solverbase<MatrixType, MatrixType>(m1, qr, size, size, size);
|
||||
|
||||
// now construct a matrix with prescribed determinant
|
||||
m1.setZero();
|
||||
@@ -95,6 +93,8 @@ template<typename MatrixType> void qr_verify_assert()
|
||||
HouseholderQR<MatrixType> qr;
|
||||
VERIFY_RAISES_ASSERT(qr.matrixQR())
|
||||
VERIFY_RAISES_ASSERT(qr.solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(qr.transpose().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(qr.adjoint().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(qr.householderQ())
|
||||
VERIFY_RAISES_ASSERT(qr.absDeterminant())
|
||||
VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
|
||||
|
||||
@@ -11,9 +11,12 @@
|
||||
#include "main.h"
|
||||
#include <Eigen/QR>
|
||||
#include <Eigen/SVD>
|
||||
#include "solverbase.h"
|
||||
|
||||
template <typename MatrixType>
|
||||
void cod() {
|
||||
STATIC_CHECK(( internal::is_same<typename CompleteOrthogonalDecomposition<MatrixType>::StorageIndex,int>::value ));
|
||||
|
||||
Index rows = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
|
||||
Index cols = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
|
||||
Index cols2 = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
|
||||
@@ -46,12 +49,12 @@ void cod() {
|
||||
MatrixType c = q * t * z * cod.colsPermutation().inverse();
|
||||
VERIFY_IS_APPROX(matrix, c);
|
||||
|
||||
check_solverbase<MatrixType, MatrixType>(matrix, cod, rows, cols, cols2);
|
||||
|
||||
// Verify that we get the same minimum-norm solution as the SVD.
|
||||
MatrixType exact_solution = MatrixType::Random(cols, cols2);
|
||||
MatrixType rhs = matrix * exact_solution;
|
||||
MatrixType cod_solution = cod.solve(rhs);
|
||||
VERIFY_IS_APPROX(rhs, matrix * cod_solution);
|
||||
|
||||
// Verify that we get the same minimum-norm solution as the SVD.
|
||||
JacobiSVD<MatrixType> svd(matrix, ComputeThinU | ComputeThinV);
|
||||
MatrixType svd_solution = svd.solve(rhs);
|
||||
VERIFY_IS_APPROX(cod_solution, svd_solution);
|
||||
@@ -77,13 +80,13 @@ void cod_fixedsize() {
|
||||
VERIFY(cod.isSurjective() == (rank == Cols));
|
||||
VERIFY(cod.isInvertible() == (cod.isInjective() && cod.isSurjective()));
|
||||
|
||||
check_solverbase<Matrix<Scalar, Cols, Cols2>, Matrix<Scalar, Rows, Cols2> >(matrix, cod, Rows, Cols, Cols2);
|
||||
|
||||
// Verify that we get the same minimum-norm solution as the SVD.
|
||||
Matrix<Scalar, Cols, Cols2> exact_solution;
|
||||
exact_solution.setRandom(Cols, Cols2);
|
||||
Matrix<Scalar, Rows, Cols2> rhs = matrix * exact_solution;
|
||||
Matrix<Scalar, Cols, Cols2> cod_solution = cod.solve(rhs);
|
||||
VERIFY_IS_APPROX(rhs, matrix * cod_solution);
|
||||
|
||||
// Verify that we get the same minimum-norm solution as the SVD.
|
||||
JacobiSVD<MatrixType> svd(matrix, ComputeFullU | ComputeFullV);
|
||||
Matrix<Scalar, Cols, Cols2> svd_solution = svd.solve(rhs);
|
||||
VERIFY_IS_APPROX(cod_solution, svd_solution);
|
||||
@@ -93,6 +96,8 @@ template<typename MatrixType> void qr()
|
||||
{
|
||||
using std::sqrt;
|
||||
|
||||
STATIC_CHECK(( internal::is_same<typename ColPivHouseholderQR<MatrixType>::StorageIndex,int>::value ));
|
||||
|
||||
Index rows = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols2 = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE);
|
||||
Index rank = internal::random<Index>(1, (std::min)(rows, cols)-1);
|
||||
|
||||
@@ -133,13 +138,10 @@ template<typename MatrixType> void qr()
|
||||
VERIFY_IS_APPROX_OR_LESS_THAN(y, x);
|
||||
}
|
||||
|
||||
MatrixType m2 = MatrixType::Random(cols,cols2);
|
||||
MatrixType m3 = m1*m2;
|
||||
m2 = MatrixType::Random(cols,cols2);
|
||||
m2 = qr.solve(m3);
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
check_solverbase<MatrixType, MatrixType>(m1, qr, rows, cols, cols2);
|
||||
|
||||
{
|
||||
MatrixType m2, m3;
|
||||
Index size = rows;
|
||||
do {
|
||||
m1 = MatrixType::Random(size,size);
|
||||
@@ -173,11 +175,8 @@ template<typename MatrixType, int Cols2> void qr_fixedsize()
|
||||
Matrix<Scalar,Rows,Cols> c = qr.householderQ() * r * qr.colsPermutation().inverse();
|
||||
VERIFY_IS_APPROX(m1, c);
|
||||
|
||||
Matrix<Scalar,Cols,Cols2> m2 = Matrix<Scalar,Cols,Cols2>::Random(Cols,Cols2);
|
||||
Matrix<Scalar,Rows,Cols2> m3 = m1*m2;
|
||||
m2 = Matrix<Scalar,Cols,Cols2>::Random(Cols,Cols2);
|
||||
m2 = qr.solve(m3);
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
check_solverbase<Matrix<Scalar,Cols,Cols2>, Matrix<Scalar,Rows,Cols2> >(m1, qr, Rows, Cols, Cols2);
|
||||
|
||||
// Verify that the absolute value of the diagonal elements in R are
|
||||
// non-increasing until they reache the singularity threshold.
|
||||
RealScalar threshold =
|
||||
@@ -264,9 +263,8 @@ template<typename MatrixType> void qr_invertible()
|
||||
}
|
||||
|
||||
ColPivHouseholderQR<MatrixType> qr(m1);
|
||||
m3 = MatrixType::Random(size,size);
|
||||
m2 = qr.solve(m3);
|
||||
//VERIFY_IS_APPROX(m3, m1*m2);
|
||||
|
||||
check_solverbase<MatrixType, MatrixType>(m1, qr, size, size, size);
|
||||
|
||||
// now construct a matrix with prescribed determinant
|
||||
m1.setZero();
|
||||
@@ -286,6 +284,8 @@ template<typename MatrixType> void qr_verify_assert()
|
||||
ColPivHouseholderQR<MatrixType> qr;
|
||||
VERIFY_RAISES_ASSERT(qr.matrixQR())
|
||||
VERIFY_RAISES_ASSERT(qr.solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(qr.transpose().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(qr.adjoint().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(qr.householderQ())
|
||||
VERIFY_RAISES_ASSERT(qr.dimensionOfKernel())
|
||||
VERIFY_RAISES_ASSERT(qr.isInjective())
|
||||
@@ -296,6 +296,25 @@ template<typename MatrixType> void qr_verify_assert()
|
||||
VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
|
||||
}
|
||||
|
||||
template<typename MatrixType> void cod_verify_assert()
|
||||
{
|
||||
MatrixType tmp;
|
||||
|
||||
CompleteOrthogonalDecomposition<MatrixType> cod;
|
||||
VERIFY_RAISES_ASSERT(cod.matrixQTZ())
|
||||
VERIFY_RAISES_ASSERT(cod.solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(cod.transpose().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(cod.adjoint().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(cod.householderQ())
|
||||
VERIFY_RAISES_ASSERT(cod.dimensionOfKernel())
|
||||
VERIFY_RAISES_ASSERT(cod.isInjective())
|
||||
VERIFY_RAISES_ASSERT(cod.isSurjective())
|
||||
VERIFY_RAISES_ASSERT(cod.isInvertible())
|
||||
VERIFY_RAISES_ASSERT(cod.pseudoInverse())
|
||||
VERIFY_RAISES_ASSERT(cod.absDeterminant())
|
||||
VERIFY_RAISES_ASSERT(cod.logAbsDeterminant())
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(qr_colpivoting)
|
||||
{
|
||||
for(int i = 0; i < g_repeat; i++) {
|
||||
@@ -330,6 +349,13 @@ EIGEN_DECLARE_TEST(qr_colpivoting)
|
||||
CALL_SUBTEST_6(qr_verify_assert<MatrixXcf>());
|
||||
CALL_SUBTEST_3(qr_verify_assert<MatrixXcd>());
|
||||
|
||||
CALL_SUBTEST_7(cod_verify_assert<Matrix3f>());
|
||||
CALL_SUBTEST_8(cod_verify_assert<Matrix3d>());
|
||||
CALL_SUBTEST_1(cod_verify_assert<MatrixXf>());
|
||||
CALL_SUBTEST_2(cod_verify_assert<MatrixXd>());
|
||||
CALL_SUBTEST_6(cod_verify_assert<MatrixXcf>());
|
||||
CALL_SUBTEST_3(cod_verify_assert<MatrixXcd>());
|
||||
|
||||
// Test problem size constructors
|
||||
CALL_SUBTEST_9(ColPivHouseholderQR<MatrixXf>(10, 20));
|
||||
|
||||
|
||||
@@ -10,9 +10,12 @@
|
||||
|
||||
#include "main.h"
|
||||
#include <Eigen/QR>
|
||||
#include "solverbase.h"
|
||||
|
||||
template<typename MatrixType> void qr()
|
||||
{
|
||||
STATIC_CHECK(( internal::is_same<typename FullPivHouseholderQR<MatrixType>::StorageIndex,int>::value ));
|
||||
|
||||
static const int Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime;
|
||||
Index max_size = EIGEN_TEST_MAX_SIZE;
|
||||
Index min_size = numext::maxi(1,EIGEN_TEST_MAX_SIZE/10);
|
||||
@@ -48,13 +51,10 @@ template<typename MatrixType> void qr()
|
||||
MatrixType tmp;
|
||||
VERIFY_IS_APPROX(tmp.noalias() = qr.matrixQ() * r, (qr.matrixQ() * r).eval());
|
||||
|
||||
MatrixType m2 = MatrixType::Random(cols,cols2);
|
||||
MatrixType m3 = m1*m2;
|
||||
m2 = MatrixType::Random(cols,cols2);
|
||||
m2 = qr.solve(m3);
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
check_solverbase<MatrixType, MatrixType>(m1, qr, rows, cols, cols2);
|
||||
|
||||
{
|
||||
MatrixType m2, m3;
|
||||
Index size = rows;
|
||||
do {
|
||||
m1 = MatrixType::Random(size,size);
|
||||
@@ -93,9 +93,7 @@ template<typename MatrixType> void qr_invertible()
|
||||
VERIFY(qr.isInvertible());
|
||||
VERIFY(qr.isSurjective());
|
||||
|
||||
m3 = MatrixType::Random(size,size);
|
||||
m2 = qr.solve(m3);
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
check_solverbase<MatrixType, MatrixType>(m1, qr, size, size, size);
|
||||
|
||||
// now construct a matrix with prescribed determinant
|
||||
m1.setZero();
|
||||
@@ -115,6 +113,8 @@ template<typename MatrixType> void qr_verify_assert()
|
||||
FullPivHouseholderQR<MatrixType> qr;
|
||||
VERIFY_RAISES_ASSERT(qr.matrixQR())
|
||||
VERIFY_RAISES_ASSERT(qr.solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(qr.transpose().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(qr.adjoint().solve(tmp))
|
||||
VERIFY_RAISES_ASSERT(qr.matrixQ())
|
||||
VERIFY_RAISES_ASSERT(qr.dimensionOfKernel())
|
||||
VERIFY_RAISES_ASSERT(qr.isInjective())
|
||||
|
||||
36
test/solverbase.h
Normal file
36
test/solverbase.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef TEST_SOLVERBASE_H
|
||||
#define TEST_SOLVERBASE_H
|
||||
|
||||
template<typename DstType, typename RhsType, typename MatrixType, typename SolverType>
|
||||
void check_solverbase(const MatrixType& matrix, const SolverType& solver, Index rows, Index cols, Index cols2)
|
||||
{
|
||||
// solve
|
||||
DstType m2 = DstType::Random(cols,cols2);
|
||||
RhsType m3 = matrix*m2;
|
||||
DstType solver_solution = DstType::Random(cols,cols2);
|
||||
solver._solve_impl(m3, solver_solution);
|
||||
VERIFY_IS_APPROX(m3, matrix*solver_solution);
|
||||
solver_solution = DstType::Random(cols,cols2);
|
||||
solver_solution = solver.solve(m3);
|
||||
VERIFY_IS_APPROX(m3, matrix*solver_solution);
|
||||
// test solve with transposed
|
||||
m3 = RhsType::Random(rows,cols2);
|
||||
m2 = matrix.transpose()*m3;
|
||||
RhsType solver_solution2 = RhsType::Random(rows,cols2);
|
||||
solver.template _solve_impl_transposed<false>(m2, solver_solution2);
|
||||
VERIFY_IS_APPROX(m2, matrix.transpose()*solver_solution2);
|
||||
solver_solution2 = RhsType::Random(rows,cols2);
|
||||
solver_solution2 = solver.transpose().solve(m2);
|
||||
VERIFY_IS_APPROX(m2, matrix.transpose()*solver_solution2);
|
||||
// test solve with conjugate transposed
|
||||
m3 = RhsType::Random(rows,cols2);
|
||||
m2 = matrix.adjoint()*m3;
|
||||
solver_solution2 = RhsType::Random(rows,cols2);
|
||||
solver.template _solve_impl_transposed<true>(m2, solver_solution2);
|
||||
VERIFY_IS_APPROX(m2, matrix.adjoint()*solver_solution2);
|
||||
solver_solution2 = RhsType::Random(rows,cols2);
|
||||
solver_solution2 = solver.adjoint().solve(m2);
|
||||
VERIFY_IS_APPROX(m2, matrix.adjoint()*solver_solution2);
|
||||
}
|
||||
|
||||
#endif // TEST_SOLVERBASE_H
|
||||
@@ -17,6 +17,7 @@
|
||||
#endif
|
||||
|
||||
#include "svd_fill.h"
|
||||
#include "solverbase.h"
|
||||
|
||||
// Check that the matrix m is properly reconstructed and that the U and V factors are unitary
|
||||
// The SVD must have already been computed.
|
||||
@@ -219,12 +220,33 @@ void svd_min_norm(const MatrixType& m, unsigned int computationOptions)
|
||||
VERIFY_IS_APPROX(x21, x3);
|
||||
}
|
||||
|
||||
template<typename MatrixType, typename SolverType>
|
||||
void svd_test_solvers(const MatrixType& m, const SolverType& solver) {
|
||||
Index rows, cols, cols2;
|
||||
|
||||
rows = m.rows();
|
||||
cols = m.cols();
|
||||
|
||||
if(MatrixType::ColsAtCompileTime==Dynamic)
|
||||
{
|
||||
cols2 = internal::random<int>(2,EIGEN_TEST_MAX_SIZE);
|
||||
}
|
||||
else
|
||||
{
|
||||
cols2 = cols;
|
||||
}
|
||||
typedef Matrix<typename MatrixType::Scalar, MatrixType::ColsAtCompileTime, MatrixType::ColsAtCompileTime> CMatrixType;
|
||||
check_solverbase<CMatrixType, MatrixType>(m, solver, rows, cols, cols2);
|
||||
}
|
||||
|
||||
// Check full, compare_to_full, least_square, and min_norm for all possible compute-options
|
||||
template<typename SvdType, typename MatrixType>
|
||||
void svd_test_all_computation_options(const MatrixType& m, bool full_only)
|
||||
{
|
||||
// if (QRPreconditioner == NoQRPreconditioner && m.rows() != m.cols())
|
||||
// return;
|
||||
STATIC_CHECK(( internal::is_same<typename SvdType::StorageIndex,int>::value ));
|
||||
|
||||
SvdType fullSvd(m, ComputeFullU|ComputeFullV);
|
||||
CALL_SUBTEST(( svd_check_full(m, fullSvd) ));
|
||||
CALL_SUBTEST(( svd_least_square<SvdType>(m, ComputeFullU | ComputeFullV) ));
|
||||
@@ -234,6 +256,9 @@ void svd_test_all_computation_options(const MatrixType& m, bool full_only)
|
||||
// remark #111: statement is unreachable
|
||||
#pragma warning disable 111
|
||||
#endif
|
||||
|
||||
svd_test_solvers(m, fullSvd);
|
||||
|
||||
if(full_only)
|
||||
return;
|
||||
|
||||
@@ -448,6 +473,8 @@ void svd_verify_assert(const MatrixType& m)
|
||||
VERIFY_RAISES_ASSERT(svd.singularValues())
|
||||
VERIFY_RAISES_ASSERT(svd.matrixV())
|
||||
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
||||
VERIFY_RAISES_ASSERT(svd.transpose().solve(rhs))
|
||||
VERIFY_RAISES_ASSERT(svd.adjoint().solve(rhs))
|
||||
MatrixType a = MatrixType::Zero(rows, cols);
|
||||
a.setZero();
|
||||
svd.compute(a, 0);
|
||||
|
||||
Reference in New Issue
Block a user