You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
5.5 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  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. #include "main.h"
  10. template<typename T>
  11. int get_random_size()
  12. {
  13. const int factor = NumTraits<T>::ReadCost;
  14. const int max_test_size = EIGEN_TEST_MAX_SIZE>2*factor ? EIGEN_TEST_MAX_SIZE/factor : EIGEN_TEST_MAX_SIZE;
  15. return internal::random<int>(1,max_test_size);
  16. }
  17. template<typename Scalar, int Mode, int TriOrder, int OtherOrder, int ResOrder, int OtherCols>
  18. void trmm(int rows=get_random_size<Scalar>(),
  19. int cols=get_random_size<Scalar>(),
  20. int otherCols = OtherCols==Dynamic?get_random_size<Scalar>():OtherCols)
  21. {
  22. typedef Matrix<Scalar,Dynamic,Dynamic,TriOrder> TriMatrix;
  23. typedef Matrix<Scalar,Dynamic,OtherCols,OtherCols==1?ColMajor:OtherOrder> OnTheRight;
  24. typedef Matrix<Scalar,OtherCols,Dynamic,OtherCols==1?RowMajor:OtherOrder> OnTheLeft;
  25. typedef Matrix<Scalar,Dynamic,OtherCols,OtherCols==1?ColMajor:ResOrder> ResXS;
  26. typedef Matrix<Scalar,OtherCols,Dynamic,OtherCols==1?RowMajor:ResOrder> ResSX;
  27. TriMatrix mat(rows,cols), tri(rows,cols), triTr(cols,rows);
  28. OnTheRight ge_right(cols,otherCols);
  29. OnTheLeft ge_left(otherCols,rows);
  30. ResSX ge_sx, ge_sx_save;
  31. ResXS ge_xs, ge_xs_save;
  32. Scalar s1 = internal::random<Scalar>(),
  33. s2 = internal::random<Scalar>();
  34. mat.setRandom();
  35. tri = mat.template triangularView<Mode>();
  36. triTr = mat.transpose().template triangularView<Mode>();
  37. ge_right.setRandom();
  38. ge_left.setRandom();
  39. VERIFY_IS_APPROX( ge_xs = mat.template triangularView<Mode>() * ge_right, tri * ge_right);
  40. VERIFY_IS_APPROX( ge_sx = ge_left * mat.template triangularView<Mode>(), ge_left * tri);
  41. VERIFY_IS_APPROX( ge_xs.noalias() = mat.template triangularView<Mode>() * ge_right, tri * ge_right);
  42. VERIFY_IS_APPROX( ge_sx.noalias() = ge_left * mat.template triangularView<Mode>(), ge_left * tri);
  43. VERIFY_IS_APPROX( ge_xs.noalias() = (s1*mat.adjoint()).template triangularView<Mode>() * (s2*ge_left.transpose()), s1*triTr.conjugate() * (s2*ge_left.transpose()));
  44. VERIFY_IS_APPROX( ge_sx.noalias() = ge_right.transpose() * mat.adjoint().template triangularView<Mode>(), ge_right.transpose() * triTr.conjugate());
  45. VERIFY_IS_APPROX( ge_xs.noalias() = (s1*mat.adjoint()).template triangularView<Mode>() * (s2*ge_left.adjoint()), s1*triTr.conjugate() * (s2*ge_left.adjoint()));
  46. VERIFY_IS_APPROX( ge_sx.noalias() = ge_right.adjoint() * mat.adjoint().template triangularView<Mode>(), ge_right.adjoint() * triTr.conjugate());
  47. ge_xs_save = ge_xs;
  48. VERIFY_IS_APPROX( (ge_xs_save + s1*triTr.conjugate() * (s2*ge_left.adjoint())).eval(), ge_xs.noalias() += (s1*mat.adjoint()).template triangularView<Mode>() * (s2*ge_left.adjoint()) );
  49. ge_sx.setRandom();
  50. ge_sx_save = ge_sx;
  51. VERIFY_IS_APPROX( ge_sx_save - (ge_right.adjoint() * (-s1 * triTr).conjugate()).eval(), ge_sx.noalias() -= (ge_right.adjoint() * (-s1 * mat).adjoint().template triangularView<Mode>()).eval());
  52. VERIFY_IS_APPROX( ge_xs = (s1*mat).adjoint().template triangularView<Mode>() * ge_left.adjoint(), numext::conj(s1) * triTr.conjugate() * ge_left.adjoint());
  53. // TODO check with sub-matrix expressions ?
  54. }
  55. template<typename Scalar, int Mode, int TriOrder>
  56. void trmv(int rows=get_random_size<Scalar>(), int cols=get_random_size<Scalar>())
  57. {
  58. trmm<Scalar,Mode,TriOrder,ColMajor,ColMajor,1>(rows,cols,1);
  59. }
  60. template<typename Scalar, int Mode, int TriOrder, int OtherOrder, int ResOrder>
  61. void trmm(int rows=get_random_size<Scalar>(), int cols=get_random_size<Scalar>(), int otherCols = get_random_size<Scalar>())
  62. {
  63. trmm<Scalar,Mode,TriOrder,OtherOrder,ResOrder,Dynamic>(rows,cols,otherCols);
  64. }
  65. #define CALL_ALL_ORDERS(NB,SCALAR,MODE) \
  66. EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, ColMajor,ColMajor,ColMajor>())); \
  67. EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, ColMajor,ColMajor,RowMajor>())); \
  68. EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, ColMajor,RowMajor,ColMajor>())); \
  69. EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, ColMajor,RowMajor,RowMajor>())); \
  70. EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, RowMajor,ColMajor,ColMajor>())); \
  71. EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, RowMajor,ColMajor,RowMajor>())); \
  72. EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, RowMajor,RowMajor,ColMajor>())); \
  73. EIGEN_CAT(CALL_SUBTEST_,NB)((trmm<SCALAR, MODE, RowMajor,RowMajor,RowMajor>())); \
  74. \
  75. EIGEN_CAT(CALL_SUBTEST_1,NB)((trmv<SCALAR, MODE, ColMajor>())); \
  76. EIGEN_CAT(CALL_SUBTEST_1,NB)((trmv<SCALAR, MODE, RowMajor>()));
  77. #define CALL_ALL(NB,SCALAR) \
  78. CALL_ALL_ORDERS(EIGEN_CAT(1,NB),SCALAR,Upper) \
  79. CALL_ALL_ORDERS(EIGEN_CAT(2,NB),SCALAR,UnitUpper) \
  80. CALL_ALL_ORDERS(EIGEN_CAT(3,NB),SCALAR,StrictlyUpper) \
  81. CALL_ALL_ORDERS(EIGEN_CAT(1,NB),SCALAR,Lower) \
  82. CALL_ALL_ORDERS(EIGEN_CAT(2,NB),SCALAR,UnitLower) \
  83. CALL_ALL_ORDERS(EIGEN_CAT(3,NB),SCALAR,StrictlyLower)
  84. void test_product_trmm()
  85. {
  86. for(int i = 0; i < g_repeat ; i++)
  87. {
  88. CALL_ALL(1,float); // EIGEN_SUFFIXES;11;111;21;121;31;131
  89. CALL_ALL(2,double); // EIGEN_SUFFIXES;12;112;22;122;32;132
  90. CALL_ALL(3,std::complex<float>); // EIGEN_SUFFIXES;13;113;23;123;33;133
  91. CALL_ALL(4,std::complex<double>); // EIGEN_SUFFIXES;14;114;24;124;34;134
  92. }
  93. }