fn_svd.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_svd
  16. //! @{
  17. template<typename T1>
  18. inline
  19. bool
  20. svd
  21. (
  22. Col<typename T1::pod_type>& S,
  23. const Base<typename T1::elem_type,T1>& X,
  24. const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0
  25. )
  26. {
  27. arma_extra_debug_sigprint();
  28. arma_ignore(junk);
  29. // it doesn't matter if X is an alias of S, as auxlib::svd() makes a copy of X
  30. const bool status = auxlib::svd_dc(S, X);
  31. if(status == false)
  32. {
  33. S.soft_reset();
  34. arma_debug_warn("svd(): decomposition failed");
  35. }
  36. return status;
  37. }
  38. template<typename T1>
  39. arma_warn_unused
  40. inline
  41. Col<typename T1::pod_type>
  42. svd
  43. (
  44. const Base<typename T1::elem_type,T1>& X,
  45. const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0
  46. )
  47. {
  48. arma_extra_debug_sigprint();
  49. arma_ignore(junk);
  50. Col<typename T1::pod_type> out;
  51. const bool status = auxlib::svd_dc(out, X);
  52. if(status == false)
  53. {
  54. out.soft_reset();
  55. arma_stop_runtime_error("svd(): decomposition failed");
  56. }
  57. return out;
  58. }
  59. template<typename T1>
  60. inline
  61. bool
  62. svd
  63. (
  64. Mat<typename T1::elem_type>& U,
  65. Col<typename T1::pod_type >& S,
  66. Mat<typename T1::elem_type>& V,
  67. const Base<typename T1::elem_type,T1>& X,
  68. const char* method = "dc",
  69. const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0
  70. )
  71. {
  72. arma_extra_debug_sigprint();
  73. arma_ignore(junk);
  74. arma_debug_check
  75. (
  76. ( ((void*)(&U) == (void*)(&S)) || (&U == &V) || ((void*)(&S) == (void*)(&V)) ),
  77. "svd(): two or more output objects are the same object"
  78. );
  79. const char sig = (method != NULL) ? method[0] : char(0);
  80. arma_debug_check( ((sig != 's') && (sig != 'd')), "svd(): unknown method specified" );
  81. // auxlib::svd() makes an internal copy of X
  82. const bool status = (sig == 'd') ? auxlib::svd_dc(U, S, V, X) : auxlib::svd(U, S, V, X);
  83. if(status == false)
  84. {
  85. U.soft_reset();
  86. S.soft_reset();
  87. V.soft_reset();
  88. arma_debug_warn("svd(): decomposition failed");
  89. }
  90. return status;
  91. }
  92. template<typename T1>
  93. inline
  94. bool
  95. svd_econ
  96. (
  97. Mat<typename T1::elem_type>& U,
  98. Col<typename T1::pod_type >& S,
  99. Mat<typename T1::elem_type>& V,
  100. const Base<typename T1::elem_type,T1>& X,
  101. const char mode,
  102. const char* method = "dc",
  103. const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0
  104. )
  105. {
  106. arma_extra_debug_sigprint();
  107. arma_ignore(junk);
  108. arma_debug_check
  109. (
  110. ( ((void*)(&U) == (void*)(&S)) || (&U == &V) || ((void*)(&S) == (void*)(&V)) ),
  111. "svd_econ(): two or more output objects are the same object"
  112. );
  113. arma_debug_check
  114. (
  115. ( (mode != 'l') && (mode != 'r') && (mode != 'b') ),
  116. "svd_econ(): parameter 'mode' is incorrect"
  117. );
  118. const char sig = (method != NULL) ? method[0] : char(0);
  119. arma_debug_check( ((sig != 's') && (sig != 'd')), "svd_econ(): unknown method specified" );
  120. const bool status = ((mode == 'b') && (sig == 'd')) ? auxlib::svd_dc_econ(U, S, V, X) : auxlib::svd_econ(U, S, V, X, mode);
  121. if(status == false)
  122. {
  123. U.soft_reset();
  124. S.soft_reset();
  125. V.soft_reset();
  126. arma_debug_warn("svd_econ(): decomposition failed");
  127. }
  128. return status;
  129. }
  130. template<typename T1>
  131. inline
  132. bool
  133. svd_econ
  134. (
  135. Mat<typename T1::elem_type>& U,
  136. Col<typename T1::pod_type >& S,
  137. Mat<typename T1::elem_type>& V,
  138. const Base<typename T1::elem_type,T1>& X,
  139. const char* mode = "both",
  140. const char* method = "dc",
  141. const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0
  142. )
  143. {
  144. arma_extra_debug_sigprint();
  145. arma_ignore(junk);
  146. return svd_econ(U, S, V, X, ((mode != NULL) ? mode[0] : char(0)), method);
  147. }
  148. //! @}