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