bdcsvd.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2013 Gauthier Brun <brun.gauthier@gmail.com>
00005 // Copyright (C) 2013 Nicolas Carre <nicolas.carre@ensimag.fr>
00006 // Copyright (C) 2013 Jean Ceccato <jean.ceccato@ensimag.fr>
00007 // Copyright (C) 2013 Pierre Zoppitelli <pierre.zoppitelli@ensimag.fr>
00008 //
00009 // This Source Code Form is subject to the terms of the Mozilla
00010 // Public License v. 2.0. If a copy of the MPL was not distributed
00011 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/
00012 
00013 #include "svd_common.h"
00014 #include <iostream>
00015 #include <Eigen/LU>
00016 
00017 // check if "svd" is the good image of "m"  
00018 template<typename MatrixType>
00019 void bdcsvd_check_full(const MatrixType& m, const BDCSVD<MatrixType>& svd)
00020 {
00021   svd_check_full< MatrixType, BDCSVD< MatrixType > >(m, svd);
00022 }
00023 
00024 // Compare to a reference value
00025 template<typename MatrixType>
00026 void bdcsvd_compare_to_full(const MatrixType& m,
00027                             unsigned int computationOptions,
00028                             const BDCSVD<MatrixType>& referenceSvd)
00029 {
00030   svd_compare_to_full< MatrixType, BDCSVD< MatrixType > >(m, computationOptions, referenceSvd);
00031 } // end bdcsvd_compare_to_full
00032 
00033 
00034 template<typename MatrixType>
00035 void bdcsvd_solve(const MatrixType& m, unsigned int computationOptions)
00036 {
00037   svd_solve< MatrixType, BDCSVD< MatrixType > >(m, computationOptions);
00038 } //  end template bdcsvd_solve
00039 
00040 
00041 // test the computations options
00042 template<typename MatrixType>
00043 void bdcsvd_test_all_computation_options(const MatrixType& m)
00044 {
00045   BDCSVD<MatrixType> fullSvd(m, ComputeFullU|ComputeFullV);
00046   svd_test_computation_options_1< MatrixType, BDCSVD< MatrixType > >(m, fullSvd); 
00047   svd_test_computation_options_2< MatrixType, BDCSVD< MatrixType > >(m, fullSvd); 
00048 } // end bdcsvd_test_all_computation_options
00049 
00050 
00051 // Call a test with all the computations options
00052 template<typename MatrixType>
00053 void bdcsvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
00054 {
00055   MatrixType m = pickrandom ? MatrixType::Random(a.rows(), a.cols()) : a;
00056   bdcsvd_test_all_computation_options<MatrixType>(m);
00057 } // end template bdcsvd
00058 
00059 
00060 // verify assert
00061 template<typename MatrixType> 
00062 void bdcsvd_verify_assert(const MatrixType& m)
00063 {
00064   svd_verify_assert< MatrixType, BDCSVD< MatrixType > >(m);
00065 }// end template bdcsvd_verify_assert
00066 
00067 
00068 // test weird values
00069 template<typename MatrixType>
00070 void bdcsvd_inf_nan()
00071 {
00072   svd_inf_nan< MatrixType, BDCSVD< MatrixType > >();
00073 }// end template bdcsvd_inf_nan
00074 
00075 
00076 
00077 void bdcsvd_preallocate()
00078 {
00079   svd_preallocate< BDCSVD< MatrixXf > >();
00080 } // end bdcsvd_preallocate
00081 
00082 
00083 // compare the Singular values returned with Jacobi and Bdc
00084 template<typename MatrixType> 
00085 void compare_bdc_jacobi(const MatrixType& a = MatrixType(), unsigned int computationOptions = 0)
00086 {
00087   std::cout << "debut compare" << std::endl;
00088   MatrixType m = MatrixType::Random(a.rows(), a.cols());
00089   BDCSVD<MatrixType> bdc_svd(m);
00090   JacobiSVD<MatrixType> jacobi_svd(m);
00091   VERIFY_IS_APPROX(bdc_svd.singularValues(), jacobi_svd.singularValues());
00092   if(computationOptions & ComputeFullU)
00093     VERIFY_IS_APPROX(bdc_svd.matrixU(), jacobi_svd.matrixU());
00094   if(computationOptions & ComputeThinU)
00095     VERIFY_IS_APPROX(bdc_svd.matrixU(), jacobi_svd.matrixU());
00096   if(computationOptions & ComputeFullV)
00097     VERIFY_IS_APPROX(bdc_svd.matrixV(), jacobi_svd.matrixV());
00098   if(computationOptions & ComputeThinV)
00099     VERIFY_IS_APPROX(bdc_svd.matrixV(), jacobi_svd.matrixV());
00100   std::cout << "fin compare" << std::endl;
00101 } // end template compare_bdc_jacobi
00102 
00103 
00104 // call the tests
00105 void test_bdcsvd()
00106 {
00107   // test of Dynamic defined Matrix (42, 42) of float 
00108   CALL_SUBTEST_11(( bdcsvd_verify_assert<Matrix<float,Dynamic,Dynamic> >
00109                     (Matrix<float,Dynamic,Dynamic>(42,42)) ));
00110   CALL_SUBTEST_11(( compare_bdc_jacobi<Matrix<float,Dynamic,Dynamic> >
00111                     (Matrix<float,Dynamic,Dynamic>(42,42), 0) ));
00112   CALL_SUBTEST_11(( bdcsvd<Matrix<float,Dynamic,Dynamic> >
00113                     (Matrix<float,Dynamic,Dynamic>(42,42)) ));
00114 
00115   // test of Dynamic defined Matrix (50, 50) of double 
00116   CALL_SUBTEST_13(( bdcsvd_verify_assert<Matrix<double,Dynamic,Dynamic> >
00117                     (Matrix<double,Dynamic,Dynamic>(50,50)) ));
00118   CALL_SUBTEST_13(( compare_bdc_jacobi<Matrix<double,Dynamic,Dynamic> >
00119                     (Matrix<double,Dynamic,Dynamic>(50,50), 0) ));
00120   CALL_SUBTEST_13(( bdcsvd<Matrix<double,Dynamic,Dynamic> >
00121                     (Matrix<double,Dynamic,Dynamic>(50, 50)) )); 
00122 
00123   // test of Dynamic defined Matrix (22, 22) of complex double
00124   CALL_SUBTEST_14(( bdcsvd_verify_assert<Matrix<std::complex<double>,Dynamic,Dynamic> >
00125                     (Matrix<std::complex<double>,Dynamic,Dynamic>(22,22)) ));
00126   CALL_SUBTEST_14(( compare_bdc_jacobi<Matrix<std::complex<double>,Dynamic,Dynamic> >
00127                     (Matrix<std::complex<double>, Dynamic, Dynamic> (22,22), 0) ));
00128   CALL_SUBTEST_14(( bdcsvd<Matrix<std::complex<double>,Dynamic,Dynamic> >
00129                     (Matrix<std::complex<double>,Dynamic,Dynamic>(22, 22)) )); 
00130 
00131   // test of Dynamic defined Matrix (10, 10) of int
00132   //CALL_SUBTEST_15(( bdcsvd_verify_assert<Matrix<int,Dynamic,Dynamic> >
00133   //                (Matrix<int,Dynamic,Dynamic>(10,10)) ));                
00134   //CALL_SUBTEST_15(( compare_bdc_jacobi<Matrix<int,Dynamic,Dynamic> >
00135   //                (Matrix<int,Dynamic,Dynamic>(10,10), 0) ));
00136   //CALL_SUBTEST_15(( bdcsvd<Matrix<int,Dynamic,Dynamic> >
00137   //                (Matrix<int,Dynamic,Dynamic>(10, 10)) )); 
00138   
00139 
00140   // test of Dynamic defined Matrix (8, 6) of double 
00141  
00142   CALL_SUBTEST_16(( bdcsvd_verify_assert<Matrix<double,Dynamic,Dynamic> >
00143                     (Matrix<double,Dynamic,Dynamic>(8,6)) ));
00144   CALL_SUBTEST_16(( compare_bdc_jacobi<Matrix<double,Dynamic,Dynamic> >
00145                     (Matrix<double,Dynamic,Dynamic>(8, 6), 0) )); 
00146   CALL_SUBTEST_16(( bdcsvd<Matrix<double,Dynamic,Dynamic> >
00147                     (Matrix<double,Dynamic,Dynamic>(8, 6)) ));
00148 
00149 
00150   
00151   // test of Dynamic defined Matrix (36, 12) of float
00152   CALL_SUBTEST_17(( compare_bdc_jacobi<Matrix<float,Dynamic,Dynamic> >
00153                     (Matrix<float,Dynamic,Dynamic>(36, 12), 0) )); 
00154   CALL_SUBTEST_17(( bdcsvd<Matrix<float,Dynamic,Dynamic> >
00155                     (Matrix<float,Dynamic,Dynamic>(36, 12)) )); 
00156 
00157   // test of Dynamic defined Matrix (5, 8) of double 
00158   CALL_SUBTEST_18(( compare_bdc_jacobi<Matrix<double,Dynamic,Dynamic> >
00159                     (Matrix<double,Dynamic,Dynamic>(5, 8), 0) )); 
00160   CALL_SUBTEST_18(( bdcsvd<Matrix<double,Dynamic,Dynamic> >
00161                     (Matrix<double,Dynamic,Dynamic>(5, 8)) )); 
00162 
00163 
00164   // non regression tests
00165   CALL_SUBTEST_3(( bdcsvd_verify_assert(Matrix3f()) ));
00166   CALL_SUBTEST_4(( bdcsvd_verify_assert(Matrix4d()) ));
00167   CALL_SUBTEST_7(( bdcsvd_verify_assert(MatrixXf(10,12)) ));
00168   CALL_SUBTEST_8(( bdcsvd_verify_assert(MatrixXcd(7,5)) ));
00169 
00170   // SUBTESTS 1 and 2 on specifics matrix
00171   for(int i = 0; i < g_repeat; i++) {
00172     Matrix2cd m;
00173     m << 0, 1,
00174       0, 1;
00175     CALL_SUBTEST_1(( bdcsvd(m, false) ));
00176     m << 1, 0,
00177       1, 0;
00178     CALL_SUBTEST_1(( bdcsvd(m, false) ));
00179 
00180     Matrix2d n;
00181     n << 0, 0,
00182       0, 0;
00183     CALL_SUBTEST_2(( bdcsvd(n, false) ));
00184     n << 0, 0,
00185       0, 1;
00186     CALL_SUBTEST_2(( bdcsvd(n, false) ));
00187     
00188     // Statics matrix don't work with BDSVD yet
00189     // bdc algo on a random 3x3 float matrix
00190     // CALL_SUBTEST_3(( bdcsvd<Matrix3f>() ));
00191     // bdc algo on a random 4x4 double matrix
00192     // CALL_SUBTEST_4(( bdcsvd<Matrix4d>() ));
00193     // bdc algo on a random 3x5 float matrix
00194     // CALL_SUBTEST_5(( bdcsvd<Matrix<float,3,5> >() ));
00195 
00196     int r = internal::random<int>(1, 30),
00197       c = internal::random<int>(1, 30);
00198     CALL_SUBTEST_7(( bdcsvd<MatrixXf>(MatrixXf(r,c)) ));
00199     CALL_SUBTEST_8(( bdcsvd<MatrixXcd>(MatrixXcd(r,c)) ));
00200     (void) r;
00201     (void) c;
00202 
00203     // Test on inf/nan matrix
00204     CALL_SUBTEST_7( bdcsvd_inf_nan<MatrixXf>() );
00205   }
00206 
00207   CALL_SUBTEST_7(( bdcsvd<MatrixXf>(MatrixXf(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
00208   CALL_SUBTEST_8(( bdcsvd<MatrixXcd>(MatrixXcd(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/3), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/3))) ));
00209 
00210   // Test problem size constructors
00211   CALL_SUBTEST_7( BDCSVD<MatrixXf>(10,10) );
00212 
00213 } // end test_bdcsvd


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:29:09