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.

214 lines
7.7 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. #define EIGEN_NO_STATIC_ASSERT
  10. #include "main.h"
  11. template<typename MatrixType> void basicStuff(const MatrixType& m)
  12. {
  13. typedef typename MatrixType::Index Index;
  14. typedef typename MatrixType::Scalar Scalar;
  15. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  16. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
  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. mzero = MatrixType::Zero(rows, cols),
  25. square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
  26. VectorType v1 = VectorType::Random(rows),
  27. vzero = VectorType::Zero(rows);
  28. SquareMatrixType sm1 = SquareMatrixType::Random(rows,rows), sm2(rows,rows);
  29. Scalar x = 0;
  30. while(x == Scalar(0)) x = internal::random<Scalar>();
  31. Index r = internal::random<Index>(0, rows-1),
  32. c = internal::random<Index>(0, cols-1);
  33. m1.coeffRef(r,c) = x;
  34. VERIFY_IS_APPROX(x, m1.coeff(r,c));
  35. m1(r,c) = x;
  36. VERIFY_IS_APPROX(x, m1(r,c));
  37. v1.coeffRef(r) = x;
  38. VERIFY_IS_APPROX(x, v1.coeff(r));
  39. v1(r) = x;
  40. VERIFY_IS_APPROX(x, v1(r));
  41. v1[r] = x;
  42. VERIFY_IS_APPROX(x, v1[r]);
  43. VERIFY_IS_APPROX( v1, v1);
  44. VERIFY_IS_NOT_APPROX( v1, 2*v1);
  45. VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1);
  46. VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.squaredNorm());
  47. VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1, v1);
  48. VERIFY_IS_APPROX( vzero, v1-v1);
  49. VERIFY_IS_APPROX( m1, m1);
  50. VERIFY_IS_NOT_APPROX( m1, 2*m1);
  51. VERIFY_IS_MUCH_SMALLER_THAN( mzero, m1);
  52. VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1, m1);
  53. VERIFY_IS_APPROX( mzero, m1-m1);
  54. // always test operator() on each read-only expression class,
  55. // in order to check const-qualifiers.
  56. // indeed, if an expression class (here Zero) is meant to be read-only,
  57. // hence has no _write() method, the corresponding MatrixBase method (here zero())
  58. // should return a const-qualified object so that it is the const-qualified
  59. // operator() that gets called, which in turn calls _read().
  60. VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1));
  61. // now test copying a row-vector into a (column-)vector and conversely.
  62. square.col(r) = square.row(r).eval();
  63. Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows);
  64. Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows);
  65. rv = square.row(r);
  66. cv = square.col(r);
  67. VERIFY_IS_APPROX(rv, cv.transpose());
  68. if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
  69. {
  70. VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
  71. }
  72. if(cols!=1 && rows!=1)
  73. {
  74. VERIFY_RAISES_ASSERT(m1[0]);
  75. VERIFY_RAISES_ASSERT((m1+m1)[0]);
  76. }
  77. VERIFY_IS_APPROX(m3 = m1,m1);
  78. MatrixType m4;
  79. VERIFY_IS_APPROX(m4 = m1,m1);
  80. m3.real() = m1.real();
  81. VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
  82. VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
  83. // check == / != operators
  84. VERIFY(m1==m1);
  85. VERIFY(m1!=m2);
  86. VERIFY(!(m1==m2));
  87. VERIFY(!(m1!=m1));
  88. m1 = m2;
  89. VERIFY(m1==m2);
  90. VERIFY(!(m1!=m2));
  91. // check automatic transposition
  92. sm2.setZero();
  93. for(typename MatrixType::Index i=0;i<rows;++i)
  94. sm2.col(i) = sm1.row(i);
  95. VERIFY_IS_APPROX(sm2,sm1.transpose());
  96. sm2.setZero();
  97. for(typename MatrixType::Index i=0;i<rows;++i)
  98. sm2.col(i).noalias() = sm1.row(i);
  99. VERIFY_IS_APPROX(sm2,sm1.transpose());
  100. sm2.setZero();
  101. for(typename MatrixType::Index i=0;i<rows;++i)
  102. sm2.col(i).noalias() += sm1.row(i);
  103. VERIFY_IS_APPROX(sm2,sm1.transpose());
  104. sm2.setZero();
  105. for(typename MatrixType::Index i=0;i<rows;++i)
  106. sm2.col(i).noalias() -= sm1.row(i);
  107. VERIFY_IS_APPROX(sm2,-sm1.transpose());
  108. }
  109. template<typename MatrixType> void basicStuffComplex(const MatrixType& m)
  110. {
  111. typedef typename MatrixType::Index Index;
  112. typedef typename MatrixType::Scalar Scalar;
  113. typedef typename NumTraits<Scalar>::Real RealScalar;
  114. typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> RealMatrixType;
  115. Index rows = m.rows();
  116. Index cols = m.cols();
  117. Scalar s1 = internal::random<Scalar>(),
  118. s2 = internal::random<Scalar>();
  119. VERIFY(numext::real(s1)==numext::real_ref(s1));
  120. VERIFY(numext::imag(s1)==numext::imag_ref(s1));
  121. numext::real_ref(s1) = numext::real(s2);
  122. numext::imag_ref(s1) = numext::imag(s2);
  123. VERIFY(internal::isApprox(s1, s2, NumTraits<RealScalar>::epsilon()));
  124. // extended precision in Intel FPUs means that s1 == s2 in the line above is not guaranteed.
  125. RealMatrixType rm1 = RealMatrixType::Random(rows,cols),
  126. rm2 = RealMatrixType::Random(rows,cols);
  127. MatrixType cm(rows,cols);
  128. cm.real() = rm1;
  129. cm.imag() = rm2;
  130. VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
  131. VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
  132. rm1.setZero();
  133. rm2.setZero();
  134. rm1 = cm.real();
  135. rm2 = cm.imag();
  136. VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
  137. VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
  138. cm.real().setZero();
  139. VERIFY(static_cast<const MatrixType&>(cm).real().isZero());
  140. VERIFY(!static_cast<const MatrixType&>(cm).imag().isZero());
  141. }
  142. #ifdef EIGEN_TEST_PART_2
  143. void casting()
  144. {
  145. Matrix4f m = Matrix4f::Random(), m2;
  146. Matrix4d n = m.cast<double>();
  147. VERIFY(m.isApprox(n.cast<float>()));
  148. m2 = m.cast<float>(); // check the specialization when NewType == Type
  149. VERIFY(m.isApprox(m2));
  150. }
  151. #endif
  152. template <typename Scalar>
  153. void fixedSizeMatrixConstruction()
  154. {
  155. const Scalar raw[3] = {1,2,3};
  156. Matrix<Scalar,3,1> m(raw);
  157. Array<Scalar,3,1> a(raw);
  158. VERIFY(m(0) == 1);
  159. VERIFY(m(1) == 2);
  160. VERIFY(m(2) == 3);
  161. VERIFY(a(0) == 1);
  162. VERIFY(a(1) == 2);
  163. VERIFY(a(2) == 3);
  164. }
  165. void test_basicstuff()
  166. {
  167. for(int i = 0; i < g_repeat; i++) {
  168. CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
  169. CALL_SUBTEST_2( basicStuff(Matrix4d()) );
  170. CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  171. CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  172. CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  173. CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) );
  174. CALL_SUBTEST_7( basicStuff(Matrix<long double,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  175. CALL_SUBTEST_3( basicStuffComplex(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  176. CALL_SUBTEST_5( basicStuffComplex(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  177. }
  178. CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
  179. CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
  180. CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
  181. CALL_SUBTEST_2(casting());
  182. }