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 | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame^] | 32 | typedef Matrix<Scalar, MatrixType::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 | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame^] | 43 | identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
Benoit Jacob | bcf7b29 | 2008-01-11 15:56:21 +0000 | [diff] [blame] | 44 | ::identity(rows, rows), |
Benoit Jacob | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame^] | 45 | square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::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 | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame^] | 77 | Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows); |
| 78 | Matrix<Scalar, MatrixType::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()); |
Gael Guennebaud | 721626d | 2008-03-08 19:02:24 +0000 | [diff] [blame] | 82 | |
Benoit Jacob | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame^] | 83 | if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic) |
Gael Guennebaud | 721626d | 2008-03-08 19:02:24 +0000 | [diff] [blame] | 84 | { |
| 85 | VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1))); |
| 86 | } |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void EigenTest::testBasicStuff() |
| 90 | { |
Benoit Jacob | 2b20814 | 2007-12-11 10:07:18 +0000 | [diff] [blame] | 91 | for(int i = 0; i < m_repeat; i++) { |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 92 | basicStuff(Matrix<float, 1, 1>()); |
Benoit Jacob | 04502cc | 2007-12-05 07:22:22 +0000 | [diff] [blame] | 93 | basicStuff(Matrix4d()); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 94 | basicStuff(MatrixXcf(3, 3)); |
| 95 | basicStuff(MatrixXi(8, 12)); |
Benoit Jacob | 04502cc | 2007-12-05 07:22:22 +0000 | [diff] [blame] | 96 | basicStuff(MatrixXcd(20, 20)); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 97 | basicStuff(Matrix<float, 100, 100>()); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 98 | } |
Gael Guennebaud | 721626d | 2008-03-08 19:02:24 +0000 | [diff] [blame] | 99 | |
| 100 | // some additional basic tests |
| 101 | { |
| 102 | Matrix3d m3; |
| 103 | Matrix4d m4; |
| 104 | VERIFY_RAISES_ASSERT(m4 = m3); |
| 105 | |
| 106 | VERIFY_RAISES_ASSERT( (m3 << 1, 2, 3, 4, 5, 6, 7, 8) ); |
| 107 | VERIFY_RAISES_ASSERT( (m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ); |
Gael Guennebaud | f64311e | 2008-03-08 19:46:06 +0000 | [diff] [blame] | 108 | |
Gael Guennebaud | 721626d | 2008-03-08 19:02:24 +0000 | [diff] [blame] | 109 | double data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
Gael Guennebaud | f64311e | 2008-03-08 19:46:06 +0000 | [diff] [blame] | 110 | |
| 111 | m3 = Matrix3d::random(); |
| 112 | m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
| 113 | VERIFY_IS_APPROX(m3, (Matrix<double,3,3,RowMajor>::map(data)) ); |
| 114 | |
| 115 | Vector3d vec[3]; |
| 116 | vec[0] << 1, 4, 7; |
| 117 | vec[1] << 2, 5, 8; |
| 118 | vec[2] << 3, 6, 9; |
| 119 | m3 = Matrix3d::random(); |
| 120 | m3 << vec[0], vec[1], vec[2]; |
| 121 | VERIFY_IS_APPROX(m3, (Matrix<double,3,3,RowMajor>::map(data)) ); |
| 122 | |
| 123 | vec[0] << 1, 2, 3; |
| 124 | vec[1] << 4, 5, 6; |
| 125 | vec[2] << 7, 8, 9; |
| 126 | m3 = Matrix3d::random(); |
| 127 | m3 << vec[0].transpose(), |
| 128 | 4, 5, 6, |
| 129 | vec[2].transpose(); |
Gael Guennebaud | 721626d | 2008-03-08 19:02:24 +0000 | [diff] [blame] | 130 | VERIFY_IS_APPROX(m3, (Matrix<double,3,3,RowMajor>::map(data)) ); |
| 131 | } |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 132 | } |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 133 | |
| 134 | } // namespace Eigen |