2013-06-19 06:03:27 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013 Gauthier Brun <brun.gauthier@gmail.com>
|
|
|
|
// Copyright (C) 2013 Nicolas Carre <nicolas.carre@ensimag.fr>
|
|
|
|
// Copyright (C) 2013 Jean Ceccato <jean.ceccato@ensimag.fr>
|
|
|
|
// Copyright (C) 2013 Pierre Zoppitelli <pierre.zoppitelli@ensimag.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// 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/
|
|
|
|
|
2014-09-19 21:25:48 +08:00
|
|
|
// discard stack allocation as that too bypasses malloc
|
|
|
|
#define EIGEN_STACK_ALLOCATION_LIMIT 0
|
|
|
|
#define EIGEN_RUNTIME_NO_MALLOC
|
|
|
|
|
|
|
|
#include "main.h"
|
2014-10-29 18:29:33 +08:00
|
|
|
#include <Eigen/SVD>
|
2013-06-19 06:03:27 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <Eigen/LU>
|
|
|
|
|
|
|
|
|
2014-09-19 21:25:48 +08:00
|
|
|
#define SVD_DEFAULT(M) BDCSVD<M>
|
2014-10-10 05:29:01 +08:00
|
|
|
#define SVD_FOR_MIN_NORM(M) BDCSVD<M>
|
2014-10-30 00:46:33 +08:00
|
|
|
#include "svd_common.h"
|
2013-06-19 06:03:27 +08:00
|
|
|
|
2014-09-19 21:25:48 +08:00
|
|
|
// Check all variants of JacobiSVD
|
2013-06-19 06:03:27 +08:00
|
|
|
template<typename MatrixType>
|
|
|
|
void bdcsvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
|
|
|
|
{
|
2019-12-19 01:26:20 +08:00
|
|
|
MatrixType m;
|
2019-12-19 20:42:14 +08:00
|
|
|
if(pickrandom) {
|
|
|
|
m.resizeLike(a);
|
2014-09-19 21:25:48 +08:00
|
|
|
svd_fill_random(m);
|
2019-12-19 20:42:14 +08:00
|
|
|
}
|
2019-12-19 01:26:20 +08:00
|
|
|
else
|
|
|
|
m = a;
|
2013-06-19 06:03:27 +08:00
|
|
|
|
2014-09-19 21:25:48 +08:00
|
|
|
CALL_SUBTEST(( svd_test_all_computation_options<BDCSVD<MatrixType> >(m, false) ));
|
|
|
|
}
|
2013-06-19 06:03:27 +08:00
|
|
|
|
2014-10-29 18:29:33 +08:00
|
|
|
template<typename MatrixType>
|
|
|
|
void bdcsvd_method()
|
|
|
|
{
|
|
|
|
enum { Size = MatrixType::RowsAtCompileTime };
|
|
|
|
typedef typename MatrixType::RealScalar RealScalar;
|
|
|
|
typedef Matrix<RealScalar, Size, 1> RealVecType;
|
|
|
|
MatrixType m = MatrixType::Identity();
|
|
|
|
VERIFY_IS_APPROX(m.bdcSvd().singularValues(), RealVecType::Ones());
|
|
|
|
VERIFY_RAISES_ASSERT(m.bdcSvd().matrixU());
|
|
|
|
VERIFY_RAISES_ASSERT(m.bdcSvd().matrixV());
|
|
|
|
VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).solve(m), m);
|
2019-01-17 08:17:39 +08:00
|
|
|
VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m);
|
|
|
|
VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m);
|
2014-10-29 18:29:33 +08:00
|
|
|
}
|
2013-06-19 06:03:27 +08:00
|
|
|
|
|
|
|
// compare the Singular values returned with Jacobi and Bdc
|
|
|
|
template<typename MatrixType>
|
|
|
|
void compare_bdc_jacobi(const MatrixType& a = MatrixType(), unsigned int computationOptions = 0)
|
|
|
|
{
|
|
|
|
MatrixType m = MatrixType::Random(a.rows(), a.cols());
|
|
|
|
BDCSVD<MatrixType> bdc_svd(m);
|
|
|
|
JacobiSVD<MatrixType> jacobi_svd(m);
|
|
|
|
VERIFY_IS_APPROX(bdc_svd.singularValues(), jacobi_svd.singularValues());
|
2014-09-19 21:25:48 +08:00
|
|
|
if(computationOptions & ComputeFullU) VERIFY_IS_APPROX(bdc_svd.matrixU(), jacobi_svd.matrixU());
|
|
|
|
if(computationOptions & ComputeThinU) VERIFY_IS_APPROX(bdc_svd.matrixU(), jacobi_svd.matrixU());
|
|
|
|
if(computationOptions & ComputeFullV) VERIFY_IS_APPROX(bdc_svd.matrixV(), jacobi_svd.matrixV());
|
|
|
|
if(computationOptions & ComputeThinV) VERIFY_IS_APPROX(bdc_svd.matrixV(), jacobi_svd.matrixV());
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(bdcsvd)
|
2013-06-19 06:03:27 +08:00
|
|
|
{
|
2014-09-19 21:25:48 +08:00
|
|
|
CALL_SUBTEST_3(( svd_verify_assert<BDCSVD<Matrix3f> >(Matrix3f()) ));
|
|
|
|
CALL_SUBTEST_4(( svd_verify_assert<BDCSVD<Matrix4d> >(Matrix4d()) ));
|
|
|
|
CALL_SUBTEST_7(( svd_verify_assert<BDCSVD<MatrixXf> >(MatrixXf(10,12)) ));
|
|
|
|
CALL_SUBTEST_8(( svd_verify_assert<BDCSVD<MatrixXcd> >(MatrixXcd(7,5)) ));
|
2013-06-19 06:03:27 +08:00
|
|
|
|
2015-06-26 22:14:23 +08:00
|
|
|
CALL_SUBTEST_101(( svd_all_trivial_2x2(bdcsvd<Matrix2cd>) ));
|
|
|
|
CALL_SUBTEST_102(( svd_all_trivial_2x2(bdcsvd<Matrix2d>) ));
|
2013-06-19 06:03:27 +08:00
|
|
|
|
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
2014-10-07 01:35:57 +08:00
|
|
|
CALL_SUBTEST_3(( bdcsvd<Matrix3f>() ));
|
|
|
|
CALL_SUBTEST_4(( bdcsvd<Matrix4d>() ));
|
|
|
|
CALL_SUBTEST_5(( bdcsvd<Matrix<float,3,5> >() ));
|
2014-09-19 21:25:48 +08:00
|
|
|
|
|
|
|
int r = internal::random<int>(1, EIGEN_TEST_MAX_SIZE/2),
|
|
|
|
c = internal::random<int>(1, EIGEN_TEST_MAX_SIZE/2);
|
2013-06-19 06:03:27 +08:00
|
|
|
|
2014-09-19 21:25:48 +08:00
|
|
|
TEST_SET_BUT_UNUSED_VARIABLE(r)
|
|
|
|
TEST_SET_BUT_UNUSED_VARIABLE(c)
|
|
|
|
|
|
|
|
CALL_SUBTEST_6(( bdcsvd(Matrix<double,Dynamic,2>(r,2)) ));
|
|
|
|
CALL_SUBTEST_7(( bdcsvd(MatrixXf(r,c)) ));
|
|
|
|
CALL_SUBTEST_7(( compare_bdc_jacobi(MatrixXf(r,c)) ));
|
|
|
|
CALL_SUBTEST_10(( bdcsvd(MatrixXd(r,c)) ));
|
|
|
|
CALL_SUBTEST_10(( compare_bdc_jacobi(MatrixXd(r,c)) ));
|
|
|
|
CALL_SUBTEST_8(( bdcsvd(MatrixXcd(r,c)) ));
|
|
|
|
CALL_SUBTEST_8(( compare_bdc_jacobi(MatrixXcd(r,c)) ));
|
2013-06-19 06:03:27 +08:00
|
|
|
|
|
|
|
// Test on inf/nan matrix
|
2014-09-19 21:25:48 +08:00
|
|
|
CALL_SUBTEST_7( (svd_inf_nan<BDCSVD<MatrixXf>, MatrixXf>()) );
|
|
|
|
CALL_SUBTEST_10( (svd_inf_nan<BDCSVD<MatrixXd>, MatrixXd>()) );
|
2013-06-19 06:03:27 +08:00
|
|
|
}
|
|
|
|
|
2014-09-19 21:25:48 +08:00
|
|
|
// test matrixbase method
|
2014-10-29 18:29:33 +08:00
|
|
|
CALL_SUBTEST_1(( bdcsvd_method<Matrix2cd>() ));
|
|
|
|
CALL_SUBTEST_3(( bdcsvd_method<Matrix3f>() ));
|
2013-06-19 06:03:27 +08:00
|
|
|
|
|
|
|
// Test problem size constructors
|
|
|
|
CALL_SUBTEST_7( BDCSVD<MatrixXf>(10,10) );
|
|
|
|
|
2014-09-19 21:25:48 +08:00
|
|
|
// Check that preallocation avoids subsequent mallocs
|
2018-03-11 22:01:44 +08:00
|
|
|
// Disabled because not supported by BDCSVD
|
2017-07-20 16:03:54 +08:00
|
|
|
// CALL_SUBTEST_9( svd_preallocate<void>() );
|
2014-09-19 21:25:48 +08:00
|
|
|
|
2015-06-22 15:58:06 +08:00
|
|
|
CALL_SUBTEST_2( svd_underoverflow<void>() );
|
2014-09-19 21:25:48 +08:00
|
|
|
}
|
|
|
|
|