fn_accu.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_accu_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. REQUIRE( accu(A) == Approx( 0.240136) );
  29. REQUIRE( accu(abs(A)) == Approx( 7.845382) );
  30. REQUIRE( accu(A-A) == Approx( 0.0 ) );
  31. REQUIRE( accu(A+A) == Approx( 0.480272) );
  32. REQUIRE( accu(2*A) == Approx( 0.480272) );
  33. REQUIRE( accu( -A) == Approx(-0.240136) );
  34. REQUIRE( accu(2*A+3*A) == Approx( 1.200680) );
  35. REQUIRE( accu(fliplr(A)) == Approx( 0.240136) );
  36. REQUIRE( accu(flipud(A)) == Approx( 0.240136) );
  37. REQUIRE( accu(A.col(1)) == Approx( 0.212265) );
  38. REQUIRE( accu(A.row(1)) == Approx( 0.632961) );
  39. REQUIRE( accu(2*A.col(1)) == Approx( 0.424530) );
  40. REQUIRE( accu(2*A.row(1)) == Approx( 1.265922) );
  41. REQUIRE( accu(A%A) == Approx(2.834218657806) );
  42. REQUIRE( accu(A*A.t()) == Approx(1.218704694166) );
  43. REQUIRE( accu(A.t()*A) == Approx(2.585464740700) );
  44. REQUIRE( accu(vectorise(A)) == Approx(0.240136) );
  45. REQUIRE( accu( A(span(1,3), span(1,4)) ) == Approx(1.273017) );
  46. REQUIRE( accu( 2*A(span(1,3), span(1,4)) ) == Approx(2.546034) );
  47. REQUIRE_THROWS( accu(A*A) );
  48. }
  49. TEST_CASE("fn_accu_2")
  50. {
  51. mat A =
  52. "\
  53. 0.061198 0.201990 0.019678 -0.493936 -0.126745 0.051408;\
  54. 0.437242 0.058956 -0.149362 -0.045465 0.296153 0.035437;\
  55. -0.492474 -0.031309 0.314156 0.419733 0.068317 -0.454499;\
  56. 0.336352 0.411541 0.458476 -0.393139 -0.135040 0.373833;\
  57. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768 0.258704;\
  58. ";
  59. cx_mat C = cx_mat(A, 2*fliplr(A));
  60. cx_mat D = cx_mat(2*fliplr(A), A);
  61. REQUIRE( abs(accu(C) - cx_double(0.240136, +0.480272)) == Approx(0.0) );
  62. REQUIRE( abs(accu(cx_double(2,3)*C) - cx_double(-0.960544000000001, +1.680951999999999)) == Approx(0.0) );
  63. REQUIRE( abs(accu(C*D.t() ) - cx_double(-0.710872588088, +3.656114082498002)) == Approx(0.0) );
  64. REQUIRE( abs(accu(C*D.st()) - cx_double(0.0, +6.093523470830000)) == Approx(0.0) );
  65. REQUIRE( abs(accu(C.t() *D) - cx_double(10.341858962800, -7.756394222100000)) == Approx(0.0) );
  66. REQUIRE( abs(accu(C.st()*D) - cx_double(0.0, +1.29273237035e+01)) == Approx(0.0) );
  67. }
  68. TEST_CASE("fn_accu_3")
  69. {
  70. vec a = linspace<vec>(1,5,5);
  71. vec b = linspace<vec>(1,5,6);
  72. vec c = -linspace<vec>(1,5,6);
  73. REQUIRE(accu(a) == Approx( 15.0));
  74. REQUIRE(accu(b) == Approx( 18.0));
  75. REQUIRE(accu(c) == Approx(-18.0));
  76. }
  77. TEST_CASE("fn_accu_4")
  78. {
  79. mat A(5,6); A.fill(2.0);
  80. mat B(5,6); B.fill(4.0);
  81. mat C(6,5); C.fill(6.0);
  82. REQUIRE( accu(A + B) == Approx(double((2+4)*(A.n_rows*A.n_cols))) );
  83. REQUIRE( accu(A(span::all,span::all) + B(span::all,span::all)) == Approx(double((2+4)*(A.n_rows*A.n_cols))) );
  84. REQUIRE( accu(A % B) == Approx(double((2*4)*(A.n_rows*A.n_cols))) );
  85. REQUIRE( accu(A(span::all,span::all) % B(span::all,span::all)) == Approx(double((2*4)*(A.n_rows*A.n_cols))) );
  86. // A and C matrices are non-conformat, so accu() will throw unless ARMA_NO_DEBUG is defined
  87. REQUIRE_THROWS( accu(A % C) );
  88. REQUIRE_THROWS( accu(A(span::all,span::all) % C(span::all,span::all)) );
  89. }
  90. TEST_CASE("fn_accu_spmat")
  91. {
  92. SpMat<unsigned int> b(4, 4);
  93. b(0, 1) = 6;
  94. b(1, 3) = 15;
  95. b(3, 1) = 14;
  96. b(2, 0) = 5;
  97. b(3, 3) = 12;
  98. REQUIRE( accu(b) == 52 );
  99. REQUIRE( accu(b.submat(1, 1, 3, 3)) == 41 );
  100. }