• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #define EIGEN_NO_STATIC_ASSERT
11 
12 #include "main.h"
13 
basicStuff(const MatrixType & m)14 template<typename MatrixType> void basicStuff(const MatrixType& m)
15 {
16   typedef typename MatrixType::Index Index;
17   typedef typename MatrixType::Scalar Scalar;
18   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
19   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
20 
21   Index rows = m.rows();
22   Index cols = m.cols();
23 
24   // this test relies a lot on Random.h, and there's not much more that we can do
25   // to test it, hence I consider that we will have tested Random.h
26   MatrixType m1 = MatrixType::Random(rows, cols),
27              m2 = MatrixType::Random(rows, cols),
28              m3(rows, cols),
29              mzero = MatrixType::Zero(rows, cols),
30              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
31   VectorType v1 = VectorType::Random(rows),
32              vzero = VectorType::Zero(rows);
33   SquareMatrixType sm1 = SquareMatrixType::Random(rows,rows), sm2(rows,rows);
34 
35   Scalar x = 0;
36   while(x == Scalar(0)) x = internal::random<Scalar>();
37 
38   Index r = internal::random<Index>(0, rows-1),
39         c = internal::random<Index>(0, cols-1);
40 
41   m1.coeffRef(r,c) = x;
42   VERIFY_IS_APPROX(x, m1.coeff(r,c));
43   m1(r,c) = x;
44   VERIFY_IS_APPROX(x, m1(r,c));
45   v1.coeffRef(r) = x;
46   VERIFY_IS_APPROX(x, v1.coeff(r));
47   v1(r) = x;
48   VERIFY_IS_APPROX(x, v1(r));
49   v1[r] = x;
50   VERIFY_IS_APPROX(x, v1[r]);
51 
52   VERIFY_IS_APPROX(               v1,    v1);
53   VERIFY_IS_NOT_APPROX(           v1,    2*v1);
54   VERIFY_IS_MUCH_SMALLER_THAN(    vzero, v1);
55   VERIFY_IS_MUCH_SMALLER_THAN(  vzero, v1.squaredNorm());
56   VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1,    v1);
57   VERIFY_IS_APPROX(               vzero, v1-v1);
58   VERIFY_IS_APPROX(               m1,    m1);
59   VERIFY_IS_NOT_APPROX(           m1,    2*m1);
60   VERIFY_IS_MUCH_SMALLER_THAN(    mzero, m1);
61   VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1,    m1);
62   VERIFY_IS_APPROX(               mzero, m1-m1);
63 
64   // always test operator() on each read-only expression class,
65   // in order to check const-qualifiers.
66   // indeed, if an expression class (here Zero) is meant to be read-only,
67   // hence has no _write() method, the corresponding MatrixBase method (here zero())
68   // should return a const-qualified object so that it is the const-qualified
69   // operator() that gets called, which in turn calls _read().
70   VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1));
71 
72   // now test copying a row-vector into a (column-)vector and conversely.
73   square.col(r) = square.row(r).eval();
74   Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows);
75   Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows);
76   rv = square.row(r);
77   cv = square.col(r);
78 
79   VERIFY_IS_APPROX(rv, cv.transpose());
80 
81   if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
82   {
83     VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
84   }
85 
86   if(cols!=1 && rows!=1)
87   {
88     VERIFY_RAISES_ASSERT(m1[0]);
89     VERIFY_RAISES_ASSERT((m1+m1)[0]);
90   }
91 
92   VERIFY_IS_APPROX(m3 = m1,m1);
93   MatrixType m4;
94   VERIFY_IS_APPROX(m4 = m1,m1);
95 
96   m3.real() = m1.real();
97   VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
98   VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
99 
100   // check == / != operators
101   VERIFY(m1==m1);
102   VERIFY(m1!=m2);
103   VERIFY(!(m1==m2));
104   VERIFY(!(m1!=m1));
105   m1 = m2;
106   VERIFY(m1==m2);
107   VERIFY(!(m1!=m2));
108 
109   // check automatic transposition
110   sm2.setZero();
111   for(typename MatrixType::Index i=0;i<rows;++i)
112     sm2.col(i) = sm1.row(i);
113   VERIFY_IS_APPROX(sm2,sm1.transpose());
114 
115   sm2.setZero();
116   for(typename MatrixType::Index i=0;i<rows;++i)
117     sm2.col(i).noalias() = sm1.row(i);
118   VERIFY_IS_APPROX(sm2,sm1.transpose());
119 
120   sm2.setZero();
121   for(typename MatrixType::Index i=0;i<rows;++i)
122     sm2.col(i).noalias() += sm1.row(i);
123   VERIFY_IS_APPROX(sm2,sm1.transpose());
124 
125   sm2.setZero();
126   for(typename MatrixType::Index i=0;i<rows;++i)
127     sm2.col(i).noalias() -= sm1.row(i);
128   VERIFY_IS_APPROX(sm2,-sm1.transpose());
129 }
130 
basicStuffComplex(const MatrixType & m)131 template<typename MatrixType> void basicStuffComplex(const MatrixType& m)
132 {
133   typedef typename MatrixType::Index Index;
134   typedef typename MatrixType::Scalar Scalar;
135   typedef typename NumTraits<Scalar>::Real RealScalar;
136   typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> RealMatrixType;
137 
138   Index rows = m.rows();
139   Index cols = m.cols();
140 
141   Scalar s1 = internal::random<Scalar>(),
142          s2 = internal::random<Scalar>();
143 
144   VERIFY(numext::real(s1)==numext::real_ref(s1));
145   VERIFY(numext::imag(s1)==numext::imag_ref(s1));
146   numext::real_ref(s1) = numext::real(s2);
147   numext::imag_ref(s1) = numext::imag(s2);
148   VERIFY(internal::isApprox(s1, s2, NumTraits<RealScalar>::epsilon()));
149   // extended precision in Intel FPUs means that s1 == s2 in the line above is not guaranteed.
150 
151   RealMatrixType rm1 = RealMatrixType::Random(rows,cols),
152                  rm2 = RealMatrixType::Random(rows,cols);
153   MatrixType cm(rows,cols);
154   cm.real() = rm1;
155   cm.imag() = rm2;
156   VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
157   VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
158   rm1.setZero();
159   rm2.setZero();
160   rm1 = cm.real();
161   rm2 = cm.imag();
162   VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
163   VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
164   cm.real().setZero();
165   VERIFY(static_cast<const MatrixType&>(cm).real().isZero());
166   VERIFY(!static_cast<const MatrixType&>(cm).imag().isZero());
167 }
168 
169 #ifdef EIGEN_TEST_PART_2
casting()170 void casting()
171 {
172   Matrix4f m = Matrix4f::Random(), m2;
173   Matrix4d n = m.cast<double>();
174   VERIFY(m.isApprox(n.cast<float>()));
175   m2 = m.cast<float>(); // check the specialization when NewType == Type
176   VERIFY(m.isApprox(m2));
177 }
178 #endif
179 
180 template <typename Scalar>
fixedSizeMatrixConstruction()181 void fixedSizeMatrixConstruction()
182 {
183   const Scalar raw[3] = {1,2,3};
184   Matrix<Scalar,3,1> m(raw);
185   Array<Scalar,3,1> a(raw);
186   VERIFY(m(0) == 1);
187   VERIFY(m(1) == 2);
188   VERIFY(m(2) == 3);
189   VERIFY(a(0) == 1);
190   VERIFY(a(1) == 2);
191   VERIFY(a(2) == 3);
192 }
193 
test_basicstuff()194 void test_basicstuff()
195 {
196   for(int i = 0; i < g_repeat; i++) {
197     CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
198     CALL_SUBTEST_2( basicStuff(Matrix4d()) );
199     CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
200     CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
201     CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
202     CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) );
203     CALL_SUBTEST_7( basicStuff(Matrix<long double,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
204 
205     CALL_SUBTEST_3( basicStuffComplex(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
206     CALL_SUBTEST_5( basicStuffComplex(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
207   }
208 
209   CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
210   CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
211   CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
212 
213   CALL_SUBTEST_2(casting());
214 }
215