Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | 28e64b0 | 2010-06-24 23:21:58 +0200 | [diff] [blame] | 4 | // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> |
Benoit Jacob | 00f89a8 | 2008-11-24 13:40:43 +0000 | [diff] [blame] | 5 | // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com> |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 6 | // |
| 7 | // Eigen is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU Lesser General Public |
| 9 | // License as published by the Free Software Foundation; either |
| 10 | // version 3 of the License, or (at your option) any later version. |
| 11 | // |
| 12 | // Alternatively, you can redistribute it and/or |
| 13 | // modify it under the terms of the GNU General Public License as |
| 14 | // published by the Free Software Foundation; either version 2 of |
| 15 | // the License, or (at your option) any later version. |
| 16 | // |
| 17 | // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the |
| 20 | // GNU General Public License for more details. |
| 21 | // |
| 22 | // You should have received a copy of the GNU Lesser General Public |
| 23 | // License and a copy of the GNU General Public License along with |
| 24 | // Eigen. If not, see <http://www.gnu.org/licenses/>. |
| 25 | |
Gael Guennebaud | e3fac69 | 2008-06-08 15:03:23 +0000 | [diff] [blame] | 26 | // this hack is needed to make this file compiles with -pedantic (gcc) |
Benoit Jacob | 9e3c731 | 2009-01-22 00:09:34 +0000 | [diff] [blame] | 27 | #ifdef __GNUC__ |
Gael Guennebaud | 60726f9 | 2008-06-04 10:15:48 +0000 | [diff] [blame] | 28 | #define throw(X) |
Benoit Jacob | 9e3c731 | 2009-01-22 00:09:34 +0000 | [diff] [blame] | 29 | #endif |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 30 | // discard stack allocation as that too bypasses malloc |
Benoit Jacob | d671205 | 2008-12-31 00:25:31 +0000 | [diff] [blame] | 31 | #define EIGEN_STACK_ALLOCATION_LIMIT 0 |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 32 | // any heap allocation will raise an assert |
| 33 | #define EIGEN_NO_MALLOC |
Benoit Jacob | d671205 | 2008-12-31 00:25:31 +0000 | [diff] [blame] | 34 | |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 35 | #include "main.h" |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 36 | #include <Eigen/Cholesky> |
| 37 | #include <Eigen/Eigenvalues> |
| 38 | #include <Eigen/LU> |
| 39 | #include <Eigen/QR> |
| 40 | #include <Eigen/SVD> |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 41 | |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 42 | template<typename MatrixType> void nomalloc(const MatrixType& m) |
| 43 | { |
| 44 | /* this test check no dynamic memory allocation are issued with fixed-size matrices |
| 45 | */ |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 46 | typedef typename MatrixType::Index Index; |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 47 | typedef typename MatrixType::Scalar Scalar; |
| 48 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
| 49 | |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 50 | Index rows = m.rows(); |
| 51 | Index cols = m.cols(); |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 52 | |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 53 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 54 | m2 = MatrixType::Random(rows, cols), |
Benoit Jacob | 0609dbe | 2011-10-31 00:51:36 -0400 | [diff] [blame] | 55 | m3(rows, cols); |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 56 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 57 | Scalar s1 = internal::random<Scalar>(); |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 58 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 59 | Index r = internal::random<Index>(0, rows-1), |
| 60 | c = internal::random<Index>(0, cols-1); |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 61 | |
| 62 | VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2); |
| 63 | VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c))); |
Gael Guennebaud | 71a171c | 2010-01-04 19:13:08 +0100 | [diff] [blame] | 64 | VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), (m1.array()*m1.array()).matrix()); |
Gael Guennebaud | 3d8e179 | 2011-01-27 18:02:49 +0100 | [diff] [blame] | 65 | VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2)); |
Gael Guennebaud | c60818f | 2011-02-01 11:38:46 +0100 | [diff] [blame] | 66 | |
Gael Guennebaud | f46ace6 | 2011-01-31 21:30:27 +0100 | [diff] [blame] | 67 | m2.col(0).noalias() = m1 * m1.col(0); |
| 68 | m2.col(0).noalias() -= m1.adjoint() * m1.col(0); |
| 69 | m2.col(0).noalias() -= m1 * m1.row(0).adjoint(); |
| 70 | m2.col(0).noalias() -= m1.adjoint() * m1.row(0).adjoint(); |
| 71 | |
| 72 | m2.row(0).noalias() = m1.row(0) * m1; |
| 73 | m2.row(0).noalias() -= m1.row(0) * m1.adjoint(); |
| 74 | m2.row(0).noalias() -= m1.col(0).adjoint() * m1; |
| 75 | m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint(); |
Gael Guennebaud | c60818f | 2011-02-01 11:38:46 +0100 | [diff] [blame] | 76 | VERIFY_IS_APPROX(m2,m2); |
| 77 | |
| 78 | m2.col(0).noalias() = m1.template triangularView<Upper>() * m1.col(0); |
| 79 | m2.col(0).noalias() -= m1.adjoint().template triangularView<Upper>() * m1.col(0); |
| 80 | m2.col(0).noalias() -= m1.template triangularView<Upper>() * m1.row(0).adjoint(); |
| 81 | m2.col(0).noalias() -= m1.adjoint().template triangularView<Upper>() * m1.row(0).adjoint(); |
| 82 | |
| 83 | m2.row(0).noalias() = m1.row(0) * m1.template triangularView<Upper>(); |
| 84 | m2.row(0).noalias() -= m1.row(0) * m1.adjoint().template triangularView<Upper>(); |
| 85 | m2.row(0).noalias() -= m1.col(0).adjoint() * m1.template triangularView<Upper>(); |
| 86 | m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint().template triangularView<Upper>(); |
| 87 | VERIFY_IS_APPROX(m2,m2); |
| 88 | |
| 89 | m2.col(0).noalias() = m1.template selfadjointView<Upper>() * m1.col(0); |
| 90 | m2.col(0).noalias() -= m1.adjoint().template selfadjointView<Upper>() * m1.col(0); |
| 91 | m2.col(0).noalias() -= m1.template selfadjointView<Upper>() * m1.row(0).adjoint(); |
| 92 | m2.col(0).noalias() -= m1.adjoint().template selfadjointView<Upper>() * m1.row(0).adjoint(); |
| 93 | |
| 94 | m2.row(0).noalias() = m1.row(0) * m1.template selfadjointView<Upper>(); |
| 95 | m2.row(0).noalias() -= m1.row(0) * m1.adjoint().template selfadjointView<Upper>(); |
| 96 | m2.row(0).noalias() -= m1.col(0).adjoint() * m1.template selfadjointView<Upper>(); |
| 97 | m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint().template selfadjointView<Upper>(); |
| 98 | VERIFY_IS_APPROX(m2,m2); |
Gael Guennebaud | 59af20b | 2011-02-01 16:46:35 +0100 | [diff] [blame] | 99 | |
| 100 | m2.template selfadjointView<Lower>().rankUpdate(m1.col(0),-1); |
| 101 | m2.template selfadjointView<Lower>().rankUpdate(m1.row(0),-1); |
Gael Guennebaud | c60818f | 2011-02-01 11:38:46 +0100 | [diff] [blame] | 102 | |
| 103 | // The following fancy matrix-matrix products are not safe yet regarding static allocation |
| 104 | // m1 += m1.template triangularView<Upper>() * m2.col(; |
| 105 | // m1.template selfadjointView<Lower>().rankUpdate(m2); |
| 106 | // m1 += m1.template triangularView<Upper>() * m2; |
| 107 | // m1 += m1.template selfadjointView<Lower>() * m2; |
| 108 | // VERIFY_IS_APPROX(m1,m1); |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Gael Guennebaud | 8a38047 | 2010-07-04 15:35:21 +0200 | [diff] [blame] | 111 | template<typename Scalar> |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 112 | void ctms_decompositions() |
| 113 | { |
| 114 | const int maxSize = 16; |
| 115 | const int size = 12; |
| 116 | |
Gael Guennebaud | 8a38047 | 2010-07-04 15:35:21 +0200 | [diff] [blame] | 117 | typedef Eigen::Matrix<Scalar, |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 118 | Eigen::Dynamic, Eigen::Dynamic, |
Benoit Jacob | b81351c | 2010-03-08 21:30:06 -0500 | [diff] [blame] | 119 | 0, |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 120 | maxSize, maxSize> Matrix; |
| 121 | |
Gael Guennebaud | 8a38047 | 2010-07-04 15:35:21 +0200 | [diff] [blame] | 122 | typedef Eigen::Matrix<Scalar, |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 123 | Eigen::Dynamic, 1, |
Benoit Jacob | b81351c | 2010-03-08 21:30:06 -0500 | [diff] [blame] | 124 | 0, |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 125 | maxSize, 1> Vector; |
| 126 | |
Gael Guennebaud | 8a38047 | 2010-07-04 15:35:21 +0200 | [diff] [blame] | 127 | typedef Eigen::Matrix<std::complex<Scalar>, |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 128 | Eigen::Dynamic, Eigen::Dynamic, |
Benoit Jacob | b81351c | 2010-03-08 21:30:06 -0500 | [diff] [blame] | 129 | 0, |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 130 | maxSize, maxSize> ComplexMatrix; |
| 131 | |
| 132 | const Matrix A(Matrix::Random(size, size)); |
| 133 | const ComplexMatrix complexA(ComplexMatrix::Random(size, size)); |
Gael Guennebaud | 5668674 | 2010-06-24 21:44:24 +0200 | [diff] [blame] | 134 | const Matrix saA = A.adjoint() * A; |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 135 | |
| 136 | // Cholesky module |
| 137 | Eigen::LLT<Matrix> LLT; LLT.compute(A); |
| 138 | Eigen::LDLT<Matrix> LDLT; LDLT.compute(A); |
| 139 | |
| 140 | // Eigenvalues module |
| 141 | Eigen::HessenbergDecomposition<ComplexMatrix> hessDecomp; hessDecomp.compute(complexA); |
| 142 | Eigen::ComplexSchur<ComplexMatrix> cSchur(size); cSchur.compute(complexA); |
Gael Guennebaud | 3d8e179 | 2011-01-27 18:02:49 +0100 | [diff] [blame] | 143 | Eigen::ComplexEigenSolver<ComplexMatrix> cEigSolver; cEigSolver.compute(complexA); |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 144 | Eigen::EigenSolver<Matrix> eigSolver; eigSolver.compute(A); |
| 145 | Eigen::SelfAdjointEigenSolver<Matrix> saEigSolver(size); saEigSolver.compute(saA); |
| 146 | Eigen::Tridiagonalization<Matrix> tridiag; tridiag.compute(saA); |
| 147 | |
| 148 | // LU module |
| 149 | Eigen::PartialPivLU<Matrix> ppLU; ppLU.compute(A); |
| 150 | Eigen::FullPivLU<Matrix> fpLU; fpLU.compute(A); |
| 151 | |
| 152 | // QR module |
| 153 | Eigen::HouseholderQR<Matrix> hQR; hQR.compute(A); |
| 154 | Eigen::ColPivHouseholderQR<Matrix> cpQR; cpQR.compute(A); |
| 155 | Eigen::FullPivHouseholderQR<Matrix> fpQR; fpQR.compute(A); |
| 156 | |
| 157 | // SVD module |
Benoit Jacob | 8eb0fc1 | 2010-10-12 10:19:59 -0400 | [diff] [blame] | 158 | Eigen::JacobiSVD<Matrix> jSVD; jSVD.compute(A, ComputeFullU | ComputeFullV); |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 161 | void test_nomalloc() |
| 162 | { |
| 163 | // check that our operator new is indeed called: |
Benoit Jacob | aaaade4 | 2010-05-30 16:00:58 -0400 | [diff] [blame] | 164 | VERIFY_RAISES_ASSERT(MatrixXd dummy(MatrixXd::Random(3,3))); |
Benoit Jacob | b81351c | 2010-03-08 21:30:06 -0500 | [diff] [blame] | 165 | CALL_SUBTEST_1(nomalloc(Matrix<float, 1, 1>()) ); |
| 166 | CALL_SUBTEST_2(nomalloc(Matrix4d()) ); |
| 167 | CALL_SUBTEST_3(nomalloc(Matrix<float,32,32>()) ); |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 168 | |
| 169 | // Check decomposition modules with dynamic matrices that have a known compile-time max size (ctms) |
Gael Guennebaud | 8a38047 | 2010-07-04 15:35:21 +0200 | [diff] [blame] | 170 | CALL_SUBTEST_4(ctms_decompositions<float>()); |
Adolfo Rodriguez Tsouroukdissian | 5a36f4a | 2010-03-08 19:31:27 +0100 | [diff] [blame] | 171 | |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 172 | } |