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
14template<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  if(!NumTraits<Scalar>::IsInteger)
56    VERIFY_IS_MUCH_SMALLER_THAN(  vzero, v1.norm());
57  VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1,    v1);
58  VERIFY_IS_APPROX(               vzero, v1-v1);
59  VERIFY_IS_APPROX(               m1,    m1);
60  VERIFY_IS_NOT_APPROX(           m1,    2*m1);
61  VERIFY_IS_MUCH_SMALLER_THAN(    mzero, m1);
62  VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1,    m1);
63  VERIFY_IS_APPROX(               mzero, m1-m1);
64
65  // always test operator() on each read-only expression class,
66  // in order to check const-qualifiers.
67  // indeed, if an expression class (here Zero) is meant to be read-only,
68  // hence has no _write() method, the corresponding MatrixBase method (here zero())
69  // should return a const-qualified object so that it is the const-qualified
70  // operator() that gets called, which in turn calls _read().
71  VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1));
72
73  // now test copying a row-vector into a (column-)vector and conversely.
74  square.col(r) = square.row(r).eval();
75  Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows);
76  Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows);
77  rv = square.row(r);
78  cv = square.col(r);
79
80  VERIFY_IS_APPROX(rv, cv.transpose());
81
82  if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
83  {
84    VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
85  }
86
87  if(cols!=1 && rows!=1)
88  {
89    VERIFY_RAISES_ASSERT(m1[0]);
90    VERIFY_RAISES_ASSERT((m1+m1)[0]);
91  }
92
93  VERIFY_IS_APPROX(m3 = m1,m1);
94  MatrixType m4;
95  VERIFY_IS_APPROX(m4 = m1,m1);
96
97  m3.real() = m1.real();
98  VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
99  VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
100
101  // check == / != operators
102  VERIFY(m1==m1);
103  VERIFY(m1!=m2);
104  VERIFY(!(m1==m2));
105  VERIFY(!(m1!=m1));
106  m1 = m2;
107  VERIFY(m1==m2);
108  VERIFY(!(m1!=m2));
109
110  // check automatic transposition
111  sm2.setZero();
112  for(typename MatrixType::Index i=0;i<rows;++i)
113    sm2.col(i) = sm1.row(i);
114  VERIFY_IS_APPROX(sm2,sm1.transpose());
115
116  sm2.setZero();
117  for(typename MatrixType::Index i=0;i<rows;++i)
118    sm2.col(i).noalias() = sm1.row(i);
119  VERIFY_IS_APPROX(sm2,sm1.transpose());
120
121  sm2.setZero();
122  for(typename MatrixType::Index i=0;i<rows;++i)
123    sm2.col(i).noalias() += sm1.row(i);
124  VERIFY_IS_APPROX(sm2,sm1.transpose());
125
126  sm2.setZero();
127  for(typename MatrixType::Index i=0;i<rows;++i)
128    sm2.col(i).noalias() -= sm1.row(i);
129  VERIFY_IS_APPROX(sm2,-sm1.transpose());
130}
131
132template<typename MatrixType> void basicStuffComplex(const MatrixType& m)
133{
134  typedef typename MatrixType::Index Index;
135  typedef typename MatrixType::Scalar Scalar;
136  typedef typename NumTraits<Scalar>::Real RealScalar;
137  typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> RealMatrixType;
138
139  Index rows = m.rows();
140  Index cols = m.cols();
141
142  Scalar s1 = internal::random<Scalar>(),
143         s2 = internal::random<Scalar>();
144
145  VERIFY(internal::real(s1)==internal::real_ref(s1));
146  VERIFY(internal::imag(s1)==internal::imag_ref(s1));
147  internal::real_ref(s1) = internal::real(s2);
148  internal::imag_ref(s1) = internal::imag(s2);
149  VERIFY(internal::isApprox(s1, s2, NumTraits<RealScalar>::epsilon()));
150  // extended precision in Intel FPUs means that s1 == s2 in the line above is not guaranteed.
151
152  RealMatrixType rm1 = RealMatrixType::Random(rows,cols),
153                 rm2 = RealMatrixType::Random(rows,cols);
154  MatrixType cm(rows,cols);
155  cm.real() = rm1;
156  cm.imag() = rm2;
157  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
158  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
159  rm1.setZero();
160  rm2.setZero();
161  rm1 = cm.real();
162  rm2 = cm.imag();
163  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
164  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
165  cm.real().setZero();
166  VERIFY(static_cast<const MatrixType&>(cm).real().isZero());
167  VERIFY(!static_cast<const MatrixType&>(cm).imag().isZero());
168}
169
170#ifdef EIGEN_TEST_PART_2
171void casting()
172{
173  Matrix4f m = Matrix4f::Random(), m2;
174  Matrix4d n = m.cast<double>();
175  VERIFY(m.isApprox(n.cast<float>()));
176  m2 = m.cast<float>(); // check the specialization when NewType == Type
177  VERIFY(m.isApprox(m2));
178}
179#endif
180
181template <typename Scalar>
182void fixedSizeMatrixConstruction()
183{
184  const Scalar raw[3] = {1,2,3};
185  Matrix<Scalar,3,1> m(raw);
186  Array<Scalar,3,1> a(raw);
187  VERIFY(m(0) == 1);
188  VERIFY(m(1) == 2);
189  VERIFY(m(2) == 3);
190  VERIFY(a(0) == 1);
191  VERIFY(a(1) == 2);
192  VERIFY(a(2) == 3);
193}
194
195void test_basicstuff()
196{
197  for(int i = 0; i < g_repeat; i++) {
198    CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
199    CALL_SUBTEST_2( basicStuff(Matrix4d()) );
200    CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
201    CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
202    CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
203    CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) );
204    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))) );
205
206    CALL_SUBTEST_3( basicStuffComplex(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
207    CALL_SUBTEST_5( basicStuffComplex(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
208  }
209
210  CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
211  CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
212  CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
213
214  CALL_SUBTEST_2(casting());
215}
216