2009-09-01 10:26:15 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
2010-06-25 05:21:58 +08:00
|
|
|
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2009-09-01 10:26:15 +08:00
|
|
|
// Copyright (C) 2009 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/>.
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include <Eigen/SVD>
|
|
|
|
|
2010-10-08 22:42:32 +08:00
|
|
|
template<typename MatrixType, int QRPreconditioner>
|
2010-10-08 22:42:40 +08:00
|
|
|
void jacobisvd_check_full(const MatrixType& m, const JacobiSVD<MatrixType, QRPreconditioner>& svd)
|
2009-09-01 10:26:15 +08:00
|
|
|
{
|
2010-06-20 23:37:56 +08:00
|
|
|
typedef typename MatrixType::Index Index;
|
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
2009-09-01 10:26:15 +08:00
|
|
|
|
|
|
|
enum {
|
|
|
|
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
|
|
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime
|
|
|
|
};
|
2009-09-16 20:35:42 +08:00
|
|
|
|
2009-09-01 10:26:15 +08:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime> MatrixUType;
|
|
|
|
typedef Matrix<Scalar, ColsAtCompileTime, ColsAtCompileTime> MatrixVType;
|
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime, 1> ColVectorType;
|
|
|
|
typedef Matrix<Scalar, ColsAtCompileTime, 1> InputVectorType;
|
2009-09-16 20:35:42 +08:00
|
|
|
|
2009-09-01 10:26:15 +08:00
|
|
|
MatrixType sigma = MatrixType::Zero(rows,cols);
|
|
|
|
sigma.diagonal() = svd.singularValues().template cast<Scalar>();
|
|
|
|
MatrixUType u = svd.matrixU();
|
|
|
|
MatrixVType v = svd.matrixV();
|
2009-09-16 20:35:42 +08:00
|
|
|
|
2010-10-08 22:42:40 +08:00
|
|
|
VERIFY_IS_APPROX(m, u * sigma * v.adjoint());
|
2009-09-01 10:26:15 +08:00
|
|
|
VERIFY_IS_UNITARY(u);
|
|
|
|
VERIFY_IS_UNITARY(v);
|
|
|
|
}
|
|
|
|
|
2010-10-08 22:42:40 +08:00
|
|
|
template<typename MatrixType, int QRPreconditioner>
|
|
|
|
void jacobisvd_compare_to_full(const MatrixType& m,
|
|
|
|
unsigned int computationOptions,
|
|
|
|
const JacobiSVD<MatrixType, QRPreconditioner>& referenceSvd)
|
|
|
|
{
|
|
|
|
typedef typename MatrixType::Index Index;
|
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
|
|
|
Index diagSize = std::min(rows, cols);
|
|
|
|
|
|
|
|
JacobiSVD<MatrixType, QRPreconditioner> svd(m, computationOptions);
|
|
|
|
|
|
|
|
VERIFY_IS_EQUAL(svd.singularValues(), referenceSvd.singularValues());
|
|
|
|
if(computationOptions & ComputeFullU)
|
|
|
|
VERIFY_IS_EQUAL(svd.matrixU(), referenceSvd.matrixU());
|
|
|
|
if(computationOptions & ComputeThinU)
|
|
|
|
VERIFY_IS_EQUAL(svd.matrixU(), referenceSvd.matrixU().leftCols(diagSize));
|
|
|
|
if(computationOptions & ComputeFullV)
|
|
|
|
VERIFY_IS_EQUAL(svd.matrixV(), referenceSvd.matrixV());
|
|
|
|
if(computationOptions & ComputeThinV)
|
|
|
|
VERIFY_IS_EQUAL(svd.matrixV(), referenceSvd.matrixV().leftCols(diagSize));
|
|
|
|
}
|
|
|
|
|
2010-10-12 03:36:04 +08:00
|
|
|
template<typename MatrixType, int QRPreconditioner>
|
|
|
|
void jacobisvd_solve(const MatrixType& m, unsigned int computationOptions)
|
|
|
|
{
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
typedef typename MatrixType::Index Index;
|
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
|
|
|
|
|
|
|
enum {
|
|
|
|
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
|
|
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime, Dynamic> RhsType;
|
|
|
|
typedef Matrix<Scalar, ColsAtCompileTime, Dynamic> SolutionType;
|
|
|
|
|
|
|
|
RhsType rhs = RhsType::Random(rows, ei_random<Index>(1, cols));
|
|
|
|
JacobiSVD<MatrixType, QRPreconditioner> svd(m, computationOptions);
|
|
|
|
SolutionType x = svd.solve(rhs);
|
|
|
|
// evaluate normal equation which works also for least-squares solutions
|
|
|
|
VERIFY_IS_APPROX(m.adjoint()*m*x,m.adjoint()*rhs);
|
|
|
|
}
|
|
|
|
|
2010-10-08 22:42:40 +08:00
|
|
|
template<typename MatrixType, int QRPreconditioner>
|
|
|
|
void jacobisvd_test_all_computation_options(const MatrixType& m)
|
|
|
|
{
|
|
|
|
if (QRPreconditioner == NoQRPreconditioner && m.rows() != m.cols())
|
|
|
|
return;
|
|
|
|
JacobiSVD<MatrixType, QRPreconditioner> fullSvd(m, ComputeFullU|ComputeFullV);
|
|
|
|
|
|
|
|
jacobisvd_check_full(m, fullSvd);
|
2010-10-12 03:36:04 +08:00
|
|
|
jacobisvd_solve<MatrixType, QRPreconditioner>(m, ComputeFullU | ComputeFullV);
|
2010-10-08 22:42:40 +08:00
|
|
|
|
|
|
|
if(QRPreconditioner == FullPivHouseholderQRPreconditioner)
|
|
|
|
return;
|
|
|
|
|
|
|
|
jacobisvd_compare_to_full(m, ComputeFullU, fullSvd);
|
|
|
|
jacobisvd_compare_to_full(m, ComputeFullV, fullSvd);
|
|
|
|
jacobisvd_compare_to_full(m, 0, fullSvd);
|
|
|
|
|
|
|
|
if (MatrixType::ColsAtCompileTime == Dynamic) {
|
|
|
|
// thin U/V are only available with dynamic number of columns
|
|
|
|
jacobisvd_compare_to_full(m, ComputeFullU|ComputeThinV, fullSvd);
|
|
|
|
jacobisvd_compare_to_full(m, ComputeThinV, fullSvd);
|
|
|
|
jacobisvd_compare_to_full(m, ComputeThinU|ComputeFullV, fullSvd);
|
|
|
|
jacobisvd_compare_to_full(m, ComputeThinU , fullSvd);
|
|
|
|
jacobisvd_compare_to_full(m, ComputeThinU|ComputeThinV, fullSvd);
|
2010-10-12 03:36:04 +08:00
|
|
|
jacobisvd_solve<MatrixType, QRPreconditioner>(m, ComputeFullU | ComputeThinV);
|
|
|
|
jacobisvd_solve<MatrixType, QRPreconditioner>(m, ComputeThinU | ComputeFullV);
|
|
|
|
jacobisvd_solve<MatrixType, QRPreconditioner>(m, ComputeThinU | ComputeThinV);
|
2010-10-08 22:42:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-08 22:42:32 +08:00
|
|
|
template<typename MatrixType>
|
2010-10-08 22:42:40 +08:00
|
|
|
void jacobisvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
|
2010-10-08 22:42:32 +08:00
|
|
|
{
|
2010-10-08 22:42:40 +08:00
|
|
|
MatrixType m = pickrandom ? MatrixType::Random(a.rows(), a.cols()) : a;
|
2010-10-12 03:36:04 +08:00
|
|
|
|
2010-10-08 22:42:40 +08:00
|
|
|
jacobisvd_test_all_computation_options<MatrixType, FullPivHouseholderQRPreconditioner>(m);
|
|
|
|
jacobisvd_test_all_computation_options<MatrixType, ColPivHouseholderQRPreconditioner>(m);
|
|
|
|
jacobisvd_test_all_computation_options<MatrixType, HouseholderQRPreconditioner>(m);
|
|
|
|
jacobisvd_test_all_computation_options<MatrixType, NoQRPreconditioner>(m);
|
2010-10-08 22:42:32 +08:00
|
|
|
}
|
|
|
|
|
2010-10-12 03:36:04 +08:00
|
|
|
template<typename MatrixType> void jacobisvd_verify_assert(const MatrixType& m)
|
2009-09-01 10:26:15 +08:00
|
|
|
{
|
2010-10-12 03:36:04 +08:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
typedef typename MatrixType::Index Index;
|
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
|
|
|
|
|
|
|
enum {
|
|
|
|
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
|
|
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime, 1> RhsType;
|
2009-09-01 10:26:15 +08:00
|
|
|
|
2010-10-12 03:36:04 +08:00
|
|
|
RhsType rhs(rows);
|
|
|
|
|
2010-10-12 09:00:42 +08:00
|
|
|
JacobiSVD<MatrixType> svd;
|
2009-09-01 10:26:15 +08:00
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixU())
|
|
|
|
VERIFY_RAISES_ASSERT(svd.singularValues())
|
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixV())
|
2010-10-12 03:36:04 +08:00
|
|
|
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
|
|
|
|
|
|
|
MatrixType a = MatrixType::Zero(rows, cols);
|
|
|
|
a.setZero();
|
|
|
|
svd.compute(a, 0);
|
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixU())
|
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixV())
|
|
|
|
svd.singularValues();
|
|
|
|
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
|
|
|
|
|
|
|
if (ColsAtCompileTime == Dynamic)
|
|
|
|
{
|
|
|
|
svd.compute(a, ComputeThinU);
|
|
|
|
svd.matrixU();
|
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixV())
|
|
|
|
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
|
|
|
|
|
|
|
svd.compute(a, ComputeThinV);
|
|
|
|
svd.matrixV();
|
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixU())
|
|
|
|
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
|
|
|
|
|
|
|
JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> svd_fullqr;
|
|
|
|
VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeFullU|ComputeThinV))
|
|
|
|
VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeThinU|ComputeThinV))
|
|
|
|
VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeThinU|ComputeFullV))
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VERIFY_RAISES_ASSERT(svd.compute(a, ComputeThinU))
|
|
|
|
VERIFY_RAISES_ASSERT(svd.compute(a, ComputeThinV))
|
|
|
|
}
|
2009-09-01 10:26:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_jacobisvd()
|
|
|
|
{
|
2010-10-12 03:36:04 +08:00
|
|
|
CALL_SUBTEST_3(( jacobisvd_verify_assert(Matrix3f()) ));
|
|
|
|
CALL_SUBTEST_4(( jacobisvd_verify_assert(Matrix4d()) ));
|
|
|
|
CALL_SUBTEST_7(( jacobisvd_verify_assert(MatrixXf(10,12)) ));
|
|
|
|
CALL_SUBTEST_8(( jacobisvd_verify_assert(MatrixXcd(7,5)) ));
|
|
|
|
|
2009-09-01 10:26:15 +08:00
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
|
|
|
Matrix2cd m;
|
|
|
|
m << 0, 1,
|
|
|
|
0, 1;
|
2010-10-08 22:42:40 +08:00
|
|
|
CALL_SUBTEST_1(( jacobisvd(m, false) ));
|
2009-09-01 10:26:15 +08:00
|
|
|
m << 1, 0,
|
|
|
|
1, 0;
|
2010-10-08 22:42:40 +08:00
|
|
|
CALL_SUBTEST_1(( jacobisvd(m, false) ));
|
2009-09-01 10:26:15 +08:00
|
|
|
Matrix2d n;
|
|
|
|
n << 1, 1,
|
|
|
|
1, -1;
|
2010-10-08 22:42:40 +08:00
|
|
|
CALL_SUBTEST_2(( jacobisvd(n, false) ));
|
|
|
|
CALL_SUBTEST_3(( jacobisvd<Matrix3f>() ));
|
|
|
|
CALL_SUBTEST_4(( jacobisvd<Matrix4d>() ));
|
|
|
|
CALL_SUBTEST_5(( jacobisvd<Matrix<float,3,5> >() ));
|
|
|
|
CALL_SUBTEST_6(( jacobisvd<Matrix<double,Dynamic,2> >(Matrix<double,Dynamic,2>(10,2)) ));
|
|
|
|
|
2010-10-12 03:36:04 +08:00
|
|
|
int r = ei_random<int>(1, 30),
|
|
|
|
c = ei_random<int>(1, 30);
|
|
|
|
CALL_SUBTEST_7(( jacobisvd<MatrixXf>(MatrixXf(r,c)) ));
|
|
|
|
CALL_SUBTEST_8(( jacobisvd<MatrixXcd>(MatrixXcd(r,c)) ));
|
|
|
|
(void) r;
|
|
|
|
(void) c;
|
2009-09-01 10:26:15 +08:00
|
|
|
}
|
2010-04-21 23:15:57 +08:00
|
|
|
|
2010-10-12 03:36:04 +08:00
|
|
|
CALL_SUBTEST_7(( jacobisvd<MatrixXf>(MatrixXf(ei_random<int>(200, 300), ei_random<int>(200, 300))) ));
|
|
|
|
CALL_SUBTEST_8(( jacobisvd<MatrixXcd>(MatrixXcd(ei_random<int>(100, 120), ei_random<int>(100, 120))) ));
|
2009-09-01 10:26:15 +08:00
|
|
|
}
|