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.

84 lines
3.1 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  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. Sum.h Difference.h Opposite.h ScalarMultiple.h
  14. */
  15. typedef typename MatrixType::Scalar Scalar;
  16. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  17. int rows = m.rows();
  18. int 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. mzero = MatrixType::Zero(rows, cols);
  25. Scalar s1 = ei_random<Scalar>();
  26. while (ei_abs(s1)<1e-3) s1 = ei_random<Scalar>();
  27. int r = ei_random<int>(0, rows-1),
  28. c = ei_random<int>(0, cols-1);
  29. VERIFY_IS_APPROX(-(-m1), m1);
  30. VERIFY_IS_APPROX(m1+m1, 2*m1);
  31. VERIFY_IS_APPROX(m1+m2-m1, m2);
  32. VERIFY_IS_APPROX(-m2+m1+m2, m1);
  33. VERIFY_IS_APPROX(m1*s1, s1*m1);
  34. VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
  35. VERIFY_IS_APPROX((-m1+m2)*s1, -s1*m1+s1*m2);
  36. m3 = m2; m3 += m1;
  37. VERIFY_IS_APPROX(m3, m1+m2);
  38. m3 = m2; m3 -= m1;
  39. VERIFY_IS_APPROX(m3, m2-m1);
  40. m3 = m2; m3 *= s1;
  41. VERIFY_IS_APPROX(m3, s1*m2);
  42. if(NumTraits<Scalar>::HasFloatingPoint)
  43. {
  44. m3 = m2; m3 /= s1;
  45. VERIFY_IS_APPROX(m3, m2/s1);
  46. }
  47. // again, test operator() to check const-qualification
  48. VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c)));
  49. VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c)));
  50. VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
  51. VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c)));
  52. VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1);
  53. if(NumTraits<Scalar>::HasFloatingPoint)
  54. VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1);
  55. // use .block to disable vectorization and compare to the vectorized version
  56. VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1);
  57. VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1);
  58. VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1);
  59. VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1);
  60. }
  61. void test_eigen2_linearstructure()
  62. {
  63. for(int i = 0; i < g_repeat; i++) {
  64. CALL_SUBTEST_1( linearStructure(Matrix<float, 1, 1>()) );
  65. CALL_SUBTEST_2( linearStructure(Matrix2f()) );
  66. CALL_SUBTEST_3( linearStructure(Vector3d()) );
  67. CALL_SUBTEST_4( linearStructure(Matrix4d()) );
  68. CALL_SUBTEST_5( linearStructure(MatrixXcf(3, 3)) );
  69. CALL_SUBTEST_6( linearStructure(MatrixXf(8, 12)) );
  70. CALL_SUBTEST_7( linearStructure(MatrixXi(8, 12)) );
  71. CALL_SUBTEST_8( linearStructure(MatrixXcd(20, 20)) );
  72. }
  73. }