Chris@49
|
1 // Copyright (C) 2008-2012 NICTA (www.nicta.com.au)
|
Chris@49
|
2 // Copyright (C) 2008-2012 Conrad Sanderson
|
Chris@49
|
3 // Copyright (C) 2012 Ryan Curtin
|
Chris@49
|
4 //
|
Chris@49
|
5 // This Source Code Form is subject to the terms of the Mozilla Public
|
Chris@49
|
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
|
Chris@49
|
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
Chris@49
|
8
|
Chris@49
|
9
|
Chris@49
|
10 //! \addtogroup operator_plus
|
Chris@49
|
11 //! @{
|
Chris@49
|
12
|
Chris@49
|
13
|
Chris@49
|
14
|
Chris@49
|
15 //! unary plus operation (does nothing, but is required for completeness)
|
Chris@49
|
16 template<typename T1>
|
Chris@49
|
17 arma_inline
|
Chris@49
|
18 typename enable_if2< is_arma_type<T1>::value, const T1& >::result
|
Chris@49
|
19 operator+
|
Chris@49
|
20 (const T1& X)
|
Chris@49
|
21 {
|
Chris@49
|
22 arma_extra_debug_sigprint();
|
Chris@49
|
23
|
Chris@49
|
24 return X;
|
Chris@49
|
25 }
|
Chris@49
|
26
|
Chris@49
|
27
|
Chris@49
|
28
|
Chris@49
|
29 //! Base + scalar
|
Chris@49
|
30 template<typename T1>
|
Chris@49
|
31 arma_inline
|
Chris@49
|
32 typename enable_if2< is_arma_type<T1>::value, const eOp<T1, eop_scalar_plus> >::result
|
Chris@49
|
33 operator+
|
Chris@49
|
34 (const T1& X, const typename T1::elem_type k)
|
Chris@49
|
35 {
|
Chris@49
|
36 arma_extra_debug_sigprint();
|
Chris@49
|
37
|
Chris@49
|
38 return eOp<T1, eop_scalar_plus>(X, k);
|
Chris@49
|
39 }
|
Chris@49
|
40
|
Chris@49
|
41
|
Chris@49
|
42
|
Chris@49
|
43 //! scalar + Base
|
Chris@49
|
44 template<typename T1>
|
Chris@49
|
45 arma_inline
|
Chris@49
|
46 typename enable_if2< is_arma_type<T1>::value, const eOp<T1, eop_scalar_plus> >::result
|
Chris@49
|
47 operator+
|
Chris@49
|
48 (const typename T1::elem_type k, const T1& X)
|
Chris@49
|
49 {
|
Chris@49
|
50 arma_extra_debug_sigprint();
|
Chris@49
|
51
|
Chris@49
|
52 return eOp<T1, eop_scalar_plus>(X, k); // NOTE: order is swapped
|
Chris@49
|
53 }
|
Chris@49
|
54
|
Chris@49
|
55
|
Chris@49
|
56
|
Chris@49
|
57 //! non-complex Base + complex scalar
|
Chris@49
|
58 template<typename T1>
|
Chris@49
|
59 arma_inline
|
Chris@49
|
60 typename
|
Chris@49
|
61 enable_if2
|
Chris@49
|
62 <
|
Chris@49
|
63 (is_arma_type<T1>::value && is_complex<typename T1::elem_type>::value == false),
|
Chris@49
|
64 const mtOp<typename std::complex<typename T1::pod_type>, T1, op_cx_scalar_plus>
|
Chris@49
|
65 >::result
|
Chris@49
|
66 operator+
|
Chris@49
|
67 (
|
Chris@49
|
68 const T1& X,
|
Chris@49
|
69 const std::complex<typename T1::pod_type>& k
|
Chris@49
|
70 )
|
Chris@49
|
71 {
|
Chris@49
|
72 arma_extra_debug_sigprint();
|
Chris@49
|
73
|
Chris@49
|
74 return mtOp<typename std::complex<typename T1::pod_type>, T1, op_cx_scalar_plus>('j', X, k);
|
Chris@49
|
75 }
|
Chris@49
|
76
|
Chris@49
|
77
|
Chris@49
|
78
|
Chris@49
|
79 //! complex scalar + non-complex Base
|
Chris@49
|
80 template<typename T1>
|
Chris@49
|
81 arma_inline
|
Chris@49
|
82 typename
|
Chris@49
|
83 enable_if2
|
Chris@49
|
84 <
|
Chris@49
|
85 (is_arma_type<T1>::value && is_complex<typename T1::elem_type>::value == false),
|
Chris@49
|
86 const mtOp<typename std::complex<typename T1::pod_type>, T1, op_cx_scalar_plus>
|
Chris@49
|
87 >::result
|
Chris@49
|
88 operator+
|
Chris@49
|
89 (
|
Chris@49
|
90 const std::complex<typename T1::pod_type>& k,
|
Chris@49
|
91 const T1& X
|
Chris@49
|
92 )
|
Chris@49
|
93 {
|
Chris@49
|
94 arma_extra_debug_sigprint();
|
Chris@49
|
95
|
Chris@49
|
96 return mtOp<typename std::complex<typename T1::pod_type>, T1, op_cx_scalar_plus>('j', X, k); // NOTE: order is swapped
|
Chris@49
|
97 }
|
Chris@49
|
98
|
Chris@49
|
99
|
Chris@49
|
100
|
Chris@49
|
101 //! addition of user-accessible Armadillo objects with same element type
|
Chris@49
|
102 template<typename T1, typename T2>
|
Chris@49
|
103 arma_inline
|
Chris@49
|
104 typename
|
Chris@49
|
105 enable_if2
|
Chris@49
|
106 <
|
Chris@49
|
107 is_arma_type<T1>::value && is_arma_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value,
|
Chris@49
|
108 const eGlue<T1, T2, eglue_plus>
|
Chris@49
|
109 >::result
|
Chris@49
|
110 operator+
|
Chris@49
|
111 (
|
Chris@49
|
112 const T1& X,
|
Chris@49
|
113 const T2& Y
|
Chris@49
|
114 )
|
Chris@49
|
115 {
|
Chris@49
|
116 arma_extra_debug_sigprint();
|
Chris@49
|
117
|
Chris@49
|
118 return eGlue<T1, T2, eglue_plus>(X, Y);
|
Chris@49
|
119 }
|
Chris@49
|
120
|
Chris@49
|
121
|
Chris@49
|
122
|
Chris@49
|
123 //! addition of user-accessible Armadillo objects with different element types
|
Chris@49
|
124 template<typename T1, typename T2>
|
Chris@49
|
125 inline
|
Chris@49
|
126 typename
|
Chris@49
|
127 enable_if2
|
Chris@49
|
128 <
|
Chris@49
|
129 (is_arma_type<T1>::value && is_arma_type<T2>::value && (is_same_type<typename T1::elem_type, typename T2::elem_type>::value == false)),
|
Chris@49
|
130 const mtGlue<typename promote_type<typename T1::elem_type, typename T2::elem_type>::result, T1, T2, glue_mixed_plus>
|
Chris@49
|
131 >::result
|
Chris@49
|
132 operator+
|
Chris@49
|
133 (
|
Chris@49
|
134 const T1& X,
|
Chris@49
|
135 const T2& Y
|
Chris@49
|
136 )
|
Chris@49
|
137 {
|
Chris@49
|
138 arma_extra_debug_sigprint();
|
Chris@49
|
139
|
Chris@49
|
140 typedef typename T1::elem_type eT1;
|
Chris@49
|
141 typedef typename T2::elem_type eT2;
|
Chris@49
|
142
|
Chris@49
|
143 typedef typename promote_type<eT1,eT2>::result out_eT;
|
Chris@49
|
144
|
Chris@49
|
145 promote_type<eT1,eT2>::check();
|
Chris@49
|
146
|
Chris@49
|
147 return mtGlue<out_eT, T1, T2, glue_mixed_plus>( X, Y );
|
Chris@49
|
148 }
|
Chris@49
|
149
|
Chris@49
|
150
|
Chris@49
|
151
|
Chris@49
|
152 //! addition of two sparse objects
|
Chris@49
|
153 template<typename T1, typename T2>
|
Chris@49
|
154 inline
|
Chris@49
|
155 arma_hot
|
Chris@49
|
156 typename
|
Chris@49
|
157 enable_if2
|
Chris@49
|
158 <
|
Chris@49
|
159 (is_arma_sparse_type<T1>::value && is_arma_sparse_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value),
|
Chris@49
|
160 SpGlue<T1,T2,spglue_plus>
|
Chris@49
|
161 >::result
|
Chris@49
|
162 operator+
|
Chris@49
|
163 (
|
Chris@49
|
164 const T1& x,
|
Chris@49
|
165 const T2& y
|
Chris@49
|
166 )
|
Chris@49
|
167 {
|
Chris@49
|
168 arma_extra_debug_sigprint();
|
Chris@49
|
169
|
Chris@49
|
170 return SpGlue<T1,T2,spglue_plus>(x, y);
|
Chris@49
|
171 }
|
Chris@49
|
172
|
Chris@49
|
173
|
Chris@49
|
174
|
Chris@49
|
175 //! addition of sparse and non-sparse object
|
Chris@49
|
176 template<typename T1, typename T2>
|
Chris@49
|
177 inline
|
Chris@49
|
178 typename
|
Chris@49
|
179 enable_if2
|
Chris@49
|
180 <
|
Chris@49
|
181 (is_arma_type<T1>::value && is_arma_sparse_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value),
|
Chris@49
|
182 Mat<typename T1::elem_type>
|
Chris@49
|
183 >::result
|
Chris@49
|
184 operator+
|
Chris@49
|
185 (
|
Chris@49
|
186 const T1& x,
|
Chris@49
|
187 const T2& y
|
Chris@49
|
188 )
|
Chris@49
|
189 {
|
Chris@49
|
190 arma_extra_debug_sigprint();
|
Chris@49
|
191
|
Chris@49
|
192 Mat<typename T1::elem_type> result(x);
|
Chris@49
|
193
|
Chris@49
|
194 const SpProxy<T2> pb(y);
|
Chris@49
|
195
|
Chris@49
|
196 arma_debug_assert_same_size( result.n_rows, result.n_cols, pb.get_n_rows(), pb.get_n_cols(), "addition" );
|
Chris@49
|
197
|
Chris@49
|
198 typename SpProxy<T2>::const_iterator_type it = pb.begin();
|
Chris@49
|
199 typename SpProxy<T2>::const_iterator_type it_end = pb.end();
|
Chris@49
|
200
|
Chris@49
|
201 while(it != it_end)
|
Chris@49
|
202 {
|
Chris@49
|
203 result.at(it.row(), it.col()) += (*it);
|
Chris@49
|
204 ++it;
|
Chris@49
|
205 }
|
Chris@49
|
206
|
Chris@49
|
207 return result;
|
Chris@49
|
208 }
|
Chris@49
|
209
|
Chris@49
|
210
|
Chris@49
|
211
|
Chris@49
|
212 //! addition of sparse and non-sparse object
|
Chris@49
|
213 template<typename T1, typename T2>
|
Chris@49
|
214 inline
|
Chris@49
|
215 typename
|
Chris@49
|
216 enable_if2
|
Chris@49
|
217 <
|
Chris@49
|
218 (is_arma_sparse_type<T1>::value && is_arma_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value),
|
Chris@49
|
219 Mat<typename T1::elem_type>
|
Chris@49
|
220 >::result
|
Chris@49
|
221 operator+
|
Chris@49
|
222 (
|
Chris@49
|
223 const T1& x,
|
Chris@49
|
224 const T2& y
|
Chris@49
|
225 )
|
Chris@49
|
226 {
|
Chris@49
|
227 arma_extra_debug_sigprint();
|
Chris@49
|
228
|
Chris@49
|
229 // Just call the other order (these operations are commutative)
|
Chris@49
|
230 return (y + x);
|
Chris@49
|
231 }
|
Chris@49
|
232
|
Chris@49
|
233
|
Chris@49
|
234
|
Chris@49
|
235 //! @}
|