fn_abs.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_abs_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 abs_A =
  29. "\
  30. 0.061198 0.201990 0.019678 0.493936 0.126745 0.051408;\
  31. 0.437242 0.058956 0.149362 0.045465 0.296153 0.035437;\
  32. 0.492474 0.031309 0.314156 0.419733 0.068317 0.454499;\
  33. 0.336352 0.411541 0.458476 0.393139 0.135040 0.373833;\
  34. 0.239585 0.428913 0.406953 0.291020 0.353768 0.258704;\
  35. ";
  36. mat X = abs(A);
  37. REQUIRE( X(0,0) == Approx(0.061198) );
  38. REQUIRE( X(1,0) == Approx(0.437242) );
  39. REQUIRE( X(2,0) == Approx(0.492474) );
  40. REQUIRE( X(3,0) == Approx(0.336352) );
  41. REQUIRE( X(4,0) == Approx(0.239585) );
  42. REQUIRE( X(0,1) == Approx(0.201990) );
  43. REQUIRE( X(1,1) == Approx(0.058956) );
  44. REQUIRE( X(2,1) == Approx(0.031309) );
  45. REQUIRE( X(3,1) == Approx(0.411541) );
  46. REQUIRE( X(4,1) == Approx(0.428913) );
  47. REQUIRE( X(0,5) == Approx(0.051408) );
  48. REQUIRE( X(1,5) == Approx(0.035437) );
  49. REQUIRE( X(2,5) == Approx(0.454499) );
  50. REQUIRE( X(3,5) == Approx(0.373833) );
  51. REQUIRE( X(4,5) == Approx(0.258704) );
  52. mat Y = abs(2*A) / 2;
  53. REQUIRE( Y(0,0) == Approx(0.061198) );
  54. REQUIRE( Y(1,0) == Approx(0.437242) );
  55. REQUIRE( Y(2,0) == Approx(0.492474) );
  56. REQUIRE( Y(3,0) == Approx(0.336352) );
  57. REQUIRE( Y(4,0) == Approx(0.239585) );
  58. REQUIRE( Y(0,1) == Approx(0.201990) );
  59. REQUIRE( Y(1,1) == Approx(0.058956) );
  60. REQUIRE( Y(2,1) == Approx(0.031309) );
  61. REQUIRE( Y(3,1) == Approx(0.411541) );
  62. REQUIRE( Y(4,1) == Approx(0.428913) );
  63. REQUIRE( Y(0,5) == Approx(0.051408) );
  64. REQUIRE( Y(1,5) == Approx(0.035437) );
  65. REQUIRE( Y(2,5) == Approx(0.454499) );
  66. REQUIRE( Y(3,5) == Approx(0.373833) );
  67. REQUIRE( Y(4,5) == Approx(0.258704) );
  68. REQUIRE( accu( abs(A) - abs_A ) == Approx(0.0) );
  69. REQUIRE( accu( 2*abs(A) - 2*abs_A ) == Approx(0.0) );
  70. REQUIRE( accu( abs(-A) - abs_A ) == Approx(0.0) );
  71. REQUIRE( accu( 2*abs(-A) - 2*abs_A ) == Approx(0.0) );
  72. // REQUIRE_THROWS( );
  73. }
  74. TEST_CASE("fn_abs_2")
  75. {
  76. mat A =
  77. "\
  78. 0.061198 0.201990 0.019678 -0.493936 -0.126745 0.051408;\
  79. 0.437242 0.058956 -0.149362 -0.045465 0.296153 0.035437;\
  80. -0.492474 -0.031309 0.314156 0.419733 0.068317 -0.454499;\
  81. 0.336352 0.411541 0.458476 -0.393139 -0.135040 0.373833;\
  82. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768 0.258704;\
  83. ";
  84. cx_mat C = cx_mat(A,fliplr(A));
  85. mat abs_C =
  86. "\
  87. 0.079925 0.238462 0.494328 0.494328 0.238462 0.079925;\
  88. 0.438676 0.301964 0.156128 0.156128 0.301964 0.438676;\
  89. 0.670149 0.075150 0.524280 0.524280 0.075150 0.670149;\
  90. 0.502876 0.433130 0.603952 0.603952 0.433130 0.502876;\
  91. 0.352603 0.555984 0.500303 0.500303 0.555984 0.352603;\
  92. ";
  93. mat X = abs(C);
  94. REQUIRE( X(0,0) == Approx(0.079925) );
  95. REQUIRE( X(1,0) == Approx(0.438676) );
  96. REQUIRE( X(2,0) == Approx(0.670149) );
  97. REQUIRE( X(3,0) == Approx(0.502876) );
  98. REQUIRE( X(4,0) == Approx(0.352603) );
  99. REQUIRE( X(0,1) == Approx(0.238462) );
  100. REQUIRE( X(1,1) == Approx(0.301964) );
  101. REQUIRE( X(2,1) == Approx(0.075150) );
  102. REQUIRE( X(3,1) == Approx(0.433130) );
  103. REQUIRE( X(4,1) == Approx(0.555984) );
  104. REQUIRE( X(0,5) == Approx(0.079925) );
  105. REQUIRE( X(1,5) == Approx(0.438676) );
  106. REQUIRE( X(2,5) == Approx(0.670149) );
  107. REQUIRE( X(3,5) == Approx(0.502876) );
  108. REQUIRE( X(4,5) == Approx(0.352603) );
  109. REQUIRE( accu( abs(C) - abs_C ) == Approx(0.0) );
  110. // REQUIRE_THROWS( );
  111. }
  112. TEST_CASE("fn_abs_3")
  113. {
  114. vec re = 2*linspace<vec>(1,5,6);
  115. vec im = -4*linspace<vec>(1,5,6);
  116. cx_vec a(re,im);
  117. vec b =
  118. {
  119. 4.47213595499958,
  120. 8.04984471899924,
  121. 11.62755348299891,
  122. 15.20526224699857,
  123. 18.78297101099824,
  124. 22.36067977499790
  125. };
  126. vec c = abs(a);
  127. REQUIRE( accu(c - b) == Approx(0.0) );
  128. REQUIRE( accu(abs(a) - b) == Approx(0.0) );
  129. }
  130. TEST_CASE("fn_abs_4")
  131. {
  132. vec a = -2*linspace<vec>(1,5,6);
  133. vec b = +2*linspace<vec>(1,5,6);
  134. REQUIRE( accu(abs(a) - b) == Approx(0.0) );
  135. REQUIRE( accu(abs(a(span::all)) - b(span::all)) == Approx(0.0) );
  136. }
  137. TEST_CASE("fn_abs_5")
  138. {
  139. mat A = randu<mat>(5,6);
  140. REQUIRE( accu(abs(-2*A) - (2*A)) == Approx(0.0) );
  141. REQUIRE( accu(abs(-2*A(span::all,span::all)) - (2*A(span::all,span::all))) == Approx(0.0) );
  142. }
  143. TEST_CASE("fn_abs_sp_mat")
  144. {
  145. SpMat<double> a(3, 3);
  146. a(0, 2) = 4.3;
  147. a(1, 1) = -5.5;
  148. a(2, 2) = -6.3;
  149. SpMat<double> b = abs(a);
  150. REQUIRE( (double) b(0, 0) == 0 );
  151. REQUIRE( (double) b(1, 0) == 0 );
  152. REQUIRE( (double) b(2, 0) == 0 );
  153. REQUIRE( (double) b(0, 1) == 0 );
  154. REQUIRE( (double) b(1, 1) == Approx(5.5) );
  155. REQUIRE( (double) b(2, 1) == 0 );
  156. REQUIRE( (double) b(0, 2) == Approx(4.3) );
  157. REQUIRE( (double) b(1, 2) == 0 );
  158. REQUIRE( (double) b(2, 2) == Approx(6.3) );
  159. // Just test that these compile and run.
  160. b = abs(a);
  161. b *= abs(a);
  162. b %= abs(a);
  163. b /= abs(a);
  164. }
  165. TEST_CASE("fn_abs_sp_mat_2")
  166. {
  167. mat x = randu<mat>(100, 100);
  168. x -= 0.5;
  169. SpMat<double> y(x);
  170. mat xr = abs(x);
  171. SpMat<double> yr = abs(y);
  172. for(size_t i = 0; i < xr.n_elem; ++i)
  173. {
  174. REQUIRE( xr[i] == Approx((double) yr[i]) );
  175. }
  176. }
  177. TEST_CASE("fn_abs_sp_cx_mat")
  178. {
  179. cx_mat x = randu<cx_mat>(100, 100);
  180. x -= cx_double(0.5, 0.5);
  181. sp_cx_mat y(x);
  182. mat xr = abs(x);
  183. SpMat<double> yr = abs(y);
  184. for(size_t i = 0; i < xr.n_elem; ++i)
  185. {
  186. REQUIRE( xr[i] == Approx((double) yr[i]) );
  187. }
  188. }