glue_polyfit_meat.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 glue_polyfit
  16. //! @{
  17. template<typename eT>
  18. inline
  19. bool
  20. glue_polyfit::apply_noalias(Mat<eT>& out, const Col<eT>& X, const Col<eT>& Y, const uword N)
  21. {
  22. arma_extra_debug_sigprint();
  23. // create Vandermonde matrix
  24. Mat<eT> V(X.n_elem, N+1);
  25. V.tail_cols(1).ones();
  26. for(uword i=1; i <= N; ++i)
  27. {
  28. const uword j = N-i;
  29. Col<eT> V_col_j (V.colptr(j ), V.n_rows, false, false);
  30. Col<eT> V_col_jp1(V.colptr(j+1), V.n_rows, false, false);
  31. V_col_j = V_col_jp1 % X;
  32. }
  33. Mat<eT> Q;
  34. Mat<eT> R;
  35. const bool status1 = auxlib::qr_econ(Q, R, V);
  36. if(status1 == false) { return false; }
  37. const bool status2 = auxlib::solve_trimat_fast(out, R, (Q.t() * Y), uword(0));
  38. if(status2 == false) { return false; }
  39. return true;
  40. }
  41. template<typename T1, typename T2>
  42. inline
  43. bool
  44. glue_polyfit::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type,T1>& X_expr, const Base<typename T1::elem_type, T2>& Y_expr, const uword N)
  45. {
  46. arma_extra_debug_sigprint();
  47. typedef typename T1::elem_type eT;
  48. const quasi_unwrap<T1> UX(X_expr.get_ref());
  49. const quasi_unwrap<T2> UY(Y_expr.get_ref());
  50. const Mat<eT>& X = UX.M;
  51. const Mat<eT>& Y = UY.M;
  52. arma_debug_check
  53. (
  54. ( ((X.is_vec() == false) && (X.is_empty() == false)) || ((Y.is_vec() == false) && (Y.is_empty() == false)) ),
  55. "polyfit(): given object is not a vector"
  56. );
  57. arma_debug_check( (X.n_elem != Y.n_elem), "polyfit(): given vectors must have the same number of elements" );
  58. if(X.n_elem == 0)
  59. {
  60. out.reset();
  61. return true;
  62. }
  63. arma_debug_check( (N >= X.n_elem), "polyfit(): N must be less than the number of elements in X" );
  64. const Col<eT> X_as_colvec( const_cast<eT*>(X.memptr()), X.n_elem, false, false);
  65. const Col<eT> Y_as_colvec( const_cast<eT*>(Y.memptr()), Y.n_elem, false, false);
  66. bool status = false;
  67. if(UX.is_alias(out) || UY.is_alias(out))
  68. {
  69. Mat<eT> tmp;
  70. status = glue_polyfit::apply_noalias(tmp, X_as_colvec, Y_as_colvec, N);
  71. out.steal_mem(tmp);
  72. }
  73. else
  74. {
  75. status = glue_polyfit::apply_noalias(out, X_as_colvec, Y_as_colvec, N);
  76. }
  77. return status;
  78. }
  79. template<typename T1, typename T2>
  80. inline
  81. void
  82. glue_polyfit::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_polyfit>& expr)
  83. {
  84. arma_extra_debug_sigprint();
  85. const bool status = glue_polyfit::apply_direct(out, expr.A, expr.B, expr.aux_uword);
  86. if(status == false)
  87. {
  88. out.soft_reset();
  89. arma_stop_runtime_error("polyfit(): failed");
  90. }
  91. }
  92. //! @}