xref: /aosp_15_r20/external/eigen/test/nestbyvalue.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2019 Gael Guennebaud <[email protected]>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #define TEST_ENABLE_TEMPORARY_TRACKING
11 
12 #include "main.h"
13 
14 typedef NestByValue<MatrixXd> CpyMatrixXd;
15 typedef CwiseBinaryOp<internal::scalar_sum_op<double,double>,const CpyMatrixXd,const CpyMatrixXd> XprType;
16 
get_xpr_with_temps(const MatrixXd & a)17 XprType get_xpr_with_temps(const MatrixXd& a)
18 {
19   MatrixXd t1 = a.rowwise().reverse();
20   MatrixXd t2 = a+a;
21   return t1.nestByValue() + t2.nestByValue();
22 }
23 
EIGEN_DECLARE_TEST(nestbyvalue)24 EIGEN_DECLARE_TEST(nestbyvalue)
25 {
26   for(int i = 0; i < g_repeat; i++) {
27     Index rows = internal::random<Index>(1,EIGEN_TEST_MAX_SIZE);
28     Index cols = internal::random<Index>(1,EIGEN_TEST_MAX_SIZE);
29     MatrixXd a = MatrixXd(rows,cols);
30     nb_temporaries = 0;
31     XprType x = get_xpr_with_temps(a);
32     VERIFY_IS_EQUAL(nb_temporaries,6);
33     MatrixXd b = x;
34     VERIFY_IS_EQUAL(nb_temporaries,6+1);
35     VERIFY_IS_APPROX(b, a.rowwise().reverse().eval() + (a+a).eval());
36   }
37 }
38