op_pinv_meat.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_pinv
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. op_pinv::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_pinv>& in)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::pod_type T;
  24. const T tol = access::tmp_real(in.aux);
  25. const bool use_divide_and_conquer = (in.aux_uword_a == 1);
  26. const bool status = op_pinv::apply_direct(out, in.m, tol, use_divide_and_conquer);
  27. if(status == false)
  28. {
  29. arma_stop_runtime_error("pinv(): svd failed");
  30. }
  31. }
  32. template<typename T1>
  33. inline
  34. bool
  35. op_pinv::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type,T1>& expr, typename T1::pod_type tol, const bool use_divide_and_conquer)
  36. {
  37. arma_extra_debug_sigprint();
  38. typedef typename T1::elem_type eT;
  39. typedef typename T1::pod_type T;
  40. arma_debug_check((tol < T(0)), "pinv(): tolerance must be >= 0");
  41. const Proxy<T1> P(expr.get_ref());
  42. const uword n_rows = P.get_n_rows();
  43. const uword n_cols = P.get_n_cols();
  44. if( (n_rows*n_cols) == 0 )
  45. {
  46. out.set_size(n_cols,n_rows);
  47. return true;
  48. }
  49. // economical SVD decomposition
  50. Mat<eT> U;
  51. Col< T> s;
  52. Mat<eT> V;
  53. bool status = false;
  54. if(use_divide_and_conquer)
  55. {
  56. status = (n_cols > n_rows) ? auxlib::svd_dc_econ(U, s, V, trans(P.Q)) : auxlib::svd_dc_econ(U, s, V, P.Q);
  57. }
  58. else
  59. {
  60. status = (n_cols > n_rows) ? auxlib::svd_econ(U, s, V, trans(P.Q), 'b') : auxlib::svd_econ(U, s, V, P.Q, 'b');
  61. }
  62. if(status == false)
  63. {
  64. out.soft_reset();
  65. return false;
  66. }
  67. const uword s_n_elem = s.n_elem;
  68. const T* s_mem = s.memptr();
  69. // set tolerance to default if it hasn't been specified
  70. if( (tol == T(0)) && (s_n_elem > 0) )
  71. {
  72. tol = (std::max)(n_rows, n_cols) * s_mem[0] * std::numeric_limits<T>::epsilon();
  73. }
  74. uword count = 0;
  75. for(uword i = 0; i < s_n_elem; ++i)
  76. {
  77. count += (s_mem[i] >= tol) ? uword(1) : uword(0);
  78. }
  79. if(count > 0)
  80. {
  81. Col<T> s2(count);
  82. T* s2_mem = s2.memptr();
  83. uword count2 = 0;
  84. for(uword i=0; i < s_n_elem; ++i)
  85. {
  86. const T val = s_mem[i];
  87. if(val >= tol) { s2_mem[count2] = (val > T(0)) ? T(T(1) / val) : T(0); ++count2; }
  88. }
  89. if(n_rows >= n_cols)
  90. {
  91. // out = ( (V.n_cols > count) ? V.cols(0,count-1) : V ) * diagmat(s2) * trans( (U.n_cols > count) ? U.cols(0,count-1) : U );
  92. Mat<eT> tmp;
  93. if(count < V.n_cols)
  94. {
  95. tmp = V.cols(0,count-1) * diagmat(s2);
  96. }
  97. else
  98. {
  99. tmp = V * diagmat(s2);
  100. }
  101. if(count < U.n_cols)
  102. {
  103. out = tmp * trans(U.cols(0,count-1));
  104. }
  105. else
  106. {
  107. out = tmp * trans(U);
  108. }
  109. }
  110. else
  111. {
  112. // out = ( (U.n_cols > count) ? U.cols(0,count-1) : U ) * diagmat(s2) * trans( (V.n_cols > count) ? V.cols(0,count-1) : V );
  113. Mat<eT> tmp;
  114. if(count < U.n_cols)
  115. {
  116. tmp = U.cols(0,count-1) * diagmat(s2);
  117. }
  118. else
  119. {
  120. tmp = U * diagmat(s2);
  121. }
  122. if(count < V.n_cols)
  123. {
  124. out = tmp * trans(V.cols(0,count-1));
  125. }
  126. else
  127. {
  128. out = tmp * trans(V);
  129. }
  130. }
  131. }
  132. else
  133. {
  134. out.zeros(n_cols, n_rows);
  135. }
  136. return true;
  137. }
  138. //! @}