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 \ |
|
234 int nr = rows (); \ |
|
235 int nc = cols (); \ |
|
236 \ |
|
237 RET_TYPE retval (nr, nc); \ |
|
238 \ |
|
239 if (nr > 0 && nc > 0) \ |
|
240 { \ |
|
241 if ((nr == 1 && dim == -1) || dim == 1) \ |
|
242 { \ |
|
243 for (int i = 0; i < nr; i++) \ |
|
244 { \ |
|
245 ELT_TYPE t = elem (i, 0); \ |
|
246 for (int j = 0; j < nc; j++) \ |
|
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 { \ |
|
256 for (int j = 0; j < nc; j++) \ |
|
257 { \ |
|
258 ELT_TYPE t = elem (0, j); \ |
|
259 for (int i = 0; i < nr; i++) \ |
|
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 \ |
|
274 int nr = rows (); \ |
|
275 int nc = cols (); \ |
|
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); \ |
|
284 for (int i = 0; i < nr; i++) \ |
|
285 { \ |
|
286 retval.elem (i, 0) = INIT_VAL; \ |
|
287 for (int j = 0; j < nc; j++) \ |
|
288 { \ |
|
289 ROW_EXPR; \ |
|
290 } \ |
|
291 } \ |
|
292 } \ |
|
293 else \ |
|
294 { \ |
|
295 retval.resize (1, nc); \ |
|
296 for (int j = 0; j < nc; j++) \ |
|
297 { \ |
|
298 retval.elem (0, j) = INIT_VAL; \ |
|
299 for (int i = 0; i < nr; i++) \ |
|
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 |
4556
|
353 #define MX_ND_ALL_EXPR elem (iter_idx) == 0 |
|
354 |
|
355 #define MX_ND_ANY_EXPR elem (iter_idx) |
|
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 } \ |
|
365 else \ |
|
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 } |
|
380 |
|
381 #define MX_ND_ALL_ANY(EVAL_EXPR) \ |
|
382 \ |
|
383 boolNDArray retval; \ |
|
384 \ |
|
385 dim_vector dv = dims (); \ |
|
386 \ |
|
387 if (dim == -1)/* We need to find first non-singleton dim */ \ |
|
388 { \ |
|
389 for (int i = 0; i < dv.length (); i++) \ |
|
390 { \ |
|
391 if (dv (i) > 1) \ |
|
392 { \ |
|
393 dim = i; \ |
|
394 break; \ |
|
395 } \ |
|
396 } \ |
|
397 if (dim == -1) \ |
|
398 { \ |
|
399 (*current_liboctave_error_handler) \ |
|
400 ("all dimensions are singleton"); \ |
|
401 return retval; \ |
|
402 } \ |
|
403 } \ |
|
404 /* Length of Dimension */\ |
|
405 int dim_length = 1; \ |
|
406 /* dim = -1 means from here that the user specified a */ \ |
|
407 /* dimension which is larger that the number of dimensions */ \ |
|
408 /* of the array */ \ |
|
409 if (dim >= dv.length ()) \ |
|
410 dim = -1; \ |
|
411 else \ |
|
412 dim_length = dv (dim); \ |
|
413 if (dim > -1) \ |
|
414 dv (dim) = 1; \ |
|
415 \ |
|
416 /* We need to find the number of elements we need to */ \ |
|
417 /* fill in retval. First we need to get last idx of */ \ |
|
418 /* the dimension vector */ \ |
|
419 Array<int> temp_dv (dv.length (), 0); \ |
|
420 for (int x = 0; x < dv.length (); x++) \ |
|
421 temp_dv (x) = dv (x) - 1; \ |
|
422 \ |
|
423 /* This finds the number of elements in retval */ \ |
|
424 int num_iter = compute_index (temp_dv, dv) + 1; \ |
|
425 \ |
|
426 /* Make sure retval has correct dimensions */ \ |
|
427 retval.resize (dv, false); \ |
|
428 \ |
|
429 Array<int> iter_idx (dv.length (), 0); \ |
|
430 \ |
|
431 /* Filling in values. */ \ |
|
432 /* First loop finds new index */ \ |
|
433 for (int j = 0; j < num_iter; j++) \ |
|
434 { \ |
|
435 for (int i = 0; i < dim_length; i++) \ |
|
436 { \ |
|
437 if (dim > -1) \ |
|
438 iter_idx (dim) = i; \ |
|
439 \ |
|
440 EVAL_EXPR \ |
|
441 } \ |
|
442 \ |
|
443 if (dim > -1) \ |
|
444 iter_idx (dim) = 0; \ |
|
445 \ |
|
446 increment_index (iter_idx, dv); \ |
|
447 } \ |
|
448 \ |
|
449 return retval |
|
450 |
|
451 |
2804
|
452 #endif |
3
|
453 |
|
454 /* |
|
455 ;;; Local Variables: *** |
|
456 ;;; mode: C++ *** |
|
457 ;;; End: *** |
|
458 */ |