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_minus
|
Chris@49
|
11 //! @{
|
Chris@49
|
12
|
Chris@49
|
13
|
Chris@49
|
14
|
Chris@49
|
15 //! unary -
|
Chris@49
|
16 template<typename T1>
|
Chris@49
|
17 arma_inline
|
Chris@49
|
18 typename
|
Chris@49
|
19 enable_if2< is_arma_type<T1>::value, const eOp<T1, eop_neg> >::result
|
Chris@49
|
20 operator-
|
Chris@49
|
21 (const T1& X)
|
Chris@49
|
22 {
|
Chris@49
|
23 arma_extra_debug_sigprint();
|
Chris@49
|
24
|
Chris@49
|
25 return eOp<T1,eop_neg>(X);
|
Chris@49
|
26 }
|
Chris@49
|
27
|
Chris@49
|
28
|
Chris@49
|
29
|
Chris@49
|
30 //! cancellation of two consecutive negations: -(-T1)
|
Chris@49
|
31 template<typename T1>
|
Chris@49
|
32 arma_inline
|
Chris@49
|
33 const T1&
|
Chris@49
|
34 operator-
|
Chris@49
|
35 (const eOp<T1, eop_neg>& X)
|
Chris@49
|
36 {
|
Chris@49
|
37 arma_extra_debug_sigprint();
|
Chris@49
|
38
|
Chris@49
|
39 return X.m;
|
Chris@49
|
40 }
|
Chris@49
|
41
|
Chris@49
|
42
|
Chris@49
|
43
|
Chris@49
|
44 //! Base - scalar
|
Chris@49
|
45 template<typename T1>
|
Chris@49
|
46 arma_inline
|
Chris@49
|
47 typename
|
Chris@49
|
48 enable_if2< is_arma_type<T1>::value, const eOp<T1, eop_scalar_minus_post> >::result
|
Chris@49
|
49 operator-
|
Chris@49
|
50 (
|
Chris@49
|
51 const T1& X,
|
Chris@49
|
52 const typename T1::elem_type k
|
Chris@49
|
53 )
|
Chris@49
|
54 {
|
Chris@49
|
55 arma_extra_debug_sigprint();
|
Chris@49
|
56
|
Chris@49
|
57 return eOp<T1, eop_scalar_minus_post>(X, k);
|
Chris@49
|
58 }
|
Chris@49
|
59
|
Chris@49
|
60
|
Chris@49
|
61
|
Chris@49
|
62 //! scalar - Base
|
Chris@49
|
63 template<typename T1>
|
Chris@49
|
64 arma_inline
|
Chris@49
|
65 typename
|
Chris@49
|
66 enable_if2< is_arma_type<T1>::value, const eOp<T1, eop_scalar_minus_pre> >::result
|
Chris@49
|
67 operator-
|
Chris@49
|
68 (
|
Chris@49
|
69 const typename T1::elem_type k,
|
Chris@49
|
70 const T1& X
|
Chris@49
|
71 )
|
Chris@49
|
72 {
|
Chris@49
|
73 arma_extra_debug_sigprint();
|
Chris@49
|
74
|
Chris@49
|
75 return eOp<T1, eop_scalar_minus_pre>(X, k);
|
Chris@49
|
76 }
|
Chris@49
|
77
|
Chris@49
|
78
|
Chris@49
|
79
|
Chris@49
|
80 //! complex scalar - non-complex Base
|
Chris@49
|
81 template<typename T1>
|
Chris@49
|
82 arma_inline
|
Chris@49
|
83 typename
|
Chris@49
|
84 enable_if2
|
Chris@49
|
85 <
|
Chris@49
|
86 (is_arma_type<T1>::value && is_complex<typename T1::elem_type>::value == false),
|
Chris@49
|
87 const mtOp<typename std::complex<typename T1::pod_type>, T1, op_cx_scalar_minus_pre>
|
Chris@49
|
88 >::result
|
Chris@49
|
89 operator-
|
Chris@49
|
90 (
|
Chris@49
|
91 const std::complex<typename T1::pod_type>& k,
|
Chris@49
|
92 const T1& X
|
Chris@49
|
93 )
|
Chris@49
|
94 {
|
Chris@49
|
95 arma_extra_debug_sigprint();
|
Chris@49
|
96
|
Chris@49
|
97 return mtOp<typename std::complex<typename T1::pod_type>, T1, op_cx_scalar_minus_pre>('j', X, k);
|
Chris@49
|
98 }
|
Chris@49
|
99
|
Chris@49
|
100
|
Chris@49
|
101
|
Chris@49
|
102 //! non-complex Base - complex scalar
|
Chris@49
|
103 template<typename T1>
|
Chris@49
|
104 arma_inline
|
Chris@49
|
105 typename
|
Chris@49
|
106 enable_if2
|
Chris@49
|
107 <
|
Chris@49
|
108 (is_arma_type<T1>::value && is_complex<typename T1::elem_type>::value == false),
|
Chris@49
|
109 const mtOp<typename std::complex<typename T1::pod_type>, T1, op_cx_scalar_minus_post>
|
Chris@49
|
110 >::result
|
Chris@49
|
111 operator-
|
Chris@49
|
112 (
|
Chris@49
|
113 const T1& X,
|
Chris@49
|
114 const std::complex<typename T1::pod_type>& k
|
Chris@49
|
115 )
|
Chris@49
|
116 {
|
Chris@49
|
117 arma_extra_debug_sigprint();
|
Chris@49
|
118
|
Chris@49
|
119 return mtOp<typename std::complex<typename T1::pod_type>, T1, op_cx_scalar_minus_post>('j', X, k);
|
Chris@49
|
120 }
|
Chris@49
|
121
|
Chris@49
|
122
|
Chris@49
|
123
|
Chris@49
|
124 //! subtraction of Base objects with same element type
|
Chris@49
|
125 template<typename T1, typename T2>
|
Chris@49
|
126 arma_inline
|
Chris@49
|
127 typename
|
Chris@49
|
128 enable_if2
|
Chris@49
|
129 <
|
Chris@49
|
130 is_arma_type<T1>::value && is_arma_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value,
|
Chris@49
|
131 const eGlue<T1, T2, eglue_minus>
|
Chris@49
|
132 >::result
|
Chris@49
|
133 operator-
|
Chris@49
|
134 (
|
Chris@49
|
135 const T1& X,
|
Chris@49
|
136 const T2& Y
|
Chris@49
|
137 )
|
Chris@49
|
138 {
|
Chris@49
|
139 arma_extra_debug_sigprint();
|
Chris@49
|
140
|
Chris@49
|
141 return eGlue<T1, T2, eglue_minus>(X, Y);
|
Chris@49
|
142 }
|
Chris@49
|
143
|
Chris@49
|
144
|
Chris@49
|
145
|
Chris@49
|
146 //! subtraction of Base objects with different element types
|
Chris@49
|
147 template<typename T1, typename T2>
|
Chris@49
|
148 inline
|
Chris@49
|
149 typename
|
Chris@49
|
150 enable_if2
|
Chris@49
|
151 <
|
Chris@49
|
152 (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
|
153 const mtGlue<typename promote_type<typename T1::elem_type, typename T2::elem_type>::result, T1, T2, glue_mixed_minus>
|
Chris@49
|
154 >::result
|
Chris@49
|
155 operator-
|
Chris@49
|
156 (
|
Chris@49
|
157 const T1& X,
|
Chris@49
|
158 const T2& Y
|
Chris@49
|
159 )
|
Chris@49
|
160 {
|
Chris@49
|
161 arma_extra_debug_sigprint();
|
Chris@49
|
162
|
Chris@49
|
163 typedef typename T1::elem_type eT1;
|
Chris@49
|
164 typedef typename T2::elem_type eT2;
|
Chris@49
|
165
|
Chris@49
|
166 typedef typename promote_type<eT1,eT2>::result out_eT;
|
Chris@49
|
167
|
Chris@49
|
168 promote_type<eT1,eT2>::check();
|
Chris@49
|
169
|
Chris@49
|
170 return mtGlue<out_eT, T1, T2, glue_mixed_minus>( X, Y );
|
Chris@49
|
171 }
|
Chris@49
|
172
|
Chris@49
|
173
|
Chris@49
|
174
|
Chris@49
|
175 //! subtraction of two sparse objects
|
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_sparse_type<T1>::value && is_arma_sparse_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value),
|
Chris@49
|
182 const SpGlue<T1,T2,spglue_minus>
|
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 return SpGlue<T1,T2,spglue_minus>(X,Y);
|
Chris@49
|
193 }
|
Chris@49
|
194
|
Chris@49
|
195
|
Chris@49
|
196
|
Chris@49
|
197 //! subtraction of one sparse and one dense object
|
Chris@49
|
198 template<typename T1, typename T2>
|
Chris@49
|
199 inline
|
Chris@49
|
200 typename
|
Chris@49
|
201 enable_if2
|
Chris@49
|
202 <
|
Chris@49
|
203 (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
|
204 Mat<typename T1::elem_type>
|
Chris@49
|
205 >::result
|
Chris@49
|
206 operator-
|
Chris@49
|
207 (
|
Chris@49
|
208 const T1& x,
|
Chris@49
|
209 const T2& y
|
Chris@49
|
210 )
|
Chris@49
|
211 {
|
Chris@49
|
212 arma_extra_debug_sigprint();
|
Chris@49
|
213
|
Chris@49
|
214 const SpProxy<T1> pa(x);
|
Chris@49
|
215
|
Chris@49
|
216 Mat<typename T1::elem_type> result(-y);
|
Chris@49
|
217
|
Chris@49
|
218 arma_debug_assert_same_size( pa.get_n_rows(), pa.get_n_cols(), result.n_rows, result.n_cols, "subtraction" );
|
Chris@49
|
219
|
Chris@49
|
220 typename SpProxy<T1>::const_iterator_type it = pa.begin();
|
Chris@49
|
221 typename SpProxy<T1>::const_iterator_type it_end = pa.end();
|
Chris@49
|
222
|
Chris@49
|
223 while(it != it_end)
|
Chris@49
|
224 {
|
Chris@49
|
225 result.at(it.row(), it.col()) += (*it);
|
Chris@49
|
226 ++it;
|
Chris@49
|
227 }
|
Chris@49
|
228
|
Chris@49
|
229 return result;
|
Chris@49
|
230 }
|
Chris@49
|
231
|
Chris@49
|
232
|
Chris@49
|
233
|
Chris@49
|
234 //! subtraction of one dense and one sparse object
|
Chris@49
|
235 template<typename T1, typename T2>
|
Chris@49
|
236 inline
|
Chris@49
|
237 typename
|
Chris@49
|
238 enable_if2
|
Chris@49
|
239 <
|
Chris@49
|
240 (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
|
241 Mat<typename T1::elem_type>
|
Chris@49
|
242 >::result
|
Chris@49
|
243 operator-
|
Chris@49
|
244 (
|
Chris@49
|
245 const T1& x,
|
Chris@49
|
246 const T2& y
|
Chris@49
|
247 )
|
Chris@49
|
248 {
|
Chris@49
|
249 arma_extra_debug_sigprint();
|
Chris@49
|
250
|
Chris@49
|
251 Mat<typename T1::elem_type> result(x);
|
Chris@49
|
252
|
Chris@49
|
253 const SpProxy<T2> pb(y.get_ref());
|
Chris@49
|
254
|
Chris@49
|
255 arma_debug_assert_same_size( result.n_rows, result.n_cols, pb.get_n_rows(), pb.get_n_cols(), "subtraction" );
|
Chris@49
|
256
|
Chris@49
|
257 typename SpProxy<T2>::const_iterator_type it = pb.begin();
|
Chris@49
|
258 typename SpProxy<T2>::const_iterator_type it_end = pb.end();
|
Chris@49
|
259
|
Chris@49
|
260 while(it != it_end)
|
Chris@49
|
261 {
|
Chris@49
|
262 result.at(it.row(), it.col()) -= (*it);
|
Chris@49
|
263 ++it;
|
Chris@49
|
264 }
|
Chris@49
|
265
|
Chris@49
|
266 return result;
|
Chris@49
|
267 }
|
Chris@49
|
268
|
Chris@49
|
269
|
Chris@49
|
270
|
Chris@49
|
271 //! @}
|