numo-narray 0.9.0.1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/README.md +47 -0
  4. data/Rakefile +41 -0
  5. data/ext/numo/narray/SFMT-params.h +97 -0
  6. data/ext/numo/narray/SFMT-params19937.h +46 -0
  7. data/ext/numo/narray/SFMT.c +620 -0
  8. data/ext/numo/narray/SFMT.h +157 -0
  9. data/ext/numo/narray/array.c +525 -0
  10. data/ext/numo/narray/data.c +901 -0
  11. data/ext/numo/narray/depend.erb +33 -0
  12. data/ext/numo/narray/extconf.rb +117 -0
  13. data/ext/numo/narray/gen/bit.erb.c +811 -0
  14. data/ext/numo/narray/gen/cogen.rb +18 -0
  15. data/ext/numo/narray/gen/def/dcomplex.rb +32 -0
  16. data/ext/numo/narray/gen/def/dfloat.rb +30 -0
  17. data/ext/numo/narray/gen/def/int16.rb +29 -0
  18. data/ext/numo/narray/gen/def/int32.rb +29 -0
  19. data/ext/numo/narray/gen/def/int64.rb +29 -0
  20. data/ext/numo/narray/gen/def/int8.rb +29 -0
  21. data/ext/numo/narray/gen/def/robject.rb +30 -0
  22. data/ext/numo/narray/gen/def/scomplex.rb +32 -0
  23. data/ext/numo/narray/gen/def/sfloat.rb +30 -0
  24. data/ext/numo/narray/gen/def/uint16.rb +29 -0
  25. data/ext/numo/narray/gen/def/uint32.rb +29 -0
  26. data/ext/numo/narray/gen/def/uint64.rb +29 -0
  27. data/ext/numo/narray/gen/def/uint8.rb +29 -0
  28. data/ext/numo/narray/gen/dtype.erb.c +328 -0
  29. data/ext/numo/narray/gen/tmpl/accum.c +36 -0
  30. data/ext/numo/narray/gen/tmpl/accum_binary.c +75 -0
  31. data/ext/numo/narray/gen/tmpl/accum_index.c +58 -0
  32. data/ext/numo/narray/gen/tmpl/allocate.c +35 -0
  33. data/ext/numo/narray/gen/tmpl/aref.c +51 -0
  34. data/ext/numo/narray/gen/tmpl/aset.c +61 -0
  35. data/ext/numo/narray/gen/tmpl/binary.c +53 -0
  36. data/ext/numo/narray/gen/tmpl/binary2.c +55 -0
  37. data/ext/numo/narray/gen/tmpl/binary_s.c +34 -0
  38. data/ext/numo/narray/gen/tmpl/bit_binary.c +94 -0
  39. data/ext/numo/narray/gen/tmpl/bit_count.c +82 -0
  40. data/ext/numo/narray/gen/tmpl/bit_unary.c +77 -0
  41. data/ext/numo/narray/gen/tmpl/cast.c +37 -0
  42. data/ext/numo/narray/gen/tmpl/cast_array.c +79 -0
  43. data/ext/numo/narray/gen/tmpl/cast_numeric.c +22 -0
  44. data/ext/numo/narray/gen/tmpl/coerce_cast.c +8 -0
  45. data/ext/numo/narray/gen/tmpl/cond_binary.c +51 -0
  46. data/ext/numo/narray/gen/tmpl/cond_unary.c +45 -0
  47. data/ext/numo/narray/gen/tmpl/cum.c +42 -0
  48. data/ext/numo/narray/gen/tmpl/each.c +43 -0
  49. data/ext/numo/narray/gen/tmpl/each_with_index.c +64 -0
  50. data/ext/numo/narray/gen/tmpl/extract.c +23 -0
  51. data/ext/numo/narray/gen/tmpl/eye.c +91 -0
  52. data/ext/numo/narray/gen/tmpl/fill.c +38 -0
  53. data/ext/numo/narray/gen/tmpl/format.c +60 -0
  54. data/ext/numo/narray/gen/tmpl/format_to_a.c +47 -0
  55. data/ext/numo/narray/gen/tmpl/head.c +25 -0
  56. data/ext/numo/narray/gen/tmpl/inspect.c +16 -0
  57. data/ext/numo/narray/gen/tmpl/map_with_index.c +94 -0
  58. data/ext/numo/narray/gen/tmpl/median.c +44 -0
  59. data/ext/numo/narray/gen/tmpl/minmax.c +47 -0
  60. data/ext/numo/narray/gen/tmpl/poly.c +49 -0
  61. data/ext/numo/narray/gen/tmpl/pow.c +74 -0
  62. data/ext/numo/narray/gen/tmpl/powint.c +17 -0
  63. data/ext/numo/narray/gen/tmpl/qsort.c +149 -0
  64. data/ext/numo/narray/gen/tmpl/rand.c +33 -0
  65. data/ext/numo/narray/gen/tmpl/rand_norm.c +46 -0
  66. data/ext/numo/narray/gen/tmpl/robj_allocate.c +32 -0
  67. data/ext/numo/narray/gen/tmpl/seq.c +61 -0
  68. data/ext/numo/narray/gen/tmpl/set2.c +56 -0
  69. data/ext/numo/narray/gen/tmpl/sort.c +36 -0
  70. data/ext/numo/narray/gen/tmpl/sort_index.c +86 -0
  71. data/ext/numo/narray/gen/tmpl/store.c +31 -0
  72. data/ext/numo/narray/gen/tmpl/store_array.c +5 -0
  73. data/ext/numo/narray/gen/tmpl/store_from.c +53 -0
  74. data/ext/numo/narray/gen/tmpl/store_numeric.c +22 -0
  75. data/ext/numo/narray/gen/tmpl/to_a.c +41 -0
  76. data/ext/numo/narray/gen/tmpl/unary.c +58 -0
  77. data/ext/numo/narray/gen/tmpl/unary2.c +58 -0
  78. data/ext/numo/narray/gen/tmpl/unary_s.c +57 -0
  79. data/ext/numo/narray/index.c +822 -0
  80. data/ext/numo/narray/kwarg.c +79 -0
  81. data/ext/numo/narray/math.c +140 -0
  82. data/ext/numo/narray/narray.c +1539 -0
  83. data/ext/numo/narray/ndloop.c +1928 -0
  84. data/ext/numo/narray/numo/compat.h +23 -0
  85. data/ext/numo/narray/numo/intern.h +112 -0
  86. data/ext/numo/narray/numo/narray.h +411 -0
  87. data/ext/numo/narray/numo/ndloop.h +99 -0
  88. data/ext/numo/narray/numo/template.h +140 -0
  89. data/ext/numo/narray/numo/types/bit.h +19 -0
  90. data/ext/numo/narray/numo/types/complex.h +410 -0
  91. data/ext/numo/narray/numo/types/complex_macro.h +205 -0
  92. data/ext/numo/narray/numo/types/dcomplex.h +11 -0
  93. data/ext/numo/narray/numo/types/dfloat.h +12 -0
  94. data/ext/numo/narray/numo/types/float_def.h +34 -0
  95. data/ext/numo/narray/numo/types/float_macro.h +277 -0
  96. data/ext/numo/narray/numo/types/int16.h +12 -0
  97. data/ext/numo/narray/numo/types/int32.h +12 -0
  98. data/ext/numo/narray/numo/types/int64.h +12 -0
  99. data/ext/numo/narray/numo/types/int8.h +12 -0
  100. data/ext/numo/narray/numo/types/int_macro.h +34 -0
  101. data/ext/numo/narray/numo/types/robj_macro.h +218 -0
  102. data/ext/numo/narray/numo/types/robject.h +21 -0
  103. data/ext/numo/narray/numo/types/scomplex.h +11 -0
  104. data/ext/numo/narray/numo/types/sfloat.h +13 -0
  105. data/ext/numo/narray/numo/types/uint16.h +12 -0
  106. data/ext/numo/narray/numo/types/uint32.h +12 -0
  107. data/ext/numo/narray/numo/types/uint64.h +12 -0
  108. data/ext/numo/narray/numo/types/uint8.h +12 -0
  109. data/ext/numo/narray/numo/types/uint_macro.h +31 -0
  110. data/ext/numo/narray/numo/types/xint_macro.h +133 -0
  111. data/ext/numo/narray/rand.c +87 -0
  112. data/ext/numo/narray/step.c +506 -0
  113. data/ext/numo/narray/struct.c +872 -0
  114. data/lib/2.1/numo/narray.so +0 -0
  115. data/lib/2.2/numo/narray.so +0 -0
  116. data/lib/2.3/numo/narray.so +0 -0
  117. data/lib/erbpp/line_number.rb +126 -0
  118. data/lib/erbpp/narray_def.rb +338 -0
  119. data/lib/erbpp.rb +286 -0
  120. data/lib/numo/narray.rb +6 -0
  121. data/numo-narray.gemspec +35 -0
  122. data/spec/bit_spec.rb +93 -0
  123. data/spec/narray_spec.rb +249 -0
  124. metadata +238 -0
@@ -0,0 +1,64 @@
1
+ static inline void
2
+ yield_each_with_index(dtype x, size_t *c, VALUE *a, int nd, int md)
3
+ {
4
+ int j;
5
+
6
+ a[0] = m_data_to_num(x);
7
+ for (j=0; j<=nd; j++) {
8
+ a[j+1] = SIZE2NUM(c[j]);
9
+ }
10
+ rb_yield(rb_ary_new4(md,a));
11
+ }
12
+
13
+
14
+ void
15
+ <%=c_iter%>(na_loop_t *const lp)
16
+ {
17
+ size_t i, s1;
18
+ char *p1;
19
+ size_t *idx1;
20
+ dtype x;
21
+ VALUE *a;
22
+ size_t *c;
23
+ int nd, md;
24
+
25
+ c = (size_t*)(lp->opt_ptr);
26
+ nd = lp->ndim - 1;
27
+ md = lp->ndim + 1;
28
+ a = ALLOCA_N(VALUE,md);
29
+
30
+ INIT_COUNTER(lp, i);
31
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
32
+ c[nd] = 0;
33
+ if (idx1) {
34
+ for (; i--;) {
35
+ GET_DATA_INDEX(p1,idx1,dtype,x);
36
+ yield_each_with_index(x,c,a,nd,md);
37
+ c[nd]++;
38
+ }
39
+ } else {
40
+ for (; i--;) {
41
+ GET_DATA_STRIDE(p1,s1,dtype,x);
42
+ yield_each_with_index(x,c,a,nd,md);
43
+ c[nd]++;
44
+ }
45
+ }
46
+ }
47
+
48
+ /*
49
+ Invokes the given block once for each element of self,
50
+ passing that element and indices along each axis as parameters.
51
+ @overload <%=method%>
52
+ @return [Numo::NArray] self
53
+ For a block {|x,i,j,...| ... }
54
+ @yield [x,i,j,...] x is an element, i,j,... are multidimensional indices.
55
+ */
56
+ static VALUE
57
+ <%=c_func%>(VALUE self)
58
+ {
59
+ ndfunc_arg_in_t ain[1] = {{Qnil,0}};
60
+ ndfunc_t ndf = {<%=c_iter%>, FULL_LOOP_NIP, 1,0, ain,0};
61
+
62
+ na_ndloop_with_index(&ndf, 1, self);
63
+ return self;
64
+ }
@@ -0,0 +1,23 @@
1
+ /*
2
+ Extract an element only if self is a dimensionless NArray.
3
+ @overload extract
4
+ @return [Numeric,Numo::NArray]
5
+ --- Extract element value as Ruby Object if self is a dimensionless NArray,
6
+ otherwise returns self.
7
+ */
8
+ VALUE
9
+ numo_<%=tp%>_extract(VALUE self)
10
+ {
11
+ volatile VALUE v;
12
+ char *ptr;
13
+ narray_t *na;
14
+ GetNArray(self,na);
15
+
16
+ if (na->ndim==0) {
17
+ ptr = na_get_pointer_for_read(self) + na_get_offset(self);
18
+ v = m_extract(ptr);
19
+ na_release_lock(self);
20
+ return v;
21
+ }
22
+ return self;
23
+ }
@@ -0,0 +1,91 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t n0, n1;
5
+ size_t i0, i1;
6
+ ssize_t s0, s1;
7
+ char *p0, *p1;
8
+ char *g;
9
+ ssize_t kofs;
10
+ dtype data;
11
+
12
+ g = (char*)(lp->opt_ptr);
13
+ kofs = *(ssize_t*)g;
14
+ data = *(dtype*)(g+sizeof(ssize_t));
15
+
16
+ n0 = lp->args[0].shape[0];
17
+ n1 = lp->args[0].shape[1];
18
+ s0 = lp->args[0].iter[0].step;
19
+ s1 = lp->args[0].iter[1].step;
20
+ p0 = NDL_PTR(lp,0);
21
+
22
+ for (i0=0; i0 < n0; i0++) {
23
+ p1 = p0;
24
+ for (i1=0; i1 < n1; i1++) {
25
+ *(dtype*)p1 = (i0+kofs==i1) ? data : m_zero;
26
+ p1 += s1;
27
+ }
28
+ p0 += s0;
29
+ }
30
+ }
31
+
32
+ /*
33
+ Eye: Set a value to diagonal components, set 0 to non-diagonal components.
34
+ @overload <%=method%>([element,offset])
35
+ @param [Numeric] element Diagonal element to be stored. Default is 1.
36
+ @param [Integer] offset Diagonal offset from the main diagonal. The
37
+ default is 0. k>0 for diagonals above the main diagonal, and k<0
38
+ for diagonals below the main diagonal.
39
+ @return [Numo::<%=class_name%>] <%=method%> of self.
40
+ */
41
+ static VALUE
42
+ <%=c_func%>(int argc, VALUE *argv, VALUE self)
43
+ {
44
+ ndfunc_arg_in_t ain[1] = {{OVERWRITE,2}};
45
+ ndfunc_t ndf = {<%=c_iter%>, NO_LOOP, 1,0, ain,0};
46
+ ssize_t kofs;
47
+ dtype data;
48
+ char *g;
49
+ int nd;
50
+ narray_t *na;
51
+
52
+ // check arguments
53
+ if (argc > 2) {
54
+ rb_raise(rb_eArgError,"too many arguments (%d for 0..2)",argc);
55
+ } else if (argc == 2) {
56
+ data = m_num_to_data(argv[0]);
57
+ kofs = NUM2SSIZE(argv[1]);
58
+ } else if (argc == 1) {
59
+ data = m_num_to_data(argv[0]);
60
+ kofs = 0;
61
+ } else {
62
+ data = m_one;
63
+ kofs = 0;
64
+ }
65
+
66
+ GetNArray(self,na);
67
+ nd = na->ndim;
68
+ if (nd < 2) {
69
+ rb_raise(nary_eDimensionError,"less than 2-d array");
70
+ }
71
+
72
+ // Diagonal offset from the main diagonal.
73
+ if (kofs >= 0) {
74
+ if ((size_t)(kofs) >= na->shape[nd-1]) {
75
+ rb_raise(rb_eArgError,"invalid diagonal offset(%ld) for "
76
+ "last dimension size(%ld)",kofs,na->shape[nd-1]);
77
+ }
78
+ } else {
79
+ if ((size_t)(-kofs) >= na->shape[nd-2]) {
80
+ rb_raise(rb_eArgError,"invalid diagonal offset(%ld) for "
81
+ "last-1 dimension size(%ld)",kofs,na->shape[nd-2]);
82
+ }
83
+ }
84
+
85
+ g = ALLOCA_N(char,sizeof(ssize_t)+sizeof(dtype));
86
+ *(ssize_t*)g = kofs;
87
+ *(dtype*)(g+sizeof(ssize_t)) = data;
88
+
89
+ na_ndloop3(&ndf, g, 1, self);
90
+ return self;
91
+ }
@@ -0,0 +1,38 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t i;
5
+ char *p1;
6
+ ssize_t s1;
7
+ size_t *idx1;
8
+ VALUE x = lp->option;
9
+ dtype y;
10
+ INIT_COUNTER(lp, i);
11
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
12
+ y = m_num_to_data(x);
13
+ if (idx1) {
14
+ for (; i--;) {
15
+ SET_DATA_INDEX(p1,idx1,dtype,y);
16
+ }
17
+ } else {
18
+ for (; i--;) {
19
+ SET_DATA_STRIDE(p1,s1,dtype,y);
20
+ }
21
+ }
22
+ }
23
+
24
+ /*
25
+ Fill elements with other.
26
+ @overload <%=method%> other
27
+ @param [Numeric] other
28
+ @return [Numo::<%=class_name%>] self.
29
+ */
30
+ static VALUE
31
+ <%=c_func%>(VALUE self, VALUE val)
32
+ {
33
+ ndfunc_arg_in_t ain[2] = {{OVERWRITE,0},{sym_option}};
34
+ ndfunc_t ndf = { <%=c_iter%>, FULL_LOOP, 2, 0, ain, 0 };
35
+
36
+ na_ndloop(&ndf, 2, self, val);
37
+ return self;
38
+ }
@@ -0,0 +1,60 @@
1
+ static VALUE
2
+ format_<%=tp%>(VALUE fmt, dtype* x)
3
+ {
4
+ // fix-me
5
+ char s[48];
6
+ int n;
7
+
8
+ if (NIL_P(fmt)) {
9
+ n = m_sprintf(s,*x);
10
+ return rb_str_new(s,n);
11
+ }
12
+ return rb_funcall(fmt, '%', 1, m_data_to_num(*x));
13
+ }
14
+
15
+ static void
16
+ <%=c_iter%>(na_loop_t *const lp)
17
+ {
18
+ size_t i;
19
+ char *p1, *p2;
20
+ ssize_t s1, s2;
21
+ size_t *idx1;
22
+ dtype *x;
23
+ VALUE y;
24
+ VALUE fmt = lp->option;
25
+ INIT_COUNTER(lp, i);
26
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
27
+ INIT_PTR(lp, 1, p2, s2);
28
+ if (idx1) {
29
+ for (; i--;) {
30
+ x = (dtype*)(p1+*idx1); idx1++;
31
+ y = format_<%=tp%>(fmt, x);
32
+ SET_DATA_STRIDE(p2, s2, VALUE, y);
33
+ }
34
+ } else {
35
+ for (; i--;) {
36
+ x = (dtype*)p1; p1+=s1;
37
+ y = format_<%=tp%>(fmt, x);
38
+ SET_DATA_STRIDE(p2, s2, VALUE, y);
39
+ }
40
+ }
41
+ }
42
+
43
+ /*
44
+ Format elements into strings.
45
+ @overload <%=method%> format
46
+ @param [String] format
47
+ @return [Numo::RObject] array of formated strings.
48
+ */
49
+ static VALUE
50
+ <%=c_func%>(int argc, VALUE *argv, VALUE self)
51
+ {
52
+ VALUE fmt=Qnil;
53
+
54
+ ndfunc_arg_in_t ain[2] = {{Qnil,0},{sym_option}};
55
+ ndfunc_arg_out_t aout[1] = {{numo_cRObject,0}};
56
+ ndfunc_t ndf = { <%=c_iter%>, FULL_LOOP_NIP, 2, 1, ain, aout };
57
+
58
+ rb_scan_args(argc, argv, "01", &fmt);
59
+ return na_ndloop(&ndf, 2, self, fmt);
60
+ }
@@ -0,0 +1,47 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t i;
5
+ char *p1;
6
+ ssize_t s1;
7
+ size_t *idx1;
8
+ dtype *x;
9
+ VALUE y;
10
+ volatile VALUE a;
11
+ VALUE fmt = lp->option;
12
+ INIT_COUNTER(lp, i);
13
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
14
+ a = rb_ary_new2(i);
15
+ rb_ary_push(lp->args[1].value, a);
16
+ if (idx1) {
17
+ for (; i--;) {
18
+ x = (dtype*)(p1 + *idx1); idx1++;
19
+ y = format_<%=tp%>(fmt, x);
20
+ rb_ary_push(a,y);
21
+ }
22
+ } else {
23
+ for (; i--;) {
24
+ x = (dtype*)p1; p1+=s1;
25
+ y = format_<%=tp%>(fmt, x);
26
+ rb_ary_push(a,y);
27
+ }
28
+ }
29
+ }
30
+
31
+ /*
32
+ Format elements into strings.
33
+ @overload <%=method%> format
34
+ @param [String] format
35
+ @return [Array] array of formated strings.
36
+ */
37
+ static VALUE
38
+ <%=c_func%>(int argc, VALUE *argv, VALUE self)
39
+ {
40
+ volatile VALUE fmt=Qnil;
41
+ ndfunc_arg_in_t ain[3] = {{Qnil,0},{sym_loop_opt},{sym_option}};
42
+ ndfunc_arg_out_t aout[1] = {{rb_cArray,0}}; // dummy?
43
+ ndfunc_t ndf = { <%=c_iter%>, FULL_LOOP_NIP, 3, 1, ain, aout };
44
+
45
+ rb_scan_args(argc, argv, "01", &fmt);
46
+ return na_ndloop_cast_narray_to_rarray(&ndf, self, fmt);
47
+ }
@@ -0,0 +1,25 @@
1
+ /*
2
+ <%=type_name%>.c
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 1999-2011,2013 by Masahiro TANAKA
5
+
6
+ This program is free software.
7
+ You can distribute/modify this program
8
+ under the same terms as Ruby itself.
9
+ NO WARRANTY.
10
+ */
11
+
12
+ #include <ruby.h>
13
+ #include <math.h>
14
+ #include "narray.h"
15
+ #include "SFMT.h"
16
+ #include "template.h"
17
+ #include "<%=type_name%>.h"
18
+
19
+ //#define cT <%=type_var%>
20
+ VALUE cT;
21
+ //#define cRT <%=real_type_var%>
22
+
23
+ #ifdef mTM
24
+ VALUE mTM;
25
+ #endif
@@ -0,0 +1,16 @@
1
+ static VALUE
2
+ <%=c_iter%>(char *ptr, size_t pos, VALUE fmt)
3
+ {
4
+ return format_<%=tp%>(fmt, (dtype*)(ptr+pos));
5
+ }
6
+
7
+ /*
8
+ Returns a string containing a human-readable representation of NArray.
9
+ @overload inspect
10
+ @return [String]
11
+ */
12
+ VALUE
13
+ <%=c_func%>(VALUE ary)
14
+ {
15
+ return na_ndloop_inspect(ary, <%=c_iter%>, Qnil);
16
+ }
@@ -0,0 +1,94 @@
1
+ static inline dtype
2
+ yield_map_with_index(dtype x, size_t *c, VALUE *a, int nd, int md)
3
+ {
4
+ int j;
5
+ VALUE y;
6
+
7
+ a[0] = m_data_to_num(x);
8
+ for (j=0; j<=nd; j++) {
9
+ a[j+1] = SIZE2NUM(c[j]);
10
+ }
11
+ y = rb_yield(rb_ary_new4(md,a));
12
+ return m_num_to_data(y);
13
+ }
14
+
15
+ void
16
+ <%=c_iter%>(na_loop_t *const lp)
17
+ {
18
+ size_t i;
19
+ char *p1, *p2;
20
+ ssize_t s1, s2;
21
+ size_t *idx1, *idx2;
22
+ dtype x;
23
+ VALUE *a;
24
+ size_t *c;
25
+ int nd, md;
26
+
27
+ c = (size_t*)(lp->opt_ptr);
28
+ nd = lp->ndim - 1;
29
+ md = lp->ndim + 1;
30
+ a = ALLOCA_N(VALUE,md);
31
+
32
+ INIT_COUNTER(lp, i);
33
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
34
+ INIT_PTR_IDX(lp, 1, p2, s2, idx2);
35
+
36
+ c[nd] = 0;
37
+ if (idx1) {
38
+ if (idx2) {
39
+ for (; i--;) {
40
+ GET_DATA_INDEX(p1,idx1,dtype,x);
41
+ x = yield_map_with_index(x,c,a,nd,md);
42
+ SET_DATA_INDEX(p2,idx2,dtype,x);
43
+ c[nd]++;
44
+ }
45
+ } else {
46
+ for (; i--;) {
47
+ GET_DATA_INDEX(p1,idx1,dtype,x);
48
+ x = yield_map_with_index(x,c,a,nd,md);
49
+ SET_DATA_STRIDE(p2,s2,dtype,x);
50
+ c[nd]++;
51
+ }
52
+ }
53
+ } else {
54
+ if (idx2) {
55
+ for (; i--;) {
56
+ GET_DATA_STRIDE(p1,s1,dtype,x);
57
+ x = yield_map_with_index(x,c,a,nd,md);
58
+ SET_DATA_INDEX(p2,idx2,dtype,x);
59
+ c[nd]++;
60
+ }
61
+ } else {
62
+ for (; i--;) {
63
+ GET_DATA_STRIDE(p1,s1,dtype,x);
64
+ x = yield_map_with_index(x,c,a,nd,md);
65
+ SET_DATA_STRIDE(p2,s2,dtype,x);
66
+ c[nd]++;
67
+ }
68
+ }
69
+ }
70
+ }
71
+
72
+ /*
73
+ Invokes the given block once for each element of self,
74
+ passing that element and indices along each axis as parameters.
75
+ Creates a new NArray containing the values returned by the block.
76
+ Inplace option is allowed, i.e., `nary.inplace.map` overwrites `nary`.
77
+
78
+ @overload <%=method%>
79
+
80
+ For a block {|x,i,j,...| ... }
81
+ @yield [x,i,j,...] x is an element, i,j,... are multidimensional indices.
82
+
83
+ @return [Numo::NArray] mapped array
84
+
85
+ */
86
+ static VALUE
87
+ <%=c_func%>(VALUE self)
88
+ {
89
+ ndfunc_arg_in_t ain[1] = {{Qnil,0}};
90
+ ndfunc_arg_out_t aout[1] = {{cT,0}};
91
+ ndfunc_t ndf = {<%=c_iter%>, FULL_LOOP, 1,1, ain,aout};
92
+
93
+ return na_ndloop_with_index(&ndf, 1, self);
94
+ }
@@ -0,0 +1,44 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t n;
5
+ char *p1, *p2;
6
+ dtype *buf;
7
+
8
+ INIT_COUNTER(lp, n);
9
+ p1 = (lp->args[0]).ptr + (lp->args[0].iter[0]).pos;
10
+ p2 = (lp->args[1]).ptr + (lp->args[1].iter[0]).pos;
11
+ buf = (dtype*)p1;
12
+
13
+ <%=tp%>_qsort(buf, n, sizeof(dtype));
14
+
15
+ <% if is_float %>
16
+ for (; n; n--) {
17
+ if (!isnan(buf[n-1])) break;
18
+ }
19
+ <% end %>
20
+
21
+ if (n==0) {
22
+ *(dtype*)p2 = buf[0];
23
+ }
24
+ else if (n%2==0) {
25
+ *(dtype*)p2 = (buf[n/2-1]+buf[n/2])/2;
26
+ }
27
+ else {
28
+ *(dtype*)p2 = buf[(n-1)/2];
29
+ }
30
+ }
31
+
32
+ static VALUE
33
+ <%=c_func%>(int argc, VALUE *argv, VALUE self)
34
+ {
35
+ VALUE reduce;
36
+ ndfunc_arg_in_t ain[2] = {{cT,0},{sym_reduce,0}};
37
+ ndfunc_arg_out_t aout[1] = {{INT2FIX(0),0}};
38
+ ndfunc_t ndf = {<%=c_iter%>, STRIDE_LOOP_NIP|NDF_FLAT_REDUCE, 2,1, ain,aout};
39
+
40
+ self = na_copy(self);
41
+ reduce = na_reduce_dimension(argc, argv, 1, &self); // v[0] = self
42
+
43
+ return na_ndloop(&ndf, 2, self, reduce);
44
+ }
@@ -0,0 +1,47 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t i;
5
+ char *p1;
6
+ ssize_t s1;
7
+ dtype x,xmin,xmax;
8
+
9
+ INIT_COUNTER(lp, i);
10
+ INIT_PTR(lp, 0, p1, s1);
11
+
12
+ xmin = xmax = *(dtype*)p1;
13
+ p1 += s1;
14
+ i--;
15
+ for (; i--;) {
16
+ x = *(dtype*)p1;
17
+ if (!m_isnan(x)) {
18
+ if (m_isnan(xmax) || m_gt(x,xmax)) {
19
+ xmax = x;
20
+ }
21
+ if (m_isnan(xmin) || m_lt(x,xmin)) {
22
+ xmin = x;
23
+ }
24
+ }
25
+ p1 += s1;
26
+ }
27
+ *(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
28
+ *(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
29
+ }
30
+
31
+ /*
32
+ <%=method.capitalize%> of self.
33
+ @overload <%=method%>(*args)
34
+ @param [Array of Numeric,Range] args Affected dimensions.
35
+ @return [Numo::<%=class_name%>,Numo::<%=class_name%>] min and max of self.
36
+ */
37
+ static VALUE
38
+ <%=c_func%>(int argc, VALUE *argv, VALUE self)
39
+ {
40
+ VALUE reduce;
41
+ ndfunc_arg_in_t ain[2] = {{cT,0},{sym_reduce,0}};
42
+ ndfunc_arg_out_t aout[2] = {{cT,0},{cT,0}};
43
+ ndfunc_t ndf = {<%=c_iter%>, STRIDE_LOOP_NIP|NDF_FLAT_REDUCE|NDF_EXTRACT, 2,2, ain,aout};
44
+
45
+ reduce = na_reduce_dimension(argc, argv, 1, &self);
46
+ return na_ndloop(&ndf, 2, self, reduce);
47
+ }
@@ -0,0 +1,49 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t i;
5
+ dtype x, y, a;
6
+
7
+ x = *(dtype*)(lp->args[0].ptr + lp->args[0].iter[0].pos);
8
+ i = lp->narg - 2;
9
+ y = *(dtype*)(lp->args[i].ptr + lp->args[i].iter[0].pos);
10
+ for (; --i;) {
11
+ y = m_mul(x,y);
12
+ a = *(dtype*)(lp->args[i].ptr + lp->args[i].iter[0].pos);
13
+ y = m_add(y,a);
14
+ }
15
+ i = lp->narg - 1;
16
+ *(dtype*)(lp->args[i].ptr + lp->args[i].iter[0].pos) = y;
17
+ }
18
+
19
+ /*
20
+ Polynomial.: a0 + a1*x + a2*x**2 + a3*x**3 + ... + an*x**n
21
+ @overload <%=method%> a0, a1, ...
22
+ @param [Numo::NArray,Numeric] a0
23
+ @param [Numo::NArray,Numeric] a1 , ...
24
+ @return [Numo::<%=class_name%>]
25
+ */
26
+ static VALUE
27
+ <%=c_func%>(VALUE self, VALUE args)
28
+ {
29
+ int argc, i;
30
+ VALUE *argv;
31
+ volatile VALUE v, a;
32
+ ndfunc_arg_out_t aout[1] = {{cT,0}};
33
+ ndfunc_t ndf = { <%=c_iter%>, NO_LOOP, 0, 1, 0, aout };
34
+
35
+ argc = RARRAY_LEN(args);
36
+ ndf.nin = argc+1;
37
+ ndf.ain = ALLOCA_N(ndfunc_arg_in_t,argc+1);
38
+ for (i=0; i<argc+1; i++) {
39
+ ndf.ain[i].type = cT;
40
+ }
41
+ argv = ALLOCA_N(VALUE,argc+1);
42
+ argv[0] = self;
43
+ for (i=0; i<argc; i++) {
44
+ argv[i+1] = RARRAY_PTR(args)[i];
45
+ }
46
+ a = rb_ary_new4(argc+1, argv);
47
+ v = na_ndloop2(&ndf, a);
48
+ return numo_<%=tp%>_extract(v);
49
+ }
@@ -0,0 +1,74 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t i;
5
+ char *p1, *p2, *p3;
6
+ ssize_t s1, s2, s3;
7
+ dtype x, y;
8
+ INIT_COUNTER(lp, i);
9
+ INIT_PTR(lp, 0, p1, s1);
10
+ INIT_PTR(lp, 1, p2, s2);
11
+ INIT_PTR(lp, 2, p3, s3);
12
+ for (; i--;) {
13
+ GET_DATA_STRIDE(p1,s1,dtype,x);
14
+ GET_DATA_STRIDE(p2,s2,dtype,y);
15
+ x = m_pow(x,y);
16
+ SET_DATA_STRIDE(p3,s3,dtype,x);
17
+ }
18
+ }
19
+
20
+ static void
21
+ <%=c_iter%>_int32(na_loop_t *const lp)
22
+ {
23
+ size_t i;
24
+ char *p1, *p2, *p3;
25
+ ssize_t s1, s2, s3;
26
+ dtype x;
27
+ int32_t y;
28
+ INIT_COUNTER(lp, i);
29
+ INIT_PTR(lp, 0, p1, s1);
30
+ INIT_PTR(lp, 1, p2, s2);
31
+ INIT_PTR(lp, 2, p3, s3);
32
+ for (; i--;) {
33
+ GET_DATA_STRIDE(p1,s1,dtype,x);
34
+ GET_DATA_STRIDE(p2,s2,int32_t,y);
35
+ x = m_pow_int(x,y);
36
+ SET_DATA_STRIDE(p3,s3,dtype,x);
37
+ }
38
+ }
39
+
40
+ static VALUE
41
+ <%=c_func%>_self(VALUE self, VALUE other)
42
+ {
43
+ ndfunc_arg_in_t ain[2] = {{cT,0},{cT,0}};
44
+ ndfunc_arg_in_t ain_i[2] = {{cT,0},{numo_cInt32,0}};
45
+ ndfunc_arg_out_t aout[1] = {{cT,0}};
46
+ ndfunc_t ndf = { <%=c_iter%>, STRIDE_LOOP, 2, 1, ain, aout };
47
+ ndfunc_t ndf_i = { <%=c_iter%>_int32, STRIDE_LOOP, 2, 1, ain_i, aout };
48
+
49
+ // fixme : use na.integer?
50
+ if (FIXNUM_P(other) || rb_obj_is_kind_of(other,numo_cInt32)) {
51
+ return na_ndloop(&ndf_i, 2, self, other);
52
+ } else {
53
+ return na_ndloop(&ndf, 2, self, other);
54
+ }
55
+ }
56
+
57
+ /*
58
+ Binary power.
59
+ @overload <%=op_map%> other
60
+ @param [Numo::NArray,Numeric] other
61
+ @return [Numo::NArray] self to the other-th power.
62
+ */
63
+ static VALUE
64
+ <%=c_func%>(VALUE self, VALUE other)
65
+ {
66
+ VALUE klass, v;
67
+ klass = na_upcast(CLASS_OF(self),CLASS_OF(other));
68
+ if (klass==cT) {
69
+ return <%=c_func%>_self(self,other);
70
+ } else {
71
+ v = rb_funcall(klass, id_cast, 1, self);
72
+ return rb_funcall(v, id_pow, 1, other);
73
+ }
74
+ }
@@ -0,0 +1,17 @@
1
+ static dtype pow_<%=tp%>(dtype x, int p)
2
+ {
3
+ dtype r = m_one;
4
+ switch(p) {
5
+ case 2: return m_square(x);
6
+ case 3: return m_mul(m_square(x),x);
7
+ case 1: return x;
8
+ case 0: return m_one;
9
+ }
10
+ if (p<0) return m_zero;
11
+ while (p) {
12
+ if ((p%2) == 1) r = m_mul(r,x);
13
+ x = m_square(x);
14
+ p /= 2;
15
+ }
16
+ return r;
17
+ }