mat_plus.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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("mat_plus_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 = fliplr(A);
  29. mat A_plus_B =
  30. "\
  31. 0.112606 0.075245 -0.474258 -0.474258 0.075245 0.112606;\
  32. 0.472679 0.355109 -0.194827 -0.194827 0.355109 0.472679;\
  33. -0.946973 0.037008 0.733889 0.733889 0.037008 -0.946973;\
  34. 0.710185 0.276501 0.065337 0.065337 0.276501 0.710185;\
  35. 0.498289 -0.782681 -0.697973 -0.697973 -0.782681 0.498289;\
  36. ";
  37. mat X = A + B;
  38. REQUIRE( X(0,0) == Approx( 0.112606) );
  39. REQUIRE( X(1,0) == Approx( 0.472679) );
  40. REQUIRE( X(2,0) == Approx(-0.946973) );
  41. REQUIRE( X(3,0) == Approx( 0.710185) );
  42. REQUIRE( X(4,0) == Approx( 0.498289) );
  43. REQUIRE( X(0,1) == Approx( 0.075245) );
  44. REQUIRE( X(1,1) == Approx( 0.355109) );
  45. REQUIRE( X(2,1) == Approx( 0.037008) );
  46. REQUIRE( X(3,1) == Approx( 0.276501) );
  47. REQUIRE( X(4,1) == Approx(-0.782681) );
  48. REQUIRE( X(0,5) == Approx( 0.112606) );
  49. REQUIRE( X(1,5) == Approx( 0.472679) );
  50. REQUIRE( X(2,5) == Approx(-0.946973) );
  51. REQUIRE( X(3,5) == Approx( 0.710185) );
  52. REQUIRE( X(4,5) == Approx( 0.498289) );
  53. mat Y = (2*A + 2*B)/2;
  54. REQUIRE( Y(0,0) == Approx( 0.112606) );
  55. REQUIRE( Y(1,0) == Approx( 0.472679) );
  56. REQUIRE( Y(2,0) == Approx(-0.946973) );
  57. REQUIRE( Y(3,0) == Approx( 0.710185) );
  58. REQUIRE( Y(4,0) == Approx( 0.498289) );
  59. REQUIRE( Y(0,1) == Approx( 0.075245) );
  60. REQUIRE( Y(1,1) == Approx( 0.355109) );
  61. REQUIRE( Y(2,1) == Approx( 0.037008) );
  62. REQUIRE( Y(3,1) == Approx( 0.276501) );
  63. REQUIRE( Y(4,1) == Approx(-0.782681) );
  64. REQUIRE( Y(0,5) == Approx( 0.112606) );
  65. REQUIRE( Y(1,5) == Approx( 0.472679) );
  66. REQUIRE( Y(2,5) == Approx(-0.946973) );
  67. REQUIRE( Y(3,5) == Approx( 0.710185) );
  68. REQUIRE( Y(4,5) == Approx( 0.498289) );
  69. REQUIRE( accu(abs( mat(A+B) - A_plus_B )) == Approx(0.0) );
  70. REQUIRE( accu(abs( (A+B) - A_plus_B )) == Approx(0.0) );
  71. REQUIRE( accu(abs( 2*(A+B) - 2*A_plus_B )) == Approx(0.0) );
  72. // REQUIRE_THROWS( );
  73. }
  74. TEST_CASE("mat_plus_2")
  75. {
  76. mat A(5,6); A.fill(1.0);
  77. mat B(5,6); B.fill(2.0);
  78. mat C(5,6); C.fill(3.0);
  79. REQUIRE( accu(A + B) == Approx(double(5*6*3)) );
  80. REQUIRE( accu(A + B + C) == Approx(double(5*6*6)) );
  81. REQUIRE( accu(A + B/2 + C) == Approx(double(5*6*5)) );
  82. mat X(6,5);
  83. REQUIRE_THROWS( A+X ); // adding non-conformant matrices will throw unless ARMA_NO_DEBUG is defined
  84. }