glue_hypot_meat.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_hypot
  16. //! @{
  17. template<typename T1, typename T2>
  18. inline
  19. void
  20. glue_hypot::apply(Mat<typename T1::elem_type>& out, const Glue<T1, T2, glue_hypot>& 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, "hypot()");
  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_hypot::apply_noalias(out, P1, P2);
  31. }
  32. else
  33. {
  34. Mat<eT> tmp;
  35. glue_hypot::apply_noalias(tmp, P1, P2);
  36. out.steal_mem(tmp);
  37. }
  38. }
  39. template<typename T1, typename T2>
  40. inline
  41. void
  42. glue_hypot::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. out.set_size(n_rows, n_cols);
  49. eT* out_mem = out.memptr();
  50. if( (Proxy<T1>::use_at == false) && (Proxy<T2>::use_at == false) )
  51. {
  52. typename Proxy<T1>::ea_type eaP1 = P1.get_ea();
  53. typename Proxy<T2>::ea_type eaP2 = P2.get_ea();
  54. const uword N = P1.get_n_elem();
  55. for(uword i=0; i<N; ++i)
  56. {
  57. out_mem[i] = arma_hypot( eaP1[i], eaP2[i] );
  58. }
  59. }
  60. else
  61. {
  62. for(uword col=0; col < n_cols; ++col)
  63. for(uword row=0; row < n_rows; ++row)
  64. {
  65. *out_mem = arma_hypot( P1.at(row,col), P2.at(row,col) );
  66. out_mem++;
  67. }
  68. }
  69. }
  70. template<typename T1, typename T2>
  71. inline
  72. void
  73. glue_hypot::apply(Cube<typename T1::elem_type>& out, const GlueCube<T1, T2, glue_hypot>& expr)
  74. {
  75. arma_extra_debug_sigprint();
  76. typedef typename T1::elem_type eT;
  77. const ProxyCube<T1> P1(expr.A);
  78. const ProxyCube<T2> P2(expr.B);
  79. arma_assert_same_size(P1, P2, "hypot()");
  80. const bool bad_alias = ( (ProxyCube<T1>::has_subview && P1.is_alias(out)) || (ProxyCube<T2>::has_subview && P2.is_alias(out)) );
  81. if(bad_alias == false)
  82. {
  83. glue_hypot::apply_noalias(out, P1, P2);
  84. }
  85. else
  86. {
  87. Cube<eT> tmp;
  88. glue_hypot::apply_noalias(tmp, P1, P2);
  89. out.steal_mem(tmp);
  90. }
  91. }
  92. template<typename T1, typename T2>
  93. inline
  94. void
  95. glue_hypot::apply_noalias(Cube<typename T1::elem_type>& out, const ProxyCube<T1>& P1, const ProxyCube<T2>& P2)
  96. {
  97. arma_extra_debug_sigprint();
  98. typedef typename T1::elem_type eT;
  99. const uword n_rows = P1.get_n_rows();
  100. const uword n_cols = P1.get_n_cols();
  101. const uword n_slices = P1.get_n_slices();
  102. out.set_size(n_rows, n_cols, n_slices);
  103. eT* out_mem = out.memptr();
  104. if( (ProxyCube<T1>::use_at == false) && (ProxyCube<T2>::use_at == false) )
  105. {
  106. typename ProxyCube<T1>::ea_type eaP1 = P1.get_ea();
  107. typename ProxyCube<T2>::ea_type eaP2 = P2.get_ea();
  108. const uword N = P1.get_n_elem();
  109. for(uword i=0; i<N; ++i)
  110. {
  111. out_mem[i] = arma_hypot( eaP1[i], eaP2[i] );
  112. }
  113. }
  114. else
  115. {
  116. for(uword slice=0; slice < n_slices; ++slice)
  117. for(uword col=0; col < n_cols; ++col )
  118. for(uword row=0; row < n_rows; ++row )
  119. {
  120. *out_mem = arma_hypot( P1.at(row,col,slice), P2.at(row,col,slice) );
  121. out_mem++;
  122. }
  123. }
  124. }
  125. //! @}