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.

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