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.

108 lines
3.9 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 basicStuff(const MatrixType& m)
  11. {
  12. typedef typename MatrixType::Scalar Scalar;
  13. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  14. int rows = m.rows();
  15. int cols = m.cols();
  16. // this test relies a lot on Random.h, and there's not much more that we can do
  17. // to test it, hence I consider that we will have tested Random.h
  18. MatrixType m1 = MatrixType::Random(rows, cols),
  19. m2 = MatrixType::Random(rows, cols),
  20. m3(rows, cols),
  21. mzero = MatrixType::Zero(rows, cols),
  22. identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
  23. ::Identity(rows, rows),
  24. square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
  25. VectorType v1 = VectorType::Random(rows),
  26. v2 = VectorType::Random(rows),
  27. vzero = VectorType::Zero(rows);
  28. Scalar x = ei_random<Scalar>();
  29. int r = ei_random<int>(0, rows-1),
  30. c = ei_random<int>(0, cols-1);
  31. m1.coeffRef(r,c) = x;
  32. VERIFY_IS_APPROX(x, m1.coeff(r,c));
  33. m1(r,c) = x;
  34. VERIFY_IS_APPROX(x, m1(r,c));
  35. v1.coeffRef(r) = x;
  36. VERIFY_IS_APPROX(x, v1.coeff(r));
  37. v1(r) = x;
  38. VERIFY_IS_APPROX(x, v1(r));
  39. v1[r] = x;
  40. VERIFY_IS_APPROX(x, v1[r]);
  41. VERIFY_IS_APPROX( v1, v1);
  42. VERIFY_IS_NOT_APPROX( v1, 2*v1);
  43. VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1);
  44. if(NumTraits<Scalar>::HasFloatingPoint)
  45. VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.norm());
  46. VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1, v1);
  47. VERIFY_IS_APPROX( vzero, v1-v1);
  48. VERIFY_IS_APPROX( m1, m1);
  49. VERIFY_IS_NOT_APPROX( m1, 2*m1);
  50. VERIFY_IS_MUCH_SMALLER_THAN( mzero, m1);
  51. VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1, m1);
  52. VERIFY_IS_APPROX( mzero, m1-m1);
  53. // always test operator() on each read-only expression class,
  54. // in order to check const-qualifiers.
  55. // indeed, if an expression class (here Zero) is meant to be read-only,
  56. // hence has no _write() method, the corresponding MatrixBase method (here zero())
  57. // should return a const-qualified object so that it is the const-qualified
  58. // operator() that gets called, which in turn calls _read().
  59. VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1));
  60. // now test copying a row-vector into a (column-)vector and conversely.
  61. square.col(r) = square.row(r).eval();
  62. Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows);
  63. Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows);
  64. rv = square.row(r);
  65. cv = square.col(r);
  66. VERIFY_IS_APPROX(rv, cv.transpose());
  67. if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
  68. {
  69. VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
  70. }
  71. VERIFY_IS_APPROX(m3 = m1,m1);
  72. MatrixType m4;
  73. VERIFY_IS_APPROX(m4 = m1,m1);
  74. // test swap
  75. m3 = m1;
  76. m1.swap(m2);
  77. VERIFY_IS_APPROX(m3, m2);
  78. if(rows*cols>=3)
  79. {
  80. VERIFY_IS_NOT_APPROX(m3, m1);
  81. }
  82. }
  83. void test_eigen2_basicstuff()
  84. {
  85. for(int i = 0; i < g_repeat; i++) {
  86. CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
  87. CALL_SUBTEST_2( basicStuff(Matrix4d()) );
  88. CALL_SUBTEST_3( basicStuff(MatrixXcf(3, 3)) );
  89. CALL_SUBTEST_4( basicStuff(MatrixXi(8, 12)) );
  90. CALL_SUBTEST_5( basicStuff(MatrixXcd(20, 20)) );
  91. CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) );
  92. CALL_SUBTEST_7( basicStuff(Matrix<long double,Dynamic,Dynamic>(10,10)) );
  93. }
  94. }