00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "main.h"
00026 #include <Eigen/Array>
00027
00028 template<typename MatrixType> void array(const MatrixType& m)
00029 {
00030
00031
00032
00033
00034 typedef typename MatrixType::Scalar Scalar;
00035 typedef typename NumTraits<Scalar>::Real RealScalar;
00036 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00037
00038 int rows = m.rows();
00039 int cols = m.cols();
00040
00041 MatrixType m1 = MatrixType::Random(rows, cols),
00042 m2 = MatrixType::Random(rows, cols),
00043 m3(rows, cols);
00044
00045 Scalar s1 = ei_random<Scalar>(),
00046 s2 = ei_random<Scalar>();
00047
00048
00049 VERIFY_IS_APPROX(m1.cwise() + s1, s1 + m1.cwise());
00050 VERIFY_IS_APPROX(m1.cwise() + s1, MatrixType::Constant(rows,cols,s1) + m1);
00051 VERIFY_IS_APPROX((m1*Scalar(2)).cwise() - s2, (m1+m1) - MatrixType::Constant(rows,cols,s2) );
00052 m3 = m1;
00053 m3.cwise() += s2;
00054 VERIFY_IS_APPROX(m3, m1.cwise() + s2);
00055 m3 = m1;
00056 m3.cwise() -= s1;
00057 VERIFY_IS_APPROX(m3, m1.cwise() - s1);
00058
00059
00060 VERIFY_IS_APPROX(m1.colwise().sum().sum(), m1.sum());
00061 VERIFY_IS_APPROX(m1.rowwise().sum().sum(), m1.sum());
00062 if (!ei_isApprox(m1.sum(), (m1+m2).sum()))
00063 VERIFY_IS_NOT_APPROX(((m1+m2).rowwise().sum()).sum(), m1.sum());
00064 VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(internal::scalar_sum_op<Scalar>()));
00065 }
00066
00067 template<typename MatrixType> void comparisons(const MatrixType& m)
00068 {
00069 typedef typename MatrixType::Scalar Scalar;
00070 typedef typename NumTraits<Scalar>::Real RealScalar;
00071 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00072
00073 int rows = m.rows();
00074 int cols = m.cols();
00075
00076 int r = ei_random<int>(0, rows-1),
00077 c = ei_random<int>(0, cols-1);
00078
00079 MatrixType m1 = MatrixType::Random(rows, cols),
00080 m2 = MatrixType::Random(rows, cols),
00081 m3(rows, cols);
00082
00083 VERIFY(((m1.cwise() + Scalar(1)).cwise() > m1).all());
00084 VERIFY(((m1.cwise() - Scalar(1)).cwise() < m1).all());
00085 if (rows*cols>1)
00086 {
00087 m3 = m1;
00088 m3(r,c) += 1;
00089 VERIFY(! (m1.cwise() < m3).all() );
00090 VERIFY(! (m1.cwise() > m3).all() );
00091 }
00092
00093
00094 VERIFY( (m1.cwise() != (m1(r,c)+1) ).any() );
00095 VERIFY( (m1.cwise() > (m1(r,c)-1) ).any() );
00096 VERIFY( (m1.cwise() < (m1(r,c)+1) ).any() );
00097 VERIFY( (m1.cwise() == m1(r,c) ).any() );
00098
00099
00100 VERIFY_IS_APPROX( (m1.cwise()<m2).select(m1,m2), m1.cwise().min(m2) );
00101 VERIFY_IS_APPROX( (m1.cwise()>m2).select(m1,m2), m1.cwise().max(m2) );
00102 Scalar mid = (m1.cwise().abs().minCoeff() + m1.cwise().abs().maxCoeff())/Scalar(2);
00103 for (int j=0; j<cols; ++j)
00104 for (int i=0; i<rows; ++i)
00105 m3(i,j) = ei_abs(m1(i,j))<mid ? 0 : m1(i,j);
00106 VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<MatrixType::Constant(rows,cols,mid))
00107 .select(MatrixType::Zero(rows,cols),m1), m3);
00108
00109 VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<MatrixType::Constant(rows,cols,mid))
00110 .select(0,m1), m3);
00111 VERIFY_IS_APPROX( (m1.cwise().abs().cwise()>=MatrixType::Constant(rows,cols,mid))
00112 .select(m1,0), m3);
00113
00114 VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<mid).select(0,m1), m3);
00115
00116
00117 VERIFY(((m1.cwise().abs().cwise()+1).cwise()>RealScalar(0.1)).count() == rows*cols);
00118 VERIFY_IS_APPROX(((m1.cwise().abs().cwise()+1).cwise()>RealScalar(0.1)).colwise().count().template cast<int>(), RowVectorXi::Constant(cols,rows));
00119 VERIFY_IS_APPROX(((m1.cwise().abs().cwise()+1).cwise()>RealScalar(0.1)).rowwise().count().template cast<int>(), VectorXi::Constant(rows, cols));
00120 }
00121
00122 template<typename VectorType> void lpNorm(const VectorType& v)
00123 {
00124 VectorType u = VectorType::Random(v.size());
00125
00126 VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), u.cwise().abs().maxCoeff());
00127 VERIFY_IS_APPROX(u.template lpNorm<1>(), u.cwise().abs().sum());
00128 VERIFY_IS_APPROX(u.template lpNorm<2>(), ei_sqrt(u.cwise().abs().cwise().square().sum()));
00129 VERIFY_IS_APPROX(ei_pow(u.template lpNorm<5>(), typename VectorType::RealScalar(5)), u.cwise().abs().cwise().pow(5).sum());
00130 }
00131
00132 void test_eigen2_array()
00133 {
00134 for(int i = 0; i < g_repeat; i++) {
00135 CALL_SUBTEST_1( array(Matrix<float, 1, 1>()) );
00136 CALL_SUBTEST_2( array(Matrix2f()) );
00137 CALL_SUBTEST_3( array(Matrix4d()) );
00138 CALL_SUBTEST_4( array(MatrixXcf(3, 3)) );
00139 CALL_SUBTEST_5( array(MatrixXf(8, 12)) );
00140 CALL_SUBTEST_6( array(MatrixXi(8, 12)) );
00141 }
00142 for(int i = 0; i < g_repeat; i++) {
00143 CALL_SUBTEST_1( comparisons(Matrix<float, 1, 1>()) );
00144 CALL_SUBTEST_2( comparisons(Matrix2f()) );
00145 CALL_SUBTEST_3( comparisons(Matrix4d()) );
00146 CALL_SUBTEST_5( comparisons(MatrixXf(8, 12)) );
00147 CALL_SUBTEST_6( comparisons(MatrixXi(8, 12)) );
00148 }
00149 for(int i = 0; i < g_repeat; i++) {
00150 CALL_SUBTEST_1( lpNorm(Matrix<float, 1, 1>()) );
00151 CALL_SUBTEST_2( lpNorm(Vector2f()) );
00152 CALL_SUBTEST_3( lpNorm(Vector3d()) );
00153 CALL_SUBTEST_4( lpNorm(Vector4f()) );
00154 CALL_SUBTEST_5( lpNorm(VectorXf(16)) );
00155 CALL_SUBTEST_7( lpNorm(VectorXcd(10)) );
00156 }
00157 }