xref: /aosp_15_r20/external/eigen/test/product_large.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 //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li 
10*bf2c3715SXin Li #include "product.h"
11*bf2c3715SXin Li #include <Eigen/LU>
12*bf2c3715SXin Li 
13*bf2c3715SXin Li template<typename T>
test_aliasing()14*bf2c3715SXin Li void test_aliasing()
15*bf2c3715SXin Li {
16*bf2c3715SXin Li   int rows = internal::random<int>(1,12);
17*bf2c3715SXin Li   int cols = internal::random<int>(1,12);
18*bf2c3715SXin Li   typedef Matrix<T,Dynamic,Dynamic> MatrixType;
19*bf2c3715SXin Li   typedef Matrix<T,Dynamic,1> VectorType;
20*bf2c3715SXin Li   VectorType x(cols); x.setRandom();
21*bf2c3715SXin Li   VectorType z(x);
22*bf2c3715SXin Li   VectorType y(rows); y.setZero();
23*bf2c3715SXin Li   MatrixType A(rows,cols); A.setRandom();
24*bf2c3715SXin Li   // CwiseBinaryOp
25*bf2c3715SXin Li   VERIFY_IS_APPROX(x = y + A*x, A*z);     // OK because "y + A*x" is marked as "assume-aliasing"
26*bf2c3715SXin Li   x = z;
27*bf2c3715SXin Li   // CwiseUnaryOp
28*bf2c3715SXin Li   VERIFY_IS_APPROX(x = T(1.)*(A*x), A*z); // OK because 1*(A*x) is replaced by (1*A*x) which is a Product<> expression
29*bf2c3715SXin Li   x = z;
30*bf2c3715SXin Li   // VERIFY_IS_APPROX(x = y-A*x, -A*z);   // Not OK in 3.3 because x is resized before A*x gets evaluated
31*bf2c3715SXin Li   x = z;
32*bf2c3715SXin Li }
33*bf2c3715SXin Li 
34*bf2c3715SXin Li template<int>
product_large_regressions()35*bf2c3715SXin Li void product_large_regressions()
36*bf2c3715SXin Li {
37*bf2c3715SXin Li   {
38*bf2c3715SXin Li     // test a specific issue in DiagonalProduct
39*bf2c3715SXin Li     int N = 1000000;
40*bf2c3715SXin Li     VectorXf v = VectorXf::Ones(N);
41*bf2c3715SXin Li     MatrixXf m = MatrixXf::Ones(N,3);
42*bf2c3715SXin Li     m = (v+v).asDiagonal() * m;
43*bf2c3715SXin Li     VERIFY_IS_APPROX(m, MatrixXf::Constant(N,3,2));
44*bf2c3715SXin Li   }
45*bf2c3715SXin Li 
46*bf2c3715SXin Li   {
47*bf2c3715SXin Li     // test deferred resizing in Matrix::operator=
48*bf2c3715SXin Li     MatrixXf a = MatrixXf::Random(10,4), b = MatrixXf::Random(4,10), c = a;
49*bf2c3715SXin Li     VERIFY_IS_APPROX((a = a * b), (c * b).eval());
50*bf2c3715SXin Li   }
51*bf2c3715SXin Li 
52*bf2c3715SXin Li   {
53*bf2c3715SXin Li     // check the functions to setup blocking sizes compile and do not segfault
54*bf2c3715SXin Li     // FIXME check they do what they are supposed to do !!
55*bf2c3715SXin Li     std::ptrdiff_t l1 = internal::random<int>(10000,20000);
56*bf2c3715SXin Li     std::ptrdiff_t l2 = internal::random<int>(100000,200000);
57*bf2c3715SXin Li     std::ptrdiff_t l3 = internal::random<int>(1000000,2000000);
58*bf2c3715SXin Li     setCpuCacheSizes(l1,l2,l3);
59*bf2c3715SXin Li     VERIFY(l1==l1CacheSize());
60*bf2c3715SXin Li     VERIFY(l2==l2CacheSize());
61*bf2c3715SXin Li     std::ptrdiff_t k1 = internal::random<int>(10,100)*16;
62*bf2c3715SXin Li     std::ptrdiff_t m1 = internal::random<int>(10,100)*16;
63*bf2c3715SXin Li     std::ptrdiff_t n1 = internal::random<int>(10,100)*16;
64*bf2c3715SXin Li     // only makes sure it compiles fine
65*bf2c3715SXin Li     internal::computeProductBlockingSizes<float,float,std::ptrdiff_t>(k1,m1,n1,1);
66*bf2c3715SXin Li   }
67*bf2c3715SXin Li 
68*bf2c3715SXin Li   {
69*bf2c3715SXin Li     // test regression in row-vector by matrix (bad Map type)
70*bf2c3715SXin Li     MatrixXf mat1(10,32); mat1.setRandom();
71*bf2c3715SXin Li     MatrixXf mat2(32,32); mat2.setRandom();
72*bf2c3715SXin Li     MatrixXf r1 = mat1.row(2)*mat2.transpose();
73*bf2c3715SXin Li     VERIFY_IS_APPROX(r1, (mat1.row(2)*mat2.transpose()).eval());
74*bf2c3715SXin Li 
75*bf2c3715SXin Li     MatrixXf r2 = mat1.row(2)*mat2;
76*bf2c3715SXin Li     VERIFY_IS_APPROX(r2, (mat1.row(2)*mat2).eval());
77*bf2c3715SXin Li   }
78*bf2c3715SXin Li 
79*bf2c3715SXin Li   {
80*bf2c3715SXin Li     Eigen::MatrixXd A(10,10), B, C;
81*bf2c3715SXin Li     A.setRandom();
82*bf2c3715SXin Li     C = A;
83*bf2c3715SXin Li     for(int k=0; k<79; ++k)
84*bf2c3715SXin Li       C = C * A;
85*bf2c3715SXin Li     B.noalias() = (((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)) * ((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)))
86*bf2c3715SXin Li                 * (((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)) * ((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)));
87*bf2c3715SXin Li     VERIFY_IS_APPROX(B,C);
88*bf2c3715SXin Li   }
89*bf2c3715SXin Li }
90*bf2c3715SXin Li 
91*bf2c3715SXin Li template<int>
bug_1622()92*bf2c3715SXin Li void bug_1622() {
93*bf2c3715SXin Li   typedef Matrix<double, 2, -1, 0, 2, -1> Mat2X;
94*bf2c3715SXin Li   Mat2X x(2,2); x.setRandom();
95*bf2c3715SXin Li   MatrixXd y(2,2); y.setRandom();
96*bf2c3715SXin Li   const Mat2X K1 = x * y.inverse();
97*bf2c3715SXin Li   const Matrix2d K2 = x * y.inverse();
98*bf2c3715SXin Li   VERIFY_IS_APPROX(K1,K2);
99*bf2c3715SXin Li }
100*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(product_large)101*bf2c3715SXin Li EIGEN_DECLARE_TEST(product_large)
102*bf2c3715SXin Li {
103*bf2c3715SXin Li   for(int i = 0; i < g_repeat; i++) {
104*bf2c3715SXin Li     CALL_SUBTEST_1( product(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
105*bf2c3715SXin Li     CALL_SUBTEST_2( product(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
106*bf2c3715SXin Li     CALL_SUBTEST_2( product(MatrixXd(internal::random<int>(1,10), internal::random<int>(1,10))) );
107*bf2c3715SXin Li 
108*bf2c3715SXin Li     CALL_SUBTEST_3( product(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
109*bf2c3715SXin Li     CALL_SUBTEST_4( product(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
110*bf2c3715SXin Li     CALL_SUBTEST_5( product(Matrix<float,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
111*bf2c3715SXin Li 
112*bf2c3715SXin Li     CALL_SUBTEST_1( test_aliasing<float>() );
113*bf2c3715SXin Li 
114*bf2c3715SXin Li     CALL_SUBTEST_6( bug_1622<1>() );
115*bf2c3715SXin Li 
116*bf2c3715SXin Li     CALL_SUBTEST_7( product(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
117*bf2c3715SXin Li     CALL_SUBTEST_8( product(Matrix<double,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
118*bf2c3715SXin Li     CALL_SUBTEST_9( product(Matrix<std::complex<float>,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
119*bf2c3715SXin Li     CALL_SUBTEST_10( product(Matrix<std::complex<double>,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
120*bf2c3715SXin Li   }
121*bf2c3715SXin Li 
122*bf2c3715SXin Li   CALL_SUBTEST_6( product_large_regressions<0>() );
123*bf2c3715SXin Li 
124*bf2c3715SXin Li   // Regression test for bug 714:
125*bf2c3715SXin Li #if defined EIGEN_HAS_OPENMP
126*bf2c3715SXin Li   omp_set_dynamic(1);
127*bf2c3715SXin Li   for(int i = 0; i < g_repeat; i++) {
128*bf2c3715SXin Li     CALL_SUBTEST_6( product(Matrix<float,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
129*bf2c3715SXin Li   }
130*bf2c3715SXin Li #endif
131*bf2c3715SXin Li }
132