basicstuff.cpp (13366B)
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 #include "random_without_cast_overflow.h" 14 15 template<typename MatrixType> void basicStuff(const MatrixType& m) 16 { 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 // test fetching with various index types. 53 Index r1 = internal::random<Index>(0, numext::mini(Index(127),rows-1)); 54 x = v1(static_cast<char>(r1)); 55 x = v1(static_cast<signed char>(r1)); 56 x = v1(static_cast<unsigned char>(r1)); 57 x = v1(static_cast<signed short>(r1)); 58 x = v1(static_cast<unsigned short>(r1)); 59 x = v1(static_cast<signed int>(r1)); 60 x = v1(static_cast<unsigned int>(r1)); 61 x = v1(static_cast<signed long>(r1)); 62 x = v1(static_cast<unsigned long>(r1)); 63 #if EIGEN_HAS_CXX11 64 x = v1(static_cast<long long int>(r1)); 65 x = v1(static_cast<unsigned long long int>(r1)); 66 #endif 67 68 VERIFY_IS_APPROX( v1, v1); 69 VERIFY_IS_NOT_APPROX( v1, 2*v1); 70 VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1); 71 VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.squaredNorm()); 72 VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1, v1); 73 VERIFY_IS_APPROX( vzero, v1-v1); 74 VERIFY_IS_APPROX( m1, m1); 75 VERIFY_IS_NOT_APPROX( m1, 2*m1); 76 VERIFY_IS_MUCH_SMALLER_THAN( mzero, m1); 77 VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1, m1); 78 VERIFY_IS_APPROX( mzero, m1-m1); 79 80 // always test operator() on each read-only expression class, 81 // in order to check const-qualifiers. 82 // indeed, if an expression class (here Zero) is meant to be read-only, 83 // hence has no _write() method, the corresponding MatrixBase method (here zero()) 84 // should return a const-qualified object so that it is the const-qualified 85 // operator() that gets called, which in turn calls _read(). 86 VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1)); 87 88 // now test copying a row-vector into a (column-)vector and conversely. 89 square.col(r) = square.row(r).eval(); 90 Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows); 91 Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows); 92 rv = square.row(r); 93 cv = square.col(r); 94 95 VERIFY_IS_APPROX(rv, cv.transpose()); 96 97 if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic) 98 { 99 VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1))); 100 } 101 102 if(cols!=1 && rows!=1) 103 { 104 VERIFY_RAISES_ASSERT(m1[0]); 105 VERIFY_RAISES_ASSERT((m1+m1)[0]); 106 } 107 108 VERIFY_IS_APPROX(m3 = m1,m1); 109 MatrixType m4; 110 VERIFY_IS_APPROX(m4 = m1,m1); 111 112 m3.real() = m1.real(); 113 VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real()); 114 VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real()); 115 116 // check == / != operators 117 VERIFY(m1==m1); 118 VERIFY(m1!=m2); 119 VERIFY(!(m1==m2)); 120 VERIFY(!(m1!=m1)); 121 m1 = m2; 122 VERIFY(m1==m2); 123 VERIFY(!(m1!=m2)); 124 125 // check automatic transposition 126 sm2.setZero(); 127 for(Index i=0;i<rows;++i) 128 sm2.col(i) = sm1.row(i); 129 VERIFY_IS_APPROX(sm2,sm1.transpose()); 130 131 sm2.setZero(); 132 for(Index i=0;i<rows;++i) 133 sm2.col(i).noalias() = sm1.row(i); 134 VERIFY_IS_APPROX(sm2,sm1.transpose()); 135 136 sm2.setZero(); 137 for(Index i=0;i<rows;++i) 138 sm2.col(i).noalias() += sm1.row(i); 139 VERIFY_IS_APPROX(sm2,sm1.transpose()); 140 141 sm2.setZero(); 142 for(Index i=0;i<rows;++i) 143 sm2.col(i).noalias() -= sm1.row(i); 144 VERIFY_IS_APPROX(sm2,-sm1.transpose()); 145 146 // check ternary usage 147 { 148 bool b = internal::random<int>(0,10)>5; 149 m3 = b ? m1 : m2; 150 if(b) VERIFY_IS_APPROX(m3,m1); 151 else VERIFY_IS_APPROX(m3,m2); 152 m3 = b ? -m1 : m2; 153 if(b) VERIFY_IS_APPROX(m3,-m1); 154 else VERIFY_IS_APPROX(m3,m2); 155 m3 = b ? m1 : -m2; 156 if(b) VERIFY_IS_APPROX(m3,m1); 157 else VERIFY_IS_APPROX(m3,-m2); 158 } 159 } 160 161 template<typename MatrixType> void basicStuffComplex(const MatrixType& m) 162 { 163 typedef typename MatrixType::Scalar Scalar; 164 typedef typename NumTraits<Scalar>::Real RealScalar; 165 typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> RealMatrixType; 166 167 Index rows = m.rows(); 168 Index cols = m.cols(); 169 170 Scalar s1 = internal::random<Scalar>(), 171 s2 = internal::random<Scalar>(); 172 173 VERIFY(numext::real(s1)==numext::real_ref(s1)); 174 VERIFY(numext::imag(s1)==numext::imag_ref(s1)); 175 numext::real_ref(s1) = numext::real(s2); 176 numext::imag_ref(s1) = numext::imag(s2); 177 VERIFY(internal::isApprox(s1, s2, NumTraits<RealScalar>::epsilon())); 178 // extended precision in Intel FPUs means that s1 == s2 in the line above is not guaranteed. 179 180 RealMatrixType rm1 = RealMatrixType::Random(rows,cols), 181 rm2 = RealMatrixType::Random(rows,cols); 182 MatrixType cm(rows,cols); 183 cm.real() = rm1; 184 cm.imag() = rm2; 185 VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1); 186 VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2); 187 rm1.setZero(); 188 rm2.setZero(); 189 rm1 = cm.real(); 190 rm2 = cm.imag(); 191 VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1); 192 VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2); 193 cm.real().setZero(); 194 VERIFY(static_cast<const MatrixType&>(cm).real().isZero()); 195 VERIFY(!static_cast<const MatrixType&>(cm).imag().isZero()); 196 } 197 198 template<typename SrcScalar, typename TgtScalar> 199 struct casting_test { 200 static void run() { 201 Matrix<SrcScalar,4,4> m; 202 for (int i=0; i<m.rows(); ++i) { 203 for (int j=0; j<m.cols(); ++j) { 204 m(i, j) = internal::random_without_cast_overflow<SrcScalar,TgtScalar>::value(); 205 } 206 } 207 Matrix<TgtScalar,4,4> n = m.template cast<TgtScalar>(); 208 for (int i=0; i<m.rows(); ++i) { 209 for (int j=0; j<m.cols(); ++j) { 210 VERIFY_IS_APPROX(n(i, j), (internal::cast<SrcScalar,TgtScalar>(m(i, j)))); 211 } 212 } 213 } 214 }; 215 216 template<typename SrcScalar, typename EnableIf = void> 217 struct casting_test_runner { 218 static void run() { 219 casting_test<SrcScalar, bool>::run(); 220 casting_test<SrcScalar, int8_t>::run(); 221 casting_test<SrcScalar, uint8_t>::run(); 222 casting_test<SrcScalar, int16_t>::run(); 223 casting_test<SrcScalar, uint16_t>::run(); 224 casting_test<SrcScalar, int32_t>::run(); 225 casting_test<SrcScalar, uint32_t>::run(); 226 #if EIGEN_HAS_CXX11 227 casting_test<SrcScalar, int64_t>::run(); 228 casting_test<SrcScalar, uint64_t>::run(); 229 #endif 230 casting_test<SrcScalar, half>::run(); 231 casting_test<SrcScalar, bfloat16>::run(); 232 casting_test<SrcScalar, float>::run(); 233 casting_test<SrcScalar, double>::run(); 234 casting_test<SrcScalar, std::complex<float> >::run(); 235 casting_test<SrcScalar, std::complex<double> >::run(); 236 } 237 }; 238 239 template<typename SrcScalar> 240 struct casting_test_runner<SrcScalar, typename internal::enable_if<(NumTraits<SrcScalar>::IsComplex)>::type> 241 { 242 static void run() { 243 // Only a few casts from std::complex<T> are defined. 244 casting_test<SrcScalar, half>::run(); 245 casting_test<SrcScalar, bfloat16>::run(); 246 casting_test<SrcScalar, std::complex<float> >::run(); 247 casting_test<SrcScalar, std::complex<double> >::run(); 248 } 249 }; 250 251 void casting_all() { 252 casting_test_runner<bool>::run(); 253 casting_test_runner<int8_t>::run(); 254 casting_test_runner<uint8_t>::run(); 255 casting_test_runner<int16_t>::run(); 256 casting_test_runner<uint16_t>::run(); 257 casting_test_runner<int32_t>::run(); 258 casting_test_runner<uint32_t>::run(); 259 #if EIGEN_HAS_CXX11 260 casting_test_runner<int64_t>::run(); 261 casting_test_runner<uint64_t>::run(); 262 #endif 263 casting_test_runner<half>::run(); 264 casting_test_runner<bfloat16>::run(); 265 casting_test_runner<float>::run(); 266 casting_test_runner<double>::run(); 267 casting_test_runner<std::complex<float> >::run(); 268 casting_test_runner<std::complex<double> >::run(); 269 } 270 271 template <typename Scalar> 272 void fixedSizeMatrixConstruction() 273 { 274 Scalar raw[4]; 275 for(int k=0; k<4; ++k) 276 raw[k] = internal::random<Scalar>(); 277 278 { 279 Matrix<Scalar,4,1> m(raw); 280 Array<Scalar,4,1> a(raw); 281 for(int k=0; k<4; ++k) VERIFY(m(k) == raw[k]); 282 for(int k=0; k<4; ++k) VERIFY(a(k) == raw[k]); 283 VERIFY_IS_EQUAL(m,(Matrix<Scalar,4,1>(raw[0],raw[1],raw[2],raw[3]))); 284 VERIFY((a==(Array<Scalar,4,1>(raw[0],raw[1],raw[2],raw[3]))).all()); 285 } 286 { 287 Matrix<Scalar,3,1> m(raw); 288 Array<Scalar,3,1> a(raw); 289 for(int k=0; k<3; ++k) VERIFY(m(k) == raw[k]); 290 for(int k=0; k<3; ++k) VERIFY(a(k) == raw[k]); 291 VERIFY_IS_EQUAL(m,(Matrix<Scalar,3,1>(raw[0],raw[1],raw[2]))); 292 VERIFY((a==Array<Scalar,3,1>(raw[0],raw[1],raw[2])).all()); 293 } 294 { 295 Matrix<Scalar,2,1> m(raw), m2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) ); 296 Array<Scalar,2,1> a(raw), a2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) ); 297 for(int k=0; k<2; ++k) VERIFY(m(k) == raw[k]); 298 for(int k=0; k<2; ++k) VERIFY(a(k) == raw[k]); 299 VERIFY_IS_EQUAL(m,(Matrix<Scalar,2,1>(raw[0],raw[1]))); 300 VERIFY((a==Array<Scalar,2,1>(raw[0],raw[1])).all()); 301 for(int k=0; k<2; ++k) VERIFY(m2(k) == DenseIndex(raw[k])); 302 for(int k=0; k<2; ++k) VERIFY(a2(k) == DenseIndex(raw[k])); 303 } 304 { 305 Matrix<Scalar,1,2> m(raw), 306 m2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) ), 307 m3( (int(raw[0])), (int(raw[1])) ), 308 m4( (float(raw[0])), (float(raw[1])) ); 309 Array<Scalar,1,2> a(raw), a2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) ); 310 for(int k=0; k<2; ++k) VERIFY(m(k) == raw[k]); 311 for(int k=0; k<2; ++k) VERIFY(a(k) == raw[k]); 312 VERIFY_IS_EQUAL(m,(Matrix<Scalar,1,2>(raw[0],raw[1]))); 313 VERIFY((a==Array<Scalar,1,2>(raw[0],raw[1])).all()); 314 for(int k=0; k<2; ++k) VERIFY(m2(k) == DenseIndex(raw[k])); 315 for(int k=0; k<2; ++k) VERIFY(a2(k) == DenseIndex(raw[k])); 316 for(int k=0; k<2; ++k) VERIFY(m3(k) == int(raw[k])); 317 for(int k=0; k<2; ++k) VERIFY((m4(k)) == Scalar(float(raw[k]))); 318 } 319 { 320 Matrix<Scalar,1,1> m(raw), m1(raw[0]), m2( (DenseIndex(raw[0])) ), m3( (int(raw[0])) ); 321 Array<Scalar,1,1> a(raw), a1(raw[0]), a2( (DenseIndex(raw[0])) ); 322 VERIFY(m(0) == raw[0]); 323 VERIFY(a(0) == raw[0]); 324 VERIFY(m1(0) == raw[0]); 325 VERIFY(a1(0) == raw[0]); 326 VERIFY(m2(0) == DenseIndex(raw[0])); 327 VERIFY(a2(0) == DenseIndex(raw[0])); 328 VERIFY(m3(0) == int(raw[0])); 329 VERIFY_IS_EQUAL(m,(Matrix<Scalar,1,1>(raw[0]))); 330 VERIFY((a==Array<Scalar,1,1>(raw[0])).all()); 331 } 332 } 333 334 EIGEN_DECLARE_TEST(basicstuff) 335 { 336 for(int i = 0; i < g_repeat; i++) { 337 CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) ); 338 CALL_SUBTEST_2( basicStuff(Matrix4d()) ); 339 CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 340 CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 341 CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 342 CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) ); 343 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))) ); 344 CALL_SUBTEST_8( casting_all() ); 345 346 CALL_SUBTEST_3( basicStuffComplex(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 347 CALL_SUBTEST_5( basicStuffComplex(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 348 } 349 350 CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>()); 351 CALL_SUBTEST_1(fixedSizeMatrixConstruction<float>()); 352 CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>()); 353 CALL_SUBTEST_1(fixedSizeMatrixConstruction<int>()); 354 CALL_SUBTEST_1(fixedSizeMatrixConstruction<long int>()); 355 CALL_SUBTEST_1(fixedSizeMatrixConstruction<std::ptrdiff_t>()); 356 }