mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-07-20 20:04:26 +08:00
Add unit test for nested_eval
This commit is contained in:
parent
78b8c344b5
commit
515ecddb97
@ -2,14 +2,35 @@
|
|||||||
// for linear algebra.
|
// for linear algebra.
|
||||||
//
|
//
|
||||||
// Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
|
// Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
|
||||||
|
// Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
|
||||||
//
|
//
|
||||||
// This Source Code Form is subject to the terms of the Mozilla
|
// This Source Code Form is subject to the terms of the Mozilla
|
||||||
// Public License v. 2.0. If a copy of the MPL was not distributed
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
||||||
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
#define TEST_ENABLE_TEMPORARY_TRACKING
|
||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
template <typename MatrixType> void run_nesting_ops(const MatrixType& _m)
|
template <int N, typename XprType>
|
||||||
|
void use_n_times(const XprType &xpr)
|
||||||
|
{
|
||||||
|
typename internal::nested_eval<XprType,N>::type mat(xpr);
|
||||||
|
typename XprType::PlainObject res(mat.rows(), mat.cols());
|
||||||
|
nb_temporaries--; // remove res
|
||||||
|
res.setZero();
|
||||||
|
for(int i=0; i<N; ++i)
|
||||||
|
res += mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N, typename ReferenceType, typename XprType>
|
||||||
|
bool verify_eval_type(const XprType &, const ReferenceType&)
|
||||||
|
{
|
||||||
|
typedef typename internal::nested_eval<XprType,N>::type EvalType;
|
||||||
|
return internal::is_same<typename internal::remove_all<EvalType>::type, typename internal::remove_all<ReferenceType>::type>::value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename MatrixType> void run_nesting_ops_1(const MatrixType& _m)
|
||||||
{
|
{
|
||||||
typename internal::nested_eval<MatrixType,2>::type m(_m);
|
typename internal::nested_eval<MatrixType,2>::type m(_m);
|
||||||
|
|
||||||
@ -24,10 +45,63 @@ template <typename MatrixType> void run_nesting_ops(const MatrixType& _m)
|
|||||||
VERIFY_IS_APPROX( (m.transpose() * m).array().abs().sum(), (m.transpose() * m).array().abs().sum() );
|
VERIFY_IS_APPROX( (m.transpose() * m).array().abs().sum(), (m.transpose() * m).array().abs().sum() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename MatrixType> void run_nesting_ops_2(const MatrixType& _m)
|
||||||
|
{
|
||||||
|
Index rows = _m.rows();
|
||||||
|
Index cols = _m.cols();
|
||||||
|
MatrixType m1 = MatrixType::Random(rows,cols);
|
||||||
|
|
||||||
|
if((MatrixType::SizeAtCompileTime==Dynamic))
|
||||||
|
{
|
||||||
|
|
||||||
|
VERIFY_EVALUATION_COUNT( use_n_times<10>(m1), 0 );
|
||||||
|
if(!NumTraits<typename MatrixType::Scalar>::IsComplex)
|
||||||
|
{
|
||||||
|
VERIFY_EVALUATION_COUNT( use_n_times<3>(2*m1), 0 );
|
||||||
|
VERIFY_EVALUATION_COUNT( use_n_times<4>(2*m1), 1 );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
VERIFY_EVALUATION_COUNT( use_n_times<1>(2*m1), 0 );
|
||||||
|
VERIFY_EVALUATION_COUNT( use_n_times<2>(2*m1), 1 );
|
||||||
|
}
|
||||||
|
VERIFY_EVALUATION_COUNT( use_n_times<2>(m1+m1), 0 );
|
||||||
|
VERIFY_EVALUATION_COUNT( use_n_times<3>(m1+m1), 1 );
|
||||||
|
VERIFY_EVALUATION_COUNT( use_n_times<1>(m1*m1.transpose()), 1 );
|
||||||
|
VERIFY_EVALUATION_COUNT( use_n_times<2>(m1*m1.transpose()), 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
VERIFY( verify_eval_type<10>(m1, m1) );
|
||||||
|
if(!NumTraits<typename MatrixType::Scalar>::IsComplex)
|
||||||
|
{
|
||||||
|
VERIFY( verify_eval_type<3>(2*m1, 2*m1) );
|
||||||
|
VERIFY( verify_eval_type<4>(2*m1, m1) );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
VERIFY( verify_eval_type<1>(2*m1, 2*m1) );
|
||||||
|
VERIFY( verify_eval_type<2>(2*m1, m1) );
|
||||||
|
}
|
||||||
|
VERIFY( verify_eval_type<2>(m1+m1, m1+m1) );
|
||||||
|
VERIFY( verify_eval_type<3>(m1+m1, m1) );
|
||||||
|
VERIFY( verify_eval_type<1>(m1*m1.transpose(), m1) );
|
||||||
|
VERIFY( verify_eval_type<1>(m1*(m1+m1).transpose(), m1) );
|
||||||
|
VERIFY( verify_eval_type<2>(m1*m1.transpose(), m1) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void test_nesting_ops()
|
void test_nesting_ops()
|
||||||
{
|
{
|
||||||
CALL_SUBTEST_1(run_nesting_ops(MatrixXf::Random(25,25)));
|
CALL_SUBTEST_1(run_nesting_ops_1(MatrixXf::Random(25,25)));
|
||||||
CALL_SUBTEST_2(run_nesting_ops(MatrixXd::Random(25,25)));
|
CALL_SUBTEST_2(run_nesting_ops_1(MatrixXcd::Random(25,25)));
|
||||||
CALL_SUBTEST_3(run_nesting_ops(Matrix4f::Random()));
|
CALL_SUBTEST_3(run_nesting_ops_1(Matrix4f::Random()));
|
||||||
CALL_SUBTEST_4(run_nesting_ops(Matrix4d::Random()));
|
CALL_SUBTEST_4(run_nesting_ops_1(Matrix2d::Random()));
|
||||||
|
|
||||||
|
CALL_SUBTEST_1( run_nesting_ops_2(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
|
||||||
|
CALL_SUBTEST_2( run_nesting_ops_2(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
|
||||||
|
CALL_SUBTEST_3( run_nesting_ops_2(Matrix4f()) );
|
||||||
|
CALL_SUBTEST_4( run_nesting_ops_2(Matrix2d()) );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user