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 op_sp_minus
20 //! @{
21 
22 
23 // scalar - SpBase
24 template<typename T1>
25 inline
26 void
apply(Mat<typename T1::elem_type> & out,const SpToDOp<T1,op_sp_minus_pre> & in)27 op_sp_minus_pre::apply(Mat<typename T1::elem_type>& out, const SpToDOp<T1,op_sp_minus_pre>& in)
28   {
29   arma_extra_debug_sigprint();
30 
31   // Note that T1 will be a sparse type, so we use SpProxy.
32   const SpProxy<T1> proxy(in.m);
33 
34   out.set_size(proxy.get_n_rows(), proxy.get_n_cols());
35   out.fill(in.aux);
36 
37   typename SpProxy<T1>::const_iterator_type it     = proxy.begin();
38   typename SpProxy<T1>::const_iterator_type it_end = proxy.end();
39 
40   for(; it != it_end; ++it)
41     {
42     out.at(it.row(), it.col()) -= (*it);
43     }
44   }
45 
46 
47 
48 // force apply into SpMat
49 template<typename T1>
50 inline
51 void
apply(SpMat<typename T1::elem_type> & out,const SpToDOp<T1,op_sp_minus_pre> & in)52 op_sp_minus_pre::apply(SpMat<typename T1::elem_type>& out, const SpToDOp<T1,op_sp_minus_pre>& in)
53   {
54   arma_extra_debug_sigprint();
55 
56   typedef typename T1::elem_type eT;
57 
58   // Note that T1 will be a sparse type, so we use SpProxy.
59   const SpProxy<T1> proxy(in.m);
60 
61   const uword n_rows = proxy.get_n_rows();
62   const uword n_cols = proxy.get_n_cols();
63 
64   out.set_size(n_rows, n_cols);
65 
66   const eT k = in.aux;
67 
68   for(uword c = 0; c < n_cols; ++c)
69   for(uword r = 0; r < n_rows; ++r)
70     {
71     out.at(r, c) = k - proxy.at(r, c);
72     }
73   }
74 
75 
76 
77 // used for the optimization of sparse % (scalar - sparse)
78 template<typename eT, typename T2, typename T3>
79 inline
80 void
apply_inside_schur(SpMat<eT> & out,const T2 & x,const SpToDOp<T3,op_sp_minus_pre> & y)81 op_sp_minus_pre::apply_inside_schur(SpMat<eT>& out, const T2& x, const SpToDOp<T3, op_sp_minus_pre>& y)
82   {
83   arma_extra_debug_sigprint();
84 
85   const SpProxy<T2> proxy2(x);
86   const SpProxy<T3> proxy3(y.m);
87 
88   arma_debug_assert_same_size(proxy2.get_n_rows(), proxy2.get_n_cols(), proxy3.get_n_rows(), proxy3.get_n_cols(), "element-wise multiplication");
89 
90   out.zeros(proxy2.get_n_rows(), proxy2.get_n_cols());
91 
92   typename SpProxy<T2>::const_iterator_type it     = proxy2.begin();
93   typename SpProxy<T2>::const_iterator_type it_end = proxy2.end();
94 
95   const eT k = y.aux;
96 
97   for(; it != it_end; ++it)
98     {
99     const uword it_row = it.row();
100     const uword it_col = it.col();
101 
102     out.at(it_row, it_col) = (*it) * (k - proxy3.at(it_row, it_col));
103     }
104   }
105 
106 
107 
108 // used for the optimization of sparse / (scalar - sparse)
109 template<typename eT, typename T2, typename T3>
110 inline
111 void
apply_inside_div(SpMat<eT> & out,const T2 & x,const SpToDOp<T3,op_sp_minus_pre> & y)112 op_sp_minus_pre::apply_inside_div(SpMat<eT>& out, const T2& x, const SpToDOp<T3, op_sp_minus_pre>& y)
113   {
114   arma_extra_debug_sigprint();
115 
116   const SpProxy<T2> proxy2(x);
117   const SpProxy<T3> proxy3(y.m);
118 
119   arma_debug_assert_same_size(proxy2.get_n_rows(), proxy2.get_n_cols(), proxy3.get_n_rows(), proxy3.get_n_cols(), "element-wise multiplication");
120 
121   out.zeros(proxy2.get_n_rows(), proxy2.get_n_cols());
122 
123   typename SpProxy<T2>::const_iterator_type it     = proxy2.begin();
124   typename SpProxy<T2>::const_iterator_type it_end = proxy2.end();
125 
126   const eT k = y.aux;
127 
128   for(; it != it_end; ++it)
129     {
130     const uword it_row = it.row();
131     const uword it_col = it.col();
132 
133     out.at(it_row, it_col) = (*it) / (k - proxy3.at(it_row, it_col));
134     }
135   }
136 
137 
138 
139 // SpBase - scalar
140 template<typename T1>
141 inline
142 void
apply(Mat<typename T1::elem_type> & out,const SpToDOp<T1,op_sp_minus_post> & in)143 op_sp_minus_post::apply(Mat<typename T1::elem_type>& out, const SpToDOp<T1,op_sp_minus_post>& in)
144   {
145   arma_extra_debug_sigprint();
146 
147   // Note that T1 will be a sparse type, so we use SpProxy.
148   const SpProxy<T1> proxy(in.m);
149 
150   out.set_size(proxy.get_n_rows(), proxy.get_n_cols());
151   out.fill(-in.aux);
152 
153   typename SpProxy<T1>::const_iterator_type it     = proxy.begin();
154   typename SpProxy<T1>::const_iterator_type it_end = proxy.end();
155 
156   for(; it != it_end; ++it)
157     {
158     out.at(it.row(), it.col()) += (*it);
159     }
160   }
161 
162 
163 
164 // force apply into sparse matrix
165 template<typename T1>
166 inline
167 void
apply(SpMat<typename T1::elem_type> & out,const SpToDOp<T1,op_sp_minus_post> & in)168 op_sp_minus_post::apply(SpMat<typename T1::elem_type>& out, const SpToDOp<T1,op_sp_minus_post>& in)
169   {
170   arma_extra_debug_sigprint();
171 
172   typedef typename T1::elem_type eT;
173 
174   // Note that T1 will be a sparse type, so we use SpProxy.
175   const SpProxy<T1> proxy(in.m);
176 
177   const uword n_rows = proxy.get_n_rows();
178   const uword n_cols = proxy.get_n_cols();
179 
180   out.set_size(n_rows, n_cols);
181 
182   const eT k = in.aux;
183 
184   for(uword c = 0; c < n_cols; ++c)
185   for(uword r = 0; r < n_rows; ++r)
186     {
187     out.at(r, c) = proxy.at(r, c) - k;
188     }
189   }
190 
191 
192 
193 // used for the optimization of sparse % (sparse - scalar)
194 template<typename eT, typename T2, typename T3>
195 inline
196 void
apply_inside_schur(SpMat<eT> & out,const T2 & x,const SpToDOp<T3,op_sp_minus_post> & y)197 op_sp_minus_post::apply_inside_schur(SpMat<eT>& out, const T2& x, const SpToDOp<T3, op_sp_minus_post>& y)
198   {
199   arma_extra_debug_sigprint();
200 
201   const SpProxy<T2> proxy2(x);
202   const SpProxy<T3> proxy3(y.m);
203 
204   arma_debug_assert_same_size(proxy2.get_n_rows(), proxy2.get_n_cols(), proxy3.get_n_rows(), proxy3.get_n_cols(), "element-wise multiplication");
205 
206   out.zeros(proxy2.get_n_rows(), proxy2.get_n_cols());
207 
208   typename SpProxy<T2>::const_iterator_type it     = proxy2.begin();
209   typename SpProxy<T2>::const_iterator_type it_end = proxy2.end();
210 
211   const eT k = y.aux;
212 
213   for(; it != it_end; ++it)
214     {
215     const uword it_row = it.row();
216     const uword it_col = it.col();
217 
218     out.at(it_row, it_col) = (*it) * (proxy3.at(it_row, it_col) - k);
219     }
220   }
221 
222 
223 
224 // used for the optimization of sparse / (sparse - scalar)
225 template<typename eT, typename T2, typename T3>
226 inline
227 void
apply_inside_div(SpMat<eT> & out,const T2 & x,const SpToDOp<T3,op_sp_minus_post> & y)228 op_sp_minus_post::apply_inside_div(SpMat<eT>& out, const T2& x, const SpToDOp<T3, op_sp_minus_post>& y)
229   {
230   arma_extra_debug_sigprint();
231 
232   const SpProxy<T2> proxy2(x);
233   const SpProxy<T3> proxy3(y.m);
234 
235   arma_debug_assert_same_size(proxy2.get_n_rows(), proxy2.get_n_cols(), proxy3.get_n_rows(), proxy3.get_n_cols(), "element-wise multiplication");
236 
237   out.zeros(proxy2.get_n_rows(), proxy2.get_n_cols());
238 
239   typename SpProxy<T2>::const_iterator_type it     = proxy2.begin();
240   typename SpProxy<T2>::const_iterator_type it_end = proxy2.end();
241 
242   const eT k = y.aux;
243 
244   for(; it != it_end; ++it)
245     {
246     const uword it_row = it.row();
247     const uword it_col = it.col();
248 
249     out.at(it_row, it_col) = (*it) / (proxy3.at(it_row, it_col) - k);
250     }
251   }
252 
253 
254 
255 //! @}
256