gtsam
3rdparty
Eigen
test
nesting_ops.cpp
Go to the documentation of this file.
1
// This file is part of Eigen, a lightweight C++ template library
2
// for linear algebra.
3
//
4
// Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
5
// Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
6
//
7
// This Source Code Form is subject to the terms of the Mozilla
8
// Public License v. 2.0. If a copy of the MPL was not distributed
9
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11
#define TEST_ENABLE_TEMPORARY_TRACKING
12
13
#include "
main.h
"
14
15
template
<
int
N,
typename
XprType>
16
void
use_n_times
(
const
XprType
&xpr)
17
{
18
typename
internal::nested_eval<XprType,N>::type
mat
(xpr);
19
typename
XprType::PlainObject
res
(
mat
.rows(),
mat
.cols());
20
nb_temporaries
--;
// remove res
21
res
.setZero();
22
for
(
int
i
=0;
i
<
N
; ++
i
)
23
res
+=
mat
;
24
}
25
26
template
<
int
N,
typename
ReferenceType,
typename
XprType>
27
bool
verify_eval_type
(
const
XprType
&,
const
ReferenceType&)
28
{
29
typedef
typename
internal::nested_eval<XprType,N>::type
EvalType;
30
return
internal::is_same<typename internal::remove_all<EvalType>::type
,
typename
internal::remove_all<ReferenceType>::type
>
::value
;
31
}
32
33
template
<
typename
MatrixType>
void
run_nesting_ops_1
(
const
MatrixType
& _m)
34
{
35
typename
internal::nested_eval<MatrixType,2>::type
m
(_m);
36
37
// Make really sure that we are in debug mode!
38
VERIFY_RAISES_ASSERT
(
eigen_assert
(
false
));
39
40
// The only intention of these tests is to ensure that this code does
41
// not trigger any asserts or segmentation faults... more to come.
42
VERIFY_IS_APPROX
( (
m
.transpose() *
m
).diagonal().sum(), (
m
.transpose() *
m
).diagonal().sum() );
43
VERIFY_IS_APPROX
( (
m
.transpose() *
m
).diagonal().array().abs().sum(), (
m
.transpose() *
m
).diagonal().array().abs().sum() );
44
45
VERIFY_IS_APPROX
( (
m
.transpose() *
m
).array().abs().sum(), (
m
.transpose() *
m
).array().abs().sum() );
46
}
47
48
template
<
typename
MatrixType>
void
run_nesting_ops_2
(
const
MatrixType
& _m)
49
{
50
typedef
typename
MatrixType::Scalar
Scalar
;
51
Index
rows
= _m.rows();
52
Index
cols
= _m.cols();
53
MatrixType
m1
= MatrixType::Random(
rows
,
cols
);
54
Matrix<Scalar,MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime,ColMajor>
m2
;
55
56
if
((MatrixType::SizeAtCompileTime==
Dynamic
))
57
{
58
VERIFY_EVALUATION_COUNT
( use_n_times<1>(
m1
+
m1
*
m1
), 1 );
59
VERIFY_EVALUATION_COUNT
( use_n_times<10>(
m1
+
m1
*
m1
), 1 );
60
61
VERIFY_EVALUATION_COUNT
( use_n_times<1>(
m1
.template
triangularView<Lower>
().solve(
m1
.col(0))), 1 );
62
VERIFY_EVALUATION_COUNT
( use_n_times<10>(
m1
.template
triangularView<Lower>
().solve(
m1
.col(0))), 1 );
63
64
VERIFY_EVALUATION_COUNT
( use_n_times<1>(
Scalar
(2)*
m1
.template
triangularView<Lower>
().solve(
m1
.col(0))), 2 );
// FIXME could be one by applying the scaling in-place on the solve result
65
VERIFY_EVALUATION_COUNT
( use_n_times<1>(
m1
.col(0)+
m1
.template
triangularView<Lower>
().solve(
m1
.col(0))), 2 );
// FIXME could be one by adding m1.col() inplace
66
VERIFY_EVALUATION_COUNT
( use_n_times<10>(
m1
.col(0)+
m1
.template
triangularView<Lower>
().solve(
m1
.col(0))), 2 );
67
}
68
69
{
70
VERIFY
( verify_eval_type<10>(
m1
,
m1
) );
71
if
(!
NumTraits<Scalar>::IsComplex
)
72
{
73
VERIFY
( verify_eval_type<3>(2*
m1
, 2*
m1
) );
74
VERIFY
( verify_eval_type<4>(2*
m1
,
m1
) );
75
}
76
else
77
{
78
VERIFY
( verify_eval_type<2>(2*
m1
, 2*
m1
) );
79
VERIFY
( verify_eval_type<3>(2*
m1
,
m1
) );
80
}
81
VERIFY
( verify_eval_type<2>(
m1
+
m1
,
m1
+
m1
) );
82
VERIFY
( verify_eval_type<3>(
m1
+
m1
,
m1
) );
83
VERIFY
( verify_eval_type<1>(
m1
*
m1
.transpose(),
m2
) );
84
VERIFY
( verify_eval_type<1>(
m1
*(
m1
+
m1
).transpose(),
m2
) );
85
VERIFY
( verify_eval_type<2>(
m1
*
m1
.transpose(),
m2
) );
86
VERIFY
( verify_eval_type<1>(
m1
+
m1
*
m1
,
m1
) );
87
88
VERIFY
( verify_eval_type<1>(
m1
.template
triangularView<Lower>
().solve(
m1
),
m1
) );
89
VERIFY
( verify_eval_type<1>(
m1
+
m1
.template
triangularView<Lower>
().solve(
m1
),
m1
) );
90
}
91
}
92
93
94
EIGEN_DECLARE_TEST
(nesting_ops)
95
{
96
CALL_SUBTEST_1
(
run_nesting_ops_1
(MatrixXf::Random(25,25)));
97
CALL_SUBTEST_2
(
run_nesting_ops_1
(MatrixXcd::Random(25,25)));
98
CALL_SUBTEST_3
(
run_nesting_ops_1
(Matrix4f::Random()));
99
CALL_SUBTEST_4
(
run_nesting_ops_1
(Matrix2d::Random()));
100
101
Index
s
= internal::random<int>(1,
EIGEN_TEST_MAX_SIZE
);
102
CALL_SUBTEST_1
(
run_nesting_ops_2
(MatrixXf(
s
,
s
)) );
103
CALL_SUBTEST_2
(
run_nesting_ops_2
(MatrixXcd(
s
,
s
)) );
104
CALL_SUBTEST_3
(
run_nesting_ops_2
(Matrix4f()) );
105
CALL_SUBTEST_4
(
run_nesting_ops_2
(Matrix2d()) );
106
TEST_SET_BUT_UNUSED_VARIABLE
(
s
)
107
}
use_n_times
void use_n_times(const XprType &xpr)
Definition:
nesting_ops.cpp:16
gtsam.examples.DogLegOptimizerExample.type
type
Definition:
DogLegOptimizerExample.py:111
s
RealScalar s
Definition:
level1_cplx_impl.h:126
Eigen::CwiseBinaryOp
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition:
CwiseBinaryOp.h:77
MatrixType
MatrixXf MatrixType
Definition:
benchmark-blocking-sizes.cpp:52
verify_eval_type
bool verify_eval_type(const XprType &, const ReferenceType &)
Definition:
nesting_ops.cpp:27
eigen_assert
#define eigen_assert(x)
Definition:
Macros.h:1037
m1
Matrix3d m1
Definition:
IOFormat.cpp:2
mat
MatrixXf mat
Definition:
Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
res
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition:
PartialRedux_count.cpp:3
rows
int rows
Definition:
Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition:
main.h:340
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition:
split_test_helper.h:22
m2
MatrixType m2(n_dims)
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition:
split_test_helper.h:16
Eigen::internal::true_type
Definition:
Meta.h:96
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition:
split_test_helper.h:4
nb_temporaries
static long int nb_temporaries
Definition:
test/sparse_product.cpp:16
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(nesting_ops)
Definition:
nesting_ops.cpp:94
run_nesting_ops_1
void run_nesting_ops_1(const MatrixType &_m)
Definition:
nesting_ops.cpp:33
Eigen::Dynamic
const int Dynamic
Definition:
Constants.h:22
m
Matrix3f m
Definition:
AngleAxis_mimic_euler.cpp:1
Eigen::Triplet< double >
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition:
split_test_helper.h:10
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition:
integer_types.cpp:15
main.h
run_nesting_ops_2
void run_nesting_ops_2(const MatrixType &_m)
Definition:
nesting_ops.cpp:48
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition:
boostmultiprec.cpp:16
VERIFY_EVALUATION_COUNT
#define VERIFY_EVALUATION_COUNT(XPR, N)
Definition:
test/sparse_product.cpp:27
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition:
3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
N
#define N
Definition:
igam.h:9
cols
int cols
Definition:
Tutorial_commainit_02.cpp:1
triangularView< Lower >
A triangularView< Lower >().adjoint().solveInPlace(B)
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition:
NumTraits.h:232
test_callbacks.value
value
Definition:
test_callbacks.py:160
i
int i
Definition:
BiCGSTAB_step_by_step.cpp:9
TEST_SET_BUT_UNUSED_VARIABLE
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition:
main.h:121
Scalar
SCALAR Scalar
Definition:
bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition:
main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition:
Meta.h:74
gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:03:12