product_large.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "product.h"
  10. #include <Eigen/LU>
  11. template<typename T>
  12. void test_aliasing()
  13. {
  14. int rows = internal::random<int>(1,12);
  15. int cols = internal::random<int>(1,12);
  16. typedef Matrix<T,Dynamic,Dynamic> MatrixType;
  17. typedef Matrix<T,Dynamic,1> VectorType;
  18. VectorType x(cols); x.setRandom();
  19. VectorType z(x);
  20. VectorType y(rows); y.setZero();
  21. MatrixType A(rows,cols); A.setRandom();
  22. // CwiseBinaryOp
  23. VERIFY_IS_APPROX(x = y + A*x, A*z); // OK because "y + A*x" is marked as "assume-aliasing"
  24. x = z;
  25. // CwiseUnaryOp
  26. VERIFY_IS_APPROX(x = T(1.)*(A*x), A*z); // OK because 1*(A*x) is replaced by (1*A*x) which is a Product<> expression
  27. x = z;
  28. // VERIFY_IS_APPROX(x = y-A*x, -A*z); // Not OK in 3.3 because x is resized before A*x gets evaluated
  29. x = z;
  30. }
  31. template<int>
  32. void product_large_regressions()
  33. {
  34. {
  35. // test a specific issue in DiagonalProduct
  36. int N = 1000000;
  37. VectorXf v = VectorXf::Ones(N);
  38. MatrixXf m = MatrixXf::Ones(N,3);
  39. m = (v+v).asDiagonal() * m;
  40. VERIFY_IS_APPROX(m, MatrixXf::Constant(N,3,2));
  41. }
  42. {
  43. // test deferred resizing in Matrix::operator=
  44. MatrixXf a = MatrixXf::Random(10,4), b = MatrixXf::Random(4,10), c = a;
  45. VERIFY_IS_APPROX((a = a * b), (c * b).eval());
  46. }
  47. {
  48. // check the functions to setup blocking sizes compile and do not segfault
  49. // FIXME check they do what they are supposed to do !!
  50. std::ptrdiff_t l1 = internal::random<int>(10000,20000);
  51. std::ptrdiff_t l2 = internal::random<int>(100000,200000);
  52. std::ptrdiff_t l3 = internal::random<int>(1000000,2000000);
  53. setCpuCacheSizes(l1,l2,l3);
  54. VERIFY(l1==l1CacheSize());
  55. VERIFY(l2==l2CacheSize());
  56. std::ptrdiff_t k1 = internal::random<int>(10,100)*16;
  57. std::ptrdiff_t m1 = internal::random<int>(10,100)*16;
  58. std::ptrdiff_t n1 = internal::random<int>(10,100)*16;
  59. // only makes sure it compiles fine
  60. internal::computeProductBlockingSizes<float,float,std::ptrdiff_t>(k1,m1,n1,1);
  61. }
  62. {
  63. // test regression in row-vector by matrix (bad Map type)
  64. MatrixXf mat1(10,32); mat1.setRandom();
  65. MatrixXf mat2(32,32); mat2.setRandom();
  66. MatrixXf r1 = mat1.row(2)*mat2.transpose();
  67. VERIFY_IS_APPROX(r1, (mat1.row(2)*mat2.transpose()).eval());
  68. MatrixXf r2 = mat1.row(2)*mat2;
  69. VERIFY_IS_APPROX(r2, (mat1.row(2)*mat2).eval());
  70. }
  71. {
  72. Eigen::MatrixXd A(10,10), B, C;
  73. A.setRandom();
  74. C = A;
  75. for(int k=0; k<79; ++k)
  76. C = C * A;
  77. B.noalias() = (((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)) * ((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)))
  78. * (((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)) * ((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)));
  79. VERIFY_IS_APPROX(B,C);
  80. }
  81. }
  82. template<int>
  83. void bug_1622() {
  84. typedef Matrix<double, 2, -1, 0, 2, -1> Mat2X;
  85. Mat2X x(2,2); x.setRandom();
  86. MatrixXd y(2,2); y.setRandom();
  87. const Mat2X K1 = x * y.inverse();
  88. const Matrix2d K2 = x * y.inverse();
  89. VERIFY_IS_APPROX(K1,K2);
  90. }
  91. EIGEN_DECLARE_TEST(product_large)
  92. {
  93. for(int i = 0; i < g_repeat; i++) {
  94. CALL_SUBTEST_1( product(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  95. CALL_SUBTEST_2( product(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  96. CALL_SUBTEST_2( product(MatrixXd(internal::random<int>(1,10), internal::random<int>(1,10))) );
  97. CALL_SUBTEST_3( product(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  98. CALL_SUBTEST_4( product(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  99. CALL_SUBTEST_5( product(Matrix<float,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  100. CALL_SUBTEST_1( test_aliasing<float>() );
  101. CALL_SUBTEST_6( bug_1622<1>() );
  102. CALL_SUBTEST_7( product(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  103. CALL_SUBTEST_8( product(Matrix<double,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  104. CALL_SUBTEST_9( product(Matrix<std::complex<float>,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  105. CALL_SUBTEST_10( product(Matrix<std::complex<double>,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  106. }
  107. CALL_SUBTEST_6( product_large_regressions<0>() );
  108. // Regression test for bug 714:
  109. #if defined EIGEN_HAS_OPENMP
  110. omp_set_dynamic(1);
  111. for(int i = 0; i < g_repeat; i++) {
  112. CALL_SUBTEST_6( product(Matrix<float,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  113. }
  114. #endif
  115. }