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.

321 lines
12 KiB

  1. #define EIGEN_ENABLE_EVALUATORS
  2. #include "main.h"
  3. using internal::copy_using_evaluator;
  4. using namespace std;
  5. #define VERIFY_IS_APPROX_EVALUATOR(DEST,EXPR) VERIFY_IS_APPROX(copy_using_evaluator(DEST,(EXPR)), (EXPR).eval());
  6. #define VERIFY_IS_APPROX_EVALUATOR2(DEST,EXPR,REF) VERIFY_IS_APPROX(copy_using_evaluator(DEST,(EXPR)), (REF).eval());
  7. void test_evaluators()
  8. {
  9. // Testing Matrix evaluator and Transpose
  10. Vector2d v = Vector2d::Random();
  11. const Vector2d v_const(v);
  12. Vector2d v2;
  13. RowVector2d w;
  14. VERIFY_IS_APPROX_EVALUATOR(v2, v);
  15. VERIFY_IS_APPROX_EVALUATOR(v2, v_const);
  16. // Testing Transpose
  17. VERIFY_IS_APPROX_EVALUATOR(w, v.transpose()); // Transpose as rvalue
  18. VERIFY_IS_APPROX_EVALUATOR(w, v_const.transpose());
  19. copy_using_evaluator(w.transpose(), v); // Transpose as lvalue
  20. VERIFY_IS_APPROX(w,v.transpose().eval());
  21. copy_using_evaluator(w.transpose(), v_const);
  22. VERIFY_IS_APPROX(w,v_const.transpose().eval());
  23. // Testing Array evaluator
  24. ArrayXXf a(2,3);
  25. ArrayXXf b(3,2);
  26. a << 1,2,3, 4,5,6;
  27. const ArrayXXf a_const(a);
  28. VERIFY_IS_APPROX_EVALUATOR(b, a.transpose());
  29. VERIFY_IS_APPROX_EVALUATOR(b, a_const.transpose());
  30. // Testing CwiseNullaryOp evaluator
  31. copy_using_evaluator(w, RowVector2d::Random());
  32. VERIFY((w.array() >= -1).all() && (w.array() <= 1).all()); // not easy to test ...
  33. VERIFY_IS_APPROX_EVALUATOR(w, RowVector2d::Zero());
  34. VERIFY_IS_APPROX_EVALUATOR(w, RowVector2d::Constant(3));
  35. // mix CwiseNullaryOp and transpose
  36. VERIFY_IS_APPROX_EVALUATOR(w, Vector2d::Zero().transpose());
  37. {
  38. // test product expressions
  39. int s = internal::random<int>(1,100);
  40. MatrixXf a(s,s), b(s,s), c(s,s), d(s,s);
  41. a.setRandom();
  42. b.setRandom();
  43. c.setRandom();
  44. d.setRandom();
  45. VERIFY_IS_APPROX_EVALUATOR(d, (a + b));
  46. VERIFY_IS_APPROX_EVALUATOR(d, (a + b).transpose());
  47. VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b), a*b);
  48. VERIFY_IS_APPROX_EVALUATOR2(d.noalias(), prod(a,b), a*b);
  49. VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b) + c, a*b + c);
  50. VERIFY_IS_APPROX_EVALUATOR2(d, s * prod(a,b), s * a*b);
  51. VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b).transpose(), (a*b).transpose());
  52. VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b) + prod(b,c), a*b + b*c);
  53. // check that prod works even with aliasing present
  54. c = a*a;
  55. copy_using_evaluator(a, prod(a,a));
  56. VERIFY_IS_APPROX(a,c);
  57. }
  58. {
  59. // test product with all possible sizes
  60. int s = internal::random<int>(1,100);
  61. Matrix<float, 1, 1> m11, res11; m11.setRandom(1,1);
  62. Matrix<float, 1, 4> m14, res14; m14.setRandom(1,4);
  63. Matrix<float, 1,Dynamic> m1X, res1X; m1X.setRandom(1,s);
  64. Matrix<float, 4, 1> m41, res41; m41.setRandom(4,1);
  65. Matrix<float, 4, 4> m44, res44; m44.setRandom(4,4);
  66. Matrix<float, 4,Dynamic> m4X, res4X; m4X.setRandom(4,s);
  67. Matrix<float,Dynamic, 1> mX1, resX1; mX1.setRandom(s,1);
  68. Matrix<float,Dynamic, 4> mX4, resX4; mX4.setRandom(s,4);
  69. Matrix<float,Dynamic,Dynamic> mXX, resXX; mXX.setRandom(s,s);
  70. VERIFY_IS_APPROX_EVALUATOR2(res11, prod(m11,m11), m11*m11);
  71. VERIFY_IS_APPROX_EVALUATOR2(res11, prod(m14,m41), m14*m41);
  72. VERIFY_IS_APPROX_EVALUATOR2(res11, prod(m1X,mX1), m1X*mX1);
  73. VERIFY_IS_APPROX_EVALUATOR2(res14, prod(m11,m14), m11*m14);
  74. VERIFY_IS_APPROX_EVALUATOR2(res14, prod(m14,m44), m14*m44);
  75. VERIFY_IS_APPROX_EVALUATOR2(res14, prod(m1X,mX4), m1X*mX4);
  76. VERIFY_IS_APPROX_EVALUATOR2(res1X, prod(m11,m1X), m11*m1X);
  77. VERIFY_IS_APPROX_EVALUATOR2(res1X, prod(m14,m4X), m14*m4X);
  78. VERIFY_IS_APPROX_EVALUATOR2(res1X, prod(m1X,mXX), m1X*mXX);
  79. VERIFY_IS_APPROX_EVALUATOR2(res41, prod(m41,m11), m41*m11);
  80. VERIFY_IS_APPROX_EVALUATOR2(res41, prod(m44,m41), m44*m41);
  81. VERIFY_IS_APPROX_EVALUATOR2(res41, prod(m4X,mX1), m4X*mX1);
  82. VERIFY_IS_APPROX_EVALUATOR2(res44, prod(m41,m14), m41*m14);
  83. VERIFY_IS_APPROX_EVALUATOR2(res44, prod(m44,m44), m44*m44);
  84. VERIFY_IS_APPROX_EVALUATOR2(res44, prod(m4X,mX4), m4X*mX4);
  85. VERIFY_IS_APPROX_EVALUATOR2(res4X, prod(m41,m1X), m41*m1X);
  86. VERIFY_IS_APPROX_EVALUATOR2(res4X, prod(m44,m4X), m44*m4X);
  87. VERIFY_IS_APPROX_EVALUATOR2(res4X, prod(m4X,mXX), m4X*mXX);
  88. VERIFY_IS_APPROX_EVALUATOR2(resX1, prod(mX1,m11), mX1*m11);
  89. VERIFY_IS_APPROX_EVALUATOR2(resX1, prod(mX4,m41), mX4*m41);
  90. VERIFY_IS_APPROX_EVALUATOR2(resX1, prod(mXX,mX1), mXX*mX1);
  91. VERIFY_IS_APPROX_EVALUATOR2(resX4, prod(mX1,m14), mX1*m14);
  92. VERIFY_IS_APPROX_EVALUATOR2(resX4, prod(mX4,m44), mX4*m44);
  93. VERIFY_IS_APPROX_EVALUATOR2(resX4, prod(mXX,mX4), mXX*mX4);
  94. VERIFY_IS_APPROX_EVALUATOR2(resXX, prod(mX1,m1X), mX1*m1X);
  95. VERIFY_IS_APPROX_EVALUATOR2(resXX, prod(mX4,m4X), mX4*m4X);
  96. VERIFY_IS_APPROX_EVALUATOR2(resXX, prod(mXX,mXX), mXX*mXX);
  97. }
  98. // this does not work because Random is eval-before-nested:
  99. // copy_using_evaluator(w, Vector2d::Random().transpose());
  100. // test CwiseUnaryOp
  101. VERIFY_IS_APPROX_EVALUATOR(v2, 3 * v);
  102. VERIFY_IS_APPROX_EVALUATOR(w, (3 * v).transpose());
  103. VERIFY_IS_APPROX_EVALUATOR(b, (a + 3).transpose());
  104. VERIFY_IS_APPROX_EVALUATOR(b, (2 * a_const + 3).transpose());
  105. // test CwiseBinaryOp
  106. VERIFY_IS_APPROX_EVALUATOR(v2, v + Vector2d::Ones());
  107. VERIFY_IS_APPROX_EVALUATOR(w, (v + Vector2d::Ones()).transpose().cwiseProduct(RowVector2d::Constant(3)));
  108. // dynamic matrices and arrays
  109. MatrixXd mat1(6,6), mat2(6,6);
  110. VERIFY_IS_APPROX_EVALUATOR(mat1, MatrixXd::Identity(6,6));
  111. VERIFY_IS_APPROX_EVALUATOR(mat2, mat1);
  112. copy_using_evaluator(mat2.transpose(), mat1);
  113. VERIFY_IS_APPROX(mat2.transpose(), mat1);
  114. ArrayXXd arr1(6,6), arr2(6,6);
  115. VERIFY_IS_APPROX_EVALUATOR(arr1, ArrayXXd::Constant(6,6, 3.0));
  116. VERIFY_IS_APPROX_EVALUATOR(arr2, arr1);
  117. // test automatic resizing
  118. mat2.resize(3,3);
  119. VERIFY_IS_APPROX_EVALUATOR(mat2, mat1);
  120. arr2.resize(9,9);
  121. VERIFY_IS_APPROX_EVALUATOR(arr2, arr1);
  122. // test direct traversal
  123. Matrix3f m3;
  124. Array33f a3;
  125. VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Identity()); // matrix, nullary
  126. // TODO: find a way to test direct traversal with array
  127. VERIFY_IS_APPROX_EVALUATOR(m3.transpose(), Matrix3f::Identity().transpose()); // transpose
  128. VERIFY_IS_APPROX_EVALUATOR(m3, 2 * Matrix3f::Identity()); // unary
  129. VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Identity() + Matrix3f::Zero()); // binary
  130. VERIFY_IS_APPROX_EVALUATOR(m3.block(0,0,2,2), Matrix3f::Identity().block(1,1,2,2)); // block
  131. // test linear traversal
  132. VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Zero()); // matrix, nullary
  133. VERIFY_IS_APPROX_EVALUATOR(a3, Array33f::Zero()); // array
  134. VERIFY_IS_APPROX_EVALUATOR(m3.transpose(), Matrix3f::Zero().transpose()); // transpose
  135. VERIFY_IS_APPROX_EVALUATOR(m3, 2 * Matrix3f::Zero()); // unary
  136. VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Zero() + m3); // binary
  137. // test inner vectorization
  138. Matrix4f m4, m4src = Matrix4f::Random();
  139. Array44f a4, a4src = Matrix4f::Random();
  140. VERIFY_IS_APPROX_EVALUATOR(m4, m4src); // matrix
  141. VERIFY_IS_APPROX_EVALUATOR(a4, a4src); // array
  142. VERIFY_IS_APPROX_EVALUATOR(m4.transpose(), m4src.transpose()); // transpose
  143. // TODO: find out why Matrix4f::Zero() does not allow inner vectorization
  144. VERIFY_IS_APPROX_EVALUATOR(m4, 2 * m4src); // unary
  145. VERIFY_IS_APPROX_EVALUATOR(m4, m4src + m4src); // binary
  146. // test linear vectorization
  147. MatrixXf mX(6,6), mXsrc = MatrixXf::Random(6,6);
  148. ArrayXXf aX(6,6), aXsrc = ArrayXXf::Random(6,6);
  149. VERIFY_IS_APPROX_EVALUATOR(mX, mXsrc); // matrix
  150. VERIFY_IS_APPROX_EVALUATOR(aX, aXsrc); // array
  151. VERIFY_IS_APPROX_EVALUATOR(mX.transpose(), mXsrc.transpose()); // transpose
  152. VERIFY_IS_APPROX_EVALUATOR(mX, MatrixXf::Zero(6,6)); // nullary
  153. VERIFY_IS_APPROX_EVALUATOR(mX, 2 * mXsrc); // unary
  154. VERIFY_IS_APPROX_EVALUATOR(mX, mXsrc + mXsrc); // binary
  155. // test blocks and slice vectorization
  156. VERIFY_IS_APPROX_EVALUATOR(m4, (mXsrc.block<4,4>(1,0)));
  157. VERIFY_IS_APPROX_EVALUATOR(aX, ArrayXXf::Constant(10, 10, 3.0).block(2, 3, 6, 6));
  158. Matrix4f m4ref = m4;
  159. copy_using_evaluator(m4.block(1, 1, 2, 3), m3.bottomRows(2));
  160. m4ref.block(1, 1, 2, 3) = m3.bottomRows(2);
  161. VERIFY_IS_APPROX(m4, m4ref);
  162. mX.setIdentity(20,20);
  163. MatrixXf mXref = MatrixXf::Identity(20,20);
  164. mXsrc = MatrixXf::Random(9,12);
  165. copy_using_evaluator(mX.block(4, 4, 9, 12), mXsrc);
  166. mXref.block(4, 4, 9, 12) = mXsrc;
  167. VERIFY_IS_APPROX(mX, mXref);
  168. // test Map
  169. const float raw[3] = {1,2,3};
  170. float buffer[3] = {0,0,0};
  171. Vector3f v3;
  172. Array3f a3f;
  173. VERIFY_IS_APPROX_EVALUATOR(v3, Map<const Vector3f>(raw));
  174. VERIFY_IS_APPROX_EVALUATOR(a3f, Map<const Array3f>(raw));
  175. Vector3f::Map(buffer) = 2*v3;
  176. VERIFY(buffer[0] == 2);
  177. VERIFY(buffer[1] == 4);
  178. VERIFY(buffer[2] == 6);
  179. // test CwiseUnaryView
  180. mat1.setRandom();
  181. mat2.setIdentity();
  182. MatrixXcd matXcd(6,6), matXcd_ref(6,6);
  183. copy_using_evaluator(matXcd.real(), mat1);
  184. copy_using_evaluator(matXcd.imag(), mat2);
  185. matXcd_ref.real() = mat1;
  186. matXcd_ref.imag() = mat2;
  187. VERIFY_IS_APPROX(matXcd, matXcd_ref);
  188. // test Select
  189. VERIFY_IS_APPROX_EVALUATOR(aX, (aXsrc > 0).select(aXsrc, -aXsrc));
  190. // test Replicate
  191. mXsrc = MatrixXf::Random(6, 6);
  192. VectorXf vX = VectorXf::Random(6);
  193. mX.resize(6, 6);
  194. VERIFY_IS_APPROX_EVALUATOR(mX, mXsrc.colwise() + vX);
  195. matXcd.resize(12, 12);
  196. VERIFY_IS_APPROX_EVALUATOR(matXcd, matXcd_ref.replicate(2,2));
  197. VERIFY_IS_APPROX_EVALUATOR(matXcd, (matXcd_ref.replicate<2,2>()));
  198. // test partial reductions
  199. VectorXd vec1(6);
  200. VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.rowwise().sum());
  201. VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.colwise().sum().transpose());
  202. // test MatrixWrapper and ArrayWrapper
  203. mat1.setRandom(6,6);
  204. arr1.setRandom(6,6);
  205. VERIFY_IS_APPROX_EVALUATOR(mat2, arr1.matrix());
  206. VERIFY_IS_APPROX_EVALUATOR(arr2, mat1.array());
  207. VERIFY_IS_APPROX_EVALUATOR(mat2, (arr1 + 2).matrix());
  208. VERIFY_IS_APPROX_EVALUATOR(arr2, mat1.array() + 2);
  209. mat2.array() = arr1 * arr1;
  210. VERIFY_IS_APPROX(mat2, (arr1 * arr1).matrix());
  211. arr2.matrix() = MatrixXd::Identity(6,6);
  212. VERIFY_IS_APPROX(arr2, MatrixXd::Identity(6,6).array());
  213. // test Reverse
  214. VERIFY_IS_APPROX_EVALUATOR(arr2, arr1.reverse());
  215. VERIFY_IS_APPROX_EVALUATOR(arr2, arr1.colwise().reverse());
  216. VERIFY_IS_APPROX_EVALUATOR(arr2, arr1.rowwise().reverse());
  217. arr2.reverse() = arr1;
  218. VERIFY_IS_APPROX(arr2, arr1.reverse());
  219. mat2.array() = mat1.array().reverse();
  220. VERIFY_IS_APPROX(mat2.array(), mat1.array().reverse());
  221. // test Diagonal
  222. VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.diagonal());
  223. vec1.resize(5);
  224. VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.diagonal(1));
  225. VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.diagonal<-1>());
  226. vec1.setRandom();
  227. mat2 = mat1;
  228. copy_using_evaluator(mat1.diagonal(1), vec1);
  229. mat2.diagonal(1) = vec1;
  230. VERIFY_IS_APPROX(mat1, mat2);
  231. copy_using_evaluator(mat1.diagonal<-1>(), mat1.diagonal(1));
  232. mat2.diagonal<-1>() = mat2.diagonal(1);
  233. VERIFY_IS_APPROX(mat1, mat2);
  234. {
  235. // test swapping
  236. MatrixXd mat1, mat2, mat1ref, mat2ref;
  237. mat1ref = mat1 = MatrixXd::Random(6, 6);
  238. mat2ref = mat2 = 2 * mat1 + MatrixXd::Identity(6, 6);
  239. swap_using_evaluator(mat1, mat2);
  240. mat1ref.swap(mat2ref);
  241. VERIFY_IS_APPROX(mat1, mat1ref);
  242. VERIFY_IS_APPROX(mat2, mat2ref);
  243. swap_using_evaluator(mat1.block(0, 0, 3, 3), mat2.block(3, 3, 3, 3));
  244. mat1ref.block(0, 0, 3, 3).swap(mat2ref.block(3, 3, 3, 3));
  245. VERIFY_IS_APPROX(mat1, mat1ref);
  246. VERIFY_IS_APPROX(mat2, mat2ref);
  247. swap_using_evaluator(mat1.row(2), mat2.col(3).transpose());
  248. mat1.row(2).swap(mat2.col(3).transpose());
  249. VERIFY_IS_APPROX(mat1, mat1ref);
  250. VERIFY_IS_APPROX(mat2, mat2ref);
  251. }
  252. {
  253. // test compound assignment
  254. const Matrix4d mat_const = Matrix4d::Random();
  255. Matrix4d mat, mat_ref;
  256. mat = mat_ref = Matrix4d::Identity();
  257. add_assign_using_evaluator(mat, mat_const);
  258. mat_ref += mat_const;
  259. VERIFY_IS_APPROX(mat, mat_ref);
  260. subtract_assign_using_evaluator(mat.row(1), 2*mat.row(2));
  261. mat_ref.row(1) -= 2*mat_ref.row(2);
  262. VERIFY_IS_APPROX(mat, mat_ref);
  263. const ArrayXXf arr_const = ArrayXXf::Random(5,3);
  264. ArrayXXf arr, arr_ref;
  265. arr = arr_ref = ArrayXXf::Constant(5, 3, 0.5);
  266. multiply_assign_using_evaluator(arr, arr_const);
  267. arr_ref *= arr_const;
  268. VERIFY_IS_APPROX(arr, arr_ref);
  269. divide_assign_using_evaluator(arr.row(1), arr.row(2) + 1);
  270. arr_ref.row(1) /= (arr_ref.row(2) + 1);
  271. VERIFY_IS_APPROX(arr, arr_ref);
  272. }
  273. }