blob: 052d5ec9116629113e77289646deca6126449f92 [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//
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
28namespace Eigen {
29
30template<typename MatrixType> void miscMatrices(const MatrixType& m)
31{
32 /* this test covers the following files:
33 DiagonalMatrix.h Ones.h
34 */
35
36 typedef typename MatrixType::Scalar Scalar;
37 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
38 typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
39 int rows = m.rows();
40 int cols = m.cols();
41
42 int r = random<int>(0, rows-1), r2 = random<int>(0, rows-1), c = random<int>(0, cols-1);
43 VERIFY_IS_APPROX(MatrixType::ones(rows,cols)(r,c), static_cast<Scalar>(1));
44 MatrixType m1 = MatrixType::ones(rows,cols);
45 VERIFY_IS_APPROX(m1(r,c), static_cast<Scalar>(1));
46 VectorType v1 = VectorType::random(rows);
47 v1[0];
48 Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
49 square = MatrixType::diagonal(v1);
50 if(r==r2) VERIFY_IS_APPROX(square(r,r2), v1[r]);
51 else VERIFY_IS_MUCH_SMALLER_THAN(square(r,r2), static_cast<Scalar>(1));
52 square = MatrixType::zero(rows, rows);
53 square.diagonal() = VectorType::ones(rows);
54 VERIFY_IS_APPROX(square, MatrixType::identity(rows));
55}
56
57void EigenTest::testMiscMatrices()
58{
59 for(int i = 0; i < m_repeat; i++) {
60 miscMatrices(Matrix<float, 1, 1>());
61 miscMatrices(Matrix4d());
62 miscMatrices(MatrixXcf(3, 3));
63 miscMatrices(MatrixXi(8, 12));
64 miscMatrices(MatrixXcd(20, 20));
65 }
66}
67
68} // namespace Eigen