fn_cumsum.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_cumsum_1")
  19. {
  20. colvec a = linspace<colvec>(1,5,6);
  21. rowvec b = linspace<rowvec>(1,5,6);
  22. colvec c = { 1.0000, 2.8000, 5.4000, 8.8000, 13.0000, 18.0000 };
  23. REQUIRE( accu(abs(cumsum(a) - c )) == Approx(0.0) );
  24. REQUIRE( accu(abs(cumsum(b) - c.t())) == Approx(0.0) );
  25. REQUIRE_THROWS( b = cumsum(a) );
  26. }
  27. TEST_CASE("fn_cumsum_2")
  28. {
  29. mat A =
  30. {
  31. { -0.78838, 0.69298, 0.41084, 0.90142 },
  32. { 0.49345, -0.12020, 0.78987, 0.53124 },
  33. { 0.73573, 0.52104, -0.22263, 0.40163 }
  34. };
  35. mat B =
  36. {
  37. { -0.78838, 0.69298, 0.41084, 0.90142 },
  38. { -0.29493, 0.57278, 1.20071, 1.43266 },
  39. { 0.44080, 1.09382, 0.97808, 1.83429 }
  40. };
  41. mat C =
  42. {
  43. {-0.78838, -0.09540, 0.31544, 1.21686 },
  44. { 0.49345, 0.37325, 1.16312, 1.69436 },
  45. { 0.73573, 1.25677, 1.03414, 1.43577 }
  46. };
  47. REQUIRE( accu(abs(cumsum(A) - B)) == Approx(0.0) );
  48. REQUIRE( accu(abs(cumsum(A,0) - B)) == Approx(0.0) );
  49. REQUIRE( accu(abs(cumsum(A,1) - C)) == Approx(0.0) );
  50. }