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
00027 template<typename MatrixType> void linearStructure(const MatrixType& m)
00028 {
00029
00030
00031
00032 typedef typename MatrixType::Index Index;
00033 typedef typename MatrixType::Scalar Scalar;
00034
00035 Index rows = m.rows();
00036 Index cols = m.cols();
00037
00038
00039
00040 MatrixType m1 = MatrixType::Random(rows, cols),
00041 m2 = MatrixType::Random(rows, cols),
00042 m3(rows, cols),
00043 mzero = MatrixType::Zero(rows, cols);
00044
00045 Scalar s1 = internal::random<Scalar>();
00046 while (internal::abs(s1)<1e-3) s1 = internal::random<Scalar>();
00047
00048 Index r = internal::random<Index>(0, rows-1),
00049 c = internal::random<Index>(0, cols-1);
00050
00051 VERIFY_IS_APPROX(-(-m1), m1);
00052 VERIFY_IS_APPROX(m1+m1, 2*m1);
00053 VERIFY_IS_APPROX(m1+m2-m1, m2);
00054 VERIFY_IS_APPROX(-m2+m1+m2, m1);
00055 VERIFY_IS_APPROX(m1*s1, s1*m1);
00056 VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
00057 VERIFY_IS_APPROX((-m1+m2)*s1, -s1*m1+s1*m2);
00058 m3 = m2; m3 += m1;
00059 VERIFY_IS_APPROX(m3, m1+m2);
00060 m3 = m2; m3 -= m1;
00061 VERIFY_IS_APPROX(m3, m2-m1);
00062 m3 = m2; m3 *= s1;
00063 VERIFY_IS_APPROX(m3, s1*m2);
00064 if(!NumTraits<Scalar>::IsInteger)
00065 {
00066 m3 = m2; m3 /= s1;
00067 VERIFY_IS_APPROX(m3, m2/s1);
00068 }
00069
00070
00071 VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c)));
00072 VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c)));
00073 VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
00074 VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c)));
00075 VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1);
00076 if(!NumTraits<Scalar>::IsInteger)
00077 VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1);
00078
00079
00080 VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1);
00081 VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), m1.cwiseProduct(m1));
00082 VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1);
00083 VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1);
00084 }
00085
00086 void test_linearstructure()
00087 {
00088 for(int i = 0; i < g_repeat; i++) {
00089 CALL_SUBTEST_1( linearStructure(Matrix<float, 1, 1>()) );
00090 CALL_SUBTEST_2( linearStructure(Matrix2f()) );
00091 CALL_SUBTEST_3( linearStructure(Vector3d()) );
00092 CALL_SUBTEST_4( linearStructure(Matrix4d()) );
00093 CALL_SUBTEST_5( linearStructure(MatrixXcf(3, 3)) );
00094 CALL_SUBTEST_6( linearStructure(MatrixXf(8, 12)) );
00095 CALL_SUBTEST_7( linearStructure(MatrixXi(8, 12)) );
00096 CALL_SUBTEST_8( linearStructure(MatrixXcd(20, 20)) );
00097 CALL_SUBTEST_9( linearStructure(ArrayXXf(12, 8)) );
00098 }
00099 }