gen_randu.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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("gen_randu_1")
  19. {
  20. const uword n_rows = 100;
  21. const uword n_cols = 101;
  22. mat A(n_rows,n_cols, fill::randu);
  23. mat B(n_rows,n_cols); B.randu();
  24. mat C; C.randu(n_rows,n_cols);
  25. REQUIRE( (accu(A)/A.n_elem) == Approx(0.5).epsilon(0.02) );
  26. REQUIRE( (accu(B)/A.n_elem) == Approx(0.5).epsilon(0.02) );
  27. REQUIRE( (accu(C)/A.n_elem) == Approx(0.5).epsilon(0.02) );
  28. REQUIRE( (mean(vectorise(A))) == Approx(0.5).epsilon(0.02) );
  29. }
  30. TEST_CASE("gen_randu_2")
  31. {
  32. mat A(50,60,fill::zeros);
  33. A(span(1,48),span(1,58)).randu();
  34. REQUIRE( accu(A.head_cols(1)) == Approx(0.0) );
  35. REQUIRE( accu(A.head_rows(1)) == Approx(0.0) );
  36. REQUIRE( accu(A.tail_cols(1)) == Approx(0.0) );
  37. REQUIRE( accu(A.tail_rows(1)) == Approx(0.0) );
  38. REQUIRE( mean(vectorise(A(span(1,48),span(1,58)))) == Approx(double(0.5)).epsilon(0.02) );
  39. }