fn_cov.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_cov_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(cov(a,b) - (+1.12)) == Approx(0.0) );
  24. REQUIRE( as_scalar(cov(a,c) - (-1.12)) == Approx(0.0) );
  25. }
  26. TEST_CASE("fn_cov_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. 0.670783 -0.191509 -0.120822 -0.211274;\
  39. -0.191509 0.183669 -0.141426 0.050641;\
  40. -0.120822 -0.141426 0.261684 0.051254;\
  41. -0.211274 0.050641 0.051254 0.067270;\
  42. ";
  43. mat AB =
  44. "\
  45. 0.335392 -0.095755 -0.060411 -0.105637;\
  46. -0.095755 0.091834 -0.070713 0.025320;\
  47. -0.060411 -0.070713 0.130842 0.025627;\
  48. -0.105637 0.025320 0.025627 0.033635;\
  49. ";
  50. mat AC =
  51. "\
  52. -0.105637 -0.060411 -0.095755 0.335392;\
  53. 0.025320 -0.070713 0.091834 -0.095755;\
  54. 0.025627 0.130842 -0.070713 -0.060411;\
  55. 0.033635 0.025627 0.025320 -0.105637;\
  56. ";
  57. REQUIRE( accu(abs(cov(A) - AA)) == Approx(0.0) );
  58. REQUIRE( accu(abs(cov(A,B) - AB)) == Approx(0.0) );
  59. REQUIRE( accu(abs(cov(A,C) - AC)) == Approx(0.0) );
  60. }