fn_cor.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_cor_1")
  19. {
  20. vec a = linspace<vec>(1,5,6);
  21. vec b = 0.5*linspace<vec>(1,5,6);
  22. vec c = flipud(b);
  23. REQUIRE( as_scalar(cor(a,b) - (+1.0)) == Approx(0.0) );
  24. REQUIRE( as_scalar(cor(a,c) - (-1.0)) == Approx(0.0) );
  25. }
  26. TEST_CASE("fn_cor_2")
  27. {
  28. mat A =
  29. {
  30. { -0.78838, 0.69298, 0.41084, 0.90142 },
  31. { 0.49345, -0.12020, 0.78987, 0.53124 },
  32. { 0.73573, 0.52104, -0.22263, 0.40163 }
  33. };
  34. mat B = 0.5 * A;
  35. mat C = fliplr(B);
  36. mat AA =
  37. "\
  38. 1.00000 -0.54561 -0.28838 -0.99459;\
  39. -0.54561 1.00000 -0.64509 0.45559;\
  40. -0.28838 -0.64509 1.00000 0.38630;\
  41. -0.99459 0.45559 0.38630 1.00000;\
  42. ";
  43. mat AB =
  44. "\
  45. 1.00000 -0.54561 -0.28838 -0.99459;\
  46. -0.54561 1.00000 -0.64509 0.45559;\
  47. -0.28838 -0.64509 1.00000 0.38630;\
  48. -0.99459 0.45559 0.38630 1.00000;\
  49. ";
  50. mat AC =
  51. "\
  52. -0.99459 -0.28838 -0.54561 1.00000;\
  53. 0.45559 -0.64509 1.00000 -0.54561;\
  54. 0.38630 1.00000 -0.64509 -0.28838;\
  55. 1.00000 0.38630 0.45559 -0.99459;\
  56. ";
  57. REQUIRE( accu(abs(cor(A) - AA)) == Approx(0.0).epsilon(0.0001) );
  58. REQUIRE( accu(abs(cor(A,B) - AA)) == Approx(0.0).epsilon(0.0001) );
  59. REQUIRE( accu(abs(cor(A,C) - AC)) == Approx(0.0).epsilon(0.0001) );
  60. }