mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
synch with main devel branch
This commit is contained in:
@@ -76,6 +76,7 @@ template<typename MatrixType> void adjoint(const MatrixType& m)
|
||||
{
|
||||
VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1));
|
||||
VERIFY_IS_APPROX(v1.norm(), v1.stableNorm());
|
||||
VERIFY_IS_APPROX(v1.blueNorm(), v1.stableNorm());
|
||||
}
|
||||
|
||||
// check compatibility of dot and adjoint
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
|
||||
// Copyright (C) 2008-2009 Gael Guennebaud <g.gael@free.fr>
|
||||
//
|
||||
// Eigen is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
|
||||
@@ -107,7 +107,7 @@ template<typename MatrixType> void basicStuff(const MatrixType& m)
|
||||
{
|
||||
VERIFY_IS_NOT_APPROX(m3, m1);
|
||||
}
|
||||
|
||||
|
||||
m3.real() = m1.real();
|
||||
VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
|
||||
VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
|
||||
@@ -121,16 +121,16 @@ template<typename MatrixType> void basicStuffComplex(const MatrixType& m)
|
||||
|
||||
int rows = m.rows();
|
||||
int cols = m.cols();
|
||||
|
||||
|
||||
Scalar s1 = ei_random<Scalar>(),
|
||||
s2 = ei_random<Scalar>();
|
||||
|
||||
|
||||
VERIFY(ei_real(s1)==ei_real_ref(s1));
|
||||
VERIFY(ei_imag(s1)==ei_imag_ref(s1));
|
||||
ei_real_ref(s1) = ei_real(s2);
|
||||
ei_imag_ref(s1) = ei_imag(s2);
|
||||
VERIFY(s1==s2);
|
||||
|
||||
|
||||
RealMatrixType rm1 = RealMatrixType::Random(rows,cols),
|
||||
rm2 = RealMatrixType::Random(rows,cols);
|
||||
MatrixType cm(rows,cols);
|
||||
@@ -162,7 +162,7 @@ void test_basicstuff()
|
||||
CALL_SUBTEST( basicStuff(MatrixXcd(20, 20)) );
|
||||
CALL_SUBTEST( basicStuff(Matrix<float, 100, 100>()) );
|
||||
CALL_SUBTEST( basicStuff(Matrix<long double,Dynamic,Dynamic>(10,10)) );
|
||||
|
||||
|
||||
CALL_SUBTEST( basicStuffComplex(MatrixXcf(21, 17)) );
|
||||
CALL_SUBTEST( basicStuffComplex(MatrixXcd(2, 3)) );
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ template<typename HyperplaneType> void hyperplane(const HyperplaneType& _plane)
|
||||
// transform
|
||||
if (!NumTraits<Scalar>::IsComplex)
|
||||
{
|
||||
MatrixType rot = MatrixType::Random(dim,dim).qr().matrixQ();
|
||||
MatrixType rot = MatrixType::Random(dim,dim).householderQr().matrixQ();
|
||||
DiagonalMatrix<Scalar,HyperplaneType::AmbientDimAtCompileTime> scaling(VectorType::Random());
|
||||
Translation<Scalar,HyperplaneType::AmbientDimAtCompileTime> translation(VectorType::Random());
|
||||
|
||||
|
||||
@@ -65,6 +65,21 @@ template<typename MatrixType> void inverse(const MatrixType& m)
|
||||
|
||||
// since for the general case we implement separately row-major and col-major, test that
|
||||
VERIFY_IS_APPROX(m1.transpose().inverse(), m1.inverse().transpose());
|
||||
|
||||
//computeInverseWithCheck tests
|
||||
//First: an invertible matrix
|
||||
bool invertible = m1.computeInverseWithCheck(&m2);
|
||||
VERIFY(invertible);
|
||||
VERIFY_IS_APPROX(identity, m1*m2);
|
||||
|
||||
//Second: a rank one matrix (not invertible, except for 1x1 matrices)
|
||||
VectorType v3 = VectorType::Random(rows);
|
||||
MatrixType m3 = v3*v3.transpose(), m4(rows,cols);
|
||||
invertible = m3.computeInverseWithCheck( &m4 );
|
||||
if( 1 == rows ){
|
||||
VERIFY( invertible ); }
|
||||
else{
|
||||
VERIFY( !invertible ); }
|
||||
}
|
||||
|
||||
void test_inverse()
|
||||
@@ -77,7 +92,7 @@ void test_inverse()
|
||||
CALL_SUBTEST( inverse(MatrixXf(8,8)) );
|
||||
CALL_SUBTEST( inverse(MatrixXcd(7,7)) );
|
||||
}
|
||||
|
||||
|
||||
// test some tricky cases for 4x4 matrices
|
||||
VERIFY_IS_APPROX((Matrix4f() << 0,0,1,0, 1,0,0,0, 0,1,0,0, 0,0,0,1).finished().inverse(),
|
||||
(Matrix4f() << 0,1,0,0, 0,0,1,0, 1,0,0,0, 0,0,0,1).finished());
|
||||
|
||||
@@ -57,6 +57,11 @@ template<typename MatrixType> void lu_non_invertible()
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
m3 = MatrixType::Random(rows,cols2);
|
||||
VERIFY(!lu.solve(m3, &m2));
|
||||
|
||||
typedef Matrix<typename MatrixType::Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
|
||||
SquareMatrixType m4(rows, rows), m5(rows, rows);
|
||||
createRandomMatrixOfRank(rows/2, rows, rows, m4);
|
||||
VERIFY(!m4.computeInverseWithCheck(&m5));
|
||||
}
|
||||
|
||||
template<typename MatrixType> void lu_invertible()
|
||||
|
||||
@@ -242,8 +242,8 @@ void createRandomMatrixOfRank(int desired_rank, int rows, int cols, Eigen::Matri
|
||||
const int diag_size = std::min(d.rows(),d.cols());
|
||||
d.diagonal().segment(desired_rank, diag_size-desired_rank) = VectorType::Zero(diag_size-desired_rank);
|
||||
|
||||
QR<MatrixType> qra(a);
|
||||
QR<MatrixType> qrb(b);
|
||||
HouseholderQR<MatrixType> qra(a);
|
||||
HouseholderQR<MatrixType> qrb(b);
|
||||
m = (qra.matrixQ() * d * qrb.matrixQ()).lazy();
|
||||
}
|
||||
|
||||
|
||||
62
test/qr.cpp
62
test/qr.cpp
@@ -36,7 +36,7 @@ template<typename MatrixType> void qr(const MatrixType& m)
|
||||
typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
|
||||
|
||||
MatrixType a = MatrixType::Random(rows,cols);
|
||||
QR<MatrixType> qrOfA(a);
|
||||
HouseholderQR<MatrixType> qrOfA(a);
|
||||
VERIFY_IS_APPROX(a, qrOfA.matrixQ() * qrOfA.matrixR());
|
||||
VERIFY_IS_NOT_APPROX(a+MatrixType::Identity(rows, cols), qrOfA.matrixQ() * qrOfA.matrixR());
|
||||
|
||||
@@ -55,40 +55,6 @@ template<typename MatrixType> void qr(const MatrixType& m)
|
||||
VERIFY_IS_APPROX(b, hess.matrixQ() * hess.matrixH() * hess.matrixQ().adjoint());
|
||||
}
|
||||
|
||||
template<typename MatrixType> void qr_non_invertible()
|
||||
{
|
||||
/* this test covers the following files: QR.h */
|
||||
int rows = ei_random<int>(20,200), cols = ei_random<int>(20,rows), cols2 = ei_random<int>(20,rows);
|
||||
int rank = ei_random<int>(1, std::min(rows, cols)-1);
|
||||
|
||||
MatrixType m1(rows, cols), m2(cols, cols2), m3(rows, cols2), k(1,1);
|
||||
createRandomMatrixOfRank(rank, rows, cols, m1);
|
||||
|
||||
QR<MatrixType> lu(m1);
|
||||
// typename LU<MatrixType>::KernelResultType m1kernel = lu.kernel();
|
||||
// typename LU<MatrixType>::ImageResultType m1image = lu.image();
|
||||
std::cerr << rows << "x" << cols << " " << rank << " " << lu.rank() << "\n";
|
||||
if (rank != lu.rank())
|
||||
std::cerr << lu.matrixR().diagonal().transpose() << "\n";
|
||||
VERIFY(rank == lu.rank());
|
||||
VERIFY(cols - lu.rank() == lu.dimensionOfKernel());
|
||||
VERIFY(!lu.isInjective());
|
||||
VERIFY(!lu.isInvertible());
|
||||
VERIFY(lu.isSurjective() == (lu.rank() == rows));
|
||||
// VERIFY((m1 * m1kernel).isMuchSmallerThan(m1));
|
||||
// VERIFY(m1image.lu().rank() == rank);
|
||||
// MatrixType sidebyside(m1.rows(), m1.cols() + m1image.cols());
|
||||
// sidebyside << m1, m1image;
|
||||
// VERIFY(sidebyside.lu().rank() == rank);
|
||||
m2 = MatrixType::Random(cols,cols2);
|
||||
m3 = m1*m2;
|
||||
m2 = MatrixType::Random(cols,cols2);
|
||||
lu.solve(m3, &m2);
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
m3 = MatrixType::Random(rows,cols2);
|
||||
VERIFY(!lu.solve(m3, &m2));
|
||||
}
|
||||
|
||||
template<typename MatrixType> void qr_invertible()
|
||||
{
|
||||
/* this test covers the following files: QR.h */
|
||||
@@ -105,33 +71,18 @@ template<typename MatrixType> void qr_invertible()
|
||||
m1 += a * a.adjoint();
|
||||
}
|
||||
|
||||
QR<MatrixType> lu(m1);
|
||||
VERIFY(0 == lu.dimensionOfKernel());
|
||||
VERIFY(size == lu.rank());
|
||||
VERIFY(lu.isInjective());
|
||||
VERIFY(lu.isSurjective());
|
||||
VERIFY(lu.isInvertible());
|
||||
// VERIFY(lu.image().lu().isInvertible());
|
||||
HouseholderQR<MatrixType> qr(m1);
|
||||
m3 = MatrixType::Random(size,size);
|
||||
lu.solve(m3, &m2);
|
||||
qr.solve(m3, &m2);
|
||||
//std::cerr << m3 - m1*m2 << "\n\n";
|
||||
VERIFY_IS_APPROX(m3, m1*m2);
|
||||
// VERIFY_IS_APPROX(m2, lu.inverse()*m3);
|
||||
m3 = MatrixType::Random(size,size);
|
||||
VERIFY(lu.solve(m3, &m2));
|
||||
}
|
||||
|
||||
template<typename MatrixType> void qr_verify_assert()
|
||||
{
|
||||
MatrixType tmp;
|
||||
|
||||
QR<MatrixType> qr;
|
||||
VERIFY_RAISES_ASSERT(qr.isFullRank())
|
||||
VERIFY_RAISES_ASSERT(qr.rank())
|
||||
VERIFY_RAISES_ASSERT(qr.dimensionOfKernel())
|
||||
VERIFY_RAISES_ASSERT(qr.isInjective())
|
||||
VERIFY_RAISES_ASSERT(qr.isSurjective())
|
||||
VERIFY_RAISES_ASSERT(qr.isInvertible())
|
||||
HouseholderQR<MatrixType> qr;
|
||||
VERIFY_RAISES_ASSERT(qr.matrixR())
|
||||
VERIFY_RAISES_ASSERT(qr.solve(tmp,&tmp))
|
||||
VERIFY_RAISES_ASSERT(qr.matrixQ())
|
||||
@@ -149,11 +100,6 @@ void test_qr()
|
||||
}
|
||||
|
||||
for(int i = 0; i < g_repeat; i++) {
|
||||
CALL_SUBTEST( qr_non_invertible<MatrixXf>() );
|
||||
CALL_SUBTEST( qr_non_invertible<MatrixXd>() );
|
||||
// TODO fix issue with complex
|
||||
// CALL_SUBTEST( qr_non_invertible<MatrixXcf>() );
|
||||
// CALL_SUBTEST( qr_non_invertible<MatrixXcd>() );
|
||||
CALL_SUBTEST( qr_invertible<MatrixXf>() );
|
||||
CALL_SUBTEST( qr_invertible<MatrixXd>() );
|
||||
// TODO fix issue with complex
|
||||
|
||||
@@ -299,6 +299,8 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
|
||||
initSparse<Scalar>(density, refMat2, m2);
|
||||
VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval());
|
||||
VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose());
|
||||
|
||||
VERIFY_IS_APPROX(SparseMatrixType(m2.adjoint()), refMat2.adjoint());
|
||||
}
|
||||
|
||||
// test prune
|
||||
|
||||
@@ -50,7 +50,7 @@ template<typename MatrixType> void svd(const MatrixType& m)
|
||||
MatrixType sigma = MatrixType::Zero(rows,cols);
|
||||
MatrixType matU = MatrixType::Zero(rows,rows);
|
||||
sigma.block(0,0,cols,cols) = svd.singularValues().asDiagonal();
|
||||
matU.block(0,0,rows,cols) = svd.matrixU();
|
||||
matU = svd.matrixU();
|
||||
VERIFY_IS_APPROX(a, matU * sigma * svd.matrixV().transpose());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user