mat_minus.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_minus_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_minus_B =
  30. "\
  31. 0.0097900 0.3287350 0.5136140 -0.5136140 -0.3287350 -0.0097900;\
  32. 0.4018050 -0.2371970 -0.1038970 0.1038970 0.2371970 -0.4018050;\
  33. -0.0379750 -0.0996260 -0.1055770 0.1055770 0.0996260 0.0379750;\
  34. -0.0374810 0.5465810 0.8516150 -0.8516150 -0.5465810 0.0374810;\
  35. -0.0191190 -0.0751450 -0.1159330 0.1159330 0.0751450 0.0191190;\
  36. ";
  37. mat neg_of_A_minus_B =
  38. "\
  39. -0.0097900 -0.3287350 -0.5136140 +0.5136140 +0.3287350 +0.0097900;\
  40. -0.4018050 +0.2371970 +0.1038970 -0.1038970 -0.2371970 +0.4018050;\
  41. +0.0379750 +0.0996260 +0.1055770 -0.1055770 -0.0996260 -0.0379750;\
  42. +0.0374810 -0.5465810 -0.8516150 +0.8516150 +0.5465810 -0.0374810;\
  43. +0.0191190 +0.0751450 +0.1159330 -0.1159330 -0.0751450 -0.0191190;\
  44. ";
  45. mat X = A - B;
  46. REQUIRE( X(0,0) == Approx( 0.0097900) );
  47. REQUIRE( X(1,0) == Approx( 0.4018050) );
  48. REQUIRE( X(2,0) == Approx(-0.0379750) );
  49. REQUIRE( X(3,0) == Approx(-0.0374810) );
  50. REQUIRE( X(4,0) == Approx(-0.0191190) );
  51. REQUIRE( X(0,1) == Approx( 0.3287350) );
  52. REQUIRE( X(1,1) == Approx(-0.2371970) );
  53. REQUIRE( X(2,1) == Approx(-0.0996260) );
  54. REQUIRE( X(3,1) == Approx( 0.5465810) );
  55. REQUIRE( X(4,1) == Approx(-0.0751450) );
  56. REQUIRE( X(0,5) == Approx(-0.0097900) );
  57. REQUIRE( X(1,5) == Approx(-0.4018050) );
  58. REQUIRE( X(2,5) == Approx( 0.0379750) );
  59. REQUIRE( X(3,5) == Approx( 0.0374810) );
  60. REQUIRE( X(4,5) == Approx( 0.0191190) );
  61. mat Y = (2*A - 2*B) / 2;
  62. REQUIRE( Y(0,0) == Approx( 0.0097900) );
  63. REQUIRE( Y(1,0) == Approx( 0.4018050) );
  64. REQUIRE( Y(2,0) == Approx(-0.0379750) );
  65. REQUIRE( Y(3,0) == Approx(-0.0374810) );
  66. REQUIRE( Y(4,0) == Approx(-0.0191190) );
  67. REQUIRE( Y(0,1) == Approx( 0.3287350) );
  68. REQUIRE( Y(1,1) == Approx(-0.2371970) );
  69. REQUIRE( Y(2,1) == Approx(-0.0996260) );
  70. REQUIRE( Y(3,1) == Approx( 0.5465810) );
  71. REQUIRE( Y(4,1) == Approx(-0.0751450) );
  72. REQUIRE( Y(0,5) == Approx(-0.0097900) );
  73. REQUIRE( Y(1,5) == Approx(-0.4018050) );
  74. REQUIRE( Y(2,5) == Approx( 0.0379750) );
  75. REQUIRE( Y(3,5) == Approx( 0.0374810) );
  76. REQUIRE( Y(4,5) == Approx( 0.0191190) );
  77. REQUIRE( accu(mat(A-B) + (neg_of_A_minus_B)) == Approx(0.0) );
  78. REQUIRE( accu( (A-B) + (neg_of_A_minus_B)) == Approx(0.0) );
  79. REQUIRE( accu(abs( 2*(A-B) + 2*neg_of_A_minus_B )) == Approx(0.0) );
  80. // REQUIRE_THROWS( );
  81. }