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