fn_dot.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_dot_1")
  19. {
  20. mat A =
  21. "\
  22. 0.061198 0.201990 0.019678 -0.493936 -0.126745;\
  23. 0.437242 0.058956 -0.149362 -0.045465 0.296153;\
  24. -0.492474 -0.031309 0.314156 0.419733 0.068317;\
  25. 0.336352 0.411541 0.458476 -0.393139 -0.135040;\
  26. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768;\
  27. ";
  28. vec a = A.head_cols(1);
  29. vec b = A.tail_cols(1);
  30. rowvec c = A.head_rows(1);
  31. rowvec d = A.tail_rows(1);
  32. REQUIRE( dot( a, b) == Approx(-0.04208883710200) );
  33. REQUIRE( dot(2*a,2+b) == Approx( 2.24343432579600) );
  34. REQUIRE( dot( c, d) == Approx( 0.108601544706000) );
  35. REQUIRE( dot(0.5*c,2-d) == Approx(-0.392115772353000) );
  36. REQUIRE( dot(a,b) == Approx( dot(A.head_cols(1), A.tail_cols(1)) ) );
  37. REQUIRE( dot(c,d) == Approx( dot(A.head_rows(1), A.tail_rows(1)) ) );
  38. }
  39. TEST_CASE("fn_dot_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. cx_vec a = cx_vec(A.col(0), A.col(1));
  50. cx_vec b = cx_vec(A.col(2), A.col(3));
  51. cx_rowvec c = cx_rowvec(A.row(0), A.row(1));
  52. cx_rowvec d = cx_rowvec(A.row(2), A.row(3));
  53. REQUIRE( abs( dot(a,b) - cx_double(-0.009544718641000, -0.110209641379000)) == Approx(0.0) );
  54. REQUIRE( abs( dot(c,d) - cx_double(-0.326993347830000, +0.061084261990000)) == Approx(0.0) );
  55. REQUIRE( abs(cdot(a,b) - cx_double(-0.314669805873000, -0.807333974477000)) == Approx(0.0) );
  56. REQUIRE( abs(cdot(c,d) - cx_double(-0.165527940664000, +0.586984291846000)) == Approx(0.0) );
  57. }
  58. TEST_CASE("fn_dot_sp_mat_mat")
  59. {
  60. // Make matrices.
  61. SpMat<double> a("3.0 0.0 0.0; 1.0 2.0 2.0; 0.0 0.0 1.0");
  62. Mat<double> b("1.0 2.0 1.0; 1.0 2.0 2.0; 3.0 4.0 5.0");
  63. REQUIRE( dot(a, b) == Approx(17.0) );
  64. REQUIRE( dot(b, a) == Approx(17.0) );
  65. }
  66. TEST_CASE("fn_dot_sp_col_col")
  67. {
  68. SpCol<unsigned int> a("3; 4; 0; 0; 0; 2; 0; 0");
  69. Col<unsigned int> b("1 6 1 2 3 7 1 2");
  70. REQUIRE( dot(a, b) == 41 );
  71. REQUIRE( dot(b, a) == 41 );
  72. }
  73. TEST_CASE("fn_dot_sp_mat_sp_mat")
  74. {
  75. SpMat<double> a("3.0 0.0 0.0; 1.0 2.0 2.0; 0.0 0.0 1.0");
  76. SpMat<double> b("3.0 0.0 0.0; 1.0 2.0 2.0; 0.0 0.0 1.0");
  77. REQUIRE( dot(a, b) == Approx(19.0) );
  78. REQUIRE( dot(b, a) == Approx(19.0) );
  79. }
  80. TEST_CASE("fn_dot_sp_col_sp_col")
  81. {
  82. SpCol<unsigned int> a("3; 4; 0; 0; 0; 2; 0; 0");
  83. SpCol<unsigned int> b("0; 8; 0; 1; 1; 0; 0; 0");
  84. REQUIRE( dot(a, b) == 32 );
  85. REQUIRE( dot(b, a) == 32 );
  86. }
  87. // TODO: norm_dot