glue_max_meat.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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_max
  16. //! @{
  17. template<typename T1, typename T2>
  18. inline
  19. void
  20. glue_max::apply(Mat<typename T1::elem_type>& out, const Glue<T1, T2, glue_max>& X)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const Proxy<T1> PA(X.A);
  25. const Proxy<T2> PB(X.B);
  26. if( (PA.is_alias(out) && PA.has_subview) || (PB.is_alias(out) && PB.has_subview) )
  27. {
  28. Mat<eT> tmp;
  29. glue_max::apply(tmp, PA, PB);
  30. out.steal_mem(tmp);
  31. }
  32. else
  33. {
  34. glue_max::apply(out, PA, PB);
  35. }
  36. }
  37. template<typename eT, typename T1, typename T2>
  38. inline
  39. void
  40. glue_max::apply(Mat<eT>& out, const Proxy<T1>& PA, const Proxy<T2>& PB)
  41. {
  42. arma_extra_debug_sigprint();
  43. const uword n_rows = PA.get_n_rows();
  44. const uword n_cols = PA.get_n_cols();
  45. arma_debug_assert_same_size(n_rows, n_cols, PB.get_n_rows(), PB.get_n_cols(), "element-wise max()");
  46. out.set_size(n_rows, n_cols);
  47. eT* out_mem = out.memptr();
  48. if( (Proxy<T1>::use_at == false) && (Proxy<T2>::use_at == false) )
  49. {
  50. typename Proxy<T1>::ea_type A = PA.get_ea();
  51. typename Proxy<T2>::ea_type B = PB.get_ea();
  52. const uword N = PA.get_n_elem();
  53. for(uword i=0; i<N; ++i)
  54. {
  55. out_mem[i] = (std::max)(A[i], B[i]);
  56. }
  57. }
  58. else
  59. {
  60. for(uword col=0; col < n_cols; ++col)
  61. for(uword row=0; row < n_rows; ++row)
  62. {
  63. *out_mem = (std::max)( PA.at(row,col), PB.at(row,col) );
  64. ++out_mem;
  65. }
  66. }
  67. }
  68. template<typename T, typename T1, typename T2>
  69. inline
  70. void
  71. glue_max::apply(Mat< std::complex<T> >& out, const Proxy<T1>& PA, const Proxy<T2>& PB)
  72. {
  73. arma_extra_debug_sigprint();
  74. typedef typename std::complex<T> eT;
  75. const uword n_rows = PA.get_n_rows();
  76. const uword n_cols = PA.get_n_cols();
  77. arma_debug_assert_same_size(n_rows, n_cols, PB.get_n_rows(), PB.get_n_cols(), "element-wise max()");
  78. out.set_size(n_rows, n_cols);
  79. eT* out_mem = out.memptr();
  80. if( (Proxy<T1>::use_at == false) && (Proxy<T2>::use_at == false) )
  81. {
  82. typename Proxy<T1>::ea_type A = PA.get_ea();
  83. typename Proxy<T2>::ea_type B = PB.get_ea();
  84. const uword N = PA.get_n_elem();
  85. for(uword i=0; i<N; ++i)
  86. {
  87. const eT A_val = A[i];
  88. const eT B_val = B[i];
  89. out_mem[i] = ( std::abs(A_val) > std::abs(B_val) ) ? A_val : B_val;
  90. }
  91. }
  92. else
  93. {
  94. for(uword col=0; col < n_cols; ++col)
  95. for(uword row=0; row < n_rows; ++row)
  96. {
  97. const eT A_val = PA.at(row,col);
  98. const eT B_val = PB.at(row,col);
  99. *out_mem = ( std::abs(A_val) > std::abs(B_val) ) ? A_val : B_val;
  100. ++out_mem;
  101. }
  102. }
  103. }
  104. template<typename T1, typename T2>
  105. inline
  106. void
  107. glue_max::apply(Cube<typename T1::elem_type>& out, const GlueCube<T1, T2, glue_max>& X)
  108. {
  109. arma_extra_debug_sigprint();
  110. typedef typename T1::elem_type eT;
  111. const ProxyCube<T1> PA(X.A);
  112. const ProxyCube<T2> PB(X.B);
  113. if( (PA.is_alias(out) && PA.has_subview) || (PB.is_alias(out) && PB.has_subview) )
  114. {
  115. Cube<eT> tmp;
  116. glue_max::apply(tmp, PA, PB);
  117. out.steal_mem(tmp);
  118. }
  119. else
  120. {
  121. glue_max::apply(out, PA, PB);
  122. }
  123. }
  124. template<typename eT, typename T1, typename T2>
  125. inline
  126. void
  127. glue_max::apply(Cube<eT>& out, const ProxyCube<T1>& PA, const ProxyCube<T2>& PB)
  128. {
  129. arma_extra_debug_sigprint();
  130. const uword n_rows = PA.get_n_rows();
  131. const uword n_cols = PA.get_n_cols();
  132. const uword n_slices = PA.get_n_slices();
  133. arma_debug_assert_same_size(n_rows, n_cols, n_slices, PB.get_n_rows(), PB.get_n_cols(), PB.get_n_slices(), "element-wise max()");
  134. out.set_size(n_rows, n_cols, n_slices);
  135. eT* out_mem = out.memptr();
  136. if( (ProxyCube<T1>::use_at == false) && (ProxyCube<T2>::use_at == false) )
  137. {
  138. typename ProxyCube<T1>::ea_type A = PA.get_ea();
  139. typename ProxyCube<T2>::ea_type B = PB.get_ea();
  140. const uword N = PA.get_n_elem();
  141. for(uword i=0; i<N; ++i)
  142. {
  143. out_mem[i] = (std::max)(A[i], B[i]);
  144. }
  145. }
  146. else
  147. {
  148. for(uword slice=0; slice < n_slices; ++slice)
  149. for(uword col=0; col < n_cols; ++col )
  150. for(uword row=0; row < n_rows; ++row )
  151. {
  152. *out_mem = (std::max)( PA.at(row,col,slice), PB.at(row,col,slice) );
  153. ++out_mem;
  154. }
  155. }
  156. }
  157. template<typename T, typename T1, typename T2>
  158. inline
  159. void
  160. glue_max::apply(Cube< std::complex<T> >& out, const ProxyCube<T1>& PA, const ProxyCube<T2>& PB)
  161. {
  162. arma_extra_debug_sigprint();
  163. typedef typename std::complex<T> eT;
  164. const uword n_rows = PA.get_n_rows();
  165. const uword n_cols = PA.get_n_cols();
  166. const uword n_slices = PA.get_n_slices();
  167. arma_debug_assert_same_size(n_rows, n_cols, n_slices, PB.get_n_rows(), PB.get_n_cols(), PB.get_n_slices(), "element-wise max()");
  168. out.set_size(n_rows, n_cols, n_slices);
  169. eT* out_mem = out.memptr();
  170. if( (ProxyCube<T1>::use_at == false) && (ProxyCube<T2>::use_at == false) )
  171. {
  172. typename ProxyCube<T1>::ea_type A = PA.get_ea();
  173. typename ProxyCube<T2>::ea_type B = PB.get_ea();
  174. const uword N = PA.get_n_elem();
  175. for(uword i=0; i<N; ++i)
  176. {
  177. const eT A_val = A[i];
  178. const eT B_val = B[i];
  179. out_mem[i] = ( std::abs(A_val) > std::abs(B_val) ) ? A_val : B_val;
  180. }
  181. }
  182. else
  183. {
  184. for(uword slice=0; slice < n_slices; ++slice)
  185. for(uword col=0; col < n_cols; ++col )
  186. for(uword row=0; row < n_rows; ++row )
  187. {
  188. const eT A_val = PA.at(row,col,slice);
  189. const eT B_val = PB.at(row,col,slice);
  190. *out_mem = ( std::abs(A_val) > std::abs(B_val) ) ? A_val : B_val;
  191. ++out_mem;
  192. }
  193. }
  194. }
  195. //! @}