blob: e04eb7ecc337c44147d0e8f334b27e64efe3bc07 [file] [log] [blame]
Benoit Jacob7c384752007-12-15 18:16:30 +00001// 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 Jacob8ba30552008-01-07 09:34:21 +00004// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
Benoit Jacob7c384752007-12-15 18:16:30 +00005//
Benoit Jacob3698d8c2008-02-28 15:44:45 +00006// 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 Jacob7c384752007-12-15 18:16:30 +000015//
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 Jacob3698d8c2008-02-28 15:44:45 +000018// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19// GNU General Public License for more details.
Benoit Jacob7c384752007-12-15 18:16:30 +000020//
Benoit Jacob3698d8c2008-02-28 15:44:45 +000021// 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 Jacob7c384752007-12-15 18:16:30 +000024
25#include "main.h"
26
27namespace Eigen {
28
29template<typename MatrixType> void miscMatrices(const MatrixType& m)
30{
31 /* this test covers the following files:
32 DiagonalMatrix.h Ones.h
33 */
34
35 typedef typename MatrixType::Scalar Scalar;
Benoit Jacob84934ea2008-01-06 16:35:21 +000036 typedef Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> VectorType;
37 typedef Matrix<Scalar, 1, MatrixType::Traits::ColsAtCompileTime> RowVectorType;
Benoit Jacob7c384752007-12-15 18:16:30 +000038 int rows = m.rows();
39 int cols = m.cols();
40
Benoit Jacob69078862008-02-28 12:38:12 +000041 int r = ei_random<int>(0, rows-1), r2 = ei_random<int>(0, rows-1), c = ei_random<int>(0, cols-1);
Benoit Jacob7c384752007-12-15 18:16:30 +000042 VERIFY_IS_APPROX(MatrixType::ones(rows,cols)(r,c), static_cast<Scalar>(1));
43 MatrixType m1 = MatrixType::ones(rows,cols);
44 VERIFY_IS_APPROX(m1(r,c), static_cast<Scalar>(1));
45 VectorType v1 = VectorType::random(rows);
46 v1[0];
Benoit Jacob84934ea2008-01-06 16:35:21 +000047 Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
Benoit Jacobd1d55e62008-01-05 10:57:14 +000048 square = v1.asDiagonal();
Benoit Jacob7c384752007-12-15 18:16:30 +000049 if(r==r2) VERIFY_IS_APPROX(square(r,r2), v1[r]);
50 else VERIFY_IS_MUCH_SMALLER_THAN(square(r,r2), static_cast<Scalar>(1));
51 square = MatrixType::zero(rows, rows);
52 square.diagonal() = VectorType::ones(rows);
Benoit Jacobbcf7b292008-01-11 15:56:21 +000053 VERIFY_IS_APPROX(square, MatrixType::identity(rows, rows));
Benoit Jacob7c384752007-12-15 18:16:30 +000054}
55
56void EigenTest::testMiscMatrices()
57{
58 for(int i = 0; i < m_repeat; i++) {
59 miscMatrices(Matrix<float, 1, 1>());
60 miscMatrices(Matrix4d());
61 miscMatrices(MatrixXcf(3, 3));
62 miscMatrices(MatrixXi(8, 12));
63 miscMatrices(MatrixXcd(20, 20));
64 }
65}
66
67} // namespace Eigen