2011-03-23 18:54:00 +08:00
|
|
|
|
|
|
|
#define EIGEN_ENABLE_EVALUATORS
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
using internal::copy_using_evaluator;
|
|
|
|
using namespace std;
|
|
|
|
|
2011-03-24 00:23:56 +08:00
|
|
|
#define VERIFY_IS_APPROX_EVALUATOR(DEST,EXPR) VERIFY_IS_APPROX(copy_using_evaluator(DEST,(EXPR)), (EXPR).eval());
|
|
|
|
#define VERIFY_IS_APPROX_EVALUATOR2(DEST,EXPR,REF) VERIFY_IS_APPROX(copy_using_evaluator(DEST,(EXPR)), (REF).eval());
|
|
|
|
|
2011-03-23 18:54:00 +08:00
|
|
|
void test_evaluators()
|
|
|
|
{
|
|
|
|
// Testing Matrix evaluator and Transpose
|
2011-03-24 00:23:56 +08:00
|
|
|
Vector2d v = Vector2d::Random();
|
2011-03-23 18:54:00 +08:00
|
|
|
const Vector2d v_const(v);
|
|
|
|
Vector2d v2;
|
|
|
|
RowVector2d w;
|
|
|
|
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX_EVALUATOR(v2, v);
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(v2, v_const);
|
2011-03-23 18:54:00 +08:00
|
|
|
|
|
|
|
// Testing Transpose
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX_EVALUATOR(w, v.transpose()); // Transpose as rvalue
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(w, v_const.transpose());
|
2011-03-23 18:54:00 +08:00
|
|
|
|
|
|
|
copy_using_evaluator(w.transpose(), v); // Transpose as lvalue
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX(w,v.transpose().eval());
|
2011-03-23 18:54:00 +08:00
|
|
|
|
|
|
|
copy_using_evaluator(w.transpose(), v_const);
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX(w,v_const.transpose().eval());
|
2011-03-23 18:54:00 +08:00
|
|
|
|
|
|
|
// Testing Array evaluator
|
|
|
|
ArrayXXf a(2,3);
|
|
|
|
ArrayXXf b(3,2);
|
|
|
|
a << 1,2,3, 4,5,6;
|
|
|
|
const ArrayXXf a_const(a);
|
|
|
|
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX_EVALUATOR(b, a.transpose());
|
2011-03-23 18:54:00 +08:00
|
|
|
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX_EVALUATOR(b, a_const.transpose());
|
2011-03-23 18:54:00 +08:00
|
|
|
|
|
|
|
// Testing CwiseNullaryOp evaluator
|
|
|
|
copy_using_evaluator(w, RowVector2d::Random());
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY((w.array() >= -1).all() && (w.array() <= 1).all()); // not easy to test ...
|
2011-03-23 18:54:00 +08:00
|
|
|
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX_EVALUATOR(w, RowVector2d::Zero());
|
2011-03-23 18:54:00 +08:00
|
|
|
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX_EVALUATOR(w, RowVector2d::Constant(3));
|
2011-03-23 18:54:00 +08:00
|
|
|
|
|
|
|
// mix CwiseNullaryOp and transpose
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX_EVALUATOR(w, Vector2d::Zero().transpose());
|
2011-03-23 18:54:00 +08:00
|
|
|
|
|
|
|
{
|
2011-03-24 00:23:56 +08:00
|
|
|
int s = internal::random<int>(1,100);
|
|
|
|
MatrixXf a(s,s), b(s,s), c(s,s), d(s,s);
|
|
|
|
a.setRandom();
|
|
|
|
b.setRandom();
|
|
|
|
c.setRandom();
|
|
|
|
d.setRandom();
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(d, (a + b));
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(d, (a + b).transpose());
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b).transpose(), (a*b).transpose());
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b) + prod(b,c), a*b + b*c);
|
2011-03-23 18:54:00 +08:00
|
|
|
|
|
|
|
// copy_using_evaluator(d, a.transpose() + (a.transpose() * (b+b)));
|
|
|
|
// cout << d << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this does not work because Random is eval-before-nested:
|
|
|
|
// copy_using_evaluator(w, Vector2d::Random().transpose());
|
|
|
|
|
|
|
|
// test CwiseUnaryOp
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX_EVALUATOR(v2, 3 * v);
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(w, (3 * v).transpose());
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(b, (a + 3).transpose());
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(b, (2 * a_const + 3).transpose());
|
2011-03-23 18:54:00 +08:00
|
|
|
|
|
|
|
// test CwiseBinaryOp
|
2011-03-24 00:23:56 +08:00
|
|
|
VERIFY_IS_APPROX_EVALUATOR(v2, v + Vector2d::Ones());
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(w, (v + Vector2d::Ones()).transpose().cwiseProduct(RowVector2d::Constant(3)));
|
2011-03-26 00:30:41 +08:00
|
|
|
|
|
|
|
// dynamic matrices and arrays
|
|
|
|
MatrixXd mat1(6,6), mat2(6,6);
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(mat1, MatrixXd::Identity(6,6));
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(mat2, mat1);
|
|
|
|
copy_using_evaluator(mat2.transpose(), mat1);
|
|
|
|
VERIFY_IS_APPROX(mat2.transpose(), mat1);
|
|
|
|
|
|
|
|
ArrayXXd arr1(6,6), arr2(6,6);
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(arr1, ArrayXXd::Constant(6,6, 3.0));
|
|
|
|
VERIFY_IS_APPROX_EVALUATOR(arr2, arr1);
|
2011-03-23 18:54:00 +08:00
|
|
|
}
|