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.

132 lines
5.3 KiB

  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. // Copyright (C) 2014 Gael Guennebaud <gael.guennebaud@inria.fr>
  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. static bool g_called;
  11. #define EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN { g_called = true; }
  12. #include "main.h"
  13. template<typename MatrixType> void linearStructure(const MatrixType& m)
  14. {
  15. using std::abs;
  16. /* this test covers the following files:
  17. CwiseUnaryOp.h, CwiseBinaryOp.h, SelfCwiseBinaryOp.h
  18. */
  19. typedef typename MatrixType::Index Index;
  20. typedef typename MatrixType::Scalar Scalar;
  21. Index rows = m.rows();
  22. Index cols = m.cols();
  23. // this test relies a lot on Random.h, and there's not much more that we can do
  24. // to test it, hence I consider that we will have tested Random.h
  25. MatrixType m1 = MatrixType::Random(rows, cols),
  26. m2 = MatrixType::Random(rows, cols),
  27. m3(rows, cols);
  28. Scalar s1 = internal::random<Scalar>();
  29. while (abs(s1)<1e-3) s1 = internal::random<Scalar>();
  30. Index r = internal::random<Index>(0, rows-1),
  31. c = internal::random<Index>(0, cols-1);
  32. VERIFY_IS_APPROX(-(-m1), m1);
  33. VERIFY_IS_APPROX(m1+m1, 2*m1);
  34. VERIFY_IS_APPROX(m1+m2-m1, m2);
  35. VERIFY_IS_APPROX(-m2+m1+m2, m1);
  36. VERIFY_IS_APPROX(m1*s1, s1*m1);
  37. VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
  38. VERIFY_IS_APPROX((-m1+m2)*s1, -s1*m1+s1*m2);
  39. m3 = m2; m3 += m1;
  40. VERIFY_IS_APPROX(m3, m1+m2);
  41. m3 = m2; m3 -= m1;
  42. VERIFY_IS_APPROX(m3, m2-m1);
  43. m3 = m2; m3 *= s1;
  44. VERIFY_IS_APPROX(m3, s1*m2);
  45. if(!NumTraits<Scalar>::IsInteger)
  46. {
  47. m3 = m2; m3 /= s1;
  48. VERIFY_IS_APPROX(m3, m2/s1);
  49. }
  50. // again, test operator() to check const-qualification
  51. VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c)));
  52. VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c)));
  53. VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
  54. VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c)));
  55. VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1);
  56. if(!NumTraits<Scalar>::IsInteger)
  57. VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1);
  58. // use .block to disable vectorization and compare to the vectorized version
  59. VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1);
  60. VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), m1.cwiseProduct(m1));
  61. VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1);
  62. VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1);
  63. }
  64. // Make sure that complex * real and real * complex are properly optimized
  65. template<typename MatrixType> void real_complex(DenseIndex rows = MatrixType::RowsAtCompileTime, DenseIndex cols = MatrixType::ColsAtCompileTime)
  66. {
  67. typedef typename MatrixType::Scalar Scalar;
  68. typedef typename MatrixType::RealScalar RealScalar;
  69. RealScalar s = internal::random<RealScalar>();
  70. MatrixType m1 = MatrixType::Random(rows, cols);
  71. g_called = false;
  72. VERIFY_IS_APPROX(s*m1, Scalar(s)*m1);
  73. VERIFY(g_called && "real * matrix<complex> not properly optimized");
  74. g_called = false;
  75. VERIFY_IS_APPROX(m1*s, m1*Scalar(s));
  76. VERIFY(g_called && "matrix<complex> * real not properly optimized");
  77. g_called = false;
  78. VERIFY_IS_APPROX(m1/s, m1/Scalar(s));
  79. VERIFY(g_called && "matrix<complex> / real not properly optimized");
  80. }
  81. void test_linearstructure()
  82. {
  83. g_called = true;
  84. VERIFY(g_called); // avoid `unneeded-internal-declaration` warning.
  85. for(int i = 0; i < g_repeat; i++) {
  86. CALL_SUBTEST_1( linearStructure(Matrix<float, 1, 1>()) );
  87. CALL_SUBTEST_2( linearStructure(Matrix2f()) );
  88. CALL_SUBTEST_3( linearStructure(Vector3d()) );
  89. CALL_SUBTEST_4( linearStructure(Matrix4d()) );
  90. CALL_SUBTEST_5( linearStructure(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  91. CALL_SUBTEST_6( linearStructure(MatrixXf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  92. CALL_SUBTEST_7( linearStructure(MatrixXi (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  93. CALL_SUBTEST_8( linearStructure(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  94. CALL_SUBTEST_9( linearStructure(ArrayXXf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  95. CALL_SUBTEST_10( linearStructure(ArrayXXcf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  96. CALL_SUBTEST_11( real_complex<Matrix4cd>() );
  97. CALL_SUBTEST_11( real_complex<MatrixXcf>(10,10) );
  98. CALL_SUBTEST_11( real_complex<ArrayXXcf>(10,10) );
  99. }
  100. #ifdef EIGEN_TEST_PART_4
  101. {
  102. // make sure that /=scalar and /scalar do not overflow
  103. // rational: 1.0/4.94e-320 overflow, but m/4.94e-320 should not
  104. Matrix4d m2, m3;
  105. m3 = m2 = Matrix4d::Random()*1e-20;
  106. m2 = m2 / 4.9e-320;
  107. VERIFY_IS_APPROX(m2.cwiseQuotient(m2), Matrix4d::Ones());
  108. m3 /= 4.9e-320;
  109. VERIFY_IS_APPROX(m3.cwiseQuotient(m3), Matrix4d::Ones());
  110. }
  111. #endif
  112. }