numru-narray 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ EXPORTS
2
+ Init_narray
3
+ na_sizeof
4
+ na_make_object
5
+ na_make_scalar
6
+ na_make_empty
7
+ na_get_typecode
8
+ na_clear_data
9
+ na_clone
10
+ na_fill
11
+ na_copy_nary
12
+ na_to_array
13
+ na_ary_to_nary
14
+ na_object_type
15
+ na_cast_object
16
+ na_cast_unless_narray
17
+ na_cast_unless_array
18
+ na_upcast_object
19
+ na_dup_w_type
20
+ na_change_type
21
+ na_upcast_type
22
+ na_to_narray
23
+ na_aset
24
+ na_aref
25
+ na_slice
26
+ na_count_true
27
+ na_count_false
28
+ na_aref_mask
29
+ na_aset_mask
@@ -0,0 +1,231 @@
1
+ /*
2
+ narray.h
3
+ NArray with big memory support
4
+ (C) Copyright 1999-2011 by Masahiro TANAKA
5
+ (C) Copyright 2013-2015 by Seiya NISHIZAWA
6
+
7
+ This program is free software.
8
+ You can distribute/modify this program
9
+ under the same terms as Ruby itself.
10
+ NO WARRANTY.
11
+ */
12
+ #ifndef NARRAY_H
13
+ #define NARRAY_H
14
+
15
+ #include <math.h>
16
+
17
+ #include "narray_config.h"
18
+
19
+ #ifdef HAVE_STDINT_H
20
+ # include <stdint.h>
21
+ #endif
22
+
23
+ #ifdef HAVE_SYS_TYPES_H
24
+ # include <sys/types.h>
25
+ #endif
26
+
27
+ #define NARRAY_VERSION "0.6.0.8"
28
+ #define NARRAY_VERSION_CODE 608
29
+
30
+ #define NUMRU_NARRAY_VERSION "1.0.0"
31
+ #define NUMRU_NARRAY_VERSION_CODE 100
32
+
33
+ /* big memory support */
34
+ #define NARRAY_BIGMEM 1
35
+
36
+ /*
37
+ Data types used in NArray :
38
+ Please modify these types if your system has any different type.
39
+ */
40
+
41
+
42
+ /* NA_BYTE : unsigned 8-bit integer */
43
+ #ifndef HAVE_U_INT8_T
44
+ # ifdef HAVE_UINT8_T
45
+ typedef uint8_t u_int8_t;
46
+ # else
47
+ typedef unsigned char u_int8_t;
48
+ # endif
49
+ #endif
50
+
51
+ #ifndef HAVE_INT16_T
52
+ # if SIZEOF_SHORT == 2
53
+ typedef short int16_t; /* NA_SINT */
54
+ # else
55
+ ---->> Please define int16_t manually because sizeof(short) != 2. <<----
56
+ # endif
57
+ #endif /* HAVE_INT16_T */
58
+
59
+ #ifndef HAVE_INT32_T
60
+ # if SIZEOF_LONG == 4
61
+ typedef long int32_t; /* NA_LINT */
62
+ # else
63
+ # if SIZEOF_INT == 4
64
+ typedef int int32_t; /* NA_LINT */
65
+ # else
66
+ ---->> Please define int32_t manually because sizeof(long) != 4. <<----
67
+ # endif
68
+ # endif
69
+ #endif /* HAVE_INT32_T */
70
+
71
+ #ifndef HAVE_INT64_T
72
+ # if SIZEOF_LONG_LONG == 8
73
+ typedef long long int64_t; /* NA_LLINT */
74
+ # else
75
+ ---->> Please define int64_t manually because sizeof(long long) != 8. <<----
76
+ # endif
77
+ #endif /* HAVE_INT64_T */
78
+
79
+ /* unsigned 64-bit integer */
80
+ #ifndef HAVE_U_INT64_T
81
+ # ifdef HAVE_UINT64_T
82
+ typedef uint64_t u_int64_t;
83
+ # else
84
+ # if SIZEOF_LONG_LONG == 8
85
+ typedef unsigned long long u_int64_t;
86
+ # else
87
+ ---->> Please define u_int64_t manually because sizeof(long long) != 8. <<----
88
+ # endif
89
+ # endif
90
+ #endif /* HAVE_U_INT64_T */
91
+
92
+ /* shape type */
93
+ #if SIZEOF_VOIDP == 4
94
+ typedef int32_t na_shape_t;
95
+ #elif SIZEOF_VOIDP == 8
96
+ typedef int64_t na_shape_t;
97
+ #else
98
+ ---->> Please define na_shape_t manually because sizeof(*void) != 4, 8. <<----
99
+ #endif
100
+ #if SIZEOF_INT == SIZEOF_VOIDP
101
+ #define NUM2SHAPE(v) NUM2INT(v)
102
+ #define SHAPE2NUM(v) INT2NUM(v)
103
+ #elif SIZEOF_LONG == SIZEOF_VOIDP
104
+ #define NUM2SHAPE(v) NUM2LONG(v)
105
+ #define SHAPE2NUM(v) LONG2NUM(v)
106
+ #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
107
+ #define NUM2SHAPE(v) NUM2LL(v)
108
+ #define SHAPE2NUM(v) LL2NUM(v)
109
+ #else
110
+ ---->> Please define NUM2SHAPE and SHAPE2NUM manually because sizeof(int,long,long long) != sizeof(*void). <<----
111
+ #endif
112
+
113
+
114
+ typedef struct { float r,i; } scomplex;
115
+ typedef struct { double r,i; } dcomplex;
116
+
117
+ enum NArray_Types {
118
+ NA_NONE,
119
+ NA_BYTE, /* 1 */
120
+ NA_SINT, /* 2 */
121
+ NA_LINT, /* 3 */
122
+ NA_LLINT, /* 4 */
123
+ NA_SFLOAT, /* 5 */
124
+ NA_DFLOAT, /* 6 */
125
+ NA_SCOMPLEX, /* 7 */
126
+ NA_DCOMPLEX, /* 8 */
127
+ NA_ROBJ, /* 9 */
128
+ NA_NTYPES /* 10 */
129
+ };
130
+
131
+ #if SIZEOF_VOIDP == 4
132
+ #define NA_SIZE NA_LINT
133
+ #elif SIZEOF_VOIDP == 8
134
+ #define NA_SIZE NA_LLINT
135
+ #else
136
+ ---->> Please define NA_SIZE manually because sizeof(*void) != 4 or 8. <<----
137
+ #endif
138
+
139
+ /* struct for Numerical Array */
140
+ struct NARRAY {
141
+ int rank; /* # of dimension */
142
+ na_shape_t total; /* # of total element */
143
+ int type; /* data type */
144
+ na_shape_t *shape;
145
+ char *ptr; /* pointer to data */
146
+ VALUE ref; /* NArray object wrapping this structure */
147
+ };
148
+
149
+ #ifndef NARRAY_C
150
+ extern VALUE cNArray;
151
+
152
+ extern const int na_sizeof[NA_NTYPES+1];
153
+ #endif
154
+
155
+ #define NA_MAX_RANK 15
156
+
157
+ #define GetNArray(obj,var) Data_Get_Struct(obj, struct NARRAY, var)
158
+ #define IsNArray(obj) (rb_obj_is_kind_of(obj,cNArray)==Qtrue)
159
+
160
+ #define NA_PTR(a,p) ((a)->ptr+(p)*na_sizeof[(a)->type])
161
+ #define NA_STRUCT(val) ((struct NARRAY*)DATA_PTR(val))
162
+ #define NA_PTR_TYPE(val,type) (type)(((struct NARRAY*)DATA_PTR(val))->ptr)
163
+ #define NA_RANK(val) (((struct NARRAY*)DATA_PTR(val))->rank)
164
+ #define NA_TYPE(val) (((struct NARRAY*)DATA_PTR(val))->type)
165
+ #define NA_TOTAL(val) (((struct NARRAY*)DATA_PTR(val))->total)
166
+ #define NA_SHAPE0(val) (((struct NARRAY*)DATA_PTR(val))->shape[0])
167
+ #define NA_SHAPE1(val) (((struct NARRAY*)DATA_PTR(val))->shape[1])
168
+
169
+ #define NA_IsNArray(obj) \
170
+ (rb_obj_is_kind_of(obj,cNArray)==Qtrue)
171
+ #define NA_IsArray(obj) \
172
+ (TYPE(obj)==T_ARRAY || rb_obj_is_kind_of(obj,cNArray)==Qtrue)
173
+ #define NA_IsROBJ(d) ((d)->type==NA_ROBJ)
174
+ #define NA_IsINTEGER(a) \
175
+ ((a)->type==NA_BYTE || (a)->type==NA_SINT || (a)->type==NA_LINT )
176
+ #define NA_IsCOMPLEX(a) \
177
+ ((a)->type==NA_SCOMPLEX || (a)->type==NA_DCOMPLEX)
178
+ #define NA_MAX(a,b) (((a)>(b))?(a):(b))
179
+ #define NA_MAX3(a,b,c) NA_MAX( (NA_MAX((a),(b))), (c) )
180
+
181
+
182
+ #define NA_SWAP(a,b,tmp) {(tmp)=(a);(a)=(b);(b)=(tmp);}
183
+
184
+ #define na_class_dim(klass) NUM2INT(rb_const_get(klass, na_id_class_dim))
185
+
186
+ #define NUM2REAL(v) NUM2DBL( rb_funcall((v),na_id_real,0) )
187
+ #define NUM2IMAG(v) NUM2DBL( rb_funcall((v),na_id_imag,0) )
188
+
189
+ #define NA_ALLOC_SLICE(slc,nc,shp,np) \
190
+ { slc = (struct slice*)xmalloc( sizeof(struct slice)*(nc) + \
191
+ sizeof(na_shape_t)*(np) ); \
192
+ shp = (na_shape_t*)&( (slc)[nc] ); }
193
+
194
+
195
+ /* Function Prototypes */
196
+
197
+ /* narray.c */
198
+ VALUE na_make_object(int type, int rank, na_shape_t *shape, VALUE klass);
199
+ VALUE na_make_scalar(VALUE obj, int type);
200
+ VALUE na_make_empty(int type, VALUE klass);
201
+ int na_get_typecode(VALUE v);
202
+ void na_clear_data(struct NARRAY *ary);
203
+ VALUE na_clone(VALUE self);
204
+ VALUE na_fill(VALUE self, volatile VALUE obj);
205
+ void na_copy_nary(struct NARRAY *dst, struct NARRAY *src);
206
+
207
+ /* na_array.c */
208
+ VALUE na_to_array(VALUE obj);
209
+ VALUE na_make_inspect(VALUE self);
210
+ VALUE na_ary_to_nary(VALUE ary, VALUE klass);
211
+ int na_object_type(VALUE v);
212
+
213
+ VALUE na_cast_object(VALUE obj, int type);
214
+ VALUE na_cast_unless_narray(VALUE obj, int type);
215
+ VALUE na_cast_unless_array(VALUE obj, int type);
216
+ VALUE na_upcast_object(VALUE obj, int type);
217
+ VALUE na_dup_w_type(VALUE obj, int type);
218
+ VALUE na_change_type(VALUE obj, int type);
219
+ VALUE na_upcast_type(VALUE obj, int type);
220
+ VALUE na_to_narray(VALUE obj);
221
+
222
+ /* na_index.c */
223
+ VALUE na_aset(int argc, VALUE *argv, VALUE self);
224
+ VALUE na_aref(int argc, VALUE *argv, VALUE self);
225
+ VALUE na_slice(int argc, VALUE *argv, VALUE self);
226
+ VALUE na_count_true(VALUE self);
227
+ VALUE na_count_false(VALUE self);
228
+ VALUE na_aref_mask(VALUE self, VALUE mask);
229
+ void na_aset_mask(VALUE self, VALUE mask, VALUE v);
230
+
231
+ #endif /* ifndef NARRAY_H */
@@ -0,0 +1,218 @@
1
+ /*
2
+ narray_local.h
3
+ Numerical Array Extention for Ruby
4
+ (C) Copyright 1999-2008 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
+ typedef int32_t na_index_t;
13
+
14
+ struct slice {
15
+ char *p; /* pointer to data --- used in loop */
16
+ na_shape_t n; /* n of indices of this rank */
17
+ na_shape_t pstep; /* = step * stride * elmsz --- set in na_init_slice */
18
+ na_shape_t pbeg; /* = beg * stride * elmsz --- set in na_init_slice */
19
+ na_shape_t stride; /* = shape[0]*shape[1]*...*shape[r-1]
20
+ --- set in na_init_slice */
21
+ na_shape_t step;
22
+ na_shape_t beg;
23
+ na_shape_t *idx; /* NULL if normal step */
24
+ };
25
+
26
+ typedef void (*na_setfunc_t[NA_NTYPES][NA_NTYPES]) ();
27
+ typedef void (*na_func_t[NA_NTYPES]) ();
28
+ typedef void (*na_ufunc_t[NA_NTYPES]) ();
29
+ typedef void (*na_bifunc_t[NA_NTYPES]) ();
30
+ typedef void (*na_mathfunc_t[NA_NTYPES]) ();
31
+ typedef int (*na_sortfunc_t[NA_NTYPES]) (const void *, const void *);
32
+
33
+ /* function arrays */
34
+ extern na_setfunc_t SetFuncs;
35
+ extern na_ufunc_t SwpFuncs;
36
+ extern na_ufunc_t H2NFuncs;
37
+ extern na_ufunc_t H2VFuncs;
38
+ extern na_ufunc_t NegFuncs;
39
+ extern na_ufunc_t RcpFuncs;
40
+ extern na_ufunc_t AbsFuncs;
41
+ extern na_ufunc_t RealFuncs;
42
+ extern na_ufunc_t ImagFuncs;
43
+ extern na_ufunc_t AnglFuncs;
44
+ extern na_ufunc_t ImagMulFuncs;
45
+ extern na_ufunc_t ConjFuncs;
46
+ extern na_ufunc_t FloorFuncs;
47
+ extern na_ufunc_t CeilFuncs;
48
+ extern na_ufunc_t RoundFuncs;
49
+ extern na_ufunc_t ToStrFuncs;
50
+ extern na_ufunc_t InspFuncs;
51
+ extern na_ufunc_t IndGenFuncs;
52
+ extern na_ufunc_t AddUFuncs;
53
+ extern na_ufunc_t SbtUFuncs;
54
+ extern na_ufunc_t MulUFuncs;
55
+ extern na_ufunc_t DivUFuncs;
56
+ extern na_ufunc_t ModUFuncs;
57
+ extern na_bifunc_t AddBFuncs;
58
+ extern na_bifunc_t SbtBFuncs;
59
+ extern na_bifunc_t MulBFuncs;
60
+ extern na_bifunc_t DivBFuncs;
61
+ extern na_bifunc_t MulAddFuncs;
62
+ extern na_bifunc_t MulSbtFuncs;
63
+ extern na_bifunc_t ModBFuncs;
64
+ extern na_bifunc_t BAnFuncs;
65
+ extern na_bifunc_t BOrFuncs;
66
+ extern na_bifunc_t BXoFuncs;
67
+ extern na_ufunc_t BRvFuncs;
68
+ extern na_bifunc_t ImgSetFuncs;
69
+ extern na_setfunc_t PowFuncs;
70
+ extern na_bifunc_t atan2Funcs;
71
+ extern na_bifunc_t CmpFuncs;
72
+ extern na_bifunc_t EqlFuncs;
73
+ extern na_ufunc_t AndFuncs;
74
+ extern na_ufunc_t Or_Funcs;
75
+ extern na_ufunc_t XorFuncs;
76
+ extern na_ufunc_t NotFuncs;
77
+ extern na_ufunc_t MinFuncs;
78
+ extern na_ufunc_t MaxFuncs;
79
+ extern na_sortfunc_t SortFuncs;
80
+ extern na_sortfunc_t SortIdxFuncs;
81
+ extern na_bifunc_t RefMaskFuncs;
82
+ extern na_bifunc_t SetMaskFuncs;
83
+
84
+ /* variables */
85
+
86
+ extern VALUE rb_mNMath;
87
+ extern ID na_id_beg, na_id_end, na_id_exclude_end;
88
+ extern ID na_id_minus, na_id_abs, na_id_power;
89
+ extern ID na_id_compare, na_id_and, na_id_or;
90
+ extern ID na_id_equal;
91
+ extern ID na_id_class_dim;
92
+ extern ID na_id_add, na_id_sbt, na_id_mul, na_id_div, na_id_mod;
93
+ extern ID na_id_real, na_id_imag;
94
+ extern ID na_id_coerce_rev;
95
+ extern ID na_id_new;
96
+ extern ID na_id_Complex;
97
+
98
+ extern const int na_upcast[NA_NTYPES][NA_NTYPES];
99
+ extern const int na_no_cast[NA_NTYPES];
100
+ extern const int na_cast_real[NA_NTYPES];
101
+ extern const int na_cast_comp[NA_NTYPES];
102
+ extern const int na_cast_round[NA_NTYPES];
103
+ extern const int na_cast_byte[NA_NTYPES];
104
+
105
+ extern const char *na_typestring[];
106
+
107
+ extern VALUE cNArrayScalar, cComplex;
108
+
109
+ /* narray.c */
110
+ VALUE na_newdim_ref(int argc, VALUE *argv, VALUE self);
111
+
112
+ /* na_func.c */
113
+ void na_shape_max3(int ndim, na_shape_t *max_shp, na_shape_t *shp1, na_shape_t *shp2, na_shape_t *shp3);
114
+ void na_shape_copy( int ndim, na_shape_t *shape, struct NARRAY *a );
115
+
116
+ void na_init_slice(struct slice *s, int rank, na_shape_t *shape, int elmsz);
117
+ void na_set_slice_1obj(int ndim, struct slice *slc, na_shape_t *shape);
118
+ int na_set_slice_3obj( int ndim,
119
+ struct slice *s1, struct slice *s2, struct slice *s3,
120
+ na_shape_t *shp1, na_shape_t *shp2, na_shape_t *shp3, na_shape_t *shape );
121
+ void na_loop_general(struct NARRAY *a1, struct NARRAY *a2,
122
+ struct slice *s1, struct slice *s2, void (*func)());
123
+ void na_loop_index_ref(struct NARRAY *a1, struct NARRAY *a2,
124
+ struct slice *s1, struct slice *s2, void (*func)());
125
+
126
+ /* na_index.c */
127
+ void na_aset_slice(struct NARRAY *dst, struct NARRAY *src, struct slice *s1);
128
+ int na_shrink_class(int class_dim, int *shrink);
129
+ VALUE na_shrink_rank(VALUE obj, int class_dim, int *shrink);
130
+
131
+ #define rb_complex_new(r,i) \
132
+ rb_funcall(rb_mKernel, na_id_Complex, 2, rb_float_new(r), rb_float_new(i))
133
+
134
+
135
+ typedef union {
136
+ u_int8_t b[2];
137
+ int16_t s;
138
+ } na_size16_t;
139
+
140
+ typedef union {
141
+ u_int8_t b[4];
142
+ int32_t i;
143
+ float f;
144
+ } na_size32_t;
145
+
146
+ typedef union {
147
+ u_int8_t b[8];
148
+ u_int64_t g;
149
+ float f[2];
150
+ double d;
151
+ } na_size64_t;
152
+
153
+ typedef union {
154
+ u_int8_t b[16];
155
+ double d[2];
156
+ } na_size128_t;
157
+
158
+
159
+ #define swap16(d,s) \
160
+ (d).b[0]=(s).b[1];\
161
+ (d).b[1]=(s).b[0];
162
+
163
+ #define swap32(d,s) \
164
+ (d).b[0]=(s).b[3];\
165
+ (d).b[1]=(s).b[2];\
166
+ (d).b[2]=(s).b[1];\
167
+ (d).b[3]=(s).b[0];
168
+
169
+ #define swap64(d,s) \
170
+ (d).b[0]=(s).b[7];\
171
+ (d).b[1]=(s).b[6];\
172
+ (d).b[2]=(s).b[5];\
173
+ (d).b[3]=(s).b[4];\
174
+ (d).b[4]=(s).b[3];\
175
+ (d).b[5]=(s).b[2];\
176
+ (d).b[6]=(s).b[1];\
177
+ (d).b[7]=(s).b[0];
178
+
179
+ #define swap64c(d,s) \
180
+ (d).b[0]=(s).b[3];\
181
+ (d).b[1]=(s).b[2];\
182
+ (d).b[2]=(s).b[1];\
183
+ (d).b[3]=(s).b[0];\
184
+ (d).b[4]=(s).b[7];\
185
+ (d).b[5]=(s).b[6];\
186
+ (d).b[6]=(s).b[5];\
187
+ (d).b[7]=(s).b[4];
188
+
189
+ #define swap128c(d,s) \
190
+ (d).b[0]=(s).b[7];\
191
+ (d).b[1]=(s).b[6];\
192
+ (d).b[2]=(s).b[5];\
193
+ (d).b[3]=(s).b[4];\
194
+ (d).b[4]=(s).b[3];\
195
+ (d).b[5]=(s).b[2];\
196
+ (d).b[6]=(s).b[1];\
197
+ (d).b[7]=(s).b[0];\
198
+ (d).b[8]=(s).b[15];\
199
+ (d).b[9]=(s).b[14];\
200
+ (d).b[10]=(s).b[13];\
201
+ (d).b[11]=(s).b[12];\
202
+ (d).b[12]=(s).b[11];\
203
+ (d).b[13]=(s).b[10];\
204
+ (d).b[14]=(s).b[9];\
205
+ (d).b[15]=(s).b[8];
206
+
207
+ #if !defined RSTRING_LEN
208
+ #define RSTRING_LEN(a) RSTRING(a)->len
209
+ #endif
210
+ #if !defined RSTRING_PTR
211
+ #define RSTRING_PTR(a) RSTRING(a)->ptr
212
+ #endif
213
+ #if !defined RARRAY_LEN
214
+ #define RARRAY_LEN(a) RARRAY(a)->len
215
+ #endif
216
+ #if !defined RARRAY_PTR
217
+ #define RARRAY_PTR(a) RARRAY(a)->ptr
218
+ #endif
@@ -0,0 +1,4 @@
1
+ unless ENV["OMP_NUM_THREADS"]
2
+ ENV["OMP_NUM_THREADS"] = "1"
3
+ end
4
+ require "numru/narray/narray"