// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008 Gael Guennebaud // Copyright (C) 2006-2008 Benoit Jacob // // 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/. // discard stack allocation as that too bypasses malloc #define EIGEN_STACK_ALLOCATION_LIMIT 0 // heap allocation will raise an assert if enabled at runtime #define EIGEN_RUNTIME_NO_MALLOC #include "main.h" #include #include #include #include #include template void nomalloc(const MatrixType& m) { /* this test check no dynamic memory allocation are issued with fixed-size matrices */ typedef typename MatrixType::Index Index; typedef typename MatrixType::Scalar Scalar; Index rows = m.rows(); Index cols = m.cols(); MatrixType m1 = MatrixType::Random(rows, cols), m2 = MatrixType::Random(rows, cols), m3(rows, cols); Scalar s1 = internal::random(); Index r = internal::random(0, rows-1), c = internal::random(0, cols-1); VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2); VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c))); VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), (m1.array()*m1.array()).matrix()); VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2)); m2.col(0).noalias() = m1 * m1.col(0); m2.col(0).noalias() -= m1.adjoint() * m1.col(0); m2.col(0).noalias() -= m1 * m1.row(0).adjoint(); m2.col(0).noalias() -= m1.adjoint() * m1.row(0).adjoint(); m2.row(0).noalias() = m1.row(0) * m1; m2.row(0).noalias() -= m1.row(0) * m1.adjoint(); m2.row(0).noalias() -= m1.col(0).adjoint() * m1; m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint(); VERIFY_IS_APPROX(m2,m2); m2.col(0).noalias() = m1.template triangularView() * m1.col(0); m2.col(0).noalias() -= m1.adjoint().template triangularView() * m1.col(0); m2.col(0).noalias() -= m1.template triangularView() * m1.row(0).adjoint(); m2.col(0).noalias() -= m1.adjoint().template triangularView() * m1.row(0).adjoint(); m2.row(0).noalias() = m1.row(0) * m1.template triangularView(); m2.row(0).noalias() -= m1.row(0) * m1.adjoint().template triangularView(); m2.row(0).noalias() -= m1.col(0).adjoint() * m1.template triangularView(); m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint().template triangularView(); VERIFY_IS_APPROX(m2,m2); m2.col(0).noalias() = m1.template selfadjointView() * m1.col(0); m2.col(0).noalias() -= m1.adjoint().template selfadjointView() * m1.col(0); m2.col(0).noalias() -= m1.template selfadjointView() * m1.row(0).adjoint(); m2.col(0).noalias() -= m1.adjoint().template selfadjointView() * m1.row(0).adjoint(); m2.row(0).noalias() = m1.row(0) * m1.template selfadjointView(); m2.row(0).noalias() -= m1.row(0) * m1.adjoint().template selfadjointView(); m2.row(0).noalias() -= m1.col(0).adjoint() * m1.template selfadjointView(); m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint().template selfadjointView(); VERIFY_IS_APPROX(m2,m2); m2.template selfadjointView().rankUpdate(m1.col(0),-1); m2.template selfadjointView().rankUpdate(m1.row(0),-1); // The following fancy matrix-matrix products are not safe yet regarding static allocation // m1 += m1.template triangularView() * m2.col(; // m1.template selfadjointView().rankUpdate(m2); // m1 += m1.template triangularView() * m2; // m1 += m1.template selfadjointView() * m2; // VERIFY_IS_APPROX(m1,m1); } template void ctms_decompositions() { const int maxSize = 16; const int size = 12; typedef StormEigen::Matrix Matrix; typedef StormEigen::Matrix Vector; typedef StormEigen::Matrix, StormEigen::Dynamic, StormEigen::Dynamic, 0, maxSize, maxSize> ComplexMatrix; const Matrix A(Matrix::Random(size, size)), B(Matrix::Random(size, size)); Matrix X(size,size); const ComplexMatrix complexA(ComplexMatrix::Random(size, size)); const Matrix saA = A.adjoint() * A; const Vector b(Vector::Random(size)); Vector x(size); // Cholesky module StormEigen::LLT LLT; LLT.compute(A); X = LLT.solve(B); x = LLT.solve(b); StormEigen::LDLT LDLT; LDLT.compute(A); X = LDLT.solve(B); x = LDLT.solve(b); // Eigenvalues module StormEigen::HessenbergDecomposition hessDecomp; hessDecomp.compute(complexA); StormEigen::ComplexSchur cSchur(size); cSchur.compute(complexA); StormEigen::ComplexEigenSolver cEigSolver; cEigSolver.compute(complexA); StormEigen::EigenSolver eigSolver; eigSolver.compute(A); StormEigen::SelfAdjointEigenSolver saEigSolver(size); saEigSolver.compute(saA); StormEigen::Tridiagonalization tridiag; tridiag.compute(saA); // LU module StormEigen::PartialPivLU ppLU; ppLU.compute(A); X = ppLU.solve(B); x = ppLU.solve(b); StormEigen::FullPivLU fpLU; fpLU.compute(A); X = fpLU.solve(B); x = fpLU.solve(b); // QR module StormEigen::HouseholderQR hQR; hQR.compute(A); X = hQR.solve(B); x = hQR.solve(b); StormEigen::ColPivHouseholderQR cpQR; cpQR.compute(A); X = cpQR.solve(B); x = cpQR.solve(b); StormEigen::FullPivHouseholderQR fpQR; fpQR.compute(A); // FIXME X = fpQR.solve(B); x = fpQR.solve(b); // SVD module StormEigen::JacobiSVD jSVD; jSVD.compute(A, ComputeFullU | ComputeFullV); } void test_zerosized() { // default constructors: StormEigen::MatrixXd A; StormEigen::VectorXd v; // explicit zero-sized: StormEigen::ArrayXXd A0(0,0); StormEigen::ArrayXd v0(0); // assigning empty objects to each other: A=A0; v=v0; } template void test_reference(const MatrixType& m) { typedef typename MatrixType::Scalar Scalar; enum { Flag = MatrixType::IsRowMajor ? StormEigen::RowMajor : StormEigen::ColMajor}; enum { TransposeFlag = !MatrixType::IsRowMajor ? StormEigen::RowMajor : StormEigen::ColMajor}; typename MatrixType::Index rows = m.rows(), cols=m.cols(); typedef StormEigen::Matrix MatrixX; typedef StormEigen::Matrix MatrixXT; // Dynamic reference: typedef StormEigen::Ref Ref; typedef StormEigen::Ref RefT; Ref r1(m); Ref r2(m.block(rows/3, cols/4, rows/2, cols/2)); RefT r3(m.transpose()); RefT r4(m.topLeftCorner(rows/2, cols/2).transpose()); VERIFY_RAISES_ASSERT(RefT r5(m)); VERIFY_RAISES_ASSERT(Ref r6(m.transpose())); VERIFY_RAISES_ASSERT(Ref r7(Scalar(2) * m)); // Copy constructors shall also never malloc Ref r8 = r1; RefT r9 = r3; // Initializing from a compatible Ref shall also never malloc StormEigen::Ref > r10=r8, r11=m; // Initializing from an incompatible Ref will malloc: typedef StormEigen::Ref RefAligned; VERIFY_RAISES_ASSERT(RefAligned r12=r10); VERIFY_RAISES_ASSERT(Ref r13=r10); // r10 has more dynamic strides } void test_nomalloc() { // create some dynamic objects StormEigen::MatrixXd M1 = MatrixXd::Random(3,3); Ref R1 = 2.0*M1; // Ref requires temporary // from here on prohibit malloc: StormEigen::internal::set_is_malloc_allowed(false); // check that our operator new is indeed called: VERIFY_RAISES_ASSERT(MatrixXd dummy(MatrixXd::Random(3,3))); CALL_SUBTEST_1(nomalloc(Matrix()) ); CALL_SUBTEST_2(nomalloc(Matrix4d()) ); CALL_SUBTEST_3(nomalloc(Matrix()) ); // Check decomposition modules with dynamic matrices that have a known compile-time max size (ctms) CALL_SUBTEST_4(ctms_decompositions()); CALL_SUBTEST_5(test_zerosized()); CALL_SUBTEST_6(test_reference(Matrix())); CALL_SUBTEST_7(test_reference(R1)); CALL_SUBTEST_8(Ref R2 = M1.topRows<2>(); test_reference(R2)); }