3
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
3
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
3
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
3
|
20 |
|
21 */ |
|
22 |
2828
|
23 #if !defined (octave_mx_inlines_h) |
|
24 #define octave_mx_inlines_h 1 |
2804
|
25 |
|
26 #include <cstddef> |
|
27 |
5525
|
28 #include "quit.h" |
|
29 |
1650
|
30 #include "oct-cmplx.h" |
461
|
31 |
2811
|
32 #define VS_OP_FCN(F, OP) \ |
|
33 template <class R, class V, class S> \ |
3262
|
34 inline void \ |
2811
|
35 F ## _vs (R *r, const V *v, size_t n, S s) \ |
|
36 { \ |
|
37 for (size_t i = 0; i < n; i++) \ |
|
38 r[i] = v[i] OP s; \ |
|
39 } |
|
40 |
3769
|
41 VS_OP_FCN (mx_inline_add, +) |
|
42 VS_OP_FCN (mx_inline_subtract, -) |
|
43 VS_OP_FCN (mx_inline_multiply, *) |
|
44 VS_OP_FCN (mx_inline_divide, /) |
3
|
45 |
2804
|
46 #define VS_OP(F, OP, R, V, S) \ |
|
47 static inline R * \ |
|
48 F (const V *v, size_t n, S s) \ |
|
49 { \ |
|
50 R *r = 0; \ |
|
51 if (n > 0) \ |
|
52 { \ |
|
53 r = new R [n]; \ |
2811
|
54 F ## _vs (r, v, n, s); \ |
2804
|
55 } \ |
|
56 return r; \ |
|
57 } |
3
|
58 |
2804
|
59 #define VS_OPS(R, V, S) \ |
3769
|
60 VS_OP (mx_inline_add, +, R, V, S) \ |
|
61 VS_OP (mx_inline_subtract, -, R, V, S) \ |
|
62 VS_OP (mx_inline_multiply, *, R, V, S) \ |
|
63 VS_OP (mx_inline_divide, /, R, V, S) |
3
|
64 |
2804
|
65 VS_OPS (double, double, double) |
|
66 VS_OPS (Complex, double, Complex) |
|
67 VS_OPS (Complex, Complex, double) |
|
68 VS_OPS (Complex, Complex, Complex) |
3
|
69 |
2811
|
70 #define SV_OP_FCN(F, OP) \ |
|
71 template <class R, class S, class V> \ |
3262
|
72 inline void \ |
2811
|
73 F ## _sv (R *r, S s, const V *v, size_t n) \ |
|
74 { \ |
|
75 for (size_t i = 0; i < n; i++) \ |
|
76 r[i] = s OP v[i]; \ |
|
77 } \ |
|
78 |
3769
|
79 SV_OP_FCN (mx_inline_add, +) |
|
80 SV_OP_FCN (mx_inline_subtract, -) |
|
81 SV_OP_FCN (mx_inline_multiply, *) |
|
82 SV_OP_FCN (mx_inline_divide, /) |
2811
|
83 |
2804
|
84 #define SV_OP(F, OP, R, S, V) \ |
|
85 static inline R * \ |
|
86 F (S s, const V *v, size_t n) \ |
|
87 { \ |
|
88 R *r = 0; \ |
|
89 if (n > 0) \ |
|
90 { \ |
|
91 r = new R [n]; \ |
2811
|
92 F ## _sv (r, s, v, n); \ |
2804
|
93 } \ |
|
94 return r; \ |
|
95 } |
3
|
96 |
2804
|
97 #define SV_OPS(R, S, V) \ |
3769
|
98 SV_OP (mx_inline_add, +, R, S, V) \ |
|
99 SV_OP (mx_inline_subtract, -, R, S, V) \ |
|
100 SV_OP (mx_inline_multiply, *, R, S, V) \ |
|
101 SV_OP (mx_inline_divide, /, R, S, V) |
3
|
102 |
2804
|
103 SV_OPS (double, double, double) |
|
104 SV_OPS (Complex, double, Complex) |
|
105 SV_OPS (Complex, Complex, double) |
|
106 SV_OPS (Complex, Complex, Complex) |
3
|
107 |
2811
|
108 #define VV_OP_FCN(F, OP) \ |
|
109 template <class R, class T1, class T2> \ |
3262
|
110 inline void \ |
2811
|
111 F ## _vv (R *r, const T1 *v1, const T2 *v2, size_t n) \ |
|
112 { \ |
|
113 for (size_t i = 0; i < n; i++) \ |
|
114 r[i] = v1[i] OP v2[i]; \ |
|
115 } \ |
|
116 |
3769
|
117 VV_OP_FCN (mx_inline_add, +) |
|
118 VV_OP_FCN (mx_inline_subtract, -) |
|
119 VV_OP_FCN (mx_inline_multiply, *) |
|
120 VV_OP_FCN (mx_inline_divide, /) |
2811
|
121 |
2804
|
122 #define VV_OP(F, OP, R, T1, T2) \ |
|
123 static inline R * \ |
|
124 F (const T1 *v1, const T2 *v2, size_t n) \ |
|
125 { \ |
|
126 R *r = 0; \ |
|
127 if (n > 0) \ |
|
128 { \ |
|
129 r = new R [n]; \ |
2811
|
130 F ## _vv (r, v1, v2, n); \ |
2804
|
131 } \ |
|
132 return r; \ |
|
133 } |
3
|
134 |
2804
|
135 #define VV_OPS(R, T1, T2) \ |
3769
|
136 VV_OP (mx_inline_add, +, R, T1, T2) \ |
|
137 VV_OP (mx_inline_subtract, -, R, T1, T2) \ |
|
138 VV_OP (mx_inline_multiply, *, R, T1, T2) \ |
|
139 VV_OP (mx_inline_divide, /, R, T1, T2) |
3
|
140 |
2804
|
141 VV_OPS (double, double, double) |
|
142 VV_OPS (Complex, double, Complex) |
|
143 VV_OPS (Complex, Complex, double) |
|
144 VV_OPS (Complex, Complex, Complex) |
3
|
145 |
2804
|
146 #define VS_OP2(F, OP, V, S) \ |
|
147 static inline V * \ |
|
148 F (V *v, size_t n, S s) \ |
|
149 { \ |
|
150 for (size_t i = 0; i < n; i++) \ |
|
151 v[i] OP s; \ |
|
152 return v; \ |
|
153 } |
3
|
154 |
2804
|
155 #define VS_OP2S(V, S) \ |
3769
|
156 VS_OP2 (mx_inline_add2, +=, V, S) \ |
|
157 VS_OP2 (mx_inline_subtract2, -=, V, S) \ |
|
158 VS_OP2 (mx_inline_multiply2, *=, V, S) \ |
|
159 VS_OP2 (mx_inline_divide2, /=, V, S) \ |
|
160 VS_OP2 (mx_inline_copy, =, V, S) |
3
|
161 |
2804
|
162 VS_OP2S (double, double) |
|
163 VS_OP2S (Complex, double) |
|
164 VS_OP2S (Complex, Complex) |
3
|
165 |
2804
|
166 #define VV_OP2(F, OP, T1, T2) \ |
|
167 static inline T1 * \ |
|
168 F (T1 *v1, const T2 *v2, size_t n) \ |
|
169 { \ |
|
170 for (size_t i = 0; i < n; i++) \ |
|
171 v1[i] OP v2[i]; \ |
|
172 return v1; \ |
|
173 } |
3
|
174 |
2804
|
175 #define VV_OP2S(T1, T2) \ |
3769
|
176 VV_OP2 (mx_inline_add2, +=, T1, T2) \ |
|
177 VV_OP2 (mx_inline_subtract2, -=, T1, T2) \ |
|
178 VV_OP2 (mx_inline_multiply2, *=, T1, T2) \ |
|
179 VV_OP2 (mx_inline_divide2, /=, T1, T2) \ |
|
180 VV_OP2 (mx_inline_copy, =, T1, T2) |
3
|
181 |
2804
|
182 VV_OP2S (double, double) |
|
183 VV_OP2S (Complex, double) |
|
184 VV_OP2S (Complex, Complex) |
3
|
185 |
2804
|
186 #define OP_EQ_FCN(T1, T2) \ |
|
187 static inline bool \ |
3769
|
188 mx_inline_equal (const T1 *x, const T2 *y, size_t n) \ |
2804
|
189 { \ |
|
190 for (size_t i = 0; i < n; i++) \ |
|
191 if (x[i] != y[i]) \ |
|
192 return false; \ |
|
193 return true; \ |
|
194 } |
3
|
195 |
2828
|
196 OP_EQ_FCN (bool, bool) |
2804
|
197 OP_EQ_FCN (char, char) |
|
198 OP_EQ_FCN (double, double) |
|
199 OP_EQ_FCN (Complex, Complex) |
3
|
200 |
2804
|
201 #define OP_DUP_FCN(OP, F, R, T) \ |
|
202 static inline R * \ |
|
203 F (const T *x, size_t n) \ |
|
204 { \ |
|
205 R *r = 0; \ |
|
206 if (n > 0) \ |
|
207 { \ |
|
208 r = new R [n]; \ |
|
209 for (size_t i = 0; i < n; i++) \ |
|
210 r[i] = OP (x[i]); \ |
|
211 } \ |
|
212 return r; \ |
|
213 } |
3
|
214 |
3769
|
215 OP_DUP_FCN (, mx_inline_dup, double, double) |
|
216 OP_DUP_FCN (, mx_inline_dup, Complex, Complex) |
3
|
217 |
2804
|
218 // These should really return a bool *. Also, they should probably be |
|
219 // in with a collection of other element-by-element boolean ops. |
3769
|
220 OP_DUP_FCN (0.0 ==, mx_inline_not, double, double) |
|
221 OP_DUP_FCN (0.0 ==, mx_inline_not, double, Complex) |
3
|
222 |
3769
|
223 OP_DUP_FCN (, mx_inline_make_complex, Complex, double) |
2804
|
224 |
3769
|
225 OP_DUP_FCN (-, mx_inline_change_sign, double, double) |
|
226 OP_DUP_FCN (-, mx_inline_change_sign, Complex, Complex) |
3
|
227 |
3769
|
228 OP_DUP_FCN (real, mx_inline_real_dup, double, Complex) |
|
229 OP_DUP_FCN (imag, mx_inline_imag_dup, double, Complex) |
|
230 OP_DUP_FCN (conj, mx_inline_conj_dup, Complex, Complex) |
3
|
231 |
3864
|
232 // Avoid some code duplication. Maybe we should use templates. |
|
233 |
4015
|
234 #define MX_CUMULATIVE_OP(RET_TYPE, ELT_TYPE, OP) \ |
3864
|
235 \ |
5275
|
236 octave_idx_type nr = rows (); \ |
|
237 octave_idx_type nc = cols (); \ |
3864
|
238 \ |
|
239 RET_TYPE retval (nr, nc); \ |
|
240 \ |
|
241 if (nr > 0 && nc > 0) \ |
|
242 { \ |
|
243 if ((nr == 1 && dim == -1) || dim == 1) \ |
|
244 { \ |
5275
|
245 for (octave_idx_type i = 0; i < nr; i++) \ |
3864
|
246 { \ |
|
247 ELT_TYPE t = elem (i, 0); \ |
5275
|
248 for (octave_idx_type j = 0; j < nc; j++) \ |
3864
|
249 { \ |
|
250 retval.elem (i, j) = t; \ |
|
251 if (j < nc - 1) \ |
|
252 t OP elem (i, j+1); \ |
|
253 } \ |
|
254 } \ |
|
255 } \ |
|
256 else \ |
|
257 { \ |
5275
|
258 for (octave_idx_type j = 0; j < nc; j++) \ |
3864
|
259 { \ |
|
260 ELT_TYPE t = elem (0, j); \ |
5275
|
261 for (octave_idx_type i = 0; i < nr; i++) \ |
3864
|
262 { \ |
|
263 retval.elem (i, j) = t; \ |
|
264 if (i < nr - 1) \ |
|
265 t OP elem (i+1, j); \ |
|
266 } \ |
|
267 } \ |
|
268 } \ |
|
269 } \ |
|
270 \ |
|
271 return retval |
|
272 |
|
273 #define MX_BASE_REDUCTION_OP(RET_TYPE, ROW_EXPR, COL_EXPR, INIT_VAL, \ |
|
274 MT_RESULT) \ |
|
275 \ |
5275
|
276 octave_idx_type nr = rows (); \ |
|
277 octave_idx_type nc = cols (); \ |
3864
|
278 \ |
|
279 RET_TYPE retval; \ |
|
280 \ |
|
281 if (nr > 0 && nc > 0) \ |
|
282 { \ |
|
283 if ((nr == 1 && dim == -1) || dim == 1) \ |
|
284 { \ |
|
285 retval.resize (nr, 1); \ |
5275
|
286 for (octave_idx_type i = 0; i < nr; i++) \ |
3864
|
287 { \ |
|
288 retval.elem (i, 0) = INIT_VAL; \ |
5275
|
289 for (octave_idx_type j = 0; j < nc; j++) \ |
3864
|
290 { \ |
|
291 ROW_EXPR; \ |
|
292 } \ |
|
293 } \ |
|
294 } \ |
|
295 else \ |
|
296 { \ |
|
297 retval.resize (1, nc); \ |
5275
|
298 for (octave_idx_type j = 0; j < nc; j++) \ |
3864
|
299 { \ |
|
300 retval.elem (0, j) = INIT_VAL; \ |
5275
|
301 for (octave_idx_type i = 0; i < nr; i++) \ |
3864
|
302 { \ |
|
303 COL_EXPR; \ |
|
304 } \ |
|
305 } \ |
|
306 } \ |
|
307 } \ |
4139
|
308 else if (nc == 0 && (nr == 0 || (nr == 1 && dim == -1))) \ |
|
309 retval.resize (1, 1, MT_RESULT); \ |
4015
|
310 else if (nr == 0 && (dim == 0 || dim == -1)) \ |
|
311 retval.resize (1, nc, MT_RESULT); \ |
|
312 else if (nc == 0 && dim == 1) \ |
|
313 retval.resize (nr, 1, MT_RESULT); \ |
|
314 else \ |
4139
|
315 retval.resize (nr > 0, nc > 0); \ |
3864
|
316 \ |
|
317 return retval |
|
318 |
|
319 #define MX_REDUCTION_OP_ROW_EXPR(OP) \ |
|
320 retval.elem (i, 0) OP elem (i, j) |
|
321 |
|
322 #define MX_REDUCTION_OP_COL_EXPR(OP) \ |
|
323 retval.elem (0, j) OP elem (i, j) |
|
324 |
|
325 #define MX_REDUCTION_OP(RET_TYPE, OP, INIT_VAL, MT_RESULT) \ |
|
326 MX_BASE_REDUCTION_OP (RET_TYPE, \ |
|
327 MX_REDUCTION_OP_ROW_EXPR (OP), \ |
|
328 MX_REDUCTION_OP_COL_EXPR (OP), \ |
|
329 INIT_VAL, MT_RESULT) |
4015
|
330 |
|
331 #define MX_ANY_ALL_OP_ROW_CODE(TEST_OP, TEST_TRUE_VAL) \ |
|
332 if (elem (i, j) TEST_OP 0.0) \ |
|
333 { \ |
|
334 retval.elem (i, 0) = TEST_TRUE_VAL; \ |
|
335 break; \ |
|
336 } |
|
337 |
|
338 #define MX_ANY_ALL_OP_COL_CODE(TEST_OP, TEST_TRUE_VAL) \ |
|
339 if (elem (i, j) TEST_OP 0.0) \ |
|
340 { \ |
|
341 retval.elem (0, j) = TEST_TRUE_VAL; \ |
|
342 break; \ |
|
343 } |
|
344 |
|
345 #define MX_ANY_ALL_OP(DIM, INIT_VAL, TEST_OP, TEST_TRUE_VAL) \ |
|
346 MX_BASE_REDUCTION_OP (boolMatrix, \ |
|
347 MX_ANY_ALL_OP_ROW_CODE (TEST_OP, TEST_TRUE_VAL), \ |
|
348 MX_ANY_ALL_OP_COL_CODE (TEST_OP, TEST_TRUE_VAL), \ |
|
349 INIT_VAL, INIT_VAL) |
|
350 |
|
351 #define MX_ALL_OP(DIM) MX_ANY_ALL_OP (DIM, true, ==, false) |
|
352 |
|
353 #define MX_ANY_OP(DIM) MX_ANY_ALL_OP (DIM, false, !=, true) |
|
354 |
4563
|
355 #define MX_ND_ALL_EXPR elem (iter_idx) == 0 |
4556
|
356 |
4563
|
357 #define MX_ND_ANY_EXPR elem (iter_idx) != 0 |
4556
|
358 |
|
359 #define MX_ND_ALL_EVAL(TEST_EXPR) \ |
5520
|
360 if (retval(result_idx) && (TEST_EXPR)) \ |
|
361 retval(result_idx) = 0; |
4556
|
362 |
|
363 #define MX_ND_ANY_EVAL(TEST_EXPR) \ |
5520
|
364 if (retval(result_idx) || (TEST_EXPR)) \ |
|
365 retval(result_idx) = 1; |
4569
|
366 |
5520
|
367 #define MX_ND_REDUCTION(EVAL_EXPR, INIT_VAL, RET_TYPE) \ |
4556
|
368 \ |
4569
|
369 RET_TYPE retval; \ |
4556
|
370 \ |
4932
|
371 dim_vector dv = this->dims (); \ |
5520
|
372 int nd = this->ndims (); \ |
4563
|
373 \ |
5955
|
374 int empty = false; \ |
4563
|
375 \ |
5520
|
376 for (int i = 0; i < nd; i++) \ |
4563
|
377 { \ |
5955
|
378 if (dv(i) == 0) \ |
4563
|
379 { \ |
5955
|
380 empty = true; \ |
4563
|
381 break; \ |
|
382 } \ |
|
383 } \ |
|
384 \ |
5972
|
385 if (nd == 2 && dv(0) == 0 && dv(1) == 0) \ |
|
386 { \ |
|
387 retval.resize (dim_vector (1, 1), INIT_VAL); \ |
|
388 return retval; \ |
|
389 } \ |
|
390 \ |
5520
|
391 /* We need to find first non-singleton dim. */ \ |
|
392 \ |
|
393 if (dim == -1) \ |
4556
|
394 { \ |
5520
|
395 dim = 0; \ |
|
396 \ |
|
397 for (int i = 0; i < nd; i++) \ |
4563
|
398 { \ |
5520
|
399 if (dv(i) != 1) \ |
4563
|
400 { \ |
|
401 dim = i; \ |
|
402 break; \ |
|
403 } \ |
|
404 } \ |
|
405 } \ |
5520
|
406 else if (dim >= nd) \ |
4563
|
407 { \ |
5520
|
408 dim = nd++; \ |
|
409 dv.resize (nd, 1); \ |
4563
|
410 } \ |
|
411 \ |
5523
|
412 /* R = op (A, DIM) \ |
4563
|
413 \ |
5523
|
414 The strategy here is to access the elements of A along the \ |
|
415 dimension specified by DIM. This means that we loop over each \ |
5615
|
416 element of R and adjust the index into A as needed. Store the \ |
|
417 cummulative product of all dimensions of A in CP_SZ. The last \ |
|
418 element of CP_SZ is the total number of elements of A. */ \ |
4563
|
419 \ |
5615
|
420 Array<octave_idx_type> cp_sz (nd+1, 1); \ |
|
421 for (int i = 1; i <= nd; i++) \ |
5523
|
422 cp_sz(i) = cp_sz(i-1)*dv(i-1); \ |
5520
|
423 \ |
5523
|
424 octave_idx_type reset_at = cp_sz(dim); \ |
|
425 octave_idx_type base_incr = cp_sz(dim+1); \ |
|
426 octave_idx_type incr = cp_sz(dim); \ |
|
427 octave_idx_type base = 0; \ |
|
428 octave_idx_type next_base = base + base_incr; \ |
|
429 octave_idx_type iter_idx = base; \ |
|
430 octave_idx_type n_elts = dv(dim); \ |
4556
|
431 \ |
5520
|
432 dv(dim) = 1; \ |
4556
|
433 \ |
5520
|
434 retval.resize (dv, INIT_VAL); \ |
4556
|
435 \ |
5955
|
436 if (! empty) \ |
4556
|
437 { \ |
5955
|
438 octave_idx_type nel = dv.numel (); \ |
5523
|
439 \ |
5955
|
440 octave_idx_type k = 1; \ |
|
441 \ |
|
442 for (octave_idx_type result_idx = 0; result_idx < nel; result_idx++) \ |
5523
|
443 { \ |
5955
|
444 OCTAVE_QUIT; \ |
5523
|
445 \ |
5955
|
446 for (octave_idx_type j = 0; j < n_elts; j++) \ |
|
447 { \ |
|
448 OCTAVE_QUIT; \ |
|
449 \ |
|
450 EVAL_EXPR; \ |
5520
|
451 \ |
5955
|
452 iter_idx += incr; \ |
|
453 } \ |
5523
|
454 \ |
5955
|
455 if (k == reset_at) \ |
|
456 { \ |
|
457 base = next_base; \ |
|
458 next_base += base_incr; \ |
|
459 iter_idx = base; \ |
|
460 k = 1; \ |
|
461 } \ |
|
462 else \ |
|
463 { \ |
|
464 base++; \ |
|
465 iter_idx = base; \ |
|
466 k++; \ |
|
467 } \ |
|
468 } \ |
4556
|
469 } \ |
|
470 \ |
4871
|
471 retval.chop_trailing_singletons (); \ |
|
472 \ |
4556
|
473 return retval |
4569
|
474 |
|
475 #define MX_ND_REAL_OP_REDUCTION(ASN_EXPR, INIT_VAL) \ |
5520
|
476 MX_ND_REDUCTION (retval(result_idx) ASN_EXPR, INIT_VAL, NDArray) |
4569
|
477 |
|
478 #define MX_ND_COMPLEX_OP_REDUCTION(ASN_EXPR, INIT_VAL) \ |
5520
|
479 MX_ND_REDUCTION (retval(result_idx) ASN_EXPR, INIT_VAL, ComplexNDArray) |
4569
|
480 |
|
481 #define MX_ND_ANY_ALL_REDUCTION(EVAL_EXPR, VAL) \ |
5520
|
482 MX_ND_REDUCTION (EVAL_EXPR, VAL, boolNDArray) |
4556
|
483 |
5523
|
484 #define MX_ND_CUMULATIVE_OP(RET_TYPE, ACC_TYPE, INIT_VAL, OP) \ |
|
485 \ |
4584
|
486 RET_TYPE retval; \ |
|
487 \ |
4932
|
488 dim_vector dv = this->dims (); \ |
5523
|
489 int nd = this->ndims (); \ |
4584
|
490 \ |
5955
|
491 bool empty = false; \ |
4584
|
492 \ |
5523
|
493 for (int i = 0; i < nd; i++) \ |
4584
|
494 { \ |
5955
|
495 if (dv(i) == 0) \ |
4584
|
496 { \ |
5955
|
497 empty = true; \ |
4584
|
498 break; \ |
|
499 } \ |
|
500 } \ |
|
501 \ |
5523
|
502 /* We need to find first non-singleton dim. */ \ |
|
503 \ |
4584
|
504 if (dim == -1) \ |
|
505 { \ |
5523
|
506 dim = 0; \ |
|
507 \ |
|
508 for (int i = 0; i < nd; i++) \ |
4584
|
509 { \ |
5523
|
510 if (dv(i) != 1) \ |
4584
|
511 { \ |
|
512 dim = i; \ |
|
513 break; \ |
|
514 } \ |
|
515 } \ |
|
516 } \ |
5523
|
517 else if (dim >= nd) \ |
4584
|
518 { \ |
5523
|
519 dim = nd++; \ |
|
520 dv.resize (nd, 1); \ |
4584
|
521 } \ |
|
522 \ |
5523
|
523 /* R = op (A, DIM) \ |
4584
|
524 \ |
5523
|
525 The strategy here is to access the elements of A along the \ |
|
526 dimension specified by DIM. This means that we loop over each \ |
5611
|
527 element of R and adjust the index into A as needed. Store the \ |
5614
|
528 cummulative product of all dimensions of A in CP_SZ. The last \ |
|
529 element of CP_SZ is the total number of elements of A. */ \ |
4584
|
530 \ |
5611
|
531 Array<octave_idx_type> cp_sz (nd+1, 1); \ |
|
532 for (int i = 1; i <= nd; i++) \ |
5523
|
533 cp_sz(i) = cp_sz(i-1)*dv(i-1); \ |
4584
|
534 \ |
5523
|
535 octave_idx_type reset_at = cp_sz(dim); \ |
|
536 octave_idx_type base_incr = cp_sz(dim+1); \ |
|
537 octave_idx_type incr = cp_sz(dim); \ |
|
538 octave_idx_type base = 0; \ |
|
539 octave_idx_type next_base = base + base_incr; \ |
|
540 octave_idx_type iter_idx = base; \ |
|
541 octave_idx_type n_elts = dv(dim); \ |
4584
|
542 \ |
5523
|
543 retval.resize (dv, INIT_VAL); \ |
|
544 \ |
5955
|
545 if (! empty) \ |
|
546 { \ |
|
547 octave_idx_type nel = dv.numel () / n_elts; \ |
5523
|
548 \ |
5955
|
549 octave_idx_type k = 1; \ |
4584
|
550 \ |
5955
|
551 for (octave_idx_type i = 0; i < nel; i++) \ |
|
552 { \ |
|
553 OCTAVE_QUIT; \ |
|
554 \ |
|
555 ACC_TYPE prev_val = INIT_VAL; \ |
|
556 \ |
|
557 for (octave_idx_type j = 0; j < n_elts; j++) \ |
|
558 { \ |
|
559 OCTAVE_QUIT; \ |
5523
|
560 \ |
5955
|
561 if (j == 0) \ |
|
562 { \ |
|
563 retval(iter_idx) = elem (iter_idx); \ |
|
564 prev_val = retval(iter_idx); \ |
|
565 } \ |
|
566 else \ |
|
567 { \ |
|
568 prev_val = prev_val OP elem (iter_idx); \ |
|
569 retval(iter_idx) = prev_val; \ |
|
570 } \ |
5523
|
571 \ |
5955
|
572 iter_idx += incr; \ |
|
573 } \ |
4584
|
574 \ |
5955
|
575 if (k == reset_at) \ |
5523
|
576 { \ |
5955
|
577 base = next_base; \ |
|
578 next_base += base_incr; \ |
|
579 iter_idx = base; \ |
|
580 k = 1; \ |
4584
|
581 } \ |
|
582 else \ |
5523
|
583 { \ |
5955
|
584 base++; \ |
|
585 iter_idx = base; \ |
|
586 k++; \ |
|
587 } \ |
4584
|
588 } \ |
5523
|
589 } \ |
4584
|
590 \ |
5523
|
591 retval.chop_trailing_singletons (); \ |
|
592 \ |
4584
|
593 return retval |
|
594 |
2804
|
595 #endif |
3
|
596 |
|
597 /* |
|
598 ;;; Local Variables: *** |
|
599 ;;; mode: C++ *** |
|
600 ;;; End: *** |
|
601 */ |