eigen/test/evaluators.cpp

93 lines
3.0 KiB
C++
Raw Normal View History

2011-03-23 18:54:00 +08:00
#define EIGEN_ENABLE_EVALUATORS
#include "main.h"
using internal::copy_using_evaluator;
using namespace std;
#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
Vector2d v = Vector2d::Random();
2011-03-23 18:54:00 +08:00
const Vector2d v_const(v);
Vector2d v2;
RowVector2d w;
VERIFY_IS_APPROX_EVALUATOR(v2, v);
VERIFY_IS_APPROX_EVALUATOR(v2, v_const);
2011-03-23 18:54:00 +08:00
// Testing Transpose
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
VERIFY_IS_APPROX(w,v.transpose().eval());
2011-03-23 18:54:00 +08:00
copy_using_evaluator(w.transpose(), v_const);
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);
VERIFY_IS_APPROX_EVALUATOR(b, a.transpose());
2011-03-23 18:54:00 +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());
VERIFY((w.array() >= -1).all() && (w.array() <= 1).all()); // not easy to test ...
2011-03-23 18:54:00 +08:00
VERIFY_IS_APPROX_EVALUATOR(w, RowVector2d::Zero());
2011-03-23 18:54:00 +08:00
VERIFY_IS_APPROX_EVALUATOR(w, RowVector2d::Constant(3));
2011-03-23 18:54:00 +08:00
// mix CwiseNullaryOp and transpose
VERIFY_IS_APPROX_EVALUATOR(w, Vector2d::Zero().transpose());
2011-03-23 18:54:00 +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
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
VERIFY_IS_APPROX_EVALUATOR(v2, v + Vector2d::Ones());
VERIFY_IS_APPROX_EVALUATOR(w, (v + Vector2d::Ones()).transpose().cwiseProduct(RowVector2d::Constant(3)));
// 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
}