matrix.t.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // matrix_tc.h
  3. // math
  4. //
  5. // Created by Sam Jaffe on 8/18/16.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "matrix.hpp"
  10. #include "matrix_helpers.hpp"
  11. #include "angle.hpp"
  12. class matrix_TestSuite : public CxxTest::TestSuite {
  13. public:
  14. using matr2i = math::matrix::matrix<int, 2, 2>;
  15. using matr2 = math::matrix::matrix<double, 2, 2>;
  16. public:
  17. void test_matrix_equals() const {
  18. using math::matrix::identity;
  19. TS_ASSERT_EQUALS((identity<int, 2>()), (matr2i{{1,0},{0,1}}));
  20. }
  21. void test_matrix_sum() const {
  22. auto iden = math::matrix::identity<int, 2>();
  23. auto result = matr2i{{2,0},{0,2}};
  24. TS_ASSERT_EQUALS(iden + iden, result);
  25. }
  26. void test_matrix_default_zero() const {
  27. auto zero = matr2i{{0,0},{0,0}};
  28. TS_ASSERT_EQUALS(matr2i{}, zero);
  29. }
  30. void test_matrix_subtract() const {
  31. auto zero = matr2i{{0,0},{0,0}};
  32. auto iden = math::matrix::identity<int, 2>();
  33. TS_ASSERT_EQUALS(iden-iden, zero);
  34. }
  35. // void test_matrix_negate() const {
  36. // auto iden = math::matrix::identity<int, 2>();
  37. // TS_ASSERT_EQUALS(-iden, (matr2i{{-1,0},{0,-1}}));
  38. // }
  39. void test_matrix_scaling() const {
  40. auto iden = math::matrix::identity<double, 2>();
  41. TS_ASSERT_EQUALS(2*iden, (matr2{{2.0,0.0},{0.0,2.0}}));
  42. TS_ASSERT_EQUALS(iden*2, (matr2{{2.0,0.0},{0.0,2.0}}));
  43. TS_ASSERT_EQUALS(iden/2.0, (matr2{{0.5,0.0},{0.0,0.5}}));
  44. }
  45. void test_matrix_multiplication_same_dim() const {
  46. auto A = matr2i{{1,2},{2,3}};
  47. auto B = matr2i{{1,0},{1,1}};
  48. TS_ASSERT_EQUALS(A*B, (matr2i{{3,2},{5,3}}));
  49. TS_ASSERT_EQUALS(B*A, (matr2i{{1,2},{3,5}}));
  50. }
  51. void test_matrix_multiplication_diff_dim() const {
  52. auto A = math::matrix::matrix<int, 3, 2>{{1,0},{0,1},{1,1}};
  53. auto B = math::matrix::matrix<int, 2, 3>{{0,1,0},{1,0,1}};
  54. TS_ASSERT_EQUALS(A*B, (math::matrix::matrix<int, 3, 3>{{0,1,0},{1,0,1},{1,1,1}}));
  55. TS_ASSERT_EQUALS(B*A, (math::matrix::matrix<int, 2, 2>{{0,1},{2,1}}));
  56. }
  57. void test_matrix_vector_multiplication() const {
  58. auto A = matr2i{{1,0},{1,2}};
  59. auto x = math::vector::vector<int, 2>{1,2};
  60. TS_ASSERT_EQUALS(A*x, (math::vector::vector<int, 2>{1,5}));
  61. }
  62. void test_matrix_composition() const {
  63. using namespace math::matrix;
  64. using vec4 = math::vector::vector<double, 4>;
  65. using vec3 = math::vector::vector<double, 3>;
  66. auto rot = rotation<4>(math::degree{90}, rotate::X_AXIS);
  67. auto mov = translation(vec3{2.0, 2.5, 1.5});
  68. auto scl = scalar(vec3{2.0, math::vector::fill});
  69. vec4 epsilon{0.00001, math::vector::fill};
  70. TS_ASSERT_DELTA((mov * scl * rot * vec4{1,2,3,1}),
  71. (vec4{4.0,-1.5,-4.5,1.0}),
  72. epsilon);
  73. }
  74. void test_matrix_from_vector() const {
  75. using vec3 = math::vector::vector<double, 3>;
  76. vec3 v = vec3{1,2,3};
  77. math::matrix::matrix<double, 3, 1> m{v};
  78. TS_ASSERT_EQUALS(m(0,0), v[0]);
  79. TS_ASSERT_EQUALS(m(1,0), v[1]);
  80. TS_ASSERT_EQUALS(m(2,0), v[2]);
  81. }
  82. void test_matrix_init_list_except() const {
  83. TS_ASSERT_THROWS((math::matrix::matrix<double, 2, 1>{{1.0}}), std::logic_error);
  84. }
  85. void test_assign_row() const {
  86. matr2i A = math::matrix::identity<int, 2>();
  87. matr2i const B = 2 * A;
  88. A[0] = B[0];
  89. TS_ASSERT_EQUALS(A, (matr2i{{2,0},{0,1}}));
  90. }
  91. private:
  92. };