numo-narray 0.9.0.5 → 0.9.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,6 +32,7 @@ static ID id_eq;
32
32
  static ID id_count_false;
33
33
  static ID id_axis;
34
34
  static ID id_nan;
35
+ static ID id_keepdims;
35
36
 
36
37
  VALUE cPointer;
37
38
 
@@ -1042,7 +1043,7 @@ nary_reverse(int argc, VALUE *argv, VALUE self)
1042
1043
  VALUE view;
1043
1044
  VALUE reduce;
1044
1045
 
1045
- reduce = na_reduce_dimension(argc, argv, 1, &self, 0);
1046
+ reduce = na_reduce_dimension(argc, argv, 1, &self, 0, 0);
1046
1047
 
1047
1048
  GetNArray(self,na);
1048
1049
  nd = na->ndim;
@@ -1448,7 +1449,8 @@ na_test_reduce(VALUE reduce, int dim)
1448
1449
 
1449
1450
 
1450
1451
  VALUE
1451
- na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv, int *propagate_nan)
1452
+ na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv,
1453
+ ndfunc_t *ndf, na_iter_func_t iter_nan)
1452
1454
  {
1453
1455
  int ndim, ndim0;
1454
1456
  int row_major;
@@ -1462,12 +1464,12 @@ na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv, int *propaga
1462
1464
  size_t m;
1463
1465
  VALUE reduce;
1464
1466
  VALUE kw_hash = Qnil;
1465
- ID kw_table[2] = {id_axis,id_nan};
1466
- VALUE opts[2] = {Qundef,Qundef};
1467
+ ID kw_table[3] = {id_axis,id_nan,id_keepdims};
1468
+ VALUE opts[3] = {Qundef,Qundef,Qundef};
1467
1469
  VALUE axes;
1468
1470
 
1469
1471
  narg = rb_scan_args(argc, argv, "*:", &axes, &kw_hash);
1470
- rb_get_kwargs(kw_hash, kw_table, 0, 2, opts);
1472
+ rb_get_kwargs(kw_hash, kw_table, 0, 3, opts);
1471
1473
 
1472
1474
  // option: axis
1473
1475
  if (opts[0] != Qundef && RTEST(opts[0])) {
@@ -1480,9 +1482,17 @@ na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv, int *propaga
1480
1482
  axes = rb_ary_new3(1,opts[0]);
1481
1483
  }
1482
1484
  }
1483
- // option: ignore_none
1484
- if (propagate_nan) {
1485
- *propagate_nan = (opts[1] != Qundef && RTEST(opts[1])) ? 1 : 0;
1485
+ if (ndf) {
1486
+ // option: nan
1487
+ if (iter_nan && opts[1] != Qundef) {
1488
+ if (RTEST(opts[1]))
1489
+ ndf->func = iter_nan; // replace to nan-aware iterator function
1490
+ }
1491
+ // option: keepdims
1492
+ if (opts[2] != Qundef) {
1493
+ if (RTEST(opts[2]))
1494
+ ndf->flag |= NDF_KEEP_DIM;
1495
+ }
1486
1496
  }
1487
1497
 
1488
1498
  if (naryc<1) {
@@ -1890,6 +1900,7 @@ Init_narray()
1890
1900
  id_count_false = rb_intern("count_false");
1891
1901
  id_axis = rb_intern("axis");
1892
1902
  id_nan = rb_intern("nan");
1903
+ id_keepdims = rb_intern("keepdims");
1893
1904
 
1894
1905
  sym_reduce = ID2SYM(rb_intern("reduce"));
1895
1906
  sym_option = ID2SYM(rb_intern("option"));
@@ -72,7 +72,8 @@ void na_release_lock(VALUE); // currently do nothing
72
72
 
73
73
  // used in reduce methods
74
74
  #define na_reduce_dimension nary_reduce_dimension
75
- VALUE nary_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv, int *ignore_nan);
75
+ VALUE nary_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv,
76
+ ndfunc_t *ndf, na_iter_func_t nan_iter);
76
77
 
77
78
  // ndloop
78
79
  VALUE na_ndloop(ndfunc_t *nf, int argc, ...);
@@ -13,8 +13,8 @@ extern "C" {
13
13
  #endif
14
14
  #endif
15
15
 
16
- #define NARRAY_VERSION "0.9.0.5"
17
- #define NARRAY_VERSION_CODE 905
16
+ #define NARRAY_VERSION "0.9.0.6"
17
+ #define NARRAY_VERSION_CODE 906
18
18
 
19
19
  #include <math.h>
20
20
  #include "numo/compat.h"
@@ -89,17 +89,6 @@ static inline dtype c_from_dcomplex(dcomplex x) {
89
89
  #define m_isneginf(x) c_isneginf(x)
90
90
  #define m_isfinite(x) c_isfinite(x)
91
91
 
92
- #define m_sum(x,y) {if (!c_isnan(x)) {y=c_add(x,y);}}
93
- #define m_sum_init INT2FIX(0)
94
-
95
- #define m_mulsum_init INT2FIX(0)
96
- #define m_mulsum(x,y,z) {z = c_add(c_mul(x,y),z);}
97
- #define m_mulsum_nan(x,y,z) {if(!m_isnan(x) && !m_isnan(y)){z = c_add(c_mul(x,y),z);}}
98
- #define m_cumsum(x,y) {(x)=c_add(x,y);}
99
- #define m_cumsum_nan(x,y) {if (c_isnan(x)) {(x)=(y);} else if (!c_isnan(y)) {(x)=c_add(x,y);}}
100
- #define m_cumprod(x,y) {(x)=c_mul(x,y);}
101
- #define m_cumprod_nan(x,y) {if (c_isnan(x)) {(x)=(y);} else if (!c_isnan(y)) {(x)=c_mul(x,y);}}
102
-
103
92
  #define m_sprintf(s,x) sprintf(s,"%g%+gi",REAL(x),IMAG(x))
104
93
 
105
94
  #define m_sqrt(x) c_sqrt(x)
@@ -125,6 +114,31 @@ static inline dtype c_from_dcomplex(dcomplex x) {
125
114
  #define m_hypot(x,y) c_hypot(x,y)
126
115
  #define m_sinc(x) c_div(c_sin(x),x)
127
116
 
117
+ #define m_sum_init INT2FIX(0)
118
+ #define m_mulsum_init INT2FIX(0)
119
+
120
+ #define m_mulsum(x,y,z) {z = m_add(m_mul(x,y),z);}
121
+ #define m_mulsum_nan(x,y,z) { \
122
+ if(!m_isnan(x) && !m_isnan(y)) { \
123
+ z = m_add(m_mul(x,y),z); \
124
+ }}
125
+
126
+ #define m_cumsum(x,y) {(x)=m_add(x,y);}
127
+ #define m_cumsum_nan(x,y) { \
128
+ if (m_isnan(x)) { \
129
+ (x) = (y); \
130
+ } else if (!m_isnan(y)) { \
131
+ (x) = m_add(x,y); \
132
+ }}
133
+
134
+ #define m_cumprod(x,y) {(x)=m_mul(x,y);}
135
+ #define m_cumprod_nan(x,y) { \
136
+ if (m_isnan(x)) { \
137
+ (x) = (y); \
138
+ } else if (!m_isnan(y)) { \
139
+ (x) = m_mul(x,y); \
140
+ }}
141
+
128
142
  static inline dtype f_sum(size_t n, char *p, ssize_t stride)
129
143
  {
130
144
  size_t i=n;
@@ -56,12 +56,6 @@ EXTERN double pow(double, double);
56
56
  #define m_isfinite(x) isfinite(x)
57
57
 
58
58
  #define m_mulsum_init INT2FIX(0)
59
- #define m_mulsum(x,y,z) {(z)+=(x)*(y);}
60
- #define m_mulsum_nan(x,y,z) {if (isnan(z)) {(z)=(x)*(y);} else if (!isnan(x) && !isnan(y)) {(z)+=(x)*(y);}}
61
- #define m_cumsum(x,y) {(x)+=(y);}
62
- #define m_cumsum_nan(x,y) {if (isnan(x)) {(x)=(y);} else if (!isnan(y)) {(x)+=(y);}}
63
- #define m_cumprod(x,y) {(x)*=(y);}
64
- #define m_cumprod_nan(x,y) {if (isnan(x)) {(x)=(y);} else if (!isnan(y)) {(x)*=(y);}}
65
59
 
66
60
  #define m_sprintf(s,x) sprintf(s,"%g",x)
67
61
 
@@ -138,37 +132,12 @@ static inline dtype pow_int(dtype x, int p)
138
132
  return r;
139
133
  }
140
134
 
141
-
142
- static inline dtype f_sum_nan(size_t n, char *p, ssize_t stride)
143
- {
144
- size_t i=n;
145
- dtype x,y=0;
146
-
147
- for (; i--;) {
148
- x = *(dtype*)p;
149
- if (m_isnan(x)) {return x;}
150
- y += x;
151
- p += stride;
152
- }
153
- return y;
154
- }
155
-
156
- static inline dtype f_sum(size_t n, char *p, ssize_t stride)
135
+ static inline dtype f_seq(dtype x, dtype y, double c)
157
136
  {
158
- size_t i=n;
159
- dtype x,y=0;
160
-
161
- for (; i--;) {
162
- x = *(dtype*)p;
163
- if (!m_isnan(x)) {
164
- y += x;
165
- }
166
- p += stride;
167
- }
168
- return y;
137
+ return x + y * c;
169
138
  }
170
139
 
171
- static inline dtype f_kahan_sum_nan(size_t n, char *p, ssize_t stride)
140
+ static inline dtype f_kahan_sum(size_t n, char *p, ssize_t stride)
172
141
  {
173
142
  size_t i=n;
174
143
  dtype x;
@@ -177,7 +146,7 @@ static inline dtype f_kahan_sum_nan(size_t n, char *p, ssize_t stride)
177
146
 
178
147
  for (; i--;) {
179
148
  x = *(dtype*)p;
180
- if (m_isnan(x)) {return x;}
149
+ p += stride;
181
150
  if (fabs(x) > fabs(y)) {
182
151
  dtype z=x; x=y; y=z;
183
152
  }
@@ -186,12 +155,11 @@ static inline dtype f_kahan_sum_nan(size_t n, char *p, ssize_t stride)
186
155
  y += r;
187
156
  t = y-t;
188
157
  r -= t;
189
- p += stride;
190
158
  }
191
159
  return y;
192
160
  }
193
161
 
194
- static inline dtype f_kahan_sum(size_t n, char *p, ssize_t stride)
162
+ static inline dtype f_kahan_sum_nan(size_t n, char *p, ssize_t stride)
195
163
  {
196
164
  size_t i=n;
197
165
  dtype x;
@@ -200,6 +168,7 @@ static inline dtype f_kahan_sum(size_t n, char *p, ssize_t stride)
200
168
 
201
169
  for (; i--;) {
202
170
  x = *(dtype*)p;
171
+ p += stride;
203
172
  if (!m_isnan(x)) {
204
173
  if (fabs(x) > fabs(y)) {
205
174
  dtype z=x; x=y; y=z;
@@ -210,375 +179,8 @@ static inline dtype f_kahan_sum(size_t n, char *p, ssize_t stride)
210
179
  t = y-t;
211
180
  r -= t;
212
181
  }
213
- p += stride;
214
- }
215
- return y;
216
- }
217
-
218
- static inline dtype f_prod_nan(size_t n, char *p, ssize_t stride)
219
- {
220
- size_t i=n;
221
- dtype x,y=1;
222
-
223
- for (; i--;) {
224
- x = *(dtype*)p;
225
- if (m_isnan(x)) {return x;}
226
- y *= x;
227
- p += stride;
228
- }
229
- return y;
230
- }
231
-
232
- static inline dtype f_prod(size_t n, char *p, ssize_t stride)
233
- {
234
- size_t i=n;
235
- dtype x,y=1;
236
-
237
- for (; i--;) {
238
- x = *(dtype*)p;
239
- if (!m_isnan(x)) {
240
- y *= x;
241
- }
242
- p += stride;
243
- }
244
- return y;
245
- }
246
-
247
- static inline dtype f_mean_nan(size_t n, char *p, ssize_t stride)
248
- {
249
- size_t i=n;
250
- size_t count=0;
251
- dtype x,y=0;
252
-
253
- for (; i--;) {
254
- x = *(dtype*)p;
255
- if (m_isnan(x)) {return x;}
256
- y += x;
257
- count++;
258
- p += stride;
259
- }
260
- return y/count;
261
- }
262
-
263
- static inline dtype f_mean(size_t n, char *p, ssize_t stride)
264
- {
265
- size_t i=n;
266
- size_t count=0;
267
- dtype x,y=0;
268
-
269
- for (; i--;) {
270
- x = *(dtype*)p;
271
- if (!m_isnan(x)) {
272
- y += x;
273
- count++;
274
- }
275
- p += stride;
276
- }
277
- return y/count;
278
- }
279
-
280
- static inline dtype f_var_nan(size_t n, char *p, ssize_t stride)
281
- {
282
- size_t i=n;
283
- size_t count=0;
284
- dtype x,y=0;
285
- dtype a,m;
286
-
287
- m = f_mean_nan(n,p,stride);
288
- if (m_isnan(m)) {return m;}
289
-
290
- for (; i--;) {
291
- x = *(dtype*)p;
292
- if (m_isnan(x)) {return x;}
293
- a = x - m;
294
- y += a*a;
295
- count++;
296
- p += stride;
297
- }
298
- return y/(count-1);
299
- }
300
-
301
- static inline dtype f_var(size_t n, char *p, ssize_t stride)
302
- {
303
- size_t i=n;
304
- size_t count=0;
305
- dtype x,y=0;
306
- dtype a,m;
307
-
308
- m = f_mean(n,p,stride);
309
-
310
- for (; i--;) {
311
- x = *(dtype*)p;
312
- if (!m_isnan(x)) {
313
- a = x - m;
314
- y += a*a;
315
- count++;
316
- }
317
- p += stride;
318
- }
319
- return y/(count-1);
320
- }
321
-
322
- static inline dtype f_stddev_nan(size_t n, char *p, ssize_t stride)
323
- {
324
- return m_sqrt(f_var_nan(n,p,stride));
325
- }
326
-
327
- static inline dtype f_stddev(size_t n, char *p, ssize_t stride)
328
- {
329
- return m_sqrt(f_var(n,p,stride));
330
- }
331
-
332
- static inline dtype f_rms_nan(size_t n, char *p, ssize_t stride)
333
- {
334
- size_t i=n;
335
- size_t count=0;
336
- dtype x,y=0;
337
-
338
- for (; i--;) {
339
- x = *(dtype*)p;
340
- if (m_isnan(x)) {return x;}
341
- y += x*x;
342
- count++;
343
- p += stride;
344
- }
345
- return m_sqrt(y/count);
346
- }
347
-
348
- static inline dtype f_rms(size_t n, char *p, ssize_t stride)
349
- {
350
- size_t i=n;
351
- size_t count=0;
352
- dtype x,y=0;
353
-
354
- for (; i--;) {
355
- x = *(dtype*)p;
356
- if (!m_isnan(x)) {
357
- y += x*x;
358
- count++;
359
- }
360
- p += stride;
361
- }
362
- return m_sqrt(y/count);
363
- }
364
-
365
- static inline dtype f_min_nan(size_t n, char *p, ssize_t stride)
366
- {
367
- dtype x,y;
368
- size_t i=n;
369
-
370
- y = *(dtype*)p;
371
- if (m_isnan(y)) {return y;}
372
- p += stride;
373
- i--;
374
- for (; i--;) {
375
- x = *(dtype*)p;
376
- if (m_isnan(x)) {return x;}
377
- if (x<y) {
378
- y = x;
379
- }
380
- p += stride;
381
- }
382
- return y;
383
- }
384
-
385
- static inline dtype f_min(size_t n, char *p, ssize_t stride)
386
- {
387
- dtype x,y;
388
- size_t i=n;
389
-
390
- y = *(dtype*)p;
391
- p += stride;
392
- i--;
393
- for (; i--;) {
394
- x = *(dtype*)p;
395
- if (!m_isnan(x) && (m_isnan(y) || x<y)) {
396
- y = x;
397
- }
398
- p += stride;
399
- }
400
- return y;
401
- }
402
-
403
- static inline dtype f_max_nan(size_t n, char *p, ssize_t stride)
404
- {
405
- dtype x,y;
406
- size_t i=n;
407
-
408
- y = *(dtype*)p;
409
- if (m_isnan(y)) {return y;}
410
- p += stride;
411
- i--;
412
- for (; i--;) {
413
- x = *(dtype*)p;
414
- if (m_isnan(x)) {return x;}
415
- if (x>y) {
416
- y = x;
417
- }
418
- p += stride;
419
- }
420
- return y;
421
- }
422
-
423
- static inline dtype f_max(size_t n, char *p, ssize_t stride)
424
- {
425
- dtype x,y;
426
- size_t i=n;
427
-
428
- y = *(dtype*)p;
429
- p += stride;
430
- i--;
431
- for (; i--;) {
432
- x = *(dtype*)p;
433
- if (!m_isnan(x) && (m_isnan(y) || x>y)) {
434
- y = x;
435
- }
436
- p += stride;
437
182
  }
438
183
  return y;
439
184
  }
440
185
 
441
- static inline size_t f_min_index_nan(size_t n, char *p, ssize_t stride)
442
- {
443
- dtype x, y;
444
- size_t i, j=0;
445
-
446
- y = *(dtype*)p;
447
- if (m_isnan(y)) {return j;}
448
- for (i=1; i<n; i++) {
449
- x = *(dtype*)(p+i*stride);
450
- if (m_isnan(x)) {return i;}
451
- if (x<y) {
452
- y = x;
453
- j = i;
454
- }
455
- }
456
- return j;
457
- }
458
-
459
- static inline size_t f_min_index(size_t n, char *p, ssize_t stride)
460
- {
461
- dtype x, y;
462
- size_t i, j=0;
463
-
464
- y = *(dtype*)p;
465
- for (i=1; i<n; i++) {
466
- x = *(dtype*)(p+i*stride);
467
- if (!m_isnan(x) && (m_isnan(y) || x<y)) {
468
- y = x;
469
- j = i;
470
- }
471
- }
472
- return j;
473
- }
474
-
475
- static inline size_t f_max_index_nan(size_t n, char *p, ssize_t stride)
476
- {
477
- dtype x, y;
478
- size_t i, j=0;
479
-
480
- y = *(dtype*)p;
481
- if (m_isnan(y)) {return j;}
482
- for (i=1; i<n; i++) {
483
- x = *(dtype*)(p+i*stride);
484
- if (m_isnan(x)) {return i;}
485
- if (x>y) {
486
- y = x;
487
- j = i;
488
- }
489
- }
490
- return j;
491
- }
492
-
493
- static inline size_t f_max_index(size_t n, char *p, ssize_t stride)
494
- {
495
- dtype x, y;
496
- size_t i, j=0;
497
-
498
- y = *(dtype*)p;
499
- for (i=1; i<n; i++) {
500
- x = *(dtype*)(p+i*stride);
501
- if (!m_isnan(x) && (m_isnan(y) || x>y)) {
502
- y = x;
503
- j = i;
504
- }
505
- }
506
- return j;
507
- }
508
-
509
- static inline void
510
- f_minmax_nan(size_t n, char *p, ssize_t stride, dtype *amin, dtype *amax)
511
- {
512
- dtype x,min,max;
513
- size_t i=n;
514
-
515
- min = max = *(dtype*)p;
516
- if (m_isnan(x)) {
517
- *amin = *amax = min;
518
- return;
519
- }
520
- p += stride;
521
- i--;
522
- for (; i--;) {
523
- x = *(dtype*)p;
524
- if (m_isnan(x)) {
525
- *amin = *amax = x;
526
- return;
527
- }
528
- if (m_lt(x,min)) {
529
- min = x;
530
- }
531
- if (m_gt(x,max)) {
532
- max = x;
533
- }
534
- p += stride;
535
- }
536
- *amin = min;
537
- *amax = max;
538
- return;
539
- }
540
-
541
- static inline dtype f_ptp_nan(size_t n, char *p, ssize_t stride)
542
- {
543
- dtype min,max;
544
- f_minmax_nan(n,p,stride,&min,&max);
545
- return m_sub(max,min);
546
- }
547
-
548
- static inline void
549
- f_minmax(size_t n, char *p, ssize_t stride, dtype *amin, dtype *amax)
550
- {
551
- dtype x,min,max;
552
- size_t i=n;
553
-
554
- min = max = *(dtype*)p;
555
- p += stride;
556
- i--;
557
- for (; i--;) {
558
- x = *(dtype*)p;
559
- if (!m_isnan(x)) {
560
- if (m_isnan(min) || m_lt(x,min)) {
561
- min = x;
562
- }
563
- if (m_isnan(max) || m_gt(x,max)) {
564
- max = x;
565
- }
566
- }
567
- p += stride;
568
- }
569
- *amin = min;
570
- *amax = max;
571
- return;
572
- }
573
-
574
- static inline dtype f_ptp(size_t n, char *p, ssize_t stride)
575
- {
576
- dtype min,max;
577
- f_minmax(n,p,stride,&min,&max);
578
- return m_sub(max,min);
579
- }
580
-
581
- static inline dtype f_seq(dtype x, dtype y, double c)
582
- {
583
- return x + y * c;
584
- }
186
+ #include "real_accum.h"