fn_pinv.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_pinv
  16. //! @{
  17. template<typename T1>
  18. arma_warn_unused
  19. inline
  20. typename enable_if2< is_real<typename T1::pod_type>::value, const Op<T1, op_pinv> >::result
  21. pinv
  22. (
  23. const Base<typename T1::elem_type,T1>& X,
  24. const typename T1::pod_type tol = 0.0,
  25. const char* method = "dc"
  26. )
  27. {
  28. arma_extra_debug_sigprint();
  29. typedef typename T1::elem_type eT;
  30. const char sig = (method != NULL) ? method[0] : char(0);
  31. arma_debug_check( ((sig != 's') && (sig != 'd')), "pinv(): unknown method specified" );
  32. return (sig == 'd') ? Op<T1, op_pinv>(X.get_ref(), eT(tol), 1, 0) : Op<T1, op_pinv>(X.get_ref(), eT(tol), 0, 0);
  33. }
  34. template<typename T1>
  35. inline
  36. typename enable_if2< is_real<typename T1::pod_type>::value, bool >::result
  37. pinv
  38. (
  39. Mat<typename T1::elem_type>& out,
  40. const Base<typename T1::elem_type,T1>& X,
  41. const typename T1::pod_type tol = 0.0,
  42. const char* method = "dc"
  43. )
  44. {
  45. arma_extra_debug_sigprint();
  46. const char sig = (method != NULL) ? method[0] : char(0);
  47. arma_debug_check( ((sig != 's') && (sig != 'd')), "pinv(): unknown method specified" );
  48. const bool use_divide_and_conquer = (sig == 'd');
  49. const bool status = op_pinv::apply_direct(out, X.get_ref(), tol, use_divide_and_conquer);
  50. if(status == false)
  51. {
  52. arma_debug_warn("pinv(): svd failed");
  53. }
  54. return status;
  55. }
  56. //! @}