fn_find_unique.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_find_unique_1")
  19. {
  20. mat A =
  21. {
  22. { 1, 3, 5, 6, 7 },
  23. { 2, 4, 5, 7, 8 },
  24. { 3, 5, 5, 6, 9 },
  25. };
  26. uvec indices = find_unique(A);
  27. uvec indices2 = { 0, 1, 2, 4, 5, 9, 10, 13, 14 };
  28. REQUIRE( indices.n_elem == indices2.n_elem );
  29. bool same = true;
  30. for(uword i=0; i < indices.n_elem; ++i)
  31. {
  32. if(indices(i) != indices2(i)) { same = false; break; }
  33. }
  34. REQUIRE( same == true );
  35. vec unique_elem = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  36. REQUIRE( accu(abs( A.elem(indices) - unique_elem )) == Approx(0.0) );
  37. // REQUIRE_THROWS( );
  38. }
  39. TEST_CASE("fn_find_unique_2")
  40. {
  41. cx_mat A =
  42. {
  43. { cx_double(1,-1), cx_double(3, 2), cx_double(5, 2), cx_double(6, 1), cx_double(7,-1) },
  44. { cx_double(2, 1), cx_double(4, 4), cx_double(5, 2), cx_double(7,-1), cx_double(8, 1) },
  45. { cx_double(3, 2), cx_double(5, 1), cx_double(5, 3), cx_double(6, 1), cx_double(9,-9) }
  46. };
  47. uvec indices = find_unique(A);
  48. uvec indices2 = { 0, 1, 2, 4, 5, 6, 8, 9, 10, 13, 14 };
  49. REQUIRE( indices.n_elem == indices2.n_elem );
  50. bool same = true;
  51. for(uword i=0; i < indices.n_elem; ++i)
  52. {
  53. if(indices(i) != indices2(i)) { same = false; break; }
  54. }
  55. REQUIRE( same == true );
  56. cx_vec unique_elem =
  57. {
  58. cx_double(1,-1),
  59. cx_double(2, 1),
  60. cx_double(3, 2),
  61. cx_double(4, 4),
  62. cx_double(5, 1),
  63. cx_double(5, 2),
  64. cx_double(5, 3),
  65. cx_double(6, 1),
  66. cx_double(7,-1),
  67. cx_double(8, 1),
  68. cx_double(9,-9)
  69. };
  70. REQUIRE( accu(abs( A.elem(indices) - unique_elem )) == Approx(0.0) );
  71. // REQUIRE_THROWS( );
  72. }