You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
8.7 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. // discard stack allocation as that too bypasses malloc
  11. #define EIGEN_STACK_ALLOCATION_LIMIT 0
  12. // heap allocation will raise an assert if enabled at runtime
  13. #define EIGEN_RUNTIME_NO_MALLOC
  14. #include "main.h"
  15. #include <Eigen/Cholesky>
  16. #include <Eigen/Eigenvalues>
  17. #include <Eigen/LU>
  18. #include <Eigen/QR>
  19. #include <Eigen/SVD>
  20. template<typename MatrixType> void nomalloc(const MatrixType& m)
  21. {
  22. /* this test check no dynamic memory allocation are issued with fixed-size matrices
  23. */
  24. typedef typename MatrixType::Index Index;
  25. typedef typename MatrixType::Scalar Scalar;
  26. Index rows = m.rows();
  27. Index cols = m.cols();
  28. MatrixType m1 = MatrixType::Random(rows, cols),
  29. m2 = MatrixType::Random(rows, cols),
  30. m3(rows, cols);
  31. Scalar s1 = internal::random<Scalar>();
  32. Index r = internal::random<Index>(0, rows-1),
  33. c = internal::random<Index>(0, cols-1);
  34. VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
  35. VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
  36. VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), (m1.array()*m1.array()).matrix());
  37. VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2));
  38. m2.col(0).noalias() = m1 * m1.col(0);
  39. m2.col(0).noalias() -= m1.adjoint() * m1.col(0);
  40. m2.col(0).noalias() -= m1 * m1.row(0).adjoint();
  41. m2.col(0).noalias() -= m1.adjoint() * m1.row(0).adjoint();
  42. m2.row(0).noalias() = m1.row(0) * m1;
  43. m2.row(0).noalias() -= m1.row(0) * m1.adjoint();
  44. m2.row(0).noalias() -= m1.col(0).adjoint() * m1;
  45. m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint();
  46. VERIFY_IS_APPROX(m2,m2);
  47. m2.col(0).noalias() = m1.template triangularView<Upper>() * m1.col(0);
  48. m2.col(0).noalias() -= m1.adjoint().template triangularView<Upper>() * m1.col(0);
  49. m2.col(0).noalias() -= m1.template triangularView<Upper>() * m1.row(0).adjoint();
  50. m2.col(0).noalias() -= m1.adjoint().template triangularView<Upper>() * m1.row(0).adjoint();
  51. m2.row(0).noalias() = m1.row(0) * m1.template triangularView<Upper>();
  52. m2.row(0).noalias() -= m1.row(0) * m1.adjoint().template triangularView<Upper>();
  53. m2.row(0).noalias() -= m1.col(0).adjoint() * m1.template triangularView<Upper>();
  54. m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint().template triangularView<Upper>();
  55. VERIFY_IS_APPROX(m2,m2);
  56. m2.col(0).noalias() = m1.template selfadjointView<Upper>() * m1.col(0);
  57. m2.col(0).noalias() -= m1.adjoint().template selfadjointView<Upper>() * m1.col(0);
  58. m2.col(0).noalias() -= m1.template selfadjointView<Upper>() * m1.row(0).adjoint();
  59. m2.col(0).noalias() -= m1.adjoint().template selfadjointView<Upper>() * m1.row(0).adjoint();
  60. m2.row(0).noalias() = m1.row(0) * m1.template selfadjointView<Upper>();
  61. m2.row(0).noalias() -= m1.row(0) * m1.adjoint().template selfadjointView<Upper>();
  62. m2.row(0).noalias() -= m1.col(0).adjoint() * m1.template selfadjointView<Upper>();
  63. m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint().template selfadjointView<Upper>();
  64. VERIFY_IS_APPROX(m2,m2);
  65. m2.template selfadjointView<Lower>().rankUpdate(m1.col(0),-1);
  66. m2.template selfadjointView<Lower>().rankUpdate(m1.row(0),-1);
  67. // The following fancy matrix-matrix products are not safe yet regarding static allocation
  68. // m1 += m1.template triangularView<Upper>() * m2.col(;
  69. // m1.template selfadjointView<Lower>().rankUpdate(m2);
  70. // m1 += m1.template triangularView<Upper>() * m2;
  71. // m1 += m1.template selfadjointView<Lower>() * m2;
  72. // VERIFY_IS_APPROX(m1,m1);
  73. }
  74. template<typename Scalar>
  75. void ctms_decompositions()
  76. {
  77. const int maxSize = 16;
  78. const int size = 12;
  79. typedef StormEigen::Matrix<Scalar,
  80. StormEigen::Dynamic, StormEigen::Dynamic,
  81. 0,
  82. maxSize, maxSize> Matrix;
  83. typedef StormEigen::Matrix<Scalar,
  84. StormEigen::Dynamic, 1,
  85. 0,
  86. maxSize, 1> Vector;
  87. typedef StormEigen::Matrix<std::complex<Scalar>,
  88. StormEigen::Dynamic, StormEigen::Dynamic,
  89. 0,
  90. maxSize, maxSize> ComplexMatrix;
  91. const Matrix A(Matrix::Random(size, size)), B(Matrix::Random(size, size));
  92. Matrix X(size,size);
  93. const ComplexMatrix complexA(ComplexMatrix::Random(size, size));
  94. const Matrix saA = A.adjoint() * A;
  95. const Vector b(Vector::Random(size));
  96. Vector x(size);
  97. // Cholesky module
  98. StormEigen::LLT<Matrix> LLT; LLT.compute(A);
  99. X = LLT.solve(B);
  100. x = LLT.solve(b);
  101. StormEigen::LDLT<Matrix> LDLT; LDLT.compute(A);
  102. X = LDLT.solve(B);
  103. x = LDLT.solve(b);
  104. // Eigenvalues module
  105. StormEigen::HessenbergDecomposition<ComplexMatrix> hessDecomp; hessDecomp.compute(complexA);
  106. StormEigen::ComplexSchur<ComplexMatrix> cSchur(size); cSchur.compute(complexA);
  107. StormEigen::ComplexEigenSolver<ComplexMatrix> cEigSolver; cEigSolver.compute(complexA);
  108. StormEigen::EigenSolver<Matrix> eigSolver; eigSolver.compute(A);
  109. StormEigen::SelfAdjointEigenSolver<Matrix> saEigSolver(size); saEigSolver.compute(saA);
  110. StormEigen::Tridiagonalization<Matrix> tridiag; tridiag.compute(saA);
  111. // LU module
  112. StormEigen::PartialPivLU<Matrix> ppLU; ppLU.compute(A);
  113. X = ppLU.solve(B);
  114. x = ppLU.solve(b);
  115. StormEigen::FullPivLU<Matrix> fpLU; fpLU.compute(A);
  116. X = fpLU.solve(B);
  117. x = fpLU.solve(b);
  118. // QR module
  119. StormEigen::HouseholderQR<Matrix> hQR; hQR.compute(A);
  120. X = hQR.solve(B);
  121. x = hQR.solve(b);
  122. StormEigen::ColPivHouseholderQR<Matrix> cpQR; cpQR.compute(A);
  123. X = cpQR.solve(B);
  124. x = cpQR.solve(b);
  125. StormEigen::FullPivHouseholderQR<Matrix> fpQR; fpQR.compute(A);
  126. // FIXME X = fpQR.solve(B);
  127. x = fpQR.solve(b);
  128. // SVD module
  129. StormEigen::JacobiSVD<Matrix> jSVD; jSVD.compute(A, ComputeFullU | ComputeFullV);
  130. }
  131. void test_zerosized() {
  132. // default constructors:
  133. StormEigen::MatrixXd A;
  134. StormEigen::VectorXd v;
  135. // explicit zero-sized:
  136. StormEigen::ArrayXXd A0(0,0);
  137. StormEigen::ArrayXd v0(0);
  138. // assigning empty objects to each other:
  139. A=A0;
  140. v=v0;
  141. }
  142. template<typename MatrixType> void test_reference(const MatrixType& m) {
  143. typedef typename MatrixType::Scalar Scalar;
  144. enum { Flag = MatrixType::IsRowMajor ? StormEigen::RowMajor : StormEigen::ColMajor};
  145. enum { TransposeFlag = !MatrixType::IsRowMajor ? StormEigen::RowMajor : StormEigen::ColMajor};
  146. typename MatrixType::Index rows = m.rows(), cols=m.cols();
  147. typedef StormEigen::Matrix<Scalar, StormEigen::Dynamic, Eigen::Dynamic, Flag > MatrixX;
  148. typedef StormEigen::Matrix<Scalar, StormEigen::Dynamic, Eigen::Dynamic, TransposeFlag> MatrixXT;
  149. // Dynamic reference:
  150. typedef StormEigen::Ref<const MatrixX > Ref;
  151. typedef StormEigen::Ref<const MatrixXT > RefT;
  152. Ref r1(m);
  153. Ref r2(m.block(rows/3, cols/4, rows/2, cols/2));
  154. RefT r3(m.transpose());
  155. RefT r4(m.topLeftCorner(rows/2, cols/2).transpose());
  156. VERIFY_RAISES_ASSERT(RefT r5(m));
  157. VERIFY_RAISES_ASSERT(Ref r6(m.transpose()));
  158. VERIFY_RAISES_ASSERT(Ref r7(Scalar(2) * m));
  159. // Copy constructors shall also never malloc
  160. Ref r8 = r1;
  161. RefT r9 = r3;
  162. // Initializing from a compatible Ref shall also never malloc
  163. StormEigen::Ref<const MatrixX, Unaligned, Stride<Dynamic, Dynamic> > r10=r8, r11=m;
  164. // Initializing from an incompatible Ref will malloc:
  165. typedef StormEigen::Ref<const MatrixX, Aligned> RefAligned;
  166. VERIFY_RAISES_ASSERT(RefAligned r12=r10);
  167. VERIFY_RAISES_ASSERT(Ref r13=r10); // r10 has more dynamic strides
  168. }
  169. void test_nomalloc()
  170. {
  171. // create some dynamic objects
  172. StormEigen::MatrixXd M1 = MatrixXd::Random(3,3);
  173. Ref<const MatrixXd> R1 = 2.0*M1; // Ref requires temporary
  174. // from here on prohibit malloc:
  175. StormEigen::internal::set_is_malloc_allowed(false);
  176. // check that our operator new is indeed called:
  177. VERIFY_RAISES_ASSERT(MatrixXd dummy(MatrixXd::Random(3,3)));
  178. CALL_SUBTEST_1(nomalloc(Matrix<float, 1, 1>()) );
  179. CALL_SUBTEST_2(nomalloc(Matrix4d()) );
  180. CALL_SUBTEST_3(nomalloc(Matrix<float,32,32>()) );
  181. // Check decomposition modules with dynamic matrices that have a known compile-time max size (ctms)
  182. CALL_SUBTEST_4(ctms_decompositions<float>());
  183. CALL_SUBTEST_5(test_zerosized());
  184. CALL_SUBTEST_6(test_reference(Matrix<float,32,32>()));
  185. CALL_SUBTEST_7(test_reference(R1));
  186. CALL_SUBTEST_8(Ref<MatrixXd> R2 = M1.topRows<2>(); test_reference(R2));
  187. }