fn_as_scalar.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_as_scalar_1")
  19. {
  20. mat A(1,1); A.fill(2.0);
  21. mat B(2,2); B.fill(2.0);
  22. REQUIRE( as_scalar(A) == Approx(2.0) );
  23. REQUIRE( as_scalar(2+A) == Approx(4.0) );
  24. REQUIRE( as_scalar(B(span(0,0), span(0,0))) == Approx(2.0) );
  25. REQUIRE_THROWS( as_scalar(B) );
  26. }
  27. TEST_CASE("fn_as_scalar_2")
  28. {
  29. rowvec r = linspace<rowvec>(1,5,6);
  30. colvec q = linspace<colvec>(1,5,6);
  31. mat X = 0.5*toeplitz(q);
  32. REQUIRE( as_scalar(r*q) == Approx(65.2) );
  33. REQUIRE( as_scalar(r*X*q) == Approx(380.848) );
  34. REQUIRE( as_scalar(r*diagmat(X)*q) == Approx(32.6) );
  35. REQUIRE( as_scalar(r*inv(diagmat(X))*q) == Approx(130.4) );
  36. }
  37. TEST_CASE("fn_as_scalar_3")
  38. {
  39. cube A(1,1,1); A.fill(2.0);
  40. cube B(2,2,2); B.fill(2.0);
  41. REQUIRE( as_scalar(A) == Approx(2.0) );
  42. REQUIRE( as_scalar(2+A) == Approx(4.0) );
  43. REQUIRE( as_scalar(B(span(0,0), span(0,0), span(0,0))) == Approx(2.0) );
  44. REQUIRE_THROWS( as_scalar(B) );
  45. }