xref: /aosp_15_r20/external/eigen/test/linearstructure.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2006-2008 Benoit Jacob <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2014 Gael Guennebaud <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li 
11*bf2c3715SXin Li static bool g_called;
12*bf2c3715SXin Li #define EIGEN_SCALAR_BINARY_OP_PLUGIN { g_called |= (!internal::is_same<LhsScalar,RhsScalar>::value); }
13*bf2c3715SXin Li 
14*bf2c3715SXin Li #include "main.h"
15*bf2c3715SXin Li 
linearStructure(const MatrixType & m)16*bf2c3715SXin Li template<typename MatrixType> void linearStructure(const MatrixType& m)
17*bf2c3715SXin Li {
18*bf2c3715SXin Li   using std::abs;
19*bf2c3715SXin Li   /* this test covers the following files:
20*bf2c3715SXin Li      CwiseUnaryOp.h, CwiseBinaryOp.h, SelfCwiseBinaryOp.h
21*bf2c3715SXin Li   */
22*bf2c3715SXin Li   typedef typename MatrixType::Scalar Scalar;
23*bf2c3715SXin Li   typedef typename MatrixType::RealScalar RealScalar;
24*bf2c3715SXin Li 
25*bf2c3715SXin Li   Index rows = m.rows();
26*bf2c3715SXin Li   Index cols = m.cols();
27*bf2c3715SXin Li 
28*bf2c3715SXin Li   // this test relies a lot on Random.h, and there's not much more that we can do
29*bf2c3715SXin Li   // to test it, hence I consider that we will have tested Random.h
30*bf2c3715SXin Li   MatrixType m1 = MatrixType::Random(rows, cols),
31*bf2c3715SXin Li              m2 = MatrixType::Random(rows, cols),
32*bf2c3715SXin Li              m3(rows, cols);
33*bf2c3715SXin Li 
34*bf2c3715SXin Li   Scalar s1 = internal::random<Scalar>();
35*bf2c3715SXin Li   while (abs(s1)<RealScalar(1e-3)) s1 = internal::random<Scalar>();
36*bf2c3715SXin Li 
37*bf2c3715SXin Li   Index r = internal::random<Index>(0, rows-1),
38*bf2c3715SXin Li         c = internal::random<Index>(0, cols-1);
39*bf2c3715SXin Li 
40*bf2c3715SXin Li   VERIFY_IS_APPROX(-(-m1),                  m1);
41*bf2c3715SXin Li   VERIFY_IS_APPROX(m1+m1,                   2*m1);
42*bf2c3715SXin Li   VERIFY_IS_APPROX(m1+m2-m1,                m2);
43*bf2c3715SXin Li   VERIFY_IS_APPROX(-m2+m1+m2,               m1);
44*bf2c3715SXin Li   VERIFY_IS_APPROX(m1*s1,                   s1*m1);
45*bf2c3715SXin Li   VERIFY_IS_APPROX((m1+m2)*s1,              s1*m1+s1*m2);
46*bf2c3715SXin Li   VERIFY_IS_APPROX((-m1+m2)*s1,             -s1*m1+s1*m2);
47*bf2c3715SXin Li   m3 = m2; m3 += m1;
48*bf2c3715SXin Li   VERIFY_IS_APPROX(m3,                      m1+m2);
49*bf2c3715SXin Li   m3 = m2; m3 -= m1;
50*bf2c3715SXin Li   VERIFY_IS_APPROX(m3,                      m2-m1);
51*bf2c3715SXin Li   m3 = m2; m3 *= s1;
52*bf2c3715SXin Li   VERIFY_IS_APPROX(m3,                      s1*m2);
53*bf2c3715SXin Li   if(!NumTraits<Scalar>::IsInteger)
54*bf2c3715SXin Li   {
55*bf2c3715SXin Li     m3 = m2; m3 /= s1;
56*bf2c3715SXin Li     VERIFY_IS_APPROX(m3,                    m2/s1);
57*bf2c3715SXin Li   }
58*bf2c3715SXin Li 
59*bf2c3715SXin Li   // again, test operator() to check const-qualification
60*bf2c3715SXin Li   VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c)));
61*bf2c3715SXin Li   VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c)));
62*bf2c3715SXin Li   VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
63*bf2c3715SXin Li   VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c)));
64*bf2c3715SXin Li   VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1);
65*bf2c3715SXin Li   if(!NumTraits<Scalar>::IsInteger)
66*bf2c3715SXin Li     VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1);
67*bf2c3715SXin Li 
68*bf2c3715SXin Li   // use .block to disable vectorization and compare to the vectorized version
69*bf2c3715SXin Li   VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1);
70*bf2c3715SXin Li   VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), m1.cwiseProduct(m1));
71*bf2c3715SXin Li   VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1);
72*bf2c3715SXin Li   VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1);
73*bf2c3715SXin Li }
74*bf2c3715SXin Li 
75*bf2c3715SXin Li // Make sure that complex * real and real * complex are properly optimized
real_complex(DenseIndex rows=MatrixType::RowsAtCompileTime,DenseIndex cols=MatrixType::ColsAtCompileTime)76*bf2c3715SXin Li template<typename MatrixType> void real_complex(DenseIndex rows = MatrixType::RowsAtCompileTime, DenseIndex cols = MatrixType::ColsAtCompileTime)
77*bf2c3715SXin Li {
78*bf2c3715SXin Li   typedef typename MatrixType::Scalar Scalar;
79*bf2c3715SXin Li   typedef typename MatrixType::RealScalar RealScalar;
80*bf2c3715SXin Li 
81*bf2c3715SXin Li   RealScalar s = internal::random<RealScalar>();
82*bf2c3715SXin Li   MatrixType m1 = MatrixType::Random(rows, cols);
83*bf2c3715SXin Li 
84*bf2c3715SXin Li   g_called = false;
85*bf2c3715SXin Li   VERIFY_IS_APPROX(s*m1, Scalar(s)*m1);
86*bf2c3715SXin Li   VERIFY(g_called && "real * matrix<complex> not properly optimized");
87*bf2c3715SXin Li 
88*bf2c3715SXin Li   g_called = false;
89*bf2c3715SXin Li   VERIFY_IS_APPROX(m1*s, m1*Scalar(s));
90*bf2c3715SXin Li   VERIFY(g_called && "matrix<complex> * real not properly optimized");
91*bf2c3715SXin Li 
92*bf2c3715SXin Li   g_called = false;
93*bf2c3715SXin Li   VERIFY_IS_APPROX(m1/s, m1/Scalar(s));
94*bf2c3715SXin Li   VERIFY(g_called && "matrix<complex> / real not properly optimized");
95*bf2c3715SXin Li 
96*bf2c3715SXin Li   g_called = false;
97*bf2c3715SXin Li   VERIFY_IS_APPROX(s+m1.array(), Scalar(s)+m1.array());
98*bf2c3715SXin Li   VERIFY(g_called && "real + matrix<complex> not properly optimized");
99*bf2c3715SXin Li 
100*bf2c3715SXin Li   g_called = false;
101*bf2c3715SXin Li   VERIFY_IS_APPROX(m1.array()+s, m1.array()+Scalar(s));
102*bf2c3715SXin Li   VERIFY(g_called && "matrix<complex> + real not properly optimized");
103*bf2c3715SXin Li 
104*bf2c3715SXin Li   g_called = false;
105*bf2c3715SXin Li   VERIFY_IS_APPROX(s-m1.array(), Scalar(s)-m1.array());
106*bf2c3715SXin Li   VERIFY(g_called && "real - matrix<complex> not properly optimized");
107*bf2c3715SXin Li 
108*bf2c3715SXin Li   g_called = false;
109*bf2c3715SXin Li   VERIFY_IS_APPROX(m1.array()-s, m1.array()-Scalar(s));
110*bf2c3715SXin Li   VERIFY(g_called && "matrix<complex> - real not properly optimized");
111*bf2c3715SXin Li }
112*bf2c3715SXin Li 
113*bf2c3715SXin Li template<int>
linearstructure_overflow()114*bf2c3715SXin Li void linearstructure_overflow()
115*bf2c3715SXin Li {
116*bf2c3715SXin Li   // make sure that /=scalar and /scalar do not overflow
117*bf2c3715SXin Li   // rational: 1.0/4.94e-320 overflow, but m/4.94e-320 should not
118*bf2c3715SXin Li   Matrix4d m2, m3;
119*bf2c3715SXin Li   m3 = m2 =  Matrix4d::Random()*1e-20;
120*bf2c3715SXin Li   m2 = m2 / 4.9e-320;
121*bf2c3715SXin Li   VERIFY_IS_APPROX(m2.cwiseQuotient(m2), Matrix4d::Ones());
122*bf2c3715SXin Li   m3 /= 4.9e-320;
123*bf2c3715SXin Li   VERIFY_IS_APPROX(m3.cwiseQuotient(m3), Matrix4d::Ones());
124*bf2c3715SXin Li }
125*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(linearstructure)126*bf2c3715SXin Li EIGEN_DECLARE_TEST(linearstructure)
127*bf2c3715SXin Li {
128*bf2c3715SXin Li   g_called = true;
129*bf2c3715SXin Li   VERIFY(g_called); // avoid `unneeded-internal-declaration` warning.
130*bf2c3715SXin Li   for(int i = 0; i < g_repeat; i++) {
131*bf2c3715SXin Li     CALL_SUBTEST_1( linearStructure(Matrix<float, 1, 1>()) );
132*bf2c3715SXin Li     CALL_SUBTEST_2( linearStructure(Matrix2f()) );
133*bf2c3715SXin Li     CALL_SUBTEST_3( linearStructure(Vector3d()) );
134*bf2c3715SXin Li     CALL_SUBTEST_4( linearStructure(Matrix4d()) );
135*bf2c3715SXin Li     CALL_SUBTEST_5( linearStructure(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
136*bf2c3715SXin Li     CALL_SUBTEST_6( linearStructure(MatrixXf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
137*bf2c3715SXin Li     CALL_SUBTEST_7( linearStructure(MatrixXi (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
138*bf2c3715SXin Li     CALL_SUBTEST_8( linearStructure(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
139*bf2c3715SXin Li     CALL_SUBTEST_9( linearStructure(ArrayXXf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
140*bf2c3715SXin Li     CALL_SUBTEST_10( linearStructure(ArrayXXcf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
141*bf2c3715SXin Li 
142*bf2c3715SXin Li     CALL_SUBTEST_11( real_complex<Matrix4cd>() );
143*bf2c3715SXin Li     CALL_SUBTEST_11( real_complex<MatrixXcf>(10,10) );
144*bf2c3715SXin Li     CALL_SUBTEST_11( real_complex<ArrayXXcf>(10,10) );
145*bf2c3715SXin Li   }
146*bf2c3715SXin Li   CALL_SUBTEST_4( linearstructure_overflow<0>() );
147*bf2c3715SXin Li }
148