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 #define EIGEN_NO_STATIC_ASSERT
00026
00027 #include "main.h"
00028
00029 template<typename MatrixType> void basicStuff(const MatrixType& m)
00030 {
00031 typedef typename MatrixType::Index Index;
00032 typedef typename MatrixType::Scalar Scalar;
00033 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00034 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
00035
00036 Index rows = m.rows();
00037 Index cols = m.cols();
00038
00039
00040
00041 MatrixType m1 = MatrixType::Random(rows, cols),
00042 m2 = MatrixType::Random(rows, cols),
00043 m3(rows, cols),
00044 mzero = MatrixType::Zero(rows, cols),
00045 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00046 ::Identity(rows, rows),
00047 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
00048 VectorType v1 = VectorType::Random(rows),
00049 v2 = VectorType::Random(rows),
00050 vzero = VectorType::Zero(rows);
00051 SquareMatrixType sm1 = SquareMatrixType::Random(rows,rows), sm2(rows,rows);
00052
00053 Scalar x = 0;
00054 while(x == Scalar(0)) x = internal::random<Scalar>();
00055
00056 Index r = internal::random<Index>(0, rows-1),
00057 c = internal::random<Index>(0, cols-1);
00058
00059 m1.coeffRef(r,c) = x;
00060 VERIFY_IS_APPROX(x, m1.coeff(r,c));
00061 m1(r,c) = x;
00062 VERIFY_IS_APPROX(x, m1(r,c));
00063 v1.coeffRef(r) = x;
00064 VERIFY_IS_APPROX(x, v1.coeff(r));
00065 v1(r) = x;
00066 VERIFY_IS_APPROX(x, v1(r));
00067 v1[r] = x;
00068 VERIFY_IS_APPROX(x, v1[r]);
00069
00070 VERIFY_IS_APPROX( v1, v1);
00071 VERIFY_IS_NOT_APPROX( v1, 2*v1);
00072 VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1);
00073 if(!NumTraits<Scalar>::IsInteger)
00074 VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.norm());
00075 VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1, v1);
00076 VERIFY_IS_APPROX( vzero, v1-v1);
00077 VERIFY_IS_APPROX( m1, m1);
00078 VERIFY_IS_NOT_APPROX( m1, 2*m1);
00079 VERIFY_IS_MUCH_SMALLER_THAN( mzero, m1);
00080 VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1, m1);
00081 VERIFY_IS_APPROX( mzero, m1-m1);
00082
00083
00084
00085
00086
00087
00088
00089 VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1));
00090
00091
00092 square.col(r) = square.row(r).eval();
00093 Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows);
00094 Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows);
00095 rv = square.row(r);
00096 cv = square.col(r);
00097
00098 VERIFY_IS_APPROX(rv, cv.transpose());
00099
00100 if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
00101 {
00102 VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
00103 }
00104
00105 if(cols!=1 && rows!=1)
00106 {
00107 VERIFY_RAISES_ASSERT(m1[0]);
00108 VERIFY_RAISES_ASSERT((m1+m1)[0]);
00109 }
00110
00111 VERIFY_IS_APPROX(m3 = m1,m1);
00112 MatrixType m4;
00113 VERIFY_IS_APPROX(m4 = m1,m1);
00114
00115 m3.real() = m1.real();
00116 VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
00117 VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
00118
00119
00120 VERIFY(m1==m1);
00121 VERIFY(m1!=m2);
00122 VERIFY(!(m1==m2));
00123 VERIFY(!(m1!=m1));
00124 m1 = m2;
00125 VERIFY(m1==m2);
00126 VERIFY(!(m1!=m2));
00127
00128
00129 sm2.setZero();
00130 for(typename MatrixType::Index i=0;i<rows;++i)
00131 sm2.col(i) = sm1.row(i);
00132 VERIFY_IS_APPROX(sm2,sm1.transpose());
00133
00134 sm2.setZero();
00135 for(typename MatrixType::Index i=0;i<rows;++i)
00136 sm2.col(i).noalias() = sm1.row(i);
00137 VERIFY_IS_APPROX(sm2,sm1.transpose());
00138
00139 sm2.setZero();
00140 for(typename MatrixType::Index i=0;i<rows;++i)
00141 sm2.col(i).noalias() += sm1.row(i);
00142 VERIFY_IS_APPROX(sm2,sm1.transpose());
00143
00144 sm2.setZero();
00145 for(typename MatrixType::Index i=0;i<rows;++i)
00146 sm2.col(i).noalias() -= sm1.row(i);
00147 VERIFY_IS_APPROX(sm2,-sm1.transpose());
00148 }
00149
00150 template<typename MatrixType> void basicStuffComplex(const MatrixType& m)
00151 {
00152 typedef typename MatrixType::Index Index;
00153 typedef typename MatrixType::Scalar Scalar;
00154 typedef typename NumTraits<Scalar>::Real RealScalar;
00155 typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> RealMatrixType;
00156
00157 Index rows = m.rows();
00158 Index cols = m.cols();
00159
00160 Scalar s1 = internal::random<Scalar>(),
00161 s2 = internal::random<Scalar>();
00162
00163 VERIFY(internal::real(s1)==internal::real_ref(s1));
00164 VERIFY(internal::imag(s1)==internal::imag_ref(s1));
00165 internal::real_ref(s1) = internal::real(s2);
00166 internal::imag_ref(s1) = internal::imag(s2);
00167 VERIFY(internal::isApprox(s1, s2, NumTraits<RealScalar>::epsilon()));
00168
00169
00170 RealMatrixType rm1 = RealMatrixType::Random(rows,cols),
00171 rm2 = RealMatrixType::Random(rows,cols);
00172 MatrixType cm(rows,cols);
00173 cm.real() = rm1;
00174 cm.imag() = rm2;
00175 VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
00176 VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
00177 rm1.setZero();
00178 rm2.setZero();
00179 rm1 = cm.real();
00180 rm2 = cm.imag();
00181 VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
00182 VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
00183 cm.real().setZero();
00184 VERIFY(static_cast<const MatrixType&>(cm).real().isZero());
00185 VERIFY(!static_cast<const MatrixType&>(cm).imag().isZero());
00186 }
00187
00188 #ifdef EIGEN_TEST_PART_2
00189 void casting()
00190 {
00191 Matrix4f m = Matrix4f::Random(), m2;
00192 Matrix4d n = m.cast<double>();
00193 VERIFY(m.isApprox(n.cast<float>()));
00194 m2 = m.cast<float>();
00195 VERIFY(m.isApprox(m2));
00196 }
00197 #endif
00198
00199 template <typename Scalar>
00200 void fixedSizeMatrixConstruction()
00201 {
00202 const Scalar raw[3] = {1,2,3};
00203 Matrix<Scalar,3,1> m(raw);
00204 Array<Scalar,3,1> a(raw);
00205 VERIFY(m(0) == 1);
00206 VERIFY(m(1) == 2);
00207 VERIFY(m(2) == 3);
00208 VERIFY(a(0) == 1);
00209 VERIFY(a(1) == 2);
00210 VERIFY(a(2) == 3);
00211 }
00212
00213 void test_basicstuff()
00214 {
00215 for(int i = 0; i < g_repeat; i++) {
00216 CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
00217 CALL_SUBTEST_2( basicStuff(Matrix4d()) );
00218 CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,100), internal::random<int>(1,100))) );
00219 CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,100), internal::random<int>(1,100))) );
00220 CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,100), internal::random<int>(1,100))) );
00221 CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) );
00222 CALL_SUBTEST_7( basicStuff(Matrix<long double,Dynamic,Dynamic>(internal::random<int>(1,100),internal::random<int>(1,100))) );
00223
00224 CALL_SUBTEST_3( basicStuffComplex(MatrixXcf(internal::random<int>(1,100), internal::random<int>(1,100))) );
00225 CALL_SUBTEST_5( basicStuffComplex(MatrixXcd(internal::random<int>(1,100), internal::random<int>(1,100))) );
00226 }
00227
00228 CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
00229 CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
00230 CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
00231
00232 CALL_SUBTEST_2(casting());
00233 }