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 GenCube
20 //! @{
21 
22 
23 
24 template<typename eT, typename gen_type>
25 arma_inline
GenCube(const uword in_n_rows,const uword in_n_cols,const uword in_n_slices)26 GenCube<eT, gen_type>::GenCube(const uword in_n_rows, const uword in_n_cols, const uword in_n_slices)
27   : n_rows  (in_n_rows  )
28   , n_cols  (in_n_cols  )
29   , n_slices(in_n_slices)
30   {
31   arma_extra_debug_sigprint();
32   }
33 
34 
35 
36 template<typename eT, typename gen_type>
37 arma_inline
~GenCube()38 GenCube<eT, gen_type>::~GenCube()
39   {
40   arma_extra_debug_sigprint();
41   }
42 
43 
44 
45 template<typename eT, typename gen_type>
46 arma_inline
47 eT
operator [](const uword) const48 GenCube<eT, gen_type>::operator[](const uword) const
49   {
50   return (*this).generate();
51   }
52 
53 
54 
55 template<typename eT, typename gen_type>
56 arma_inline
57 eT
at(const uword,const uword,const uword) const58 GenCube<eT, gen_type>::at(const uword, const uword, const uword) const
59   {
60   return (*this).generate();
61   }
62 
63 
64 
65 template<typename eT, typename gen_type>
66 arma_inline
67 eT
at_alt(const uword) const68 GenCube<eT, gen_type>::at_alt(const uword) const
69   {
70   return (*this).generate();
71   }
72 
73 
74 
75 template<typename eT, typename gen_type>
76 inline
77 void
apply(Cube<eT> & out) const78 GenCube<eT, gen_type>::apply(Cube<eT>& out) const
79   {
80   arma_extra_debug_sigprint();
81 
82   // NOTE: we're assuming that the cube has already been set to the correct size;
83   // this is done by either the Cube contructor or operator=()
84 
85        if(is_same_type<gen_type, gen_ones >::yes) { out.ones();  }
86   else if(is_same_type<gen_type, gen_zeros>::yes) { out.zeros(); }
87   else if(is_same_type<gen_type, gen_randu>::yes) { out.randu(); }
88   else if(is_same_type<gen_type, gen_randn>::yes) { out.randn(); }
89   }
90 
91 
92 
93 template<typename eT, typename gen_type>
94 inline
95 void
apply_inplace_plus(Cube<eT> & out) const96 GenCube<eT, gen_type>::apply_inplace_plus(Cube<eT>& out) const
97   {
98   arma_extra_debug_sigprint();
99 
100   arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "addition");
101 
102 
103         eT*   out_mem = out.memptr();
104   const uword n_elem  = out.n_elem;
105 
106   uword i,j;
107 
108   for(i=0, j=1; j<n_elem; i+=2, j+=2)
109     {
110     const eT tmp_i = (*this).generate();
111     const eT tmp_j = (*this).generate();
112 
113     out_mem[i] += tmp_i;
114     out_mem[j] += tmp_j;
115     }
116 
117   if(i < n_elem)
118     {
119     out_mem[i] += (*this).generate();
120     }
121   }
122 
123 
124 
125 
126 template<typename eT, typename gen_type>
127 inline
128 void
apply_inplace_minus(Cube<eT> & out) const129 GenCube<eT, gen_type>::apply_inplace_minus(Cube<eT>& out) const
130   {
131   arma_extra_debug_sigprint();
132 
133   arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "subtraction");
134 
135 
136         eT*   out_mem = out.memptr();
137   const uword n_elem  = out.n_elem;
138 
139   uword i,j;
140 
141   for(i=0, j=1; j<n_elem; i+=2, j+=2)
142     {
143     const eT tmp_i = (*this).generate();
144     const eT tmp_j = (*this).generate();
145 
146     out_mem[i] -= tmp_i;
147     out_mem[j] -= tmp_j;
148     }
149 
150   if(i < n_elem)
151     {
152     out_mem[i] -= (*this).generate();
153     }
154   }
155 
156 
157 
158 
159 template<typename eT, typename gen_type>
160 inline
161 void
apply_inplace_schur(Cube<eT> & out) const162 GenCube<eT, gen_type>::apply_inplace_schur(Cube<eT>& out) const
163   {
164   arma_extra_debug_sigprint();
165 
166   arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "element-wise multiplication");
167 
168 
169         eT*   out_mem = out.memptr();
170   const uword n_elem  = out.n_elem;
171 
172   uword i,j;
173 
174   for(i=0, j=1; j<n_elem; i+=2, j+=2)
175     {
176     const eT tmp_i = (*this).generate();
177     const eT tmp_j = (*this).generate();
178 
179     out_mem[i] *= tmp_i;
180     out_mem[j] *= tmp_j;
181     }
182 
183   if(i < n_elem)
184     {
185     out_mem[i] *= (*this).generate();
186     }
187   }
188 
189 
190 
191 
192 template<typename eT, typename gen_type>
193 inline
194 void
apply_inplace_div(Cube<eT> & out) const195 GenCube<eT, gen_type>::apply_inplace_div(Cube<eT>& out) const
196   {
197   arma_extra_debug_sigprint();
198 
199   arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "element-wise division");
200 
201 
202         eT*   out_mem = out.memptr();
203   const uword n_elem  = out.n_elem;
204 
205   uword i,j;
206 
207   for(i=0, j=1; j<n_elem; i+=2, j+=2)
208     {
209     const eT tmp_i = (*this).generate();
210     const eT tmp_j = (*this).generate();
211 
212     out_mem[i] /= tmp_i;
213     out_mem[j] /= tmp_j;
214     }
215 
216   if(i < n_elem)
217     {
218     out_mem[i] /= (*this).generate();
219     }
220   }
221 
222 
223 
224 template<typename eT, typename gen_type>
225 inline
226 void
apply(subview_cube<eT> & out) const227 GenCube<eT, gen_type>::apply(subview_cube<eT>& out) const
228   {
229   arma_extra_debug_sigprint();
230 
231   // NOTE: we're assuming that the subcube has the same dimensions as the GenCube object
232   // this is checked by subview_cube::operator=()
233 
234        if(is_same_type<gen_type, gen_ones >::yes) { out.ones();  }
235   else if(is_same_type<gen_type, gen_zeros>::yes) { out.zeros(); }
236   else if(is_same_type<gen_type, gen_randu>::yes) { out.randu(); }
237   else if(is_same_type<gen_type, gen_randn>::yes) { out.randn(); }
238   }
239 
240 
241 
242 //! @}
243