diff options
author | Hauke Heibel <hauke.heibel@gmail.com> | 2009-05-17 16:07:12 +0200 |
---|---|---|
committer | Hauke Heibel <hauke.heibel@gmail.com> | 2009-05-17 16:07:12 +0200 |
commit | 6358c129987383d817b339642ef450c056f134a5 (patch) | |
tree | 789e18a630b46b91ea6cc5733d95839a280719fe /test | |
parent | 934d6b47499d7dc883e4298fcbd94534f0c6ab0f (diff) |
* introduced method createRandomMatrixOfRank (R = U*D*V where U,V unitary, D r-by-c diag. with rank non-zero values)
* switched lu/qr tests to be using createRandomMatrixOfRank
* removed unused methods doSomeRankPreservingOperations
* removed NOTE about doSomeRankPreservingOperations
Diffstat (limited to 'test')
-rw-r--r-- | test/lu.cpp | 30 | ||||
-rw-r--r-- | test/main.h | 21 | ||||
-rw-r--r-- | test/qr.cpp | 30 |
3 files changed, 23 insertions, 58 deletions
diff --git a/test/lu.cpp b/test/lu.cpp index 6147419fd..625241330 100644 --- a/test/lu.cpp +++ b/test/lu.cpp @@ -25,44 +25,16 @@ #include "main.h" #include <Eigen/LU> -template<typename Derived> -void doSomeRankPreservingOperations(Eigen::MatrixBase<Derived>& m) -{ - typedef typename Derived::RealScalar RealScalar; - for(int a = 0; a < 3*(m.rows()+m.cols()); a++) - { - RealScalar d = Eigen::ei_random<RealScalar>(-1,1); - int i = Eigen::ei_random<int>(0,m.rows()-1); // i is a random row number - int j; - do { - j = Eigen::ei_random<int>(0,m.rows()-1); - } while (i==j); // j is another one (must be different) - m.row(i) += d * m.row(j); - - i = Eigen::ei_random<int>(0,m.cols()-1); // i is a random column number - do { - j = Eigen::ei_random<int>(0,m.cols()-1); - } while (i==j); // j is another one (must be different) - m.col(i) += d * m.col(j); - } -} - template<typename MatrixType> void lu_non_invertible() { /* this test covers the following files: LU.h */ - // NOTE there seems to be a problem with too small sizes -- could easily lie in the doSomeRankPreservingOperations function int rows = ei_random<int>(20,200), cols = ei_random<int>(20,200), cols2 = ei_random<int>(20,200); int rank = ei_random<int>(1, std::min(rows, cols)-1); MatrixType m1(rows, cols), m2(cols, cols2), m3(rows, cols2), k(1,1); - m1 = MatrixType::Random(rows,cols); - if(rows <= cols) - for(int i = rank; i < rows; i++) m1.row(i).setZero(); - else - for(int i = rank; i < cols; i++) m1.col(i).setZero(); - doSomeRankPreservingOperations(m1); + createRandomMatrixOfRank(rank, rows, cols, m1); LU<MatrixType> lu(m1); typename LU<MatrixType>::KernelResultType m1kernel = lu.kernel(); diff --git a/test/main.h b/test/main.h index 489f78bff..1574d5377 100644 --- a/test/main.h +++ b/test/main.h @@ -136,6 +136,7 @@ namespace Eigen #define EIGEN_INTERNAL_DEBUGGING #define EIGEN_NICE_RANDOM #include <Eigen/Array> +#include <Eigen/QR> // required for createRandomMatrixOfRank #define VERIFY(a) do { if (!(a)) { \ @@ -225,6 +226,26 @@ inline bool test_ei_isMuchSmallerThan(const MatrixBase<Derived>& m, return m.isMuchSmallerThan(s, test_precision<typename ei_traits<Derived>::Scalar>()); } +template<typename Derived> +void createRandomMatrixOfRank(int desired_rank, int rows, int cols, Eigen::MatrixBase<Derived>& m) +{ + typedef Derived MatrixType; + typedef MatrixType::Scalar Scalar; + typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType; + + MatrixType a = MatrixType::Random(rows,rows); + MatrixType d = MatrixType::Identity(rows,cols); + MatrixType b = MatrixType::Random(cols,cols); + + // set the diagonal such that only desired_rank non-zero entries reamain + 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); + m = (qra.matrixQ() * d * qrb.matrixQ()).lazy(); +} + } // end namespace Eigen template<typename T> struct GetDifferentType; diff --git a/test/qr.cpp b/test/qr.cpp index 345ea17fd..8fe356444 100644 --- a/test/qr.cpp +++ b/test/qr.cpp @@ -55,42 +55,14 @@ template<typename MatrixType> void qr(const MatrixType& m) VERIFY_IS_APPROX(b, hess.matrixQ() * hess.matrixH() * hess.matrixQ().adjoint()); } -template<typename Derived> -void doSomeRankPreservingOperations(Eigen::MatrixBase<Derived>& m) -{ - typedef typename Derived::RealScalar RealScalar; - for(int a = 0; a < 3*(m.rows()+m.cols()); a++) - { - RealScalar d = Eigen::ei_random<RealScalar>(-1,1); - int i = Eigen::ei_random<int>(0,m.rows()-1); // i is a random row number - int j; - do { - j = Eigen::ei_random<int>(0,m.rows()-1); - } while (i==j); // j is another one (must be different) - m.row(i) += d * m.row(j); - - i = Eigen::ei_random<int>(0,m.cols()-1); // i is a random column number - do { - j = Eigen::ei_random<int>(0,m.cols()-1); - } while (i==j); // j is another one (must be different) - m.col(i) += d * m.col(j); - } -} - template<typename MatrixType> void qr_non_invertible() { /* this test covers the following files: QR.h */ - // NOTE there seems to be a problem with too small sizes -- could easily lie in the doSomeRankPreservingOperations function 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); - m1 = MatrixType::Random(rows,cols); - if(rows <= cols) - for(int i = rank; i < rows; i++) m1.row(i).setZero(); - else - for(int i = rank; i < cols; i++) m1.col(i).setZero(); - doSomeRankPreservingOperations(m1); + createRandomMatrixOfRank(rank, rows, cols, m1); QR<MatrixType> lu(m1); // typename LU<MatrixType>::KernelResultType m1kernel = lu.kernel(); |