expr_elem.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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("expr_elem_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 A_plus_2 =
  29. "\
  30. 2.061198 2.201990 2.019678 1.506064 1.873255 2.051408;\
  31. 2.437242 2.058956 1.850638 1.954535 2.296153 2.035437;\
  32. 1.507526 1.968691 2.314156 2.419733 2.068317 1.545501;\
  33. 2.336352 2.411541 2.458476 1.606861 1.864960 2.373833;\
  34. 2.239585 1.571087 1.593047 1.708980 1.646232 2.258704;\
  35. ";
  36. mat A_minus_2 =
  37. "\
  38. -1.938802 -1.798010 -1.980322 -2.493936 -2.126745 -1.948592;\
  39. -1.562758 -1.941044 -2.149362 -2.045465 -1.703847 -1.964563;\
  40. -2.492474 -2.031309 -1.685844 -1.580267 -1.931683 -2.454499;\
  41. -1.663648 -1.588459 -1.541524 -2.393139 -2.135040 -1.626167;\
  42. -1.760415 -2.428913 -2.406953 -2.291020 -2.353768 -1.741296;\
  43. ";
  44. mat two_minus_A =
  45. "\
  46. 1.938802 1.798010 1.980322 2.493936 2.126745 1.948592;\
  47. 1.562758 1.941044 2.149362 2.045465 1.703847 1.964563;\
  48. 2.492474 2.031309 1.685844 1.580267 1.931683 2.454499;\
  49. 1.663648 1.588459 1.541524 2.393139 2.135040 1.626167;\
  50. 1.760415 2.428913 2.406953 2.291020 2.353768 1.741296;\
  51. ";
  52. mat A_times_2 =
  53. "\
  54. 0.122396 0.403980 0.039356 -0.987872 -0.253490 0.102816;\
  55. 0.874484 0.117912 -0.298724 -0.090930 0.592306 0.070874;\
  56. -0.984948 -0.062618 0.628312 0.839466 0.136634 -0.908998;\
  57. 0.672704 0.823082 0.916952 -0.786278 -0.270080 0.747666;\
  58. 0.479170 -0.857826 -0.813906 -0.582040 -0.707536 0.517408;\
  59. ";
  60. mat A_elem_mul_A =
  61. "\
  62. 3.74519520400000e-03 4.07999601000000e-02 3.87223684000000e-04 2.43972772096000e-01 1.60642950250000e-02 2.64278246400000e-03;\
  63. 1.91180566564000e-01 3.47580993600000e-03 2.23090070440000e-02 2.06706622500000e-03 8.77065994090000e-02 1.25578096900000e-03;\
  64. 2.42530640676000e-01 9.80253481000000e-04 9.86939923360000e-02 1.76175791289000e-01 4.66721248900000e-03 2.06569341001000e-01;\
  65. 1.13132667904000e-01 1.69365994681000e-01 2.10200242576000e-01 1.54558273321000e-01 1.82358016000000e-02 1.39751111889000e-01;\
  66. 5.74009722250000e-02 1.83966361569000e-01 1.65610744209000e-01 8.46926404000000e-02 1.25151797824000e-01 6.69277596160000e-02;\
  67. ";
  68. mat A_div_2 =
  69. "\
  70. 0.03059900000000000 0.10099500000000000 0.00983900000000000 -0.24696799999999999 -0.06337250000000000 0.02570400000000000;\
  71. 0.21862100000000001 0.02947800000000000 -0.07468100000000000 -0.02273250000000000 0.14807650000000000 0.01771850000000000;\
  72. -0.24623700000000001 -0.01565450000000000 0.15707800000000000 0.20986650000000001 0.03415850000000000 -0.22724949999999999;\
  73. 0.16817599999999999 0.20577050000000000 0.22923800000000000 -0.19656950000000001 -0.06752000000000000 0.18691650000000001;\
  74. 0.11979250000000000 -0.21445649999999999 -0.20347650000000000 -0.14551000000000000 -0.17688400000000001 0.12935199999999999;\
  75. ";
  76. mat two_div_A =
  77. "\
  78. 32.68080656230595 9.90148027130056 101.63634515702815 -4.04910757669010 -15.77971517614107 38.90445066915655;\
  79. 4.57412599887476 33.92360404369360 -13.39028668603795 -43.98988232706478 6.75326604829261 56.43818607669949;\
  80. -4.06112810016366 -63.87939570091667 6.36626389437095 4.76493389845449 29.27529019131402 -4.40044972596199;\
  81. 5.94615165065170 4.85978310787990 4.36227850530889 -5.08725921366234 -14.81042654028436 5.34998247880738;\
  82. 8.34776801552685 -4.66295029528133 -4.91457244448376 -6.87237990516116 -5.65342258203116 7.73084297111757;\
  83. ";
  84. REQUIRE( accu(abs(( A - A ))) == Approx(0.0) );
  85. REQUIRE( accu(abs((2*A - 2*A ))) == Approx(0.0) );
  86. REQUIRE( accu(abs((2*A - (A+A)))) == Approx(0.0) );
  87. REQUIRE( accu(abs((A+0) - A )) == Approx(0.0) );
  88. REQUIRE( accu(abs((A-0) - A )) == Approx(0.0) );
  89. REQUIRE( accu(abs((A*1) - A )) == Approx(0.0) );
  90. REQUIRE( accu(abs((A/1) - A )) == Approx(0.0) );
  91. REQUIRE( accu(abs((A+2) - A_plus_2)) == Approx(0.0) );
  92. REQUIRE( accu(abs((2+A) - A_plus_2)) == Approx(0.0) );
  93. REQUIRE( accu(abs((A*2) - A_times_2)) == Approx(0.0) );
  94. REQUIRE( accu(abs((2*A) - A_times_2)) == Approx(0.0) );
  95. REQUIRE( accu(abs((A+A) - A_times_2)) == Approx(0.0) );
  96. REQUIRE( accu(abs((A+A+A - A) - A_times_2)) == Approx(0.0) );
  97. REQUIRE( accu(abs((3*A - A) - A_times_2)) == Approx(0.0) );
  98. REQUIRE( accu(abs((A/2 ) - A_div_2)) == Approx(0.0) );
  99. REQUIRE( accu(abs((A*0.5) - A_div_2)) == Approx(0.0) );
  100. REQUIRE( accu(abs((2/A ) - two_div_A)) == Approx(0.0) );
  101. REQUIRE( accu(abs( (A % A) - A_elem_mul_A )) == Approx(0.0) );
  102. REQUIRE( accu(abs( (A % A) - square(A) )) == Approx(0.0) );
  103. REQUIRE( accu(abs(( (2/A) % (A/2) ) - ones(size(A)))) == Approx(0.0) );
  104. REQUIRE( accu(abs(( A - A(span::all,span::all) ))) == Approx(0.0) );
  105. REQUIRE( accu(abs((A(span::all,span::all)+2) - A_plus_2)) == Approx(0.0) );
  106. REQUIRE( accu(abs((2+A(span::all,span::all)) - A_plus_2)) == Approx(0.0) );
  107. REQUIRE( accu(abs(((A+A+A) / (3*A)) - ones(size(A)))) == Approx(0.0) );
  108. REQUIRE_THROWS( A + randu<mat>(A.n_rows+1, A.n_cols ) );
  109. REQUIRE_THROWS( A + randu<mat>(A.n_rows , A.n_cols+1) );
  110. }