fn_diff.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_diff_1")
  19. {
  20. colvec a = square( linspace<colvec>(1,5,6) );
  21. rowvec b = square( linspace<rowvec>(1,5,5) );
  22. colvec a_diff_1 = { 2.2400, 3.5200, 4.8000, 6.0800, 7.3600 };
  23. colvec a_diff_2 = { 1.2800, 1.2800, 1.2800, 1.2800 };
  24. colvec a_diff_9;
  25. rowvec b_diff_1 = { 3, 5, 7, 9 };
  26. rowvec b_diff_2 = { 2, 2, 2 };
  27. rowvec b_diff_9;
  28. REQUIRE( accu(abs(diff(a,0) - a )) == Approx(0.0) );
  29. REQUIRE( accu(abs(diff(a ) - a_diff_1)) == Approx(0.0) );
  30. REQUIRE( accu(abs(diff(a,1) - a_diff_1)) == Approx(0.0) );
  31. REQUIRE( accu(abs(diff(a,2) - a_diff_2)) == Approx(0.0) );
  32. REQUIRE( accu(abs(diff(a,9) - a_diff_9)) == Approx(0.0) );
  33. REQUIRE( accu(abs(diff(b,0) - b )) == Approx(0.0) );
  34. REQUIRE( accu(abs(diff(b ) - b_diff_1)) == Approx(0.0) );
  35. REQUIRE( accu(abs(diff(b,1) - b_diff_1)) == Approx(0.0) );
  36. REQUIRE( accu(abs(diff(b,2) - b_diff_2)) == Approx(0.0) );
  37. REQUIRE( accu(abs(diff(b,9) - b_diff_9)) == Approx(0.0) );
  38. }
  39. TEST_CASE("fn_diff_2")
  40. {
  41. mat A =
  42. "\
  43. 0.061198 0.201990 0.019678 -0.493936 -0.126745;\
  44. 0.437242 0.058956 -0.149362 -0.045465 0.296153;\
  45. -0.492474 -0.031309 0.314156 0.419733 0.068317;\
  46. 0.336352 0.411541 0.458476 -0.393139 -0.135040;\
  47. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768;\
  48. ";
  49. mat A_diff1_0 =
  50. "\
  51. 0.376044 -0.143034 -0.169040 0.448471 0.422898;\
  52. -0.929716 -0.090265 0.463518 0.465198 -0.227836;\
  53. 0.828826 0.442850 0.144320 -0.812872 -0.203357;\
  54. -0.096767 -0.840454 -0.865429 0.102119 -0.218728;\
  55. ";
  56. mat A_diff2_0 =
  57. "\
  58. -1.305760 0.052769 0.632558 0.016727 -0.650734;\
  59. 1.758542 0.533115 -0.319198 -1.278070 0.024479;\
  60. -0.925593 -1.283304 -1.009749 0.914991 -0.015371;\
  61. ";
  62. mat A_diff3_0 =
  63. "\
  64. 3.064302 0.480346 -0.951756 -1.294797 0.675213;\
  65. -2.684135 -1.816419 -0.690551 2.193061 -0.039850;\
  66. ";
  67. mat A_diff1_1 =
  68. "\
  69. 0.140792 -0.182312 -0.513614 0.367191;\
  70. -0.378286 -0.208318 0.103897 0.341618;\
  71. 0.461165 0.345465 0.105577 -0.351416;\
  72. 0.075189 0.046935 -0.851615 0.258099;\
  73. -0.668498 0.021960 0.115933 -0.062748;\
  74. ";
  75. mat A_diff2_1 =
  76. "\
  77. -0.323104 -0.331302 0.880805;\
  78. 0.169968 0.312215 0.237721;\
  79. -0.115700 -0.239888 -0.456993;\
  80. -0.028254 -0.898550 1.109714;\
  81. 0.690458 0.093973 -0.178681;\
  82. ";
  83. mat A_diff3_1 =
  84. "\
  85. -0.0081980 1.2121070;\
  86. 0.1422470 -0.0744940;\
  87. -0.1241880 -0.2171050;\
  88. -0.8702960 2.0082640;\
  89. -0.5964850 -0.2726540;\
  90. ";
  91. REQUIRE( accu(abs(diff(A,0) - A )) == Approx(0.0) );
  92. REQUIRE( accu(abs(diff(A ) - A_diff1_0)) == Approx(0.0) );
  93. REQUIRE( accu(abs(diff(A,1) - A_diff1_0)) == Approx(0.0) );
  94. REQUIRE( accu(abs(diff(A,2) - A_diff2_0)) == Approx(0.0) );
  95. REQUIRE( accu(abs(diff(A,3) - A_diff3_0)) == Approx(0.0) );
  96. REQUIRE( accu(abs(diff(A,0,0) - A )) == Approx(0.0) );
  97. REQUIRE( accu(abs(diff(A,1,0) - A_diff1_0)) == Approx(0.0) );
  98. REQUIRE( accu(abs(diff(A,2,0) - A_diff2_0)) == Approx(0.0) );
  99. REQUIRE( accu(abs(diff(A,3,0) - A_diff3_0)) == Approx(0.0) );
  100. REQUIRE( accu(abs(diff(A,0,1) - A )) == Approx(0.0) );
  101. REQUIRE( accu(abs(diff(A,1,1) - A_diff1_1)) == Approx(0.0) );
  102. REQUIRE( accu(abs(diff(A,2,1) - A_diff2_1)) == Approx(0.0) );
  103. REQUIRE( accu(abs(diff(A,3,1) - A_diff3_1)) == Approx(0.0) );
  104. }