fn_cumprod.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_cumprod_1")
  19. {
  20. colvec a = linspace<colvec>(1,5,6);
  21. rowvec b = linspace<rowvec>(1,5,6);
  22. colvec c = { 1.0000, 1.8000, 4.6800, 15.9120, 66.8304, 334.1520 };
  23. REQUIRE( accu(abs(cumprod(a) - c )) == Approx(0.0) );
  24. REQUIRE( accu(abs(cumprod(b) - c.t())) == Approx(0.0) );
  25. REQUIRE_THROWS( b = cumprod(a) );
  26. }
  27. TEST_CASE("fn_cumprod_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.788380, 0.692980, 0.410840, 0.901420 },
  38. { -0.389026, -0.083296, 0.324510, 0.478870 },
  39. { -0.286218, -0.043401, -0.072246, 0.192329 }
  40. };
  41. mat C =
  42. {
  43. { -0.788380, -0.546332, -0.224455, -0.202328 },
  44. { 0.493450, -0.059313, -0.046849, -0.024888 },
  45. { 0.735730, 0.383345, -0.085344, -0.034277 }
  46. };
  47. REQUIRE( accu(abs(cumprod(A) - B)) == Approx(0.0) );
  48. REQUIRE( accu(abs(cumprod(A,0) - B)) == Approx(0.0) );
  49. REQUIRE( accu(abs(cumprod(A,1) - C)) == Approx(0.0) );
  50. }