1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
4 // Copyright 2008-2016 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17 
18 
19 //! \addtogroup glue_hypot
20 //! @{
21 
22 
23 
24 template<typename T1, typename T2>
25 inline
26 void
apply(Mat<typename T1::elem_type> & out,const Glue<T1,T2,glue_hypot> & expr)27 glue_hypot::apply(Mat<typename T1::elem_type>& out, const Glue<T1, T2, glue_hypot>& expr)
28   {
29   arma_extra_debug_sigprint();
30 
31   typedef typename T1::elem_type eT;
32 
33   const Proxy<T1> P1(expr.A);
34   const Proxy<T2> P2(expr.B);
35 
36   arma_assert_same_size(P1, P2, "hypot()");
37 
38   const bool bad_alias = ( (Proxy<T1>::has_subview && P1.is_alias(out)) || (Proxy<T2>::has_subview && P2.is_alias(out)) );
39 
40   if(bad_alias == false)
41     {
42     glue_hypot::apply_noalias(out, P1, P2);
43     }
44   else
45     {
46     Mat<eT> tmp;
47 
48     glue_hypot::apply_noalias(tmp, P1, P2);
49 
50     out.steal_mem(tmp);
51     }
52   }
53 
54 
55 
56 template<typename T1, typename T2>
57 inline
58 void
apply_noalias(Mat<typename T1::elem_type> & out,const Proxy<T1> & P1,const Proxy<T2> & P2)59 glue_hypot::apply_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P1, const Proxy<T2>& P2)
60   {
61   arma_extra_debug_sigprint();
62 
63   typedef typename T1::elem_type eT;
64 
65   const uword n_rows = P1.get_n_rows();
66   const uword n_cols = P1.get_n_cols();
67 
68   out.set_size(n_rows, n_cols);
69 
70   eT* out_mem = out.memptr();
71 
72   if( (Proxy<T1>::use_at == false) && (Proxy<T2>::use_at == false) )
73     {
74     typename Proxy<T1>::ea_type eaP1 = P1.get_ea();
75     typename Proxy<T2>::ea_type eaP2 = P2.get_ea();
76 
77     const uword N = P1.get_n_elem();
78 
79     for(uword i=0; i<N; ++i)
80       {
81       out_mem[i] = arma_hypot( eaP1[i], eaP2[i] );
82       }
83     }
84   else
85     {
86     for(uword col=0; col < n_cols; ++col)
87     for(uword row=0; row < n_rows; ++row)
88       {
89       *out_mem = arma_hypot( P1.at(row,col), P2.at(row,col) );
90       out_mem++;
91       }
92     }
93   }
94 
95 
96 
97 template<typename T1, typename T2>
98 inline
99 void
apply(Cube<typename T1::elem_type> & out,const GlueCube<T1,T2,glue_hypot> & expr)100 glue_hypot::apply(Cube<typename T1::elem_type>& out, const GlueCube<T1, T2, glue_hypot>& expr)
101   {
102   arma_extra_debug_sigprint();
103 
104   typedef typename T1::elem_type eT;
105 
106   const ProxyCube<T1> P1(expr.A);
107   const ProxyCube<T2> P2(expr.B);
108 
109   arma_assert_same_size(P1, P2, "hypot()");
110 
111   const bool bad_alias = ( (ProxyCube<T1>::has_subview && P1.is_alias(out)) || (ProxyCube<T2>::has_subview && P2.is_alias(out)) );
112 
113   if(bad_alias == false)
114     {
115     glue_hypot::apply_noalias(out, P1, P2);
116     }
117   else
118     {
119     Cube<eT> tmp;
120 
121     glue_hypot::apply_noalias(tmp, P1, P2);
122 
123     out.steal_mem(tmp);
124     }
125   }
126 
127 
128 
129 template<typename T1, typename T2>
130 inline
131 void
apply_noalias(Cube<typename T1::elem_type> & out,const ProxyCube<T1> & P1,const ProxyCube<T2> & P2)132 glue_hypot::apply_noalias(Cube<typename T1::elem_type>& out, const ProxyCube<T1>& P1, const ProxyCube<T2>& P2)
133   {
134   arma_extra_debug_sigprint();
135 
136   typedef typename T1::elem_type eT;
137 
138   const uword n_rows   = P1.get_n_rows();
139   const uword n_cols   = P1.get_n_cols();
140   const uword n_slices = P1.get_n_slices();
141 
142   out.set_size(n_rows, n_cols, n_slices);
143 
144   eT* out_mem = out.memptr();
145 
146   if( (ProxyCube<T1>::use_at == false) && (ProxyCube<T2>::use_at == false) )
147     {
148     typename ProxyCube<T1>::ea_type eaP1 = P1.get_ea();
149     typename ProxyCube<T2>::ea_type eaP2 = P2.get_ea();
150 
151     const uword N = P1.get_n_elem();
152 
153     for(uword i=0; i<N; ++i)
154       {
155       out_mem[i] = arma_hypot( eaP1[i], eaP2[i] );
156       }
157     }
158   else
159     {
160     for(uword slice=0; slice < n_slices; ++slice)
161     for(uword   col=0;   col < n_cols;   ++col  )
162     for(uword   row=0;   row < n_rows;   ++row  )
163       {
164       *out_mem = arma_hypot( P1.at(row,col,slice), P2.at(row,col,slice) );
165       out_mem++;
166       }
167     }
168   }
169 
170 
171 
172 //! @}
173