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