init_misc.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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("init_misc_1")
  19. {
  20. const uword n_rows = 5;
  21. const uword n_cols = 6;
  22. mat A(5,6, fill::zeros);
  23. for(uword row=0; row<n_rows; ++row)
  24. for(uword col=0; col<n_cols; ++col)
  25. {
  26. REQUIRE(A(row,col) == 0);
  27. }
  28. mat B(5,6, fill::ones);
  29. for(uword row=0; row<n_rows; ++row)
  30. for(uword col=0; col<n_cols; ++col)
  31. {
  32. REQUIRE(B(row,col) == 1);
  33. }
  34. mat C(2,3);
  35. C(0,0) = 0;
  36. C(1,0) = 1;
  37. C(0,1) = 2;
  38. C(1,1) = 3;
  39. C(0,2) = 4;
  40. C(1,2) = 5;
  41. REQUIRE(C(0,0) == 0);
  42. REQUIRE(C(1,0) == 1);
  43. REQUIRE(C(0,1) == 2);
  44. REQUIRE(C(1,1) == 3);
  45. REQUIRE(C(0,2) == 4);
  46. REQUIRE(C(1,2) == 5);
  47. mat D = { {0, 2, 4},
  48. {1, 3, 5} };
  49. REQUIRE(D(0,0) == 0);
  50. REQUIRE(D(1,0) == 1);
  51. REQUIRE(D(0,1) == 2);
  52. REQUIRE(D(1,1) == 3);
  53. REQUIRE(D(0,2) == 4);
  54. REQUIRE(D(1,2) == 5);
  55. mat E;
  56. E << 0 << 2 << 4 << endr
  57. << 1 << 3 << 5 << endr;
  58. REQUIRE(E(0,0) == 0);
  59. REQUIRE(E(1,0) == 1);
  60. REQUIRE(E(0,1) == 2);
  61. REQUIRE(E(1,1) == 3);
  62. REQUIRE(E(0,2) == 4);
  63. REQUIRE(E(1,2) == 5);
  64. }
  65. TEST_CASE("init_misc_2")
  66. {
  67. mat A =
  68. {
  69. { -0.78838, 0.69298, 0.41084, 0.90142 },
  70. { 0.49345, -0.12020, 0.78987, 0.53124 },
  71. { 0.73573, 0.52104, -0.22263, 0.40163 }
  72. };
  73. mat B =
  74. "\
  75. -0.78838, 0.69298, 0.41084, 0.90142;\
  76. 0.49345, -0.12020, 0.78987, 0.53124;\
  77. 0.73573, 0.52104, -0.22263, 0.40163;\
  78. ";
  79. mat C =
  80. "\
  81. -0.78838 0.69298 0.41084 0.90142;\
  82. 0.49345 -0.12020 0.78987 0.53124;\
  83. 0.73573 0.52104 -0.22263 0.40163;\
  84. ";
  85. mat D;
  86. D << -0.78838 << 0.69298 << 0.41084 << 0.90142 << endr
  87. << 0.49345 << -0.12020 << 0.78987 << 0.53124 << endr
  88. << 0.73573 << 0.52104 << -0.22263 << 0.40163 << endr;
  89. REQUIRE( A.n_rows == 3 );
  90. REQUIRE( A.n_cols == 4 );
  91. REQUIRE( A(0,0) == Approx(-0.78838) );
  92. REQUIRE( A(1,0) == Approx( 0.49345) );
  93. REQUIRE( A(2,0) == Approx( 0.73573) );
  94. REQUIRE( A(0,1) == Approx( 0.69298) );
  95. REQUIRE( A(1,1) == Approx(-0.12020) );
  96. REQUIRE( A(2,1) == Approx( 0.52104) );
  97. REQUIRE( A(0,2) == Approx( 0.41084) );
  98. REQUIRE( A(1,2) == Approx( 0.78987) );
  99. REQUIRE( A(2,2) == Approx(-0.22263) );
  100. REQUIRE( A(0,3) == Approx( 0.90142) );
  101. REQUIRE( A(1,3) == Approx( 0.53124) );
  102. REQUIRE( A(2,3) == Approx( 0.40163) );
  103. REQUIRE( accu(abs(A-B)) == Approx(0.0) );
  104. REQUIRE( accu(abs(A-C)) == Approx(0.0) );
  105. REQUIRE( accu(abs(A-D)) == Approx(0.0) );
  106. }
  107. TEST_CASE("init_misc_3")
  108. {
  109. const uword n_rows = 5;
  110. const uword n_cols = 6;
  111. cx_mat A(5,6, fill::zeros);
  112. for(uword row=0; row<n_rows; ++row)
  113. for(uword col=0; col<n_cols; ++col)
  114. {
  115. REQUIRE(A(row,col) == cx_double(0,0));
  116. }
  117. cx_mat B(5,6, fill::ones);
  118. for(uword row=0; row<5; ++row)
  119. for(uword col=0; col<6; ++col)
  120. {
  121. REQUIRE(B(row,col) == cx_double(1,0));
  122. }
  123. cx_mat C(2,3);
  124. C(0,0) = cx_double(0.0, 1.0);
  125. C(1,0) = cx_double(1.0, 2.0);
  126. C(0,1) = cx_double(3.0, 4.0);
  127. C(1,1) = cx_double(5.0, 6.0);
  128. C(0,2) = cx_double(7.0, 8.0);
  129. C(1,2) = cx_double(9.0, 10.0);
  130. REQUIRE(C(0,0) == cx_double(0.0, 1.0));
  131. REQUIRE(C(1,0) == cx_double(1.0, 2.0));
  132. REQUIRE(C(0,1) == cx_double(3.0, 4.0));
  133. REQUIRE(C(1,1) == cx_double(5.0, 6.0));
  134. REQUIRE(C(0,2) == cx_double(7.0, 8.0));
  135. REQUIRE(C(1,2) == cx_double(9.0, 10.0));
  136. }