blob: 053f97327b447e5a16ebe3557509f9fa52e26c48 [file] [log] [blame]
Gael Guennebaud8cef5412008-06-21 17:28:07 +00001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacob6347b1d2009-05-22 20:25:33 +02002// for linear algebra.
Gael Guennebaud8cef5412008-06-21 17:28:07 +00003//
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
Gael Guennebaud59dc1da2008-09-03 17:16:28 +000028template<typename MatrixType> void array(const MatrixType& m)
Gael Guennebaud8cef5412008-06-21 17:28:07 +000029{
30 /* this test covers the following files:
31 Array.cpp
32 */
33
34 typedef typename MatrixType::Scalar Scalar;
Gael Guennebaud2120fed2008-08-23 15:14:20 +000035 typedef typename NumTraits<Scalar>::Real RealScalar;
Gael Guennebaud8cef5412008-06-21 17:28:07 +000036 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
37
38 int rows = m.rows();
39 int cols = m.cols();
40
Benoit Jacob46fe7a32008-09-01 17:31:21 +000041 MatrixType m1 = MatrixType::Random(rows, cols),
42 m2 = MatrixType::Random(rows, cols),
Gael Guennebaud8cef5412008-06-21 17:28:07 +000043 m3(rows, cols);
44
Benoit Jacob46fe7a32008-09-01 17:31:21 +000045 Scalar s1 = ei_random<Scalar>(),
46 s2 = ei_random<Scalar>();
Gael Guennebaud8cef5412008-06-21 17:28:07 +000047
Gael Guennebaud59dc1da2008-09-03 17:16:28 +000048 // scalar addition
Benoit Jacobf5791ee2008-07-08 00:49:10 +000049 VERIFY_IS_APPROX(m1.cwise() + s1, s1 + m1.cwise());
Gael Guennebaudc10f0692008-07-21 00:34:46 +000050 VERIFY_IS_APPROX(m1.cwise() + s1, MatrixType::Constant(rows,cols,s1) + m1);
51 VERIFY_IS_APPROX((m1*Scalar(2)).cwise() - s2, (m1+m1) - MatrixType::Constant(rows,cols,s2) );
Gael Guennebaud8cef5412008-06-21 17:28:07 +000052 m3 = m1;
Benoit Jacobf5791ee2008-07-08 00:49:10 +000053 m3.cwise() += s2;
54 VERIFY_IS_APPROX(m3, m1.cwise() + s2);
Gael Guennebaud8cef5412008-06-21 17:28:07 +000055 m3 = m1;
Benoit Jacobf5791ee2008-07-08 00:49:10 +000056 m3.cwise() -= s1;
57 VERIFY_IS_APPROX(m3, m1.cwise() - s1);
Gael Guennebaud05ad0832008-07-19 13:03:23 +000058
Gael Guennebaud59dc1da2008-09-03 17:16:28 +000059 // reductions
Gael Guennebaud05ad0832008-07-19 13:03:23 +000060 VERIFY_IS_APPROX(m1.colwise().sum().sum(), m1.sum());
61 VERIFY_IS_APPROX(m1.rowwise().sum().sum(), m1.sum());
Gael Guennebaud2120fed2008-08-23 15:14:20 +000062 if (!ei_isApprox(m1.sum(), (m1+m2).sum()))
63 VERIFY_IS_NOT_APPROX(((m1+m2).rowwise().sum()).sum(), m1.sum());
Gael Guennebaud05ad0832008-07-19 13:03:23 +000064 VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(ei_scalar_sum_op<Scalar>()));
Gael Guennebaud8cef5412008-06-21 17:28:07 +000065}
66
67template<typename MatrixType> void comparisons(const MatrixType& m)
68{
69 typedef typename MatrixType::Scalar Scalar;
Benoit Jacob874ff5a2009-01-26 17:56:04 +000070 typedef typename NumTraits<Scalar>::Real RealScalar;
Gael Guennebaud8cef5412008-06-21 17:28:07 +000071 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
72
73 int rows = m.rows();
74 int cols = m.cols();
75
76 int r = ei_random<int>(0, rows-1),
77 c = ei_random<int>(0, cols-1);
78
Gael Guennebaudc10f0692008-07-21 00:34:46 +000079 MatrixType m1 = MatrixType::Random(rows, cols),
80 m2 = MatrixType::Random(rows, cols),
Gael Guennebaud8cef5412008-06-21 17:28:07 +000081 m3(rows, cols);
82
Benoit Jacobf5791ee2008-07-08 00:49:10 +000083 VERIFY(((m1.cwise() + Scalar(1)).cwise() > m1).all());
84 VERIFY(((m1.cwise() - Scalar(1)).cwise() < m1).all());
Gael Guennebaud8cef5412008-06-21 17:28:07 +000085 if (rows*cols>1)
86 {
87 m3 = m1;
88 m3(r,c) += 1;
Benoit Jacobf5791ee2008-07-08 00:49:10 +000089 VERIFY(! (m1.cwise() < m3).all() );
90 VERIFY(! (m1.cwise() > m3).all() );
Gael Guennebaud8cef5412008-06-21 17:28:07 +000091 }
Gael Guennebaud59dc1da2008-09-03 17:16:28 +000092
Gael Guennebaude14aa8c2008-09-03 17:56:06 +000093 // comparisons to scalar
94 VERIFY( (m1.cwise() != (m1(r,c)+1) ).any() );
95 VERIFY( (m1.cwise() > (m1(r,c)-1) ).any() );
96 VERIFY( (m1.cwise() < (m1(r,c)+1) ).any() );
97 VERIFY( (m1.cwise() == m1(r,c) ).any() );
98
Gael Guennebaud59dc1da2008-09-03 17:16:28 +000099 // test Select
100 VERIFY_IS_APPROX( (m1.cwise()<m2).select(m1,m2), m1.cwise().min(m2) );
101 VERIFY_IS_APPROX( (m1.cwise()>m2).select(m1,m2), m1.cwise().max(m2) );
102 Scalar mid = (m1.cwise().abs().minCoeff() + m1.cwise().abs().maxCoeff())/Scalar(2);
103 for (int j=0; j<cols; ++j)
104 for (int i=0; i<rows; ++i)
105 m3(i,j) = ei_abs(m1(i,j))<mid ? 0 : m1(i,j);
106 VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<MatrixType::Constant(rows,cols,mid))
107 .select(MatrixType::Zero(rows,cols),m1), m3);
Gael Guennebaude14aa8c2008-09-03 17:56:06 +0000108 // shorter versions:
Gael Guennebaud59dc1da2008-09-03 17:16:28 +0000109 VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<MatrixType::Constant(rows,cols,mid))
110 .select(0,m1), m3);
111 VERIFY_IS_APPROX( (m1.cwise().abs().cwise()>=MatrixType::Constant(rows,cols,mid))
112 .select(m1,0), m3);
Gael Guennebaude14aa8c2008-09-03 17:56:06 +0000113 // even shorter version:
114 VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<mid).select(0,m1), m3);
Gael Guennebaud56c7e162009-01-24 15:22:44 +0000115
116 // count
Benoit Jacob874ff5a2009-01-26 17:56:04 +0000117 VERIFY(((m1.cwise().abs().cwise()+1).cwise()>RealScalar(0.1)).count() == rows*cols);
118 VERIFY_IS_APPROX(((m1.cwise().abs().cwise()+1).cwise()>RealScalar(0.1)).colwise().count(), RowVectorXi::Constant(cols,rows));
119 VERIFY_IS_APPROX(((m1.cwise().abs().cwise()+1).cwise()>RealScalar(0.1)).rowwise().count(), VectorXi::Constant(rows, cols));
Gael Guennebaud8cef5412008-06-21 17:28:07 +0000120}
121
Benoit Jacobe8009992008-11-03 22:47:00 +0000122template<typename VectorType> void lpNorm(const VectorType& v)
123{
124 VectorType u = VectorType::Random(v.size());
125
126 VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), u.cwise().abs().maxCoeff());
127 VERIFY_IS_APPROX(u.template lpNorm<1>(), u.cwise().abs().sum());
128 VERIFY_IS_APPROX(u.template lpNorm<2>(), ei_sqrt(u.cwise().abs().cwise().square().sum()));
129 VERIFY_IS_APPROX(ei_pow(u.template lpNorm<5>(), typename VectorType::RealScalar(5)), u.cwise().abs().cwise().pow(5).sum());
130}
131
Gael Guennebaud8cef5412008-06-21 17:28:07 +0000132void test_array()
133{
134 for(int i = 0; i < g_repeat; i++) {
Gael Guennebaud59dc1da2008-09-03 17:16:28 +0000135 CALL_SUBTEST( array(Matrix<float, 1, 1>()) );
136 CALL_SUBTEST( array(Matrix2f()) );
137 CALL_SUBTEST( array(Matrix4d()) );
138 CALL_SUBTEST( array(MatrixXcf(3, 3)) );
139 CALL_SUBTEST( array(MatrixXf(8, 12)) );
140 CALL_SUBTEST( array(MatrixXi(8, 12)) );
Gael Guennebaud8cef5412008-06-21 17:28:07 +0000141 }
142 for(int i = 0; i < g_repeat; i++) {
143 CALL_SUBTEST( comparisons(Matrix<float, 1, 1>()) );
144 CALL_SUBTEST( comparisons(Matrix2f()) );
145 CALL_SUBTEST( comparisons(Matrix4d()) );
146 CALL_SUBTEST( comparisons(MatrixXf(8, 12)) );
147 CALL_SUBTEST( comparisons(MatrixXi(8, 12)) );
148 }
Benoit Jacobe8009992008-11-03 22:47:00 +0000149 for(int i = 0; i < g_repeat; i++) {
150 CALL_SUBTEST( lpNorm(Matrix<float, 1, 1>()) );
151 CALL_SUBTEST( lpNorm(Vector2f()) );
152 CALL_SUBTEST( lpNorm(Vector3d()) );
153 CALL_SUBTEST( lpNorm(Vector4f()) );
154 CALL_SUBTEST( lpNorm(VectorXf(16)) );
155 CALL_SUBTEST( lpNorm(VectorXcd(10)) );
156 }
Gael Guennebaud8cef5412008-06-21 17:28:07 +0000157}