fn_symmat.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2015 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2015 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. #include <armadillo>
  16. #include "catch.hpp"
  17. using namespace arma;
  18. TEST_CASE("fn_symmat_1")
  19. {
  20. mat A =
  21. "\
  22. 0.061198 0.201990 0.019678 -0.493936 -0.126745 0.051408;\
  23. 0.437242 0.058956 -0.149362 -0.045465 0.296153 0.035437;\
  24. -0.492474 -0.031309 0.314156 0.419733 0.068317 -0.454499;\
  25. 0.336352 0.411541 0.458476 -0.393139 -0.135040 0.373833;\
  26. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768 0.258704;\
  27. ";
  28. mat B = symmatu( A(0,0,size(5,5)) );
  29. mat C = symmatl( A(0,0,size(5,5)) );
  30. mat BB =
  31. "\
  32. 0.061198 0.201990 0.019678 -0.493936 -0.126745;\
  33. 0.201990 0.058956 -0.149362 -0.045465 0.296153;\
  34. 0.019678 -0.149362 0.314156 0.419733 0.068317;\
  35. -0.493936 -0.045465 0.419733 -0.393139 -0.135040;\
  36. -0.126745 0.296153 0.068317 -0.135040 -0.353768;\
  37. ";
  38. mat CC =
  39. "\
  40. 0.061198 0.437242 -0.492474 0.336352 0.239585;\
  41. 0.437242 0.058956 -0.031309 0.411541 -0.428913;\
  42. -0.492474 -0.031309 0.314156 0.458476 -0.406953;\
  43. 0.336352 0.411541 0.458476 -0.393139 -0.291020;\
  44. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768;\
  45. ";
  46. REQUIRE( accu(abs( B - BB )) == Approx(0.0) );
  47. REQUIRE( accu(abs( C - CC )) == Approx(0.0) );
  48. mat X;
  49. REQUIRE_THROWS( X = symmatu(A) ); // symmatu() and symmatl() currently handle only square matrices
  50. }
  51. TEST_CASE("fn_symmat_2")
  52. {
  53. mat A =
  54. "\
  55. 0.061198 0.201990 0.019678 -0.493936 -0.126745 0.051408;\
  56. 0.437242 0.058956 -0.149362 -0.045465 0.296153 0.035437;\
  57. -0.492474 -0.031309 0.314156 0.419733 0.068317 -0.454499;\
  58. 0.336352 0.411541 0.458476 -0.393139 -0.135040 0.373833;\
  59. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768 0.258704;\
  60. ";
  61. cx_mat B1 = symmatu( cx_mat( A(0,0,size(3,3)), A(0,3,size(3,3)) ) );
  62. cx_mat C1 = symmatl( cx_mat( A(0,0,size(3,3)), A(0,3,size(3,3)) ) );
  63. cx_mat B2 = symmatu( cx_mat( A(0,0,size(3,3)), A(0,3,size(3,3)) ), true );
  64. cx_mat C2 = symmatl( cx_mat( A(0,0,size(3,3)), A(0,3,size(3,3)) ), true );
  65. cx_mat D = symmatu( cx_mat( A(0,0,size(3,3)), A(0,3,size(3,3)) ), false );
  66. cx_mat E = symmatl( cx_mat( A(0,0,size(3,3)), A(0,3,size(3,3)) ), false );
  67. cx_mat BB =
  68. {
  69. { cx_double( 0.06120, -0.49394), cx_double( 0.20199, -0.12674), cx_double( 0.01968, +0.05141) },
  70. { cx_double( 0.20199, +0.12674), cx_double( 0.05896, +0.29615), cx_double(-0.14936, +0.03544) },
  71. { cx_double( 0.01968, -0.05141), cx_double(-0.14936, -0.03544), cx_double( 0.31416, -0.45450) }
  72. };
  73. cx_mat CC =
  74. {
  75. { cx_double( 0.06120, -0.49394), cx_double( 0.43724, +0.04546), cx_double(-0.49247, -0.41973) },
  76. { cx_double( 0.43724, -0.04546), cx_double( 0.05896, +0.29615), cx_double(-0.03131, -0.06832) },
  77. { cx_double(-0.49247, +0.41973), cx_double(-0.03131, +0.06832), cx_double( 0.31416, -0.45450) }
  78. };
  79. cx_mat DD =
  80. {
  81. { cx_double( 0.06120, -0.49394), cx_double( 0.20199, -0.12674), cx_double( 0.01968, +0.05141) },
  82. { cx_double( 0.20199, -0.12674), cx_double( 0.05896, +0.29615), cx_double(-0.14936, +0.03544) },
  83. { cx_double( 0.01968, +0.05141), cx_double(-0.14936, +0.03544), cx_double( 0.31416, -0.45450) }
  84. };
  85. cx_mat EE =
  86. {
  87. { cx_double( 0.06120, -0.49394), cx_double( 0.43724, -0.04546), cx_double(-0.49247, +0.41973) },
  88. { cx_double( 0.43724, -0.04546), cx_double( 0.05896, +0.29615), cx_double(-0.03131, +0.06832) },
  89. { cx_double(-0.49247, +0.41973), cx_double(-0.03131, +0.06832), cx_double( 0.31416, -0.45450) }
  90. };
  91. REQUIRE( accu(abs( B1 - BB )) == Approx(0.0).epsilon(0.0001) );
  92. REQUIRE( accu(abs( C1 - CC )) == Approx(0.0).epsilon(0.0001) );
  93. REQUIRE( accu(abs( B2 - BB )) == Approx(0.0).epsilon(0.0001) );
  94. REQUIRE( accu(abs( C2 - CC )) == Approx(0.0).epsilon(0.0001) );
  95. REQUIRE( accu(abs( D - DD )) == Approx(0.0).epsilon(0.0001) );
  96. REQUIRE( accu(abs( E - EE )) == Approx(0.0).epsilon(0.0001) );
  97. cx_mat X;
  98. REQUIRE_THROWS( X = symmatu( cx_mat(A(0,0,size(2,3)), A(0,3,size(2,3))) ) ); // symmatu() and symmatl() currently handle only square matrices
  99. }