glue_polyval_meat.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_polyval
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. glue_polyval::apply_noalias(Mat<eT>& out, const Mat<eT>& P, const Mat<eT>& X)
  21. {
  22. arma_extra_debug_sigprint();
  23. out.set_size(X.n_rows, X.n_cols);
  24. const eT* P_mem = P.memptr();
  25. const uword P_n_elem = P.n_elem;
  26. out.fill(P_mem[0]);
  27. for(uword i=1; i < P_n_elem; ++i)
  28. {
  29. out = out % X + P_mem[i];
  30. }
  31. }
  32. template<typename T1, typename T2>
  33. inline
  34. void
  35. glue_polyval::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_polyval>& expr)
  36. {
  37. arma_extra_debug_sigprint();
  38. typedef typename T1::elem_type eT;
  39. const quasi_unwrap<T1> UP(expr.A);
  40. const quasi_unwrap<T2> UX(expr.B);
  41. const Mat<eT>& P = UP.M;
  42. const Mat<eT>& X = UX.M;
  43. arma_debug_check( ((P.is_vec() == false) && (P.is_empty() == false)), "polyval(): argument P must be a vector" );
  44. if(P.is_empty() || X.is_empty())
  45. {
  46. out.zeros(X.n_rows, X.n_cols);
  47. return;
  48. }
  49. if(UP.is_alias(out) || UX.is_alias(out))
  50. {
  51. Mat<eT> tmp;
  52. glue_polyval::apply_noalias(tmp, P, X);
  53. out.steal_mem(tmp);
  54. }
  55. else
  56. {
  57. glue_polyval::apply_noalias(out, P, X);
  58. }
  59. }
  60. //! @}