glue_atan2_meat.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_atan2
  16. //! @{
  17. template<typename T1, typename T2>
  18. inline
  19. void
  20. glue_atan2::apply(Mat<typename T1::elem_type>& out, const Glue<T1, T2, glue_atan2>& expr)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const Proxy<T1> P1(expr.A);
  25. const Proxy<T2> P2(expr.B);
  26. arma_assert_same_size(P1, P2, "atan2()");
  27. const bool bad_alias = ( (Proxy<T1>::has_subview && P1.is_alias(out)) || (Proxy<T2>::has_subview && P2.is_alias(out)) );
  28. if(bad_alias == false)
  29. {
  30. glue_atan2::apply_noalias(out, P1, P2);
  31. }
  32. else
  33. {
  34. Mat<eT> tmp;
  35. glue_atan2::apply_noalias(tmp, P1, P2);
  36. out.steal_mem(tmp);
  37. }
  38. }
  39. template<typename T1, typename T2>
  40. inline
  41. void
  42. glue_atan2::apply_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P1, const Proxy<T2>& P2)
  43. {
  44. arma_extra_debug_sigprint();
  45. typedef typename T1::elem_type eT;
  46. const uword n_rows = P1.get_n_rows();
  47. const uword n_cols = P1.get_n_cols();
  48. const uword n_elem = P1.get_n_elem();
  49. out.set_size(n_rows, n_cols);
  50. eT* out_mem = out.memptr();
  51. const bool use_mp = arma_config::cxx11 && arma_config::openmp && mp_gate<eT, (Proxy<T1>::use_mp || Proxy<T2>::use_mp)>::eval(n_elem);
  52. const bool use_at = Proxy<T1>::use_at || Proxy<T2>::use_at;
  53. if(use_at == false)
  54. {
  55. typename Proxy<T1>::ea_type eaP1 = P1.get_ea();
  56. typename Proxy<T2>::ea_type eaP2 = P2.get_ea();
  57. if(use_mp)
  58. {
  59. #if defined(ARMA_USE_OPENMP)
  60. {
  61. const int n_threads = mp_thread_limit::get();
  62. #pragma omp parallel for schedule(static) num_threads(n_threads)
  63. for(uword i=0; i<n_elem; ++i)
  64. {
  65. out_mem[i] = std::atan2( eaP1[i], eaP2[i] );
  66. }
  67. }
  68. #endif
  69. }
  70. else
  71. {
  72. for(uword i=0; i<n_elem; ++i)
  73. {
  74. out_mem[i] = std::atan2( eaP1[i], eaP2[i] );
  75. }
  76. }
  77. }
  78. else
  79. {
  80. if(use_mp)
  81. {
  82. const unwrap<typename Proxy<T1>::stored_type> U1(P1.Q);
  83. const unwrap<typename Proxy<T2>::stored_type> U2(P2.Q);
  84. out = arma::atan2(U1.M, U2.M);
  85. }
  86. else
  87. {
  88. for(uword col=0; col < n_cols; ++col)
  89. for(uword row=0; row < n_rows; ++row)
  90. {
  91. *out_mem = std::atan2( P1.at(row,col), P2.at(row,col) );
  92. out_mem++;
  93. }
  94. }
  95. }
  96. }
  97. template<typename T1, typename T2>
  98. inline
  99. void
  100. glue_atan2::apply(Cube<typename T1::elem_type>& out, const GlueCube<T1, T2, glue_atan2>& expr)
  101. {
  102. arma_extra_debug_sigprint();
  103. typedef typename T1::elem_type eT;
  104. const ProxyCube<T1> P1(expr.A);
  105. const ProxyCube<T2> P2(expr.B);
  106. arma_assert_same_size(P1, P2, "atan2()");
  107. const bool bad_alias = ( (ProxyCube<T1>::has_subview && P1.is_alias(out)) || (ProxyCube<T2>::has_subview && P2.is_alias(out)) );
  108. if(bad_alias == false)
  109. {
  110. glue_atan2::apply_noalias(out, P1, P2);
  111. }
  112. else
  113. {
  114. Cube<eT> tmp;
  115. glue_atan2::apply_noalias(tmp, P1, P2);
  116. out.steal_mem(tmp);
  117. }
  118. }
  119. template<typename T1, typename T2>
  120. inline
  121. void
  122. glue_atan2::apply_noalias(Cube<typename T1::elem_type>& out, const ProxyCube<T1>& P1, const ProxyCube<T2>& P2)
  123. {
  124. arma_extra_debug_sigprint();
  125. typedef typename T1::elem_type eT;
  126. const uword n_rows = P1.get_n_rows();
  127. const uword n_cols = P1.get_n_cols();
  128. const uword n_slices = P1.get_n_slices();
  129. const uword n_elem = P1.get_n_elem();
  130. out.set_size(n_rows, n_cols, n_slices);
  131. eT* out_mem = out.memptr();
  132. const bool use_mp = arma_config::cxx11 && arma_config::openmp && mp_gate<eT, (ProxyCube<T1>::use_mp || ProxyCube<T2>::use_mp)>::eval(n_elem);
  133. const bool use_at = ProxyCube<T1>::use_at || ProxyCube<T2>::use_at;
  134. if(use_at == false)
  135. {
  136. typename ProxyCube<T1>::ea_type eaP1 = P1.get_ea();
  137. typename ProxyCube<T2>::ea_type eaP2 = P2.get_ea();
  138. if(use_mp)
  139. {
  140. #if defined(ARMA_USE_OPENMP)
  141. {
  142. const int n_threads = mp_thread_limit::get();
  143. #pragma omp parallel for schedule(static) num_threads(n_threads)
  144. for(uword i=0; i<n_elem; ++i)
  145. {
  146. out_mem[i] = std::atan2( eaP1[i], eaP2[i] );
  147. }
  148. }
  149. #endif
  150. }
  151. else
  152. {
  153. for(uword i=0; i<n_elem; ++i)
  154. {
  155. out_mem[i] = std::atan2( eaP1[i], eaP2[i] );
  156. }
  157. }
  158. }
  159. else
  160. {
  161. if(use_mp)
  162. {
  163. const unwrap_cube<typename ProxyCube<T1>::stored_type> U1(P1.Q);
  164. const unwrap_cube<typename ProxyCube<T2>::stored_type> U2(P2.Q);
  165. out = arma::atan2(U1.M, U2.M);
  166. }
  167. else
  168. {
  169. for(uword slice=0; slice < n_slices; ++slice)
  170. for(uword col=0; col < n_cols; ++col )
  171. for(uword row=0; row < n_rows; ++row )
  172. {
  173. *out_mem = std::atan2( P1.at(row,col,slice), P2.at(row,col,slice) );
  174. out_mem++;
  175. }
  176. }
  177. }
  178. }
  179. //! @}