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.

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