fn_conv_to.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_conv_to_1")
  19. {
  20. typedef std::vector<double> stdvec;
  21. stdvec x(3);
  22. x[0] = 10.0; x[1] = 20.0; x[2] = 30.0;
  23. colvec y = conv_to< colvec >::from(x);
  24. stdvec z = conv_to< stdvec >::from(y);
  25. REQUIRE( z[0] == Approx(10.0) );
  26. REQUIRE( z[1] == Approx(20.0) );
  27. REQUIRE( z[2] == Approx(30.0) );
  28. }
  29. TEST_CASE("fn_conv_to2")
  30. {
  31. mat A(5,6); A.fill(0.1);
  32. umat uA = conv_to<umat>::from(A);
  33. imat iA = conv_to<imat>::from(A);
  34. REQUIRE( (uA.n_rows - A.n_rows) == 0 );
  35. REQUIRE( (iA.n_rows - A.n_rows) == 0 );
  36. REQUIRE( (uA.n_cols - A.n_cols) == 0 );
  37. REQUIRE( (iA.n_cols - A.n_cols) == 0 );
  38. REQUIRE( any(vectorise(uA)) == false);
  39. REQUIRE( any(vectorise(iA)) == false);
  40. }
  41. TEST_CASE("fn_conv_to3")
  42. {
  43. mat A(5,6); A.fill(1.0);
  44. umat uA = conv_to<umat>::from(A);
  45. imat iA = conv_to<imat>::from(A);
  46. REQUIRE( all(vectorise(uA)) == true);
  47. REQUIRE( all(vectorise(iA)) == true);
  48. }
  49. TEST_CASE("fn_conv_to4")
  50. {
  51. mat A = linspace<rowvec>(1,5,6);
  52. mat B = 2*linspace<colvec>(1,5,6);
  53. mat C = randu<mat>(5,6);
  54. REQUIRE( as_scalar( conv_to<rowvec>::from(A) * conv_to<colvec>::from(B) ) == Approx(130.40) );
  55. REQUIRE( conv_to<double>::from(A * B) == Approx(130.40) );
  56. REQUIRE_THROWS( conv_to<colvec>::from(C) );
  57. }