1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com> 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 #include "product.h" 11 12 template<typename T> 13 void test_aliasing() 14 { 15 int rows = internal::random<int>(1,12); 16 int cols = internal::random<int>(1,12); 17 typedef Matrix<T,Dynamic,Dynamic> MatrixType; 18 typedef Matrix<T,Dynamic,1> VectorType; 19 VectorType x(cols); x.setRandom(); 20 VectorType z(x); 21 VectorType y(rows); y.setZero(); 22 MatrixType A(rows,cols); A.setRandom(); 23 // CwiseBinaryOp 24 VERIFY_IS_APPROX(x = y + A*x, A*z); // OK because "y + A*x" is marked as "assume-aliasing" 25 x = z; 26 // CwiseUnaryOp 27 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 28 x = z; 29 // VERIFY_IS_APPROX(x = y-A*x, -A*z); // Not OK in 3.3 because x is resized before A*x gets evaluated 30 x = z; 31 } 32 33 void test_product_large() 34 { 35 for(int i = 0; i < g_repeat; i++) { 36 CALL_SUBTEST_1( product(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 37 CALL_SUBTEST_2( product(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 38 CALL_SUBTEST_3( product(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 39 CALL_SUBTEST_4( product(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) ); 40 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))) ); 41 42 CALL_SUBTEST_1( test_aliasing<float>() ); 43 } 44 45 #if defined EIGEN_TEST_PART_6 46 { 47 // test a specific issue in DiagonalProduct 48 int N = 1000000; 49 VectorXf v = VectorXf::Ones(N); 50 MatrixXf m = MatrixXf::Ones(N,3); 51 m = (v+v).asDiagonal() * m; 52 VERIFY_IS_APPROX(m, MatrixXf::Constant(N,3,2)); 53 } 54 55 { 56 // test deferred resizing in Matrix::operator= 57 MatrixXf a = MatrixXf::Random(10,4), b = MatrixXf::Random(4,10), c = a; 58 VERIFY_IS_APPROX((a = a * b), (c * b).eval()); 59 } 60 61 { 62 // check the functions to setup blocking sizes compile and do not segfault 63 // FIXME check they do what they are supposed to do !! 64 std::ptrdiff_t l1 = internal::random<int>(10000,20000); 65 std::ptrdiff_t l2 = internal::random<int>(100000,200000); 66 std::ptrdiff_t l3 = internal::random<int>(1000000,2000000); 67 setCpuCacheSizes(l1,l2,l3); 68 VERIFY(l1==l1CacheSize()); 69 VERIFY(l2==l2CacheSize()); 70 std::ptrdiff_t k1 = internal::random<int>(10,100)*16; 71 std::ptrdiff_t m1 = internal::random<int>(10,100)*16; 72 std::ptrdiff_t n1 = internal::random<int>(10,100)*16; 73 // only makes sure it compiles fine 74 internal::computeProductBlockingSizes<float,float,std::ptrdiff_t>(k1,m1,n1,1); 75 } 76 77 { 78 // test regression in row-vector by matrix (bad Map type) 79 MatrixXf mat1(10,32); mat1.setRandom(); 80 MatrixXf mat2(32,32); mat2.setRandom(); 81 MatrixXf r1 = mat1.row(2)*mat2.transpose(); 82 VERIFY_IS_APPROX(r1, (mat1.row(2)*mat2.transpose()).eval()); 83 84 MatrixXf r2 = mat1.row(2)*mat2; 85 VERIFY_IS_APPROX(r2, (mat1.row(2)*mat2).eval()); 86 } 87 88 { 89 Eigen::MatrixXd A(10,10), B, C; 90 A.setRandom(); 91 C = A; 92 for(int k=0; k<79; ++k) 93 C = C * A; 94 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))) 95 * (((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))); 96 VERIFY_IS_APPROX(B,C); 97 } 98 #endif 99 100 // Regression test for bug 714: 101 #if defined EIGEN_HAS_OPENMP 102 omp_set_dynamic(1); 103 for(int i = 0; i < g_repeat; i++) { 104 CALL_SUBTEST_6( product(Matrix<float,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 105 } 106 #endif 107 } 108