Benoit Jacob | dad245a | 2007-12-25 17:20:58 +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 | dad245a | 2007-12-25 17:20:58 +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 |
| 8 | // License as published by the Free Software Foundation; either |
| 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 |
| 13 | // published by the Free Software Foundation; either version 2 of |
| 14 | // the License, or (at your option) any later version. |
Benoit Jacob | dad245a | 2007-12-25 17:20:58 +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 | dad245a | 2007-12-25 17:20:58 +0000 | [diff] [blame] | 20 | // |
Benoit Jacob | 3698d8c | 2008-02-28 15:44:45 +0000 | [diff] [blame] | 21 | // You should have received a copy of the GNU Lesser General Public |
| 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 | dad245a | 2007-12-25 17:20:58 +0000 | [diff] [blame] | 24 | |
| 25 | #include "main.h" |
| 26 | |
| 27 | namespace Eigen { |
| 28 | |
| 29 | template<typename MatrixType> void linearStructure(const MatrixType& m) |
| 30 | { |
| 31 | /* this test covers the following files: |
| 32 | Sum.h Difference.h Opposite.h ScalarMultiple.h |
| 33 | */ |
| 34 | |
| 35 | typedef typename MatrixType::Scalar Scalar; |
Benoit Jacob | 84934ea | 2008-01-06 16:35:21 +0000 | [diff] [blame] | 36 | typedef Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> VectorType; |
Benoit Jacob | dad245a | 2007-12-25 17:20:58 +0000 | [diff] [blame] | 37 | |
| 38 | int rows = m.rows(); |
| 39 | int cols = m.cols(); |
| 40 | |
| 41 | // this test relies a lot on Random.h, and there's not much more that we can do |
| 42 | // to test it, hence I consider that we will have tested Random.h |
| 43 | MatrixType m1 = MatrixType::random(rows, cols), |
| 44 | m2 = MatrixType::random(rows, cols), |
| 45 | m3(rows, cols), |
| 46 | mzero = MatrixType::zero(rows, cols), |
Benoit Jacob | 84934ea | 2008-01-06 16:35:21 +0000 | [diff] [blame] | 47 | identity = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime> |
Benoit Jacob | bcf7b29 | 2008-01-11 15:56:21 +0000 | [diff] [blame] | 48 | ::identity(rows, rows), |
Benoit Jacob | 84934ea | 2008-01-06 16:35:21 +0000 | [diff] [blame] | 49 | square = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime> |
Benoit Jacob | dad245a | 2007-12-25 17:20:58 +0000 | [diff] [blame] | 50 | ::random(rows, rows); |
| 51 | VectorType v1 = VectorType::random(rows), |
| 52 | v2 = VectorType::random(rows), |
| 53 | vzero = VectorType::zero(rows); |
| 54 | |
Benoit Jacob | 6907886 | 2008-02-28 12:38:12 +0000 | [diff] [blame] | 55 | Scalar s1 = ei_random<Scalar>(), |
| 56 | s2 = ei_random<Scalar>(); |
Benoit Jacob | dad245a | 2007-12-25 17:20:58 +0000 | [diff] [blame] | 57 | |
Benoit Jacob | 6907886 | 2008-02-28 12:38:12 +0000 | [diff] [blame] | 58 | int r = ei_random<int>(0, rows-1), |
| 59 | c = ei_random<int>(0, cols-1); |
Benoit Jacob | dad245a | 2007-12-25 17:20:58 +0000 | [diff] [blame] | 60 | |
| 61 | VERIFY_IS_APPROX(-(-m1), m1); |
| 62 | VERIFY_IS_APPROX(m1+m1, 2*m1); |
| 63 | VERIFY_IS_APPROX(m1+m2-m1, m2); |
| 64 | VERIFY_IS_APPROX(-m2+m1+m2, m1); |
| 65 | VERIFY_IS_APPROX(m1*s1, s1*m1); |
| 66 | VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2); |
| 67 | VERIFY_IS_APPROX((s1+s2)*m1, m1*s1+m1*s2); |
| 68 | VERIFY_IS_APPROX((m1-m2)*s1, s1*m1-s1*m2); |
| 69 | VERIFY_IS_APPROX((s1-s2)*m1, m1*s1-m1*s2); |
| 70 | VERIFY_IS_APPROX((-m1+m2)*s1, -s1*m1+s1*m2); |
| 71 | VERIFY_IS_APPROX((-s1+s2)*m1, -m1*s1+m1*s2); |
| 72 | m3 = m2; m3 += m1; |
| 73 | VERIFY_IS_APPROX(m3, m1+m2); |
| 74 | m3 = m2; m3 -= m1; |
| 75 | VERIFY_IS_APPROX(m3, m2-m1); |
| 76 | m3 = m2; m3 *= s1; |
| 77 | VERIFY_IS_APPROX(m3, s1*m2); |
| 78 | if(NumTraits<Scalar>::HasFloatingPoint) |
| 79 | { |
| 80 | m3 = m2; m3 /= s1; |
| 81 | VERIFY_IS_APPROX(m3, m2/s1); |
| 82 | } |
| 83 | |
| 84 | // again, test operator() to check const-qualification |
| 85 | VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c))); |
| 86 | VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c))); |
| 87 | VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c))); |
| 88 | VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c))); |
| 89 | VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1); |
| 90 | if(NumTraits<Scalar>::HasFloatingPoint) |
| 91 | VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1); |
| 92 | } |
| 93 | |
| 94 | void EigenTest::testLinearStructure() |
| 95 | { |
| 96 | for(int i = 0; i < m_repeat; i++) { |
| 97 | linearStructure(Matrix<float, 1, 1>()); |
| 98 | linearStructure(Matrix4d()); |
| 99 | linearStructure(MatrixXcf(3, 3)); |
| 100 | linearStructure(MatrixXi(8, 12)); |
| 101 | linearStructure(MatrixXcd(20, 20)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | } // namespace Eigen |