commainitializer.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 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<int M1, int M2, int N1, int N2>
  11. void test_blocks()
  12. {
  13. Matrix<int, M1+M2, N1+N2> m_fixed;
  14. MatrixXi m_dynamic(M1+M2, N1+N2);
  15. Matrix<int, M1, N1> mat11; mat11.setRandom();
  16. Matrix<int, M1, N2> mat12; mat12.setRandom();
  17. Matrix<int, M2, N1> mat21; mat21.setRandom();
  18. Matrix<int, M2, N2> mat22; mat22.setRandom();
  19. MatrixXi matx11 = mat11, matx12 = mat12, matx21 = mat21, matx22 = mat22;
  20. {
  21. VERIFY_IS_EQUAL((m_fixed << mat11, mat12, mat21, matx22).finished(), (m_dynamic << mat11, matx12, mat21, matx22).finished());
  22. VERIFY_IS_EQUAL((m_fixed.template topLeftCorner<M1,N1>()), mat11);
  23. VERIFY_IS_EQUAL((m_fixed.template topRightCorner<M1,N2>()), mat12);
  24. VERIFY_IS_EQUAL((m_fixed.template bottomLeftCorner<M2,N1>()), mat21);
  25. VERIFY_IS_EQUAL((m_fixed.template bottomRightCorner<M2,N2>()), mat22);
  26. VERIFY_IS_EQUAL((m_fixed << mat12, mat11, matx21, mat22).finished(), (m_dynamic << mat12, matx11, matx21, mat22).finished());
  27. }
  28. if(N1 > 0)
  29. {
  30. VERIFY_RAISES_ASSERT((m_fixed << mat11, mat12, mat11, mat21, mat22));
  31. VERIFY_RAISES_ASSERT((m_fixed << mat11, mat12, mat21, mat21, mat22));
  32. }
  33. else
  34. {
  35. // allow insertion of zero-column blocks:
  36. VERIFY_IS_EQUAL((m_fixed << mat11, mat12, mat11, mat11, mat21, mat21, mat22).finished(), (m_dynamic << mat12, mat22).finished());
  37. }
  38. if(M1 != M2)
  39. {
  40. VERIFY_RAISES_ASSERT((m_fixed << mat11, mat21, mat12, mat22));
  41. }
  42. }
  43. template<int N>
  44. struct test_block_recursion
  45. {
  46. static void run()
  47. {
  48. test_blocks<(N>>6)&3, (N>>4)&3, (N>>2)&3, N & 3>();
  49. test_block_recursion<N-1>::run();
  50. }
  51. };
  52. template<>
  53. struct test_block_recursion<-1>
  54. {
  55. static void run() { }
  56. };
  57. void test_commainitializer()
  58. {
  59. Matrix3d m3;
  60. Matrix4d m4;
  61. VERIFY_RAISES_ASSERT( (m3 << 1, 2, 3, 4, 5, 6, 7, 8) );
  62. #ifndef _MSC_VER
  63. VERIFY_RAISES_ASSERT( (m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) );
  64. #endif
  65. double data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  66. Matrix3d ref = Map<Matrix<double,3,3,RowMajor> >(data);
  67. m3 = Matrix3d::Random();
  68. m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
  69. VERIFY_IS_APPROX(m3, ref );
  70. Vector3d vec[3];
  71. vec[0] << 1, 4, 7;
  72. vec[1] << 2, 5, 8;
  73. vec[2] << 3, 6, 9;
  74. m3 = Matrix3d::Random();
  75. m3 << vec[0], vec[1], vec[2];
  76. VERIFY_IS_APPROX(m3, ref);
  77. vec[0] << 1, 2, 3;
  78. vec[1] << 4, 5, 6;
  79. vec[2] << 7, 8, 9;
  80. m3 = Matrix3d::Random();
  81. m3 << vec[0].transpose(),
  82. 4, 5, 6,
  83. vec[2].transpose();
  84. VERIFY_IS_APPROX(m3, ref);
  85. // recursively test all block-sizes from 0 to 3:
  86. test_block_recursion<(1<<8) - 1>();
  87. }