fn_det.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_det_1")
  19. {
  20. mat A =
  21. "\
  22. 0.061198 0.201990 0.019678 -0.493936 -0.126745 0.051408;\
  23. 0.437242 0.058956 -0.149362 -0.045465 0.296153 0.035437;\
  24. -0.492474 -0.031309 0.314156 0.419733 0.068317 -0.454499;\
  25. 0.336352 0.411541 0.458476 -0.393139 -0.135040 0.373833;\
  26. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768 0.258704;\
  27. ";
  28. REQUIRE( det(A(0,0,size(0,0))) == Approx(+1.0 ) );
  29. REQUIRE( det(A(0,0,size(1,1))) == Approx(+0.0611980000000000) );
  30. REQUIRE( det(A(0,0,size(2,2))) == Approx(-0.0847105222920000) );
  31. REQUIRE( det(A(0,0,size(3,3))) == Approx(-0.0117387923199772) );
  32. REQUIRE( det(A(0,0,size(4,4))) == Approx(+0.0126070917169865) );
  33. REQUIRE( det(A(0,0,size(5,5))) == Approx(+0.0100409091117668) );
  34. REQUIRE_THROWS( det(A) );
  35. }
  36. TEST_CASE("fn_det_2")
  37. {
  38. mat A = toeplitz(linspace(1,5,6));
  39. REQUIRE( det(A) == Approx(-31.45728) );
  40. mat B(6, 6, fill::zeros); B.diag() = linspace(1,5,6);
  41. REQUIRE( det(B) == Approx(334.152) );
  42. REQUIRE( det(diagmat(B)) == Approx(334.152) );
  43. mat C(5,6, fill::randu);
  44. REQUIRE_THROWS( det(C) );
  45. REQUIRE_THROWS( det(diagmat(C)) );
  46. }
  47. TEST_CASE("fn_det_3")
  48. {
  49. mat A = toeplitz(linspace(1,5,6));
  50. double val;
  51. double sign;
  52. log_det(val, sign, A);
  53. REQUIRE( val == Approx(3.44863) );
  54. REQUIRE( sign == Approx(-1.0) );
  55. REQUIRE( (std::exp(val)*sign) == Approx( det(A) ) );
  56. mat B(5,6, fill::randu);
  57. REQUIRE_THROWS( log_det(val, sign, B) );
  58. }