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.

280 lines
10 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. // check ternary usage
  109. {
  110. bool b = internal::random<int>(0,10)>5;
  111. m3 = b ? m1 : m2;
  112. if(b) VERIFY_IS_APPROX(m3,m1);
  113. else VERIFY_IS_APPROX(m3,m2);
  114. m3 = b ? -m1 : m2;
  115. if(b) VERIFY_IS_APPROX(m3,-m1);
  116. else VERIFY_IS_APPROX(m3,m2);
  117. m3 = b ? m1 : -m2;
  118. if(b) VERIFY_IS_APPROX(m3,m1);
  119. else VERIFY_IS_APPROX(m3,-m2);
  120. }
  121. }
  122. template<typename MatrixType> void basicStuffComplex(const MatrixType& m)
  123. {
  124. typedef typename MatrixType::Index Index;
  125. typedef typename MatrixType::Scalar Scalar;
  126. typedef typename NumTraits<Scalar>::Real RealScalar;
  127. typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> RealMatrixType;
  128. Index rows = m.rows();
  129. Index cols = m.cols();
  130. Scalar s1 = internal::random<Scalar>(),
  131. s2 = internal::random<Scalar>();
  132. VERIFY(numext::real(s1)==numext::real_ref(s1));
  133. VERIFY(numext::imag(s1)==numext::imag_ref(s1));
  134. numext::real_ref(s1) = numext::real(s2);
  135. numext::imag_ref(s1) = numext::imag(s2);
  136. VERIFY(internal::isApprox(s1, s2, NumTraits<RealScalar>::epsilon()));
  137. // extended precision in Intel FPUs means that s1 == s2 in the line above is not guaranteed.
  138. RealMatrixType rm1 = RealMatrixType::Random(rows,cols),
  139. rm2 = RealMatrixType::Random(rows,cols);
  140. MatrixType cm(rows,cols);
  141. cm.real() = rm1;
  142. cm.imag() = rm2;
  143. VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
  144. VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
  145. rm1.setZero();
  146. rm2.setZero();
  147. rm1 = cm.real();
  148. rm2 = cm.imag();
  149. VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
  150. VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
  151. cm.real().setZero();
  152. VERIFY(static_cast<const MatrixType&>(cm).real().isZero());
  153. VERIFY(!static_cast<const MatrixType&>(cm).imag().isZero());
  154. }
  155. #ifdef EIGEN_TEST_PART_2
  156. void casting()
  157. {
  158. Matrix4f m = Matrix4f::Random(), m2;
  159. Matrix4d n = m.cast<double>();
  160. VERIFY(m.isApprox(n.cast<float>()));
  161. m2 = m.cast<float>(); // check the specialization when NewType == Type
  162. VERIFY(m.isApprox(m2));
  163. }
  164. #endif
  165. template <typename Scalar>
  166. void fixedSizeMatrixConstruction()
  167. {
  168. Scalar raw[4];
  169. for(int k=0; k<4; ++k)
  170. raw[k] = internal::random<Scalar>();
  171. {
  172. Matrix<Scalar,4,1> m(raw);
  173. Array<Scalar,4,1> a(raw);
  174. for(int k=0; k<4; ++k) VERIFY(m(k) == raw[k]);
  175. for(int k=0; k<4; ++k) VERIFY(a(k) == raw[k]);
  176. VERIFY_IS_EQUAL(m,(Matrix<Scalar,4,1>(raw[0],raw[1],raw[2],raw[3])));
  177. VERIFY((a==(Array<Scalar,4,1>(raw[0],raw[1],raw[2],raw[3]))).all());
  178. }
  179. {
  180. Matrix<Scalar,3,1> m(raw);
  181. Array<Scalar,3,1> a(raw);
  182. for(int k=0; k<3; ++k) VERIFY(m(k) == raw[k]);
  183. for(int k=0; k<3; ++k) VERIFY(a(k) == raw[k]);
  184. VERIFY_IS_EQUAL(m,(Matrix<Scalar,3,1>(raw[0],raw[1],raw[2])));
  185. VERIFY((a==Array<Scalar,3,1>(raw[0],raw[1],raw[2])).all());
  186. }
  187. {
  188. Matrix<Scalar,2,1> m(raw), m2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) );
  189. Array<Scalar,2,1> a(raw), a2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) );
  190. for(int k=0; k<2; ++k) VERIFY(m(k) == raw[k]);
  191. for(int k=0; k<2; ++k) VERIFY(a(k) == raw[k]);
  192. VERIFY_IS_EQUAL(m,(Matrix<Scalar,2,1>(raw[0],raw[1])));
  193. VERIFY((a==Array<Scalar,2,1>(raw[0],raw[1])).all());
  194. for(int k=0; k<2; ++k) VERIFY(m2(k) == DenseIndex(raw[k]));
  195. for(int k=0; k<2; ++k) VERIFY(a2(k) == DenseIndex(raw[k]));
  196. }
  197. {
  198. Matrix<Scalar,1,2> m(raw),
  199. m2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) ),
  200. m3( (int(raw[0])), (int(raw[1])) ),
  201. m4( (float(raw[0])), (float(raw[1])) );
  202. Array<Scalar,1,2> a(raw), a2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) );
  203. for(int k=0; k<2; ++k) VERIFY(m(k) == raw[k]);
  204. for(int k=0; k<2; ++k) VERIFY(a(k) == raw[k]);
  205. VERIFY_IS_EQUAL(m,(Matrix<Scalar,1,2>(raw[0],raw[1])));
  206. VERIFY((a==Array<Scalar,1,2>(raw[0],raw[1])).all());
  207. for(int k=0; k<2; ++k) VERIFY(m2(k) == DenseIndex(raw[k]));
  208. for(int k=0; k<2; ++k) VERIFY(a2(k) == DenseIndex(raw[k]));
  209. for(int k=0; k<2; ++k) VERIFY(m3(k) == int(raw[k]));
  210. for(int k=0; k<2; ++k) VERIFY((m4(k)) == Scalar(float(raw[k])));
  211. }
  212. {
  213. Matrix<Scalar,1,1> m(raw), m1(raw[0]), m2( (DenseIndex(raw[0])) ), m3( (int(raw[0])) );
  214. Array<Scalar,1,1> a(raw), a1(raw[0]), a2( (DenseIndex(raw[0])) );
  215. VERIFY(m(0) == raw[0]);
  216. VERIFY(a(0) == raw[0]);
  217. VERIFY(m1(0) == raw[0]);
  218. VERIFY(a1(0) == raw[0]);
  219. VERIFY(m2(0) == DenseIndex(raw[0]));
  220. VERIFY(a2(0) == DenseIndex(raw[0]));
  221. VERIFY(m3(0) == int(raw[0]));
  222. VERIFY_IS_EQUAL(m,(Matrix<Scalar,1,1>(raw[0])));
  223. VERIFY((a==Array<Scalar,1,1>(raw[0])).all());
  224. }
  225. }
  226. void test_basicstuff()
  227. {
  228. for(int i = 0; i < g_repeat; i++) {
  229. CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
  230. CALL_SUBTEST_2( basicStuff(Matrix4d()) );
  231. CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  232. CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  233. CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  234. CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) );
  235. 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))) );
  236. CALL_SUBTEST_3( basicStuffComplex(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  237. CALL_SUBTEST_5( basicStuffComplex(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  238. }
  239. CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
  240. CALL_SUBTEST_1(fixedSizeMatrixConstruction<float>());
  241. CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
  242. CALL_SUBTEST_1(fixedSizeMatrixConstruction<int>());
  243. CALL_SUBTEST_1(fixedSizeMatrixConstruction<long int>());
  244. CALL_SUBTEST_1(fixedSizeMatrixConstruction<std::ptrdiff_t>());
  245. CALL_SUBTEST_2(casting());
  246. }