op_cond_meat.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 op_cond
  16. //! @{
  17. template<typename T1>
  18. inline
  19. typename T1::pod_type
  20. op_cond::cond(const Base<typename T1::elem_type, T1>& X)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::pod_type T;
  24. Col<T> S;
  25. const bool status = auxlib::svd_dc(S, X);
  26. if(status == false)
  27. {
  28. arma_debug_warn("cond(): svd failed");
  29. return T(0);
  30. }
  31. return (S.n_elem > 0) ? T( max(S) / min(S) ) : T(0);
  32. }
  33. template<typename T1>
  34. inline
  35. typename T1::pod_type
  36. op_cond::rcond(const Base<typename T1::elem_type, T1>& X)
  37. {
  38. arma_extra_debug_sigprint();
  39. typedef typename T1::elem_type eT;
  40. typedef typename T1::pod_type T;
  41. if(strip_trimat<T1>::do_trimat)
  42. {
  43. const strip_trimat<T1> S(X.get_ref());
  44. const quasi_unwrap<typename strip_trimat<T1>::stored_type> U(S.M);
  45. arma_debug_check( (U.M.is_square() == false), "rcond(): matrix must be square sized" );
  46. const uword layout = (S.do_triu) ? uword(0) : uword(1);
  47. return auxlib::rcond_trimat(U.M, layout);
  48. }
  49. Mat<eT> A = X.get_ref();
  50. arma_debug_check( (A.is_square() == false), "rcond(): matrix must be square sized" );
  51. if(A.is_empty()) { return Datum<T>::inf; }
  52. const bool is_triu = trimat_helper::is_triu(A);
  53. const bool is_tril = (is_triu) ? false : trimat_helper::is_tril(A);
  54. if(is_triu || is_tril)
  55. {
  56. const uword layout = (is_triu) ? uword(0) : uword(1);
  57. return auxlib::rcond_trimat(A, layout);
  58. }
  59. #if defined(ARMA_OPTIMISE_SYMPD)
  60. const bool try_sympd = auxlib::crippled_lapack(A) ? false : sympd_helper::guess_sympd_anysize(A);
  61. #else
  62. const bool try_sympd = false;
  63. #endif
  64. if(try_sympd)
  65. {
  66. bool calc_ok = false;
  67. const T out_val = auxlib::rcond_sympd(A, calc_ok);
  68. if(calc_ok) { return out_val; }
  69. // auxlib::rcond_sympd() may have failed because A isn't really sympd
  70. // restore A, as auxlib::rcond_sympd() may have destroyed it
  71. A = X.get_ref();
  72. // fallthrough to the next return statement
  73. }
  74. return auxlib::rcond(A);
  75. }
  76. //! @}