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.

83 lines
3.6 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. //
  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 "main.h"
  10. template<typename MatrixType> void linearStructure(const MatrixType& m)
  11. {
  12. /* this test covers the following files:
  13. CwiseUnaryOp.h, CwiseBinaryOp.h, SelfCwiseBinaryOp.h
  14. */
  15. typedef typename MatrixType::Index Index;
  16. typedef typename MatrixType::Scalar Scalar;
  17. Index rows = m.rows();
  18. Index cols = m.cols();
  19. // this test relies a lot on Random.h, and there's not much more that we can do
  20. // to test it, hence I consider that we will have tested Random.h
  21. MatrixType m1 = MatrixType::Random(rows, cols),
  22. m2 = MatrixType::Random(rows, cols),
  23. m3(rows, cols);
  24. Scalar s1 = internal::random<Scalar>();
  25. while (internal::abs(s1)<1e-3) s1 = internal::random<Scalar>();
  26. Index r = internal::random<Index>(0, rows-1),
  27. c = internal::random<Index>(0, cols-1);
  28. VERIFY_IS_APPROX(-(-m1), m1);
  29. VERIFY_IS_APPROX(m1+m1, 2*m1);
  30. VERIFY_IS_APPROX(m1+m2-m1, m2);
  31. VERIFY_IS_APPROX(-m2+m1+m2, m1);
  32. VERIFY_IS_APPROX(m1*s1, s1*m1);
  33. VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
  34. VERIFY_IS_APPROX((-m1+m2)*s1, -s1*m1+s1*m2);
  35. m3 = m2; m3 += m1;
  36. VERIFY_IS_APPROX(m3, m1+m2);
  37. m3 = m2; m3 -= m1;
  38. VERIFY_IS_APPROX(m3, m2-m1);
  39. m3 = m2; m3 *= s1;
  40. VERIFY_IS_APPROX(m3, s1*m2);
  41. if(!NumTraits<Scalar>::IsInteger)
  42. {
  43. m3 = m2; m3 /= s1;
  44. VERIFY_IS_APPROX(m3, m2/s1);
  45. }
  46. // again, test operator() to check const-qualification
  47. VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c)));
  48. VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c)));
  49. VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
  50. VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c)));
  51. VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1);
  52. if(!NumTraits<Scalar>::IsInteger)
  53. VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1);
  54. // use .block to disable vectorization and compare to the vectorized version
  55. VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1);
  56. VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), m1.cwiseProduct(m1));
  57. VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1);
  58. VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1);
  59. }
  60. void test_linearstructure()
  61. {
  62. for(int i = 0; i < g_repeat; i++) {
  63. CALL_SUBTEST_1( linearStructure(Matrix<float, 1, 1>()) );
  64. CALL_SUBTEST_2( linearStructure(Matrix2f()) );
  65. CALL_SUBTEST_3( linearStructure(Vector3d()) );
  66. CALL_SUBTEST_4( linearStructure(Matrix4d()) );
  67. CALL_SUBTEST_5( linearStructure(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  68. CALL_SUBTEST_6( linearStructure(MatrixXf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  69. CALL_SUBTEST_7( linearStructure(MatrixXi (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  70. CALL_SUBTEST_8( linearStructure(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  71. CALL_SUBTEST_9( linearStructure(ArrayXXf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  72. }
  73. }