op_cumprod_meat.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_cumprod
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. op_cumprod::apply_noalias(Mat<eT>& out, const Mat<eT>& X, const uword dim)
  21. {
  22. arma_extra_debug_sigprint();
  23. uword n_rows = X.n_rows;
  24. uword n_cols = X.n_cols;
  25. out.set_size(n_rows,n_cols);
  26. if(out.n_elem == 0) { return; }
  27. if(dim == 0)
  28. {
  29. if(n_cols == 1)
  30. {
  31. const eT* X_mem = X.memptr();
  32. eT* out_mem = out.memptr();
  33. eT acc = eT(1);
  34. for(uword row=0; row < n_rows; ++row)
  35. {
  36. acc *= X_mem[row];
  37. out_mem[row] = acc;
  38. }
  39. }
  40. else
  41. {
  42. for(uword col=0; col < n_cols; ++col)
  43. {
  44. const eT* X_colmem = X.colptr(col);
  45. eT* out_colmem = out.colptr(col);
  46. eT acc = eT(1);
  47. for(uword row=0; row < n_rows; ++row)
  48. {
  49. acc *= X_colmem[row];
  50. out_colmem[row] = acc;
  51. }
  52. }
  53. }
  54. }
  55. else
  56. if(dim == 1)
  57. {
  58. if(n_rows == 1)
  59. {
  60. const eT* X_mem = X.memptr();
  61. eT* out_mem = out.memptr();
  62. eT acc = eT(1);
  63. for(uword col=0; col < n_cols; ++col)
  64. {
  65. acc *= X_mem[col];
  66. out_mem[col] = acc;
  67. }
  68. }
  69. else
  70. {
  71. if(n_cols > 0)
  72. {
  73. arrayops::copy( out.colptr(0), X.colptr(0), n_rows );
  74. for(uword col=1; col < n_cols; ++col)
  75. {
  76. const eT* out_colmem_prev = out.colptr(col-1);
  77. eT* out_colmem = out.colptr(col );
  78. const eT* X_colmem = X.colptr(col );
  79. for(uword row=0; row < n_rows; ++row)
  80. {
  81. out_colmem[row] = out_colmem_prev[row] * X_colmem[row];
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. template<typename T1>
  89. inline
  90. void
  91. op_cumprod::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_cumprod>& in)
  92. {
  93. arma_extra_debug_sigprint();
  94. typedef typename T1::elem_type eT;
  95. const uword dim = in.aux_uword_a;
  96. arma_debug_check( (dim > 1), "cumprod(): parameter 'dim' must be 0 or 1" );
  97. const quasi_unwrap<T1> U(in.m);
  98. if(U.is_alias(out))
  99. {
  100. Mat<eT> tmp;
  101. op_cumprod::apply_noalias(tmp, U.M, dim);
  102. out.steal_mem(tmp);
  103. }
  104. else
  105. {
  106. op_cumprod::apply_noalias(out, U.M, dim);
  107. }
  108. }
  109. template<typename T1>
  110. inline
  111. void
  112. op_cumprod_vec::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_cumprod_vec>& in)
  113. {
  114. arma_extra_debug_sigprint();
  115. typedef typename T1::elem_type eT;
  116. const quasi_unwrap<T1> U(in.m);
  117. const uword dim = (T1::is_xvec) ? uword(U.M.is_rowvec() ? 1 : 0) : uword((T1::is_row) ? 1 : 0);
  118. if(U.is_alias(out))
  119. {
  120. Mat<eT> tmp;
  121. op_cumprod::apply_noalias(tmp, U.M, dim);
  122. out.steal_mem(tmp);
  123. }
  124. else
  125. {
  126. op_cumprod::apply_noalias(out, U.M, dim);
  127. }
  128. }
  129. //! @}