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.

499 lines
19 KiB

  1. #include "main.h"
  2. namespace StormEigen {
  3. template<typename Lhs,typename Rhs>
  4. const Product<Lhs,Rhs>
  5. prod(const Lhs& lhs, const Rhs& rhs)
  6. {
  7. return Product<Lhs,Rhs>(lhs,rhs);
  8. }
  9. template<typename Lhs,typename Rhs>
  10. const Product<Lhs,Rhs,LazyProduct>
  11. lazyprod(const Lhs& lhs, const Rhs& rhs)
  12. {
  13. return Product<Lhs,Rhs,LazyProduct>(lhs,rhs);
  14. }
  15. template<typename DstXprType, typename SrcXprType>
  16. EIGEN_STRONG_INLINE
  17. DstXprType& copy_using_evaluator(const EigenBase<DstXprType> &dst, const SrcXprType &src)
  18. {
  19. call_assignment(dst.const_cast_derived(), src.derived(), internal::assign_op<typename DstXprType::Scalar>());
  20. return dst.const_cast_derived();
  21. }
  22. template<typename DstXprType, template <typename> class StorageBase, typename SrcXprType>
  23. EIGEN_STRONG_INLINE
  24. const DstXprType& copy_using_evaluator(const NoAlias<DstXprType, StorageBase>& dst, const SrcXprType &src)
  25. {
  26. call_assignment(dst, src.derived(), internal::assign_op<typename DstXprType::Scalar>());
  27. return dst.expression();
  28. }
  29. template<typename DstXprType, typename SrcXprType>
  30. EIGEN_STRONG_INLINE
  31. DstXprType& copy_using_evaluator(const PlainObjectBase<DstXprType> &dst, const SrcXprType &src)
  32. {
  33. #ifdef EIGEN_NO_AUTOMATIC_RESIZING
  34. eigen_assert((dst.size()==0 || (IsVectorAtCompileTime ? (dst.size() == src.size())
  35. : (dst.rows() == src.rows() && dst.cols() == src.cols())))
  36. && "Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
  37. #else
  38. dst.const_cast_derived().resizeLike(src.derived());
  39. #endif
  40. call_assignment(dst.const_cast_derived(), src.derived(), internal::assign_op<typename DstXprType::Scalar>());
  41. return dst.const_cast_derived();
  42. }
  43. template<typename DstXprType, typename SrcXprType>
  44. void add_assign_using_evaluator(const DstXprType& dst, const SrcXprType& src)
  45. {
  46. typedef typename DstXprType::Scalar Scalar;
  47. call_assignment(const_cast<DstXprType&>(dst), src.derived(), internal::add_assign_op<Scalar>());
  48. }
  49. template<typename DstXprType, typename SrcXprType>
  50. void subtract_assign_using_evaluator(const DstXprType& dst, const SrcXprType& src)
  51. {
  52. typedef typename DstXprType::Scalar Scalar;
  53. call_assignment(const_cast<DstXprType&>(dst), src.derived(), internal::sub_assign_op<Scalar>());
  54. }
  55. template<typename DstXprType, typename SrcXprType>
  56. void multiply_assign_using_evaluator(const DstXprType& dst, const SrcXprType& src)
  57. {
  58. typedef typename DstXprType::Scalar Scalar;
  59. call_assignment(dst.const_cast_derived(), src.derived(), internal::mul_assign_op<Scalar>());
  60. }
  61. template<typename DstXprType, typename SrcXprType>
  62. void divide_assign_using_evaluator(const DstXprType& dst, const SrcXprType& src)
  63. {
  64. typedef typename DstXprType::Scalar Scalar;
  65. call_assignment(dst.const_cast_derived(), src.derived(), internal::div_assign_op<Scalar>());
  66. }
  67. template<typename DstXprType, typename SrcXprType>
  68. void swap_using_evaluator(const DstXprType& dst, const SrcXprType& src)
  69. {
  70. typedef typename DstXprType::Scalar Scalar;
  71. call_assignment(dst.const_cast_derived(), src.const_cast_derived(), internal::swap_assign_op<Scalar>());
  72. }
  73. namespace internal {
  74. template<typename Dst, template <typename> class StorageBase, typename Src, typename Func>
  75. EIGEN_DEVICE_FUNC void call_assignment(const NoAlias<Dst,StorageBase>& dst, const Src& src, const Func& func)
  76. {
  77. call_assignment_no_alias(dst.expression(), src, func);
  78. }
  79. }
  80. }
  81. template<typename XprType> long get_cost(const XprType& ) { return StormEigen::internal::evaluator<XprType>::CoeffReadCost; }
  82. using namespace std;
  83. #define VERIFY_IS_APPROX_EVALUATOR(DEST,EXPR) VERIFY_IS_APPROX(copy_using_evaluator(DEST,(EXPR)), (EXPR).eval());
  84. #define VERIFY_IS_APPROX_EVALUATOR2(DEST,EXPR,REF) VERIFY_IS_APPROX(copy_using_evaluator(DEST,(EXPR)), (REF).eval());
  85. void test_evaluators()
  86. {
  87. // Testing Matrix evaluator and Transpose
  88. Vector2d v = Vector2d::Random();
  89. const Vector2d v_const(v);
  90. Vector2d v2;
  91. RowVector2d w;
  92. VERIFY_IS_APPROX_EVALUATOR(v2, v);
  93. VERIFY_IS_APPROX_EVALUATOR(v2, v_const);
  94. // Testing Transpose
  95. VERIFY_IS_APPROX_EVALUATOR(w, v.transpose()); // Transpose as rvalue
  96. VERIFY_IS_APPROX_EVALUATOR(w, v_const.transpose());
  97. copy_using_evaluator(w.transpose(), v); // Transpose as lvalue
  98. VERIFY_IS_APPROX(w,v.transpose().eval());
  99. copy_using_evaluator(w.transpose(), v_const);
  100. VERIFY_IS_APPROX(w,v_const.transpose().eval());
  101. // Testing Array evaluator
  102. {
  103. ArrayXXf a(2,3);
  104. ArrayXXf b(3,2);
  105. a << 1,2,3, 4,5,6;
  106. const ArrayXXf a_const(a);
  107. VERIFY_IS_APPROX_EVALUATOR(b, a.transpose());
  108. VERIFY_IS_APPROX_EVALUATOR(b, a_const.transpose());
  109. // Testing CwiseNullaryOp evaluator
  110. copy_using_evaluator(w, RowVector2d::Random());
  111. VERIFY((w.array() >= -1).all() && (w.array() <= 1).all()); // not easy to test ...
  112. VERIFY_IS_APPROX_EVALUATOR(w, RowVector2d::Zero());
  113. VERIFY_IS_APPROX_EVALUATOR(w, RowVector2d::Constant(3));
  114. // mix CwiseNullaryOp and transpose
  115. VERIFY_IS_APPROX_EVALUATOR(w, Vector2d::Zero().transpose());
  116. }
  117. {
  118. // test product expressions
  119. int s = internal::random<int>(1,100);
  120. MatrixXf a(s,s), b(s,s), c(s,s), d(s,s);
  121. a.setRandom();
  122. b.setRandom();
  123. c.setRandom();
  124. d.setRandom();
  125. VERIFY_IS_APPROX_EVALUATOR(d, (a + b));
  126. VERIFY_IS_APPROX_EVALUATOR(d, (a + b).transpose());
  127. VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b), a*b);
  128. VERIFY_IS_APPROX_EVALUATOR2(d.noalias(), prod(a,b), a*b);
  129. VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b) + c, a*b + c);
  130. VERIFY_IS_APPROX_EVALUATOR2(d, s * prod(a,b), s * a*b);
  131. VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b).transpose(), (a*b).transpose());
  132. VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b) + prod(b,c), a*b + b*c);
  133. // check that prod works even with aliasing present
  134. c = a*a;
  135. copy_using_evaluator(a, prod(a,a));
  136. VERIFY_IS_APPROX(a,c);
  137. // check compound assignment of products
  138. d = c;
  139. add_assign_using_evaluator(c.noalias(), prod(a,b));
  140. d.noalias() += a*b;
  141. VERIFY_IS_APPROX(c, d);
  142. d = c;
  143. subtract_assign_using_evaluator(c.noalias(), prod(a,b));
  144. d.noalias() -= a*b;
  145. VERIFY_IS_APPROX(c, d);
  146. }
  147. {
  148. // test product with all possible sizes
  149. int s = internal::random<int>(1,100);
  150. Matrix<float, 1, 1> m11, res11; m11.setRandom(1,1);
  151. Matrix<float, 1, 4> m14, res14; m14.setRandom(1,4);
  152. Matrix<float, 1,Dynamic> m1X, res1X; m1X.setRandom(1,s);
  153. Matrix<float, 4, 1> m41, res41; m41.setRandom(4,1);
  154. Matrix<float, 4, 4> m44, res44; m44.setRandom(4,4);
  155. Matrix<float, 4,Dynamic> m4X, res4X; m4X.setRandom(4,s);
  156. Matrix<float,Dynamic, 1> mX1, resX1; mX1.setRandom(s,1);
  157. Matrix<float,Dynamic, 4> mX4, resX4; mX4.setRandom(s,4);
  158. Matrix<float,Dynamic,Dynamic> mXX, resXX; mXX.setRandom(s,s);
  159. VERIFY_IS_APPROX_EVALUATOR2(res11, prod(m11,m11), m11*m11);
  160. VERIFY_IS_APPROX_EVALUATOR2(res11, prod(m14,m41), m14*m41);
  161. VERIFY_IS_APPROX_EVALUATOR2(res11, prod(m1X,mX1), m1X*mX1);
  162. VERIFY_IS_APPROX_EVALUATOR2(res14, prod(m11,m14), m11*m14);
  163. VERIFY_IS_APPROX_EVALUATOR2(res14, prod(m14,m44), m14*m44);
  164. VERIFY_IS_APPROX_EVALUATOR2(res14, prod(m1X,mX4), m1X*mX4);
  165. VERIFY_IS_APPROX_EVALUATOR2(res1X, prod(m11,m1X), m11*m1X);
  166. VERIFY_IS_APPROX_EVALUATOR2(res1X, prod(m14,m4X), m14*m4X);
  167. VERIFY_IS_APPROX_EVALUATOR2(res1X, prod(m1X,mXX), m1X*mXX);
  168. VERIFY_IS_APPROX_EVALUATOR2(res41, prod(m41,m11), m41*m11);
  169. VERIFY_IS_APPROX_EVALUATOR2(res41, prod(m44,m41), m44*m41);
  170. VERIFY_IS_APPROX_EVALUATOR2(res41, prod(m4X,mX1), m4X*mX1);
  171. VERIFY_IS_APPROX_EVALUATOR2(res44, prod(m41,m14), m41*m14);
  172. VERIFY_IS_APPROX_EVALUATOR2(res44, prod(m44,m44), m44*m44);
  173. VERIFY_IS_APPROX_EVALUATOR2(res44, prod(m4X,mX4), m4X*mX4);
  174. VERIFY_IS_APPROX_EVALUATOR2(res4X, prod(m41,m1X), m41*m1X);
  175. VERIFY_IS_APPROX_EVALUATOR2(res4X, prod(m44,m4X), m44*m4X);
  176. VERIFY_IS_APPROX_EVALUATOR2(res4X, prod(m4X,mXX), m4X*mXX);
  177. VERIFY_IS_APPROX_EVALUATOR2(resX1, prod(mX1,m11), mX1*m11);
  178. VERIFY_IS_APPROX_EVALUATOR2(resX1, prod(mX4,m41), mX4*m41);
  179. VERIFY_IS_APPROX_EVALUATOR2(resX1, prod(mXX,mX1), mXX*mX1);
  180. VERIFY_IS_APPROX_EVALUATOR2(resX4, prod(mX1,m14), mX1*m14);
  181. VERIFY_IS_APPROX_EVALUATOR2(resX4, prod(mX4,m44), mX4*m44);
  182. VERIFY_IS_APPROX_EVALUATOR2(resX4, prod(mXX,mX4), mXX*mX4);
  183. VERIFY_IS_APPROX_EVALUATOR2(resXX, prod(mX1,m1X), mX1*m1X);
  184. VERIFY_IS_APPROX_EVALUATOR2(resXX, prod(mX4,m4X), mX4*m4X);
  185. VERIFY_IS_APPROX_EVALUATOR2(resXX, prod(mXX,mXX), mXX*mXX);
  186. }
  187. {
  188. ArrayXXf a(2,3);
  189. ArrayXXf b(3,2);
  190. a << 1,2,3, 4,5,6;
  191. const ArrayXXf a_const(a);
  192. // this does not work because Random is eval-before-nested:
  193. // copy_using_evaluator(w, Vector2d::Random().transpose());
  194. // test CwiseUnaryOp
  195. VERIFY_IS_APPROX_EVALUATOR(v2, 3 * v);
  196. VERIFY_IS_APPROX_EVALUATOR(w, (3 * v).transpose());
  197. VERIFY_IS_APPROX_EVALUATOR(b, (a + 3).transpose());
  198. VERIFY_IS_APPROX_EVALUATOR(b, (2 * a_const + 3).transpose());
  199. // test CwiseBinaryOp
  200. VERIFY_IS_APPROX_EVALUATOR(v2, v + Vector2d::Ones());
  201. VERIFY_IS_APPROX_EVALUATOR(w, (v + Vector2d::Ones()).transpose().cwiseProduct(RowVector2d::Constant(3)));
  202. // dynamic matrices and arrays
  203. MatrixXd mat1(6,6), mat2(6,6);
  204. VERIFY_IS_APPROX_EVALUATOR(mat1, MatrixXd::Identity(6,6));
  205. VERIFY_IS_APPROX_EVALUATOR(mat2, mat1);
  206. copy_using_evaluator(mat2.transpose(), mat1);
  207. VERIFY_IS_APPROX(mat2.transpose(), mat1);
  208. ArrayXXd arr1(6,6), arr2(6,6);
  209. VERIFY_IS_APPROX_EVALUATOR(arr1, ArrayXXd::Constant(6,6, 3.0));
  210. VERIFY_IS_APPROX_EVALUATOR(arr2, arr1);
  211. // test automatic resizing
  212. mat2.resize(3,3);
  213. VERIFY_IS_APPROX_EVALUATOR(mat2, mat1);
  214. arr2.resize(9,9);
  215. VERIFY_IS_APPROX_EVALUATOR(arr2, arr1);
  216. // test direct traversal
  217. Matrix3f m3;
  218. Array33f a3;
  219. VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Identity()); // matrix, nullary
  220. // TODO: find a way to test direct traversal with array
  221. VERIFY_IS_APPROX_EVALUATOR(m3.transpose(), Matrix3f::Identity().transpose()); // transpose
  222. VERIFY_IS_APPROX_EVALUATOR(m3, 2 * Matrix3f::Identity()); // unary
  223. VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Identity() + Matrix3f::Zero()); // binary
  224. VERIFY_IS_APPROX_EVALUATOR(m3.block(0,0,2,2), Matrix3f::Identity().block(1,1,2,2)); // block
  225. // test linear traversal
  226. VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Zero()); // matrix, nullary
  227. VERIFY_IS_APPROX_EVALUATOR(a3, Array33f::Zero()); // array
  228. VERIFY_IS_APPROX_EVALUATOR(m3.transpose(), Matrix3f::Zero().transpose()); // transpose
  229. VERIFY_IS_APPROX_EVALUATOR(m3, 2 * Matrix3f::Zero()); // unary
  230. VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Zero() + m3); // binary
  231. // test inner vectorization
  232. Matrix4f m4, m4src = Matrix4f::Random();
  233. Array44f a4, a4src = Matrix4f::Random();
  234. VERIFY_IS_APPROX_EVALUATOR(m4, m4src); // matrix
  235. VERIFY_IS_APPROX_EVALUATOR(a4, a4src); // array
  236. VERIFY_IS_APPROX_EVALUATOR(m4.transpose(), m4src.transpose()); // transpose
  237. // TODO: find out why Matrix4f::Zero() does not allow inner vectorization
  238. VERIFY_IS_APPROX_EVALUATOR(m4, 2 * m4src); // unary
  239. VERIFY_IS_APPROX_EVALUATOR(m4, m4src + m4src); // binary
  240. // test linear vectorization
  241. MatrixXf mX(6,6), mXsrc = MatrixXf::Random(6,6);
  242. ArrayXXf aX(6,6), aXsrc = ArrayXXf::Random(6,6);
  243. VERIFY_IS_APPROX_EVALUATOR(mX, mXsrc); // matrix
  244. VERIFY_IS_APPROX_EVALUATOR(aX, aXsrc); // array
  245. VERIFY_IS_APPROX_EVALUATOR(mX.transpose(), mXsrc.transpose()); // transpose
  246. VERIFY_IS_APPROX_EVALUATOR(mX, MatrixXf::Zero(6,6)); // nullary
  247. VERIFY_IS_APPROX_EVALUATOR(mX, 2 * mXsrc); // unary
  248. VERIFY_IS_APPROX_EVALUATOR(mX, mXsrc + mXsrc); // binary
  249. // test blocks and slice vectorization
  250. VERIFY_IS_APPROX_EVALUATOR(m4, (mXsrc.block<4,4>(1,0)));
  251. VERIFY_IS_APPROX_EVALUATOR(aX, ArrayXXf::Constant(10, 10, 3.0).block(2, 3, 6, 6));
  252. Matrix4f m4ref = m4;
  253. copy_using_evaluator(m4.block(1, 1, 2, 3), m3.bottomRows(2));
  254. m4ref.block(1, 1, 2, 3) = m3.bottomRows(2);
  255. VERIFY_IS_APPROX(m4, m4ref);
  256. mX.setIdentity(20,20);
  257. MatrixXf mXref = MatrixXf::Identity(20,20);
  258. mXsrc = MatrixXf::Random(9,12);
  259. copy_using_evaluator(mX.block(4, 4, 9, 12), mXsrc);
  260. mXref.block(4, 4, 9, 12) = mXsrc;
  261. VERIFY_IS_APPROX(mX, mXref);
  262. // test Map
  263. const float raw[3] = {1,2,3};
  264. float buffer[3] = {0,0,0};
  265. Vector3f v3;
  266. Array3f a3f;
  267. VERIFY_IS_APPROX_EVALUATOR(v3, Map<const Vector3f>(raw));
  268. VERIFY_IS_APPROX_EVALUATOR(a3f, Map<const Array3f>(raw));
  269. Vector3f::Map(buffer) = 2*v3;
  270. VERIFY(buffer[0] == 2);
  271. VERIFY(buffer[1] == 4);
  272. VERIFY(buffer[2] == 6);
  273. // test CwiseUnaryView
  274. mat1.setRandom();
  275. mat2.setIdentity();
  276. MatrixXcd matXcd(6,6), matXcd_ref(6,6);
  277. copy_using_evaluator(matXcd.real(), mat1);
  278. copy_using_evaluator(matXcd.imag(), mat2);
  279. matXcd_ref.real() = mat1;
  280. matXcd_ref.imag() = mat2;
  281. VERIFY_IS_APPROX(matXcd, matXcd_ref);
  282. // test Select
  283. VERIFY_IS_APPROX_EVALUATOR(aX, (aXsrc > 0).select(aXsrc, -aXsrc));
  284. // test Replicate
  285. mXsrc = MatrixXf::Random(6, 6);
  286. VectorXf vX = VectorXf::Random(6);
  287. mX.resize(6, 6);
  288. VERIFY_IS_APPROX_EVALUATOR(mX, mXsrc.colwise() + vX);
  289. matXcd.resize(12, 12);
  290. VERIFY_IS_APPROX_EVALUATOR(matXcd, matXcd_ref.replicate(2,2));
  291. VERIFY_IS_APPROX_EVALUATOR(matXcd, (matXcd_ref.replicate<2,2>()));
  292. // test partial reductions
  293. VectorXd vec1(6);
  294. VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.rowwise().sum());
  295. VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.colwise().sum().transpose());
  296. // test MatrixWrapper and ArrayWrapper
  297. mat1.setRandom(6,6);
  298. arr1.setRandom(6,6);
  299. VERIFY_IS_APPROX_EVALUATOR(mat2, arr1.matrix());
  300. VERIFY_IS_APPROX_EVALUATOR(arr2, mat1.array());
  301. VERIFY_IS_APPROX_EVALUATOR(mat2, (arr1 + 2).matrix());
  302. VERIFY_IS_APPROX_EVALUATOR(arr2, mat1.array() + 2);
  303. mat2.array() = arr1 * arr1;
  304. VERIFY_IS_APPROX(mat2, (arr1 * arr1).matrix());
  305. arr2.matrix() = MatrixXd::Identity(6,6);
  306. VERIFY_IS_APPROX(arr2, MatrixXd::Identity(6,6).array());
  307. // test Reverse
  308. VERIFY_IS_APPROX_EVALUATOR(arr2, arr1.reverse());
  309. VERIFY_IS_APPROX_EVALUATOR(arr2, arr1.colwise().reverse());
  310. VERIFY_IS_APPROX_EVALUATOR(arr2, arr1.rowwise().reverse());
  311. arr2.reverse() = arr1;
  312. VERIFY_IS_APPROX(arr2, arr1.reverse());
  313. mat2.array() = mat1.array().reverse();
  314. VERIFY_IS_APPROX(mat2.array(), mat1.array().reverse());
  315. // test Diagonal
  316. VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.diagonal());
  317. vec1.resize(5);
  318. VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.diagonal(1));
  319. VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.diagonal<-1>());
  320. vec1.setRandom();
  321. mat2 = mat1;
  322. copy_using_evaluator(mat1.diagonal(1), vec1);
  323. mat2.diagonal(1) = vec1;
  324. VERIFY_IS_APPROX(mat1, mat2);
  325. copy_using_evaluator(mat1.diagonal<-1>(), mat1.diagonal(1));
  326. mat2.diagonal<-1>() = mat2.diagonal(1);
  327. VERIFY_IS_APPROX(mat1, mat2);
  328. }
  329. {
  330. // test swapping
  331. MatrixXd mat1, mat2, mat1ref, mat2ref;
  332. mat1ref = mat1 = MatrixXd::Random(6, 6);
  333. mat2ref = mat2 = 2 * mat1 + MatrixXd::Identity(6, 6);
  334. swap_using_evaluator(mat1, mat2);
  335. mat1ref.swap(mat2ref);
  336. VERIFY_IS_APPROX(mat1, mat1ref);
  337. VERIFY_IS_APPROX(mat2, mat2ref);
  338. swap_using_evaluator(mat1.block(0, 0, 3, 3), mat2.block(3, 3, 3, 3));
  339. mat1ref.block(0, 0, 3, 3).swap(mat2ref.block(3, 3, 3, 3));
  340. VERIFY_IS_APPROX(mat1, mat1ref);
  341. VERIFY_IS_APPROX(mat2, mat2ref);
  342. swap_using_evaluator(mat1.row(2), mat2.col(3).transpose());
  343. mat1.row(2).swap(mat2.col(3).transpose());
  344. VERIFY_IS_APPROX(mat1, mat1ref);
  345. VERIFY_IS_APPROX(mat2, mat2ref);
  346. }
  347. {
  348. // test compound assignment
  349. const Matrix4d mat_const = Matrix4d::Random();
  350. Matrix4d mat, mat_ref;
  351. mat = mat_ref = Matrix4d::Identity();
  352. add_assign_using_evaluator(mat, mat_const);
  353. mat_ref += mat_const;
  354. VERIFY_IS_APPROX(mat, mat_ref);
  355. subtract_assign_using_evaluator(mat.row(1), 2*mat.row(2));
  356. mat_ref.row(1) -= 2*mat_ref.row(2);
  357. VERIFY_IS_APPROX(mat, mat_ref);
  358. const ArrayXXf arr_const = ArrayXXf::Random(5,3);
  359. ArrayXXf arr, arr_ref;
  360. arr = arr_ref = ArrayXXf::Constant(5, 3, 0.5);
  361. multiply_assign_using_evaluator(arr, arr_const);
  362. arr_ref *= arr_const;
  363. VERIFY_IS_APPROX(arr, arr_ref);
  364. divide_assign_using_evaluator(arr.row(1), arr.row(2) + 1);
  365. arr_ref.row(1) /= (arr_ref.row(2) + 1);
  366. VERIFY_IS_APPROX(arr, arr_ref);
  367. }
  368. {
  369. // test triangular shapes
  370. MatrixXd A = MatrixXd::Random(6,6), B(6,6), C(6,6), D(6,6);
  371. A.setRandom();B.setRandom();
  372. VERIFY_IS_APPROX_EVALUATOR2(B, A.triangularView<Upper>(), MatrixXd(A.triangularView<Upper>()));
  373. A.setRandom();B.setRandom();
  374. VERIFY_IS_APPROX_EVALUATOR2(B, A.triangularView<UnitLower>(), MatrixXd(A.triangularView<UnitLower>()));
  375. A.setRandom();B.setRandom();
  376. VERIFY_IS_APPROX_EVALUATOR2(B, A.triangularView<UnitUpper>(), MatrixXd(A.triangularView<UnitUpper>()));
  377. A.setRandom();B.setRandom();
  378. C = B; C.triangularView<Upper>() = A;
  379. copy_using_evaluator(B.triangularView<Upper>(), A);
  380. VERIFY(B.isApprox(C) && "copy_using_evaluator(B.triangularView<Upper>(), A)");
  381. A.setRandom();B.setRandom();
  382. C = B; C.triangularView<Lower>() = A.triangularView<Lower>();
  383. copy_using_evaluator(B.triangularView<Lower>(), A.triangularView<Lower>());
  384. VERIFY(B.isApprox(C) && "copy_using_evaluator(B.triangularView<Lower>(), A.triangularView<Lower>())");
  385. A.setRandom();B.setRandom();
  386. C = B; C.triangularView<Lower>() = A.triangularView<Upper>().transpose();
  387. copy_using_evaluator(B.triangularView<Lower>(), A.triangularView<Upper>().transpose());
  388. VERIFY(B.isApprox(C) && "copy_using_evaluator(B.triangularView<Lower>(), A.triangularView<Lower>().transpose())");
  389. A.setRandom();B.setRandom(); C = B; D = A;
  390. C.triangularView<Upper>().swap(D.triangularView<Upper>());
  391. swap_using_evaluator(B.triangularView<Upper>(), A.triangularView<Upper>());
  392. VERIFY(B.isApprox(C) && "swap_using_evaluator(B.triangularView<Upper>(), A.triangularView<Upper>())");
  393. VERIFY_IS_APPROX_EVALUATOR2(B, prod(A.triangularView<Upper>(),A), MatrixXd(A.triangularView<Upper>()*A));
  394. VERIFY_IS_APPROX_EVALUATOR2(B, prod(A.selfadjointView<Upper>(),A), MatrixXd(A.selfadjointView<Upper>()*A));
  395. }
  396. {
  397. // test diagonal shapes
  398. VectorXd d = VectorXd::Random(6);
  399. MatrixXd A = MatrixXd::Random(6,6), B(6,6);
  400. A.setRandom();B.setRandom();
  401. VERIFY_IS_APPROX_EVALUATOR2(B, lazyprod(d.asDiagonal(),A), MatrixXd(d.asDiagonal()*A));
  402. VERIFY_IS_APPROX_EVALUATOR2(B, lazyprod(A,d.asDiagonal()), MatrixXd(A*d.asDiagonal()));
  403. }
  404. {
  405. // test CoeffReadCost
  406. Matrix4d a, b;
  407. VERIFY_IS_EQUAL( get_cost(a), 1 );
  408. VERIFY_IS_EQUAL( get_cost(a+b), 3);
  409. VERIFY_IS_EQUAL( get_cost(2*a+b), 4);
  410. VERIFY_IS_EQUAL( get_cost(a*b), 1);
  411. VERIFY_IS_EQUAL( get_cost(a.lazyProduct(b)), 15);
  412. VERIFY_IS_EQUAL( get_cost(a*(a*b)), 1);
  413. VERIFY_IS_EQUAL( get_cost(a.lazyProduct(a*b)), 15);
  414. VERIFY_IS_EQUAL( get_cost(a*(a+b)), 1);
  415. VERIFY_IS_EQUAL( get_cost(a.lazyProduct(a+b)), 15);
  416. }
  417. }