GenCube_meat.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 GenCube
  16. //! @{
  17. template<typename eT, typename gen_type>
  18. arma_inline
  19. GenCube<eT, gen_type>::GenCube(const uword in_n_rows, const uword in_n_cols, const uword in_n_slices)
  20. : n_rows (in_n_rows )
  21. , n_cols (in_n_cols )
  22. , n_slices(in_n_slices)
  23. {
  24. arma_extra_debug_sigprint();
  25. }
  26. template<typename eT, typename gen_type>
  27. arma_inline
  28. GenCube<eT, gen_type>::~GenCube()
  29. {
  30. arma_extra_debug_sigprint();
  31. }
  32. template<typename eT, typename gen_type>
  33. arma_inline
  34. eT
  35. GenCube<eT, gen_type>::operator[](const uword) const
  36. {
  37. return (*this).generate();
  38. }
  39. template<typename eT, typename gen_type>
  40. arma_inline
  41. eT
  42. GenCube<eT, gen_type>::at(const uword, const uword, const uword) const
  43. {
  44. return (*this).generate();
  45. }
  46. template<typename eT, typename gen_type>
  47. arma_inline
  48. eT
  49. GenCube<eT, gen_type>::at_alt(const uword) const
  50. {
  51. return (*this).generate();
  52. }
  53. template<typename eT, typename gen_type>
  54. inline
  55. void
  56. GenCube<eT, gen_type>::apply(Cube<eT>& out) const
  57. {
  58. arma_extra_debug_sigprint();
  59. // NOTE: we're assuming that the cube has already been set to the correct size;
  60. // this is done by either the Cube contructor or operator=()
  61. if(is_same_type<gen_type, gen_ones >::yes) { out.ones(); }
  62. else if(is_same_type<gen_type, gen_zeros>::yes) { out.zeros(); }
  63. else if(is_same_type<gen_type, gen_randu>::yes) { out.randu(); }
  64. else if(is_same_type<gen_type, gen_randn>::yes) { out.randn(); }
  65. }
  66. template<typename eT, typename gen_type>
  67. inline
  68. void
  69. GenCube<eT, gen_type>::apply_inplace_plus(Cube<eT>& out) const
  70. {
  71. arma_extra_debug_sigprint();
  72. arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "addition");
  73. eT* out_mem = out.memptr();
  74. const uword n_elem = out.n_elem;
  75. uword i,j;
  76. for(i=0, j=1; j<n_elem; i+=2, j+=2)
  77. {
  78. const eT tmp_i = (*this).generate();
  79. const eT tmp_j = (*this).generate();
  80. out_mem[i] += tmp_i;
  81. out_mem[j] += tmp_j;
  82. }
  83. if(i < n_elem)
  84. {
  85. out_mem[i] += (*this).generate();
  86. }
  87. }
  88. template<typename eT, typename gen_type>
  89. inline
  90. void
  91. GenCube<eT, gen_type>::apply_inplace_minus(Cube<eT>& out) const
  92. {
  93. arma_extra_debug_sigprint();
  94. arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "subtraction");
  95. eT* out_mem = out.memptr();
  96. const uword n_elem = out.n_elem;
  97. uword i,j;
  98. for(i=0, j=1; j<n_elem; i+=2, j+=2)
  99. {
  100. const eT tmp_i = (*this).generate();
  101. const eT tmp_j = (*this).generate();
  102. out_mem[i] -= tmp_i;
  103. out_mem[j] -= tmp_j;
  104. }
  105. if(i < n_elem)
  106. {
  107. out_mem[i] -= (*this).generate();
  108. }
  109. }
  110. template<typename eT, typename gen_type>
  111. inline
  112. void
  113. GenCube<eT, gen_type>::apply_inplace_schur(Cube<eT>& out) const
  114. {
  115. arma_extra_debug_sigprint();
  116. arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "element-wise multiplication");
  117. eT* out_mem = out.memptr();
  118. const uword n_elem = out.n_elem;
  119. uword i,j;
  120. for(i=0, j=1; j<n_elem; i+=2, j+=2)
  121. {
  122. const eT tmp_i = (*this).generate();
  123. const eT tmp_j = (*this).generate();
  124. out_mem[i] *= tmp_i;
  125. out_mem[j] *= tmp_j;
  126. }
  127. if(i < n_elem)
  128. {
  129. out_mem[i] *= (*this).generate();
  130. }
  131. }
  132. template<typename eT, typename gen_type>
  133. inline
  134. void
  135. GenCube<eT, gen_type>::apply_inplace_div(Cube<eT>& out) const
  136. {
  137. arma_extra_debug_sigprint();
  138. arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "element-wise division");
  139. eT* out_mem = out.memptr();
  140. const uword n_elem = out.n_elem;
  141. uword i,j;
  142. for(i=0, j=1; j<n_elem; i+=2, j+=2)
  143. {
  144. const eT tmp_i = (*this).generate();
  145. const eT tmp_j = (*this).generate();
  146. out_mem[i] /= tmp_i;
  147. out_mem[j] /= tmp_j;
  148. }
  149. if(i < n_elem)
  150. {
  151. out_mem[i] /= (*this).generate();
  152. }
  153. }
  154. template<typename eT, typename gen_type>
  155. inline
  156. void
  157. GenCube<eT, gen_type>::apply(subview_cube<eT>& out) const
  158. {
  159. arma_extra_debug_sigprint();
  160. // NOTE: we're assuming that the subcube has the same dimensions as the GenCube object
  161. // this is checked by subview_cube::operator=()
  162. if(is_same_type<gen_type, gen_ones >::yes) { out.ones(); }
  163. else if(is_same_type<gen_type, gen_zeros>::yes) { out.zeros(); }
  164. else if(is_same_type<gen_type, gen_randu>::yes) { out.randu(); }
  165. else if(is_same_type<gen_type, gen_randn>::yes) { out.randn(); }
  166. }
  167. //! @}