blob: 05b37895e73fe4972123ce841b649c96bfb440e7 [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//
Gael Guennebaudec5c6082009-07-10 16:10:03 +02004// Copyright (C) 2008-2009 Gael Guennebaud <g.gael@free.fr>
Gael Guennebaud8cef5412008-06-21 17:28:07 +00005//
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{
Gael Guennebaud8cef5412008-06-21 17:28:07 +000030 typedef typename MatrixType::Scalar Scalar;
Gael Guennebaud2120fed2008-08-23 15:14:20 +000031 typedef typename NumTraits<Scalar>::Real RealScalar;
Gael Guennebaudc70d5422010-01-18 22:54:20 +010032 typedef Array<Scalar, MatrixType::RowsAtCompileTime, 1> ColVectorType;
33 typedef Array<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
Gael Guennebaud8cef5412008-06-21 17:28:07 +000034
35 int rows = m.rows();
36 int cols = m.cols();
37
Benoit Jacob46fe7a32008-09-01 17:31:21 +000038 MatrixType m1 = MatrixType::Random(rows, cols),
39 m2 = MatrixType::Random(rows, cols),
Gael Guennebaud8cef5412008-06-21 17:28:07 +000040 m3(rows, cols);
41
Gael Guennebaud627595a2009-06-10 11:20:30 +020042 ColVectorType cv1 = ColVectorType::Random(rows);
43 RowVectorType rv1 = RowVectorType::Random(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
Gael Guennebaudc70d5422010-01-18 22:54:20 +010049 VERIFY_IS_APPROX(m1 + s1, s1 + m1);
50 VERIFY_IS_APPROX(m1 + s1, MatrixType::Constant(rows,cols,s1) + m1);
51 VERIFY_IS_APPROX(s1 - m1, (-m1)+s1 );
52 VERIFY_IS_APPROX(m1 - s1, m1 - MatrixType::Constant(rows,cols,s1));
53 VERIFY_IS_APPROX(s1 - m1, MatrixType::Constant(rows,cols,s1) - m1);
54 VERIFY_IS_APPROX((m1*Scalar(2)) - s2, (m1+m1) - MatrixType::Constant(rows,cols,s2) );
Gael Guennebaud8cef5412008-06-21 17:28:07 +000055 m3 = m1;
Gael Guennebaudc70d5422010-01-18 22:54:20 +010056 m3 += s2;
57 VERIFY_IS_APPROX(m3, m1 + s2);
Gael Guennebaud8cef5412008-06-21 17:28:07 +000058 m3 = m1;
Gael Guennebaudc70d5422010-01-18 22:54:20 +010059 m3 -= s1;
60 VERIFY_IS_APPROX(m3, m1 - s1);
Gael Guennebaud05ad0832008-07-19 13:03:23 +000061
Gael Guennebaud59dc1da2008-09-03 17:16:28 +000062 // reductions
Gael Guennebaud05ad0832008-07-19 13:03:23 +000063 VERIFY_IS_APPROX(m1.colwise().sum().sum(), m1.sum());
64 VERIFY_IS_APPROX(m1.rowwise().sum().sum(), m1.sum());
Gael Guennebaud2120fed2008-08-23 15:14:20 +000065 if (!ei_isApprox(m1.sum(), (m1+m2).sum()))
66 VERIFY_IS_NOT_APPROX(((m1+m2).rowwise().sum()).sum(), m1.sum());
Gael Guennebaud05ad0832008-07-19 13:03:23 +000067 VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(ei_scalar_sum_op<Scalar>()));
Gael Guennebaud627595a2009-06-10 11:20:30 +020068
69 // vector-wise ops
70 m3 = m1;
71 VERIFY_IS_APPROX(m3.colwise() += cv1, m1.colwise() + cv1);
72 m3 = m1;
73 VERIFY_IS_APPROX(m3.colwise() -= cv1, m1.colwise() - cv1);
74 m3 = m1;
75 VERIFY_IS_APPROX(m3.rowwise() += rv1, m1.rowwise() + rv1);
76 m3 = m1;
77 VERIFY_IS_APPROX(m3.rowwise() -= rv1, m1.rowwise() - rv1);
Gael Guennebaud8cef5412008-06-21 17:28:07 +000078}
79
80template<typename MatrixType> void comparisons(const MatrixType& m)
81{
82 typedef typename MatrixType::Scalar Scalar;
Benoit Jacob874ff5a2009-01-26 17:56:04 +000083 typedef typename NumTraits<Scalar>::Real RealScalar;
Gael Guennebaudc70d5422010-01-18 22:54:20 +010084 typedef Array<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
Gael Guennebaud8cef5412008-06-21 17:28:07 +000085
86 int rows = m.rows();
87 int cols = m.cols();
88
89 int r = ei_random<int>(0, rows-1),
90 c = ei_random<int>(0, cols-1);
91
Gael Guennebaudc10f0692008-07-21 00:34:46 +000092 MatrixType m1 = MatrixType::Random(rows, cols),
93 m2 = MatrixType::Random(rows, cols),
Gael Guennebaud8cef5412008-06-21 17:28:07 +000094 m3(rows, cols);
95
Gael Guennebaudc70d5422010-01-18 22:54:20 +010096 VERIFY(((m1 + Scalar(1)) > m1).all());
97 VERIFY(((m1 - Scalar(1)) < m1).all());
Gael Guennebaud8cef5412008-06-21 17:28:07 +000098 if (rows*cols>1)
99 {
100 m3 = m1;
101 m3(r,c) += 1;
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100102 VERIFY(! (m1 < m3).all() );
103 VERIFY(! (m1 > m3).all() );
Gael Guennebaud8cef5412008-06-21 17:28:07 +0000104 }
Gael Guennebaud627595a2009-06-10 11:20:30 +0200105
Gael Guennebaude14aa8c2008-09-03 17:56:06 +0000106 // comparisons to scalar
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100107 VERIFY( (m1 != (m1(r,c)+1) ).any() );
108 VERIFY( (m1 > (m1(r,c)-1) ).any() );
109 VERIFY( (m1 < (m1(r,c)+1) ).any() );
110 VERIFY( (m1 == m1(r,c) ).any() );
Gael Guennebaud627595a2009-06-10 11:20:30 +0200111
Gael Guennebaud59dc1da2008-09-03 17:16:28 +0000112 // test Select
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100113 VERIFY_IS_APPROX( (m1<m2).select(m1,m2), m1.cwiseMin(m2) );
114 VERIFY_IS_APPROX( (m1>m2).select(m1,m2), m1.cwiseMax(m2) );
Gael Guennebaud9f795582009-12-16 19:18:40 +0100115 Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2);
Gael Guennebaud59dc1da2008-09-03 17:16:28 +0000116 for (int j=0; j<cols; ++j)
117 for (int i=0; i<rows; ++i)
118 m3(i,j) = ei_abs(m1(i,j))<mid ? 0 : m1(i,j);
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100119 VERIFY_IS_APPROX( (m1.abs()<MatrixType::Constant(rows,cols,mid))
Gael Guennebaud59dc1da2008-09-03 17:16:28 +0000120 .select(MatrixType::Zero(rows,cols),m1), m3);
Gael Guennebaude14aa8c2008-09-03 17:56:06 +0000121 // shorter versions:
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100122 VERIFY_IS_APPROX( (m1.abs()<MatrixType::Constant(rows,cols,mid))
Gael Guennebaud59dc1da2008-09-03 17:16:28 +0000123 .select(0,m1), m3);
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100124 VERIFY_IS_APPROX( (m1.abs()>=MatrixType::Constant(rows,cols,mid))
Gael Guennebaud59dc1da2008-09-03 17:16:28 +0000125 .select(m1,0), m3);
Gael Guennebaude14aa8c2008-09-03 17:56:06 +0000126 // even shorter version:
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100127 VERIFY_IS_APPROX( (m1.abs()<mid).select(0,m1), m3);
Gael Guennebaud627595a2009-06-10 11:20:30 +0200128
Gael Guennebaud56c7e162009-01-24 15:22:44 +0000129 // count
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100130 VERIFY(((m1.abs()+1)>RealScalar(0.1)).count() == rows*cols);
Gael Guennebaud9f795582009-12-16 19:18:40 +0100131 // TODO allows colwise/rowwise for array
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100132 VERIFY_IS_APPROX(((m1.abs()+1)>RealScalar(0.1)).colwise().count(), ArrayXi::Constant(cols,rows).transpose());
133 VERIFY_IS_APPROX(((m1.abs()+1)>RealScalar(0.1)).rowwise().count(), ArrayXi::Constant(rows, cols));
Benoit Jacobe8009992008-11-03 22:47:00 +0000134}
135
Gael Guennebaud8cef5412008-06-21 17:28:07 +0000136void test_array()
137{
138 for(int i = 0; i < g_repeat; i++) {
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100139 CALL_SUBTEST_1( array(Array<float, 1, 1>()) );
140 CALL_SUBTEST_2( array(Array22f()) );
141 CALL_SUBTEST_3( array(Array44d()) );
142 CALL_SUBTEST_4( array(ArrayXXcf(3, 3)) );
143 CALL_SUBTEST_5( array(ArrayXXf(8, 12)) );
144 CALL_SUBTEST_6( array(ArrayXXi(8, 12)) );
Gael Guennebaud8cef5412008-06-21 17:28:07 +0000145 }
146 for(int i = 0; i < g_repeat; i++) {
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100147 CALL_SUBTEST_1( comparisons(Array<float, 1, 1>()) );
148 CALL_SUBTEST_2( comparisons(Array22f()) );
149 CALL_SUBTEST_3( comparisons(Array44d()) );
150 CALL_SUBTEST_5( comparisons(ArrayXXf(8, 12)) );
151 CALL_SUBTEST_6( comparisons(ArrayXXi(8, 12)) );
Benoit Jacobe8009992008-11-03 22:47:00 +0000152 }
Gael Guennebaud8cef5412008-06-21 17:28:07 +0000153}