Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
| 2 | // for linear algebra. Eigen itself is part of the KDE project. |
| 3 | // |
| 4 | // Copyright (C) 2006-2007 Benoit Jacob <jacob@math.jussieu.fr> |
| 5 | // |
| 6 | // Eigen is free software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the Free Software |
| 8 | // Foundation; either version 2 or (at your option) any later version. |
| 9 | // |
| 10 | // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 | // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 13 | // details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License along |
| 16 | // with Eigen; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | // Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | // |
| 19 | // As a special exception, if other files instantiate templates or use macros |
| 20 | // or functions from this file, or you compile this file and link it |
| 21 | // with other works to produce a work based on this file, this file does not |
| 22 | // by itself cause the resulting work to be covered by the GNU General Public |
| 23 | // License. This exception does not invalidate any other reasons why a work |
| 24 | // based on this file might be covered by the GNU General Public License. |
| 25 | |
| 26 | #include "main.h" |
| 27 | |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 28 | namespace Eigen { |
| 29 | |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 30 | template<typename MatrixType> void basicStuff(const MatrixType& m) |
| 31 | { |
| 32 | typedef typename MatrixType::Scalar Scalar; |
Benoit Jacob | 84934ea | 2008-01-06 16:35:21 +0000 | [diff] [blame^] | 33 | typedef Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> VectorType; |
Benoit Jacob | dad245a | 2007-12-25 17:20:58 +0000 | [diff] [blame] | 34 | |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 35 | int rows = m.rows(); |
| 36 | int cols = m.cols(); |
| 37 | |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 38 | // this test relies a lot on Random.h, and there's not much more that we can do |
| 39 | // to test it, hence I consider that we will have tested Random.h |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 40 | MatrixType m1 = MatrixType::random(rows, cols), |
| 41 | m2 = MatrixType::random(rows, cols), |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 42 | m3(rows, cols), |
Benoit Jacob | 6c8f159 | 2007-10-14 09:18:18 +0000 | [diff] [blame] | 43 | mzero = MatrixType::zero(rows, cols), |
Benoit Jacob | 84934ea | 2008-01-06 16:35:21 +0000 | [diff] [blame^] | 44 | identity = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime> |
Benoit Jacob | 3f97918 | 2007-10-14 10:01:25 +0000 | [diff] [blame] | 45 | ::identity(rows), |
Benoit Jacob | 84934ea | 2008-01-06 16:35:21 +0000 | [diff] [blame^] | 46 | square = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime> |
Benoit Jacob | 6c8f159 | 2007-10-14 09:18:18 +0000 | [diff] [blame] | 47 | ::random(rows, rows); |
| 48 | VectorType v1 = VectorType::random(rows), |
| 49 | v2 = VectorType::random(rows), |
| 50 | vzero = VectorType::zero(rows); |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 51 | |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame] | 52 | int r = random<int>(0, rows-1), |
| 53 | c = random<int>(0, cols-1); |
| 54 | |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 55 | VERIFY_IS_APPROX( v1, v1); |
| 56 | VERIFY_IS_NOT_APPROX( v1, 2*v1); |
| 57 | VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1); |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 58 | if(NumTraits<Scalar>::HasFloatingPoint) |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 59 | VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.norm()); |
| 60 | VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1, v1); |
| 61 | VERIFY_IS_APPROX( vzero, v1-v1); |
| 62 | VERIFY_IS_APPROX( m1, m1); |
| 63 | VERIFY_IS_NOT_APPROX( m1, 2*m1); |
| 64 | VERIFY_IS_MUCH_SMALLER_THAN( mzero, m1); |
| 65 | VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1, m1); |
| 66 | VERIFY_IS_APPROX( mzero, m1-m1); |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 67 | |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame] | 68 | // always test operator() on each read-only expression class, |
| 69 | // in order to check const-qualifiers. |
| 70 | // indeed, if an expression class (here Zero) is meant to be read-only, |
| 71 | // hence has no _write() method, the corresponding MatrixBase method (here zero()) |
| 72 | // should return a const-qualified object so that it is the const-qualified |
| 73 | // operator() that gets called, which in turn calls _read(). |
Benoit Jacob | 7c38475 | 2007-12-15 18:16:30 +0000 | [diff] [blame] | 74 | VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::zero(rows,cols)(r,c), static_cast<Scalar>(1)); |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame] | 75 | |
Benoit Jacob | dad245a | 2007-12-25 17:20:58 +0000 | [diff] [blame] | 76 | // now test copying a row-vector into a (column-)vector and conversely. |
| 77 | square.col(r) = square.row(r).eval(); |
Benoit Jacob | 84934ea | 2008-01-06 16:35:21 +0000 | [diff] [blame^] | 78 | Matrix<Scalar, 1, MatrixType::Traits::RowsAtCompileTime> rv(rows); |
| 79 | Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> cv(rows); |
Benoit Jacob | dad245a | 2007-12-25 17:20:58 +0000 | [diff] [blame] | 80 | rv = square.col(r); |
| 81 | cv = square.row(r); |
| 82 | VERIFY_IS_APPROX(rv, cv.transpose()); |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void EigenTest::testBasicStuff() |
| 86 | { |
Benoit Jacob | 2b20814 | 2007-12-11 10:07:18 +0000 | [diff] [blame] | 87 | for(int i = 0; i < m_repeat; i++) { |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 88 | basicStuff(Matrix<float, 1, 1>()); |
Benoit Jacob | 04502cc | 2007-12-05 07:22:22 +0000 | [diff] [blame] | 89 | basicStuff(Matrix4d()); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 90 | basicStuff(MatrixXcf(3, 3)); |
| 91 | basicStuff(MatrixXi(8, 12)); |
Benoit Jacob | 04502cc | 2007-12-05 07:22:22 +0000 | [diff] [blame] | 92 | basicStuff(MatrixXcd(20, 20)); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 93 | } |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 94 | } |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 95 | |
| 96 | } // namespace Eigen |