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 | { |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 32 | /* this test covers the following files: |
| 33 | 1) Explicitly (see comments below): |
| 34 | Random.h Zero.h Identity.h Fuzzy.h Sum.h Difference.h |
| 35 | Opposite.h Product.h ScalarMultiple.h FromArray.h |
| 36 | |
| 37 | 2) Implicitly (the core stuff): |
Benoit Jacob | 39f1776 | 2007-11-27 13:57:51 +0000 | [diff] [blame] | 38 | MatrixBase.h Matrix.h MatrixStorage.h CopyHelper.h MatrixRef.h |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 39 | NumTraits.h Util.h |
| 40 | */ |
| 41 | |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 42 | typedef typename MatrixType::Scalar Scalar; |
| 43 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
| 44 | int rows = m.rows(); |
| 45 | int cols = m.cols(); |
| 46 | |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 47 | // this test relies a lot on Random.h, and there's not much more that we can do |
| 48 | // 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] | 49 | MatrixType m1 = MatrixType::random(rows, cols), |
| 50 | m2 = MatrixType::random(rows, cols), |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 51 | m3(rows, cols), |
Benoit Jacob | 6c8f159 | 2007-10-14 09:18:18 +0000 | [diff] [blame] | 52 | mzero = MatrixType::zero(rows, cols), |
| 53 | identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
Benoit Jacob | 3f97918 | 2007-10-14 10:01:25 +0000 | [diff] [blame] | 54 | ::identity(rows), |
Benoit Jacob | 6c8f159 | 2007-10-14 09:18:18 +0000 | [diff] [blame] | 55 | square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
| 56 | ::random(rows, rows); |
| 57 | VectorType v1 = VectorType::random(rows), |
| 58 | v2 = VectorType::random(rows), |
| 59 | vzero = VectorType::zero(rows); |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 60 | |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 61 | Scalar s1 = random<Scalar>(), |
| 62 | s2 = random<Scalar>(); |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 63 | |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame^] | 64 | int r = random<int>(0, rows-1), |
| 65 | c = random<int>(0, cols-1); |
| 66 | |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 67 | // test Fuzzy.h and Zero.h. |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 68 | VERIFY_IS_APPROX( v1, v1); |
| 69 | VERIFY_IS_NOT_APPROX( v1, 2*v1); |
| 70 | VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1); |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 71 | if(NumTraits<Scalar>::HasFloatingPoint) |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 72 | VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.norm()); |
| 73 | VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1, v1); |
| 74 | VERIFY_IS_APPROX( vzero, v1-v1); |
| 75 | VERIFY_IS_APPROX( m1, m1); |
| 76 | VERIFY_IS_NOT_APPROX( m1, 2*m1); |
| 77 | VERIFY_IS_MUCH_SMALLER_THAN( mzero, m1); |
| 78 | VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1, m1); |
| 79 | VERIFY_IS_APPROX( mzero, m1-m1); |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 80 | |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame^] | 81 | // always test operator() on each read-only expression class, |
| 82 | // in order to check const-qualifiers. |
| 83 | // indeed, if an expression class (here Zero) is meant to be read-only, |
| 84 | // hence has no _write() method, the corresponding MatrixBase method (here zero()) |
| 85 | // should return a const-qualified object so that it is the const-qualified |
| 86 | // operator() that gets called, which in turn calls _read(). |
| 87 | VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::zero()(r,c), static_cast<Scalar>(1)); |
| 88 | |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 89 | // test the linear structure, i.e. the following files: |
| 90 | // Sum.h Difference.h Opposite.h ScalarMultiple.h |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 91 | VERIFY_IS_APPROX(-(-m1), m1); |
| 92 | VERIFY_IS_APPROX(m1+m1, 2*m1); |
| 93 | VERIFY_IS_APPROX(m1+m2-m1, m2); |
| 94 | VERIFY_IS_APPROX(-m2+m1+m2, m1); |
| 95 | VERIFY_IS_APPROX(m1*s1, s1*m1); |
| 96 | VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2); |
| 97 | VERIFY_IS_APPROX((s1+s2)*m1, m1*s1+m1*s2); |
| 98 | VERIFY_IS_APPROX((m1-m2)*s1, s1*m1-s1*m2); |
| 99 | VERIFY_IS_APPROX((s1-s2)*m1, m1*s1-m1*s2); |
| 100 | VERIFY_IS_APPROX((-m1+m2)*s1, -s1*m1+s1*m2); |
| 101 | VERIFY_IS_APPROX((-s1+s2)*m1, -m1*s1+m1*s2); |
| 102 | m3 = m2; m3 += m1; |
| 103 | VERIFY_IS_APPROX(m3, m1+m2); |
| 104 | m3 = m2; m3 -= m1; |
| 105 | VERIFY_IS_APPROX(m3, m2-m1); |
| 106 | m3 = m2; m3 *= s1; |
| 107 | VERIFY_IS_APPROX(m3, s1*m2); |
| 108 | if(NumTraits<Scalar>::HasFloatingPoint) |
| 109 | { |
| 110 | m3 = m2; m3 /= s1; |
| 111 | VERIFY_IS_APPROX(m3, m2/s1); |
| 112 | } |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 113 | |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame^] | 114 | // again, test operator() to check const-qualification |
| 115 | VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c))); |
| 116 | VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c))); |
| 117 | VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c))); |
| 118 | VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c))); |
| 119 | VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1); |
| 120 | if(NumTraits<Scalar>::HasFloatingPoint) |
| 121 | VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1); |
| 122 | |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 123 | // begin testing Product.h: only associativity for now |
| 124 | // (we use Transpose.h but this doesn't count as a test for it) |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 125 | VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2)); |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 126 | m3 = m1; |
| 127 | m3 *= (m1.transpose() * m2); |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 128 | VERIFY_IS_APPROX(m3, m1*(m1.transpose()*m2)); |
| 129 | VERIFY_IS_APPROX(m3, m1.lazyProduct(m1.transpose()*m2)); |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 130 | |
| 131 | // continue testing Product.h: distributivity |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 132 | VERIFY_IS_APPROX(square*(m1 + m2), square*m1+square*m2); |
| 133 | VERIFY_IS_APPROX(square*(m1 - m2), square*m1-square*m2); |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 134 | |
| 135 | // continue testing Product.h: compatibility with ScalarMultiple.h |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 136 | VERIFY_IS_APPROX(s1*(square*m1), (s1*square)*m1); |
| 137 | VERIFY_IS_APPROX(s1*(square*m1), square*(m1*s1)); |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 138 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 139 | // continue testing Product.h: lazyProduct |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 140 | VERIFY_IS_APPROX(square.lazyProduct(m1), square*m1); |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame^] | 141 | // again, test operator() to check const-qualification |
| 142 | s1 += square.lazyProduct(m1)(r,c); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 143 | |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame^] | 144 | // test Product.h together with Identity.h |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 145 | VERIFY_IS_APPROX(m1, identity*m1); |
| 146 | VERIFY_IS_APPROX(v1, identity*v1); |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame^] | 147 | // again, test operator() to check const-qualification |
| 148 | VERIFY_IS_APPROX(MatrixType::identity()(r,c), static_cast<Scalar>(r==c)); |
Benoit Jacob | 6c8f159 | 2007-10-14 09:18:18 +0000 | [diff] [blame] | 149 | |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 150 | // test FromArray.h |
Benoit Jacob | f355ef2 | 2007-10-14 18:02:16 +0000 | [diff] [blame] | 151 | Scalar* array1 = new Scalar[rows]; |
| 152 | Scalar* array2 = new Scalar[rows]; |
| 153 | Matrix<Scalar, Dynamic, 1>::fromArray(array1, rows) = Matrix<Scalar, Dynamic, 1>::random(rows); |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 154 | Matrix<Scalar, Dynamic, 1>::fromArray(array2, rows) |
| 155 | = Matrix<Scalar, Dynamic, 1>::fromArray(array1, rows); |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 156 | Matrix<Scalar, Dynamic, 1> ma1 = Matrix<Scalar, Dynamic, 1>::fromArray(array1, rows); |
| 157 | Matrix<Scalar, Dynamic, 1> ma2 = Matrix<Scalar, Dynamic, 1>::fromArray(array2, rows); |
| 158 | VERIFY_IS_APPROX(ma1, ma2); |
Benoit Jacob | 5309ef5 | 2007-11-26 08:47:07 +0000 | [diff] [blame] | 159 | delete[] array1; |
| 160 | delete[] array2; |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void EigenTest::testBasicStuff() |
| 164 | { |
Benoit Jacob | 2b20814 | 2007-12-11 10:07:18 +0000 | [diff] [blame] | 165 | for(int i = 0; i < m_repeat; i++) { |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 166 | basicStuff(Matrix<float, 1, 1>()); |
Benoit Jacob | 04502cc | 2007-12-05 07:22:22 +0000 | [diff] [blame] | 167 | basicStuff(Matrix4d()); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 168 | basicStuff(MatrixXcf(3, 3)); |
| 169 | basicStuff(MatrixXi(8, 12)); |
Benoit Jacob | 04502cc | 2007-12-05 07:22:22 +0000 | [diff] [blame] | 170 | basicStuff(MatrixXcd(20, 20)); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 171 | } |
Benoit Jacob | e445f50 | 2007-10-13 16:56:24 +0000 | [diff] [blame] | 172 | } |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 173 | |
| 174 | } // namespace Eigen |