2008-02-29 22:35:14 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-23 02:25:33 +08:00
|
|
|
// for linear algebra.
|
2008-02-29 22:35:14 +08:00
|
|
|
//
|
2008-03-03 19:02:52 +08:00
|
|
|
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
|
2008-11-24 21:40:43 +08:00
|
|
|
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2008-02-29 22:35:14 +08:00
|
|
|
//
|
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
2008-03-03 19:02:52 +08:00
|
|
|
// License as published by the Free Software Foundation; either
|
2008-02-29 22:35:14 +08:00
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
2008-03-03 19:02:52 +08:00
|
|
|
// published by the Free Software Foundation; either version 2 of
|
2008-02-29 22:35:14 +08:00
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
2008-03-03 19:02:52 +08:00
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
2008-02-29 22:35:14 +08:00
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2009-11-19 01:15:19 +08:00
|
|
|
#define EIGEN2_SUPPORT
|
2010-04-29 06:51:38 +08:00
|
|
|
#define EIGEN_NO_STATIC_ASSERT
|
2008-02-29 22:35:14 +08:00
|
|
|
#include "main.h"
|
2008-04-04 02:13:27 +08:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
using namespace std;
|
2008-02-29 22:35:14 +08:00
|
|
|
|
2008-04-03 19:10:17 +08:00
|
|
|
template<typename Scalar> struct AddIfNull {
|
|
|
|
const Scalar operator() (const Scalar a, const Scalar b) const {return a<=1e-3 ? b : a;}
|
|
|
|
enum { Cost = NumTraits<Scalar>::AddCost };
|
2008-02-29 22:35:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename MatrixType> void cwiseops(const MatrixType& m)
|
|
|
|
{
|
2010-06-20 23:37:56 +08:00
|
|
|
typedef typename MatrixType::Index Index;
|
2008-02-29 22:35:14 +08:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
2008-08-23 06:49:09 +08:00
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
2008-03-13 01:17:36 +08:00
|
|
|
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
|
2008-04-04 02:13:27 +08:00
|
|
|
|
2010-06-20 23:37:56 +08:00
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
2008-04-04 02:13:27 +08:00
|
|
|
|
2008-09-02 01:31:21 +08:00
|
|
|
MatrixType m1 = MatrixType::Random(rows, cols),
|
|
|
|
m2 = MatrixType::Random(rows, cols),
|
2008-02-29 22:35:14 +08:00
|
|
|
m3(rows, cols),
|
2009-01-24 18:52:18 +08:00
|
|
|
m4(rows, cols),
|
2008-07-21 08:34:46 +08:00
|
|
|
mzero = MatrixType::Zero(rows, cols),
|
|
|
|
mones = MatrixType::Ones(rows, cols),
|
2008-03-13 01:17:36 +08:00
|
|
|
identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
|
2008-07-21 08:34:46 +08:00
|
|
|
::Identity(rows, rows),
|
2008-09-02 01:31:21 +08:00
|
|
|
square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
|
|
|
|
VectorType v1 = VectorType::Random(rows),
|
|
|
|
v2 = VectorType::Random(rows),
|
2009-05-07 05:40:24 +08:00
|
|
|
vzero = VectorType::Zero(rows),
|
|
|
|
vones = VectorType::Ones(rows),
|
|
|
|
v3(rows);
|
2008-02-29 22:35:14 +08:00
|
|
|
|
2010-06-21 17:36:00 +08:00
|
|
|
Index r = ei_random<Index>(0, rows-1),
|
|
|
|
c = ei_random<Index>(0, cols-1);
|
2009-11-19 01:15:19 +08:00
|
|
|
|
2009-01-24 18:52:18 +08:00
|
|
|
Scalar s1 = ei_random<Scalar>();
|
2009-11-19 01:15:19 +08:00
|
|
|
|
2009-01-24 18:52:18 +08:00
|
|
|
// test Zero, Ones, Constant, and the set* variants
|
|
|
|
m3 = MatrixType::Constant(rows, cols, s1);
|
|
|
|
for (int j=0; j<cols; ++j)
|
|
|
|
for (int i=0; i<rows; ++i)
|
|
|
|
{
|
|
|
|
VERIFY_IS_APPROX(mzero(i,j), Scalar(0));
|
|
|
|
VERIFY_IS_APPROX(mones(i,j), Scalar(1));
|
|
|
|
VERIFY_IS_APPROX(m3(i,j), s1);
|
|
|
|
}
|
2009-05-07 05:40:24 +08:00
|
|
|
VERIFY(mzero.isZero());
|
|
|
|
VERIFY(mones.isOnes());
|
|
|
|
VERIFY(m3.isConstant(s1));
|
|
|
|
VERIFY(identity.isIdentity());
|
2009-01-24 18:52:18 +08:00
|
|
|
VERIFY_IS_APPROX(m4.setConstant(s1), m3);
|
2009-05-07 05:40:24 +08:00
|
|
|
VERIFY_IS_APPROX(m4.setConstant(rows,cols,s1), m3);
|
2009-01-24 18:52:18 +08:00
|
|
|
VERIFY_IS_APPROX(m4.setZero(), mzero);
|
2009-05-07 05:40:24 +08:00
|
|
|
VERIFY_IS_APPROX(m4.setZero(rows,cols), mzero);
|
2009-01-24 18:52:18 +08:00
|
|
|
VERIFY_IS_APPROX(m4.setOnes(), mones);
|
2009-05-07 05:40:24 +08:00
|
|
|
VERIFY_IS_APPROX(m4.setOnes(rows,cols), mones);
|
2009-01-24 18:52:18 +08:00
|
|
|
m4.fill(s1);
|
|
|
|
VERIFY_IS_APPROX(m4, m3);
|
2009-11-19 01:15:19 +08:00
|
|
|
|
2009-05-07 05:40:24 +08:00
|
|
|
VERIFY_IS_APPROX(v3.setConstant(rows, s1), VectorType::Constant(rows,s1));
|
|
|
|
VERIFY_IS_APPROX(v3.setZero(rows), vzero);
|
|
|
|
VERIFY_IS_APPROX(v3.setOnes(rows), vones);
|
2008-08-23 06:49:09 +08:00
|
|
|
|
2008-07-08 08:49:10 +08:00
|
|
|
m2 = m2.template binaryExpr<AddIfNull<Scalar> >(mones);
|
2008-04-04 02:13:27 +08:00
|
|
|
|
2008-08-23 06:49:09 +08:00
|
|
|
VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().abs2());
|
|
|
|
VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
|
|
|
|
VERIFY_IS_APPROX(m1.cwise().pow(3), m1.cwise().cube());
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(m1 + mones, m1.cwise()+Scalar(1));
|
|
|
|
VERIFY_IS_APPROX(m1 - mones, m1.cwise()-Scalar(1));
|
|
|
|
m3 = m1; m3.cwise() += 1;
|
|
|
|
VERIFY_IS_APPROX(m1 + mones, m3);
|
|
|
|
m3 = m1; m3.cwise() -= 1;
|
|
|
|
VERIFY_IS_APPROX(m1 - mones, m3);
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(m2, m2.cwise() * mones);
|
|
|
|
VERIFY_IS_APPROX(m1.cwise() * m2, m2.cwise() * m1);
|
2009-01-12 05:04:23 +08:00
|
|
|
m3 = m1;
|
|
|
|
m3.cwise() *= m2;
|
|
|
|
VERIFY_IS_APPROX(m3, m1.cwise() * m2);
|
2009-11-19 01:15:19 +08:00
|
|
|
|
2008-08-23 06:49:09 +08:00
|
|
|
VERIFY_IS_APPROX(mones, m2.cwise()/m2);
|
2010-04-29 06:51:38 +08:00
|
|
|
if(!NumTraits<Scalar>::IsInteger)
|
2008-06-24 23:13:00 +08:00
|
|
|
{
|
2008-08-23 06:49:09 +08:00
|
|
|
VERIFY_IS_APPROX(m1.cwise() / m2, m1.cwise() * (m2.cwise().inverse()));
|
|
|
|
m3 = m1.cwise().abs().cwise().sqrt();
|
|
|
|
VERIFY_IS_APPROX(m3.cwise().square(), m1.cwise().abs());
|
|
|
|
VERIFY_IS_APPROX(m1.cwise().square().cwise().sqrt(), m1.cwise().abs());
|
|
|
|
VERIFY_IS_APPROX(m1.cwise().abs().cwise().log().cwise().exp() , m1.cwise().abs());
|
2009-01-24 18:52:18 +08:00
|
|
|
|
|
|
|
VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
|
2009-01-27 01:56:04 +08:00
|
|
|
m3 = (m1.cwise().abs().cwise()<=RealScalar(0.01)).select(mones,m1);
|
2009-01-24 18:52:18 +08:00
|
|
|
VERIFY_IS_APPROX(m3.cwise().pow(-1), m3.cwise().inverse());
|
|
|
|
m3 = m1.cwise().abs();
|
2009-01-27 01:56:04 +08:00
|
|
|
VERIFY_IS_APPROX(m3.cwise().pow(RealScalar(0.5)), m3.cwise().sqrt());
|
2009-11-19 01:15:19 +08:00
|
|
|
|
2008-08-23 06:49:09 +08:00
|
|
|
// VERIFY_IS_APPROX(m1.cwise().tan(), m1.cwise().sin().cwise() / m1.cwise().cos());
|
|
|
|
VERIFY_IS_APPROX(mones, m1.cwise().sin().cwise().square() + m1.cwise().cos().cwise().square());
|
2009-01-12 05:04:23 +08:00
|
|
|
m3 = m1;
|
|
|
|
m3.cwise() /= m2;
|
|
|
|
VERIFY_IS_APPROX(m3, m1.cwise() / m2);
|
2008-08-23 06:49:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check min
|
|
|
|
VERIFY_IS_APPROX( m1.cwise().min(m2), m2.cwise().min(m1) );
|
|
|
|
VERIFY_IS_APPROX( m1.cwise().min(m1+mones), m1 );
|
|
|
|
VERIFY_IS_APPROX( m1.cwise().min(m1-mones), m1-mones );
|
|
|
|
|
|
|
|
// check max
|
|
|
|
VERIFY_IS_APPROX( m1.cwise().max(m2), m2.cwise().max(m1) );
|
|
|
|
VERIFY_IS_APPROX( m1.cwise().max(m1-mones), m1 );
|
|
|
|
VERIFY_IS_APPROX( m1.cwise().max(m1+mones), m1+mones );
|
2009-11-19 01:15:19 +08:00
|
|
|
|
2008-08-23 06:49:09 +08:00
|
|
|
VERIFY( (m1.cwise() == m1).all() );
|
|
|
|
VERIFY( (m1.cwise() != m2).any() );
|
|
|
|
VERIFY(!(m1.cwise() == (m1+mones)).any() );
|
|
|
|
if (rows*cols>1)
|
|
|
|
{
|
|
|
|
m3 = m1;
|
|
|
|
m3(r,c) += 1;
|
|
|
|
VERIFY( (m1.cwise() == m3).any() );
|
|
|
|
VERIFY( !(m1.cwise() == m3).all() );
|
2008-06-24 23:13:00 +08:00
|
|
|
}
|
2008-08-23 06:49:09 +08:00
|
|
|
VERIFY( (m1.cwise().min(m2).cwise() <= m2).all() );
|
|
|
|
VERIFY( (m1.cwise().max(m2).cwise() >= m2).all() );
|
|
|
|
VERIFY( (m1.cwise().min(m2).cwise() < (m1+mones)).all() );
|
|
|
|
VERIFY( (m1.cwise().max(m2).cwise() > (m1-mones)).all() );
|
2008-04-04 02:13:27 +08:00
|
|
|
|
2008-07-08 08:49:10 +08:00
|
|
|
VERIFY( (m1.cwise()<m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).all() );
|
|
|
|
VERIFY( !(m1.cwise()<m1.unaryExpr(bind2nd(minus<Scalar>(), Scalar(1)))).all() );
|
|
|
|
VERIFY( !(m1.cwise()>m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).any() );
|
2008-02-29 22:35:14 +08:00
|
|
|
}
|
|
|
|
|
2008-05-22 20:18:55 +08:00
|
|
|
void test_cwiseop()
|
2008-02-29 22:35:14 +08:00
|
|
|
{
|
2008-05-22 20:18:55 +08:00
|
|
|
for(int i = 0; i < g_repeat ; i++) {
|
2009-10-29 06:19:29 +08:00
|
|
|
CALL_SUBTEST_1( cwiseops(Matrix<float, 1, 1>()) );
|
|
|
|
CALL_SUBTEST_2( cwiseops(Matrix4d()) );
|
|
|
|
CALL_SUBTEST_3( cwiseops(MatrixXf(3, 3)) );
|
|
|
|
CALL_SUBTEST_4( cwiseops(MatrixXf(22, 22)) );
|
|
|
|
CALL_SUBTEST_5( cwiseops(MatrixXi(8, 12)) );
|
|
|
|
CALL_SUBTEST_6( cwiseops(MatrixXd(20, 20)) );
|
2008-02-29 22:35:14 +08:00
|
|
|
}
|
|
|
|
}
|