fn_det.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 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. //! \addtogroup fn_det
  16. //! @{
  17. template<typename T1>
  18. arma_warn_unused
  19. inline
  20. typename enable_if2< is_supported_blas_type<typename T1::elem_type>::value, typename T1::elem_type >::result
  21. det(const Base<typename T1::elem_type,T1>& X)
  22. {
  23. arma_extra_debug_sigprint();
  24. const quasi_unwrap<T1> U(X.get_ref());
  25. arma_debug_check( (U.M.is_square() == false), "det(): given matrix must be square sized" );
  26. if( (U.M.n_rows > 4) && (U.M.is_diagmat()) )
  27. {
  28. return det(diagmat(U.M)); // call the specialised version of det()
  29. }
  30. return auxlib::det(U.M);
  31. }
  32. template<typename T1>
  33. arma_warn_unused
  34. inline
  35. typename T1::elem_type
  36. det(const Op<T1, op_diagmat>& X)
  37. {
  38. arma_extra_debug_sigprint();
  39. typedef typename T1::elem_type eT;
  40. const diagmat_proxy<T1> A(X.m);
  41. arma_debug_check( (A.n_rows != A.n_cols), "det(): given matrix must be square sized" );
  42. const uword N = (std::min)(A.n_rows, A.n_cols);
  43. eT val1 = eT(1);
  44. eT val2 = eT(1);
  45. uword i,j;
  46. for(i=0, j=1; j<N; i+=2, j+=2)
  47. {
  48. val1 *= A[i];
  49. val2 *= A[j];
  50. }
  51. if(i < N)
  52. {
  53. val1 *= A[i];
  54. }
  55. return val1 * val2;
  56. }
  57. template<typename T1>
  58. arma_warn_unused
  59. inline
  60. typename T1::elem_type
  61. det(const Op<T1, op_trimat>& X)
  62. {
  63. arma_extra_debug_sigprint();
  64. typedef typename T1::elem_type eT;
  65. const Proxy<T1> P(X.m);
  66. const uword N = P.get_n_rows();
  67. arma_debug_check( (N != P.get_n_cols()), "det(): given matrix must be square sized" );
  68. eT val1 = eT(1);
  69. eT val2 = eT(1);
  70. uword i,j;
  71. for(i=0, j=1; j<N; i+=2, j+=2)
  72. {
  73. val1 *= P.at(i,i);
  74. val2 *= P.at(j,j);
  75. }
  76. if(i < N)
  77. {
  78. val1 *= P.at(i,i);
  79. }
  80. return val1 * val2;
  81. }
  82. //! NOTE: don't use this form: it will be removed
  83. template<typename T1>
  84. arma_deprecated
  85. inline
  86. typename enable_if2< is_supported_blas_type<typename T1::elem_type>::value, typename T1::elem_type >::result
  87. det
  88. (
  89. const Base<typename T1::elem_type,T1>& X,
  90. const bool // argument kept only for compatibility with old user code
  91. )
  92. {
  93. arma_extra_debug_sigprint();
  94. // arma_debug_warn("det(X,bool) is deprecated and will be removed; change to det(X)");
  95. return det(X.get_ref());
  96. }
  97. //! NOTE: don't use this form: it will be removed
  98. template<typename T1>
  99. arma_deprecated
  100. inline
  101. typename enable_if2< is_supported_blas_type<typename T1::elem_type>::value, typename T1::elem_type >::result
  102. det
  103. (
  104. const Base<typename T1::elem_type,T1>& X,
  105. const char* // argument kept only for compatibility with old user code
  106. )
  107. {
  108. arma_extra_debug_sigprint();
  109. // arma_debug_warn("det(X,char*) is deprecated and will be removed; change to det(X)");
  110. return det(X.get_ref());
  111. }
  112. template<typename T>
  113. arma_warn_unused
  114. arma_inline
  115. typename arma_scalar_only<T>::result
  116. det(const T& x)
  117. {
  118. return x;
  119. }
  120. //! @}