blob: 6233aaec83d61afb31a76bf8e0c05d8b6faf8e4a [file] [log] [blame]
Gael Guennebaud8cef5412008-06-21 17:28:07 +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) 2008 Gael Guennebaud <g.gael@free.fr>
5//
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.
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
18// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19// GNU General Public License for more details.
20//
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/>.
24
25#include "main.h"
26#include <Eigen/Array>
27
28template<typename MatrixType> void scalarAdd(const MatrixType& m)
29{
30 /* this test covers the following files:
31 Array.cpp
32 */
33
34 typedef typename MatrixType::Scalar Scalar;
35 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
36
37 int rows = m.rows();
38 int cols = m.cols();
39
40 MatrixType m1 = MatrixType::random(rows, cols),
41 m2 = MatrixType::random(rows, cols),
42 m3(rows, cols);
43
44 Scalar s1 = ei_random<Scalar>(),
45 s2 = ei_random<Scalar>();
46
Benoit Jacobf5791ee2008-07-08 00:49:10 +000047 VERIFY_IS_APPROX(m1.cwise() + s1, s1 + m1.cwise());
48 VERIFY_IS_APPROX(m1.cwise() + s1, MatrixType::constant(rows,cols,s1) + m1);
49 VERIFY_IS_APPROX((m1*Scalar(2)).cwise() - s2, (m1+m1) - MatrixType::constant(rows,cols,s2) );
Gael Guennebaud8cef5412008-06-21 17:28:07 +000050 m3 = m1;
Benoit Jacobf5791ee2008-07-08 00:49:10 +000051 m3.cwise() += s2;
52 VERIFY_IS_APPROX(m3, m1.cwise() + s2);
Gael Guennebaud8cef5412008-06-21 17:28:07 +000053 m3 = m1;
Benoit Jacobf5791ee2008-07-08 00:49:10 +000054 m3.cwise() -= s1;
55 VERIFY_IS_APPROX(m3, m1.cwise() - s1);
Gael Guennebaud8cef5412008-06-21 17:28:07 +000056}
57
58template<typename MatrixType> void comparisons(const MatrixType& m)
59{
60 typedef typename MatrixType::Scalar Scalar;
61 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
62
63 int rows = m.rows();
64 int cols = m.cols();
65
66 int r = ei_random<int>(0, rows-1),
67 c = ei_random<int>(0, cols-1);
68
69 MatrixType m1 = MatrixType::random(rows, cols),
70 m2 = MatrixType::random(rows, cols),
71 m3(rows, cols);
72
Benoit Jacobf5791ee2008-07-08 00:49:10 +000073 VERIFY(((m1.cwise() + Scalar(1)).cwise() > m1).all());
74 VERIFY(((m1.cwise() - Scalar(1)).cwise() < m1).all());
Gael Guennebaud8cef5412008-06-21 17:28:07 +000075 if (rows*cols>1)
76 {
77 m3 = m1;
78 m3(r,c) += 1;
Benoit Jacobf5791ee2008-07-08 00:49:10 +000079 VERIFY(! (m1.cwise() < m3).all() );
80 VERIFY(! (m1.cwise() > m3).all() );
Gael Guennebaud8cef5412008-06-21 17:28:07 +000081 }
82}
83
84void test_array()
85{
86 for(int i = 0; i < g_repeat; i++) {
87 CALL_SUBTEST( scalarAdd(Matrix<float, 1, 1>()) );
88 CALL_SUBTEST( scalarAdd(Matrix2f()) );
89 CALL_SUBTEST( scalarAdd(Matrix4d()) );
90 CALL_SUBTEST( scalarAdd(MatrixXcf(3, 3)) );
91 CALL_SUBTEST( scalarAdd(MatrixXf(8, 12)) );
92 CALL_SUBTEST( scalarAdd(MatrixXi(8, 12)) );
93 }
94 for(int i = 0; i < g_repeat; i++) {
95 CALL_SUBTEST( comparisons(Matrix<float, 1, 1>()) );
96 CALL_SUBTEST( comparisons(Matrix2f()) );
97 CALL_SUBTEST( comparisons(Matrix4d()) );
98 CALL_SUBTEST( comparisons(MatrixXf(8, 12)) );
99 CALL_SUBTEST( comparisons(MatrixXi(8, 12)) );
100 }
101}