forked from ruby-numo/numo-narray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec.rb
More file actions
427 lines (371 loc) · 7.67 KB
/
spec.rb
File metadata and controls
427 lines (371 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
def_id "cast"
def_id "mulsum"
def_id "to_a"
if is_complex
def_id "real"
def_id "imag"
else
def_id "divmod"
end
if is_float
def_id "nearly_eq"
def_id "copysign"
end
if is_int
def_id "<<","left_shift"
def_id ">>","right_shift"
end
if is_comparable && !is_object
def_id "gt"
def_id "ge"
def_id "lt"
def_id "le"
end
if is_comparable
def_id "nan"
end
if is_object
def_id "bit_and"
def_id "bit_or"
def_id "bit_xor"
def_id "bit_not"
def_id "abs"
def_id "reciprocal"
def_id "square"
def_id "floor"
def_id "round"
def_id "ceil"
def_id "truncate"
def_id "nan?"
def_id "infinite?"
def_id "finite?"
def_id "-@","minus"
def_id "==","eq"
def_id "!=","ne"
def_id ">" ,"gt"
def_id ">=","ge"
def_id "<" ,"lt"
def_id "<=","le"
def_id "<=>","ufo"
else
def_id "eq"
def_id "ne"
end
if is_int && !is_object
def_id "minlength" # for bincount
end
# Constants
if is_bit
def_const "ELEMENT_BIT_SIZE", "INT2FIX(1)"
def_const "ELEMENT_BYTE_SIZE", "rb_float_new(1.0/8)"
def_const "CONTIGUOUS_STRIDE", "INT2FIX(1)"
else
def_const "ELEMENT_BIT_SIZE", "INT2FIX(sizeof(dtype)*8)"
def_const "ELEMENT_BYTE_SIZE", "INT2FIX(sizeof(dtype))"
def_const "CONTIGUOUS_STRIDE", "INT2FIX(sizeof(dtype))"
end
if !is_object
if is_float
def_const "EPSILON", "M_EPSILON"
end
if is_float || is_int
def_const "MAX", "M_MAX"
def_const "MIN", "M_MIN"
end
end
# Un-define
if is_object
undef_singleton_method "from_binary"
undef_method "to_binary"
undef_method "swap_byte"
undef_method "to_network"
undef_method "to_vacs"
undef_method "to_host"
undef_method "to_swapped"
end
# Allocation
def_alloc_func "alloc_func"
def_method "allocate"
# Type conversion
def_method "extract"
def_method "new_dim0"
def_method "store" do
extend StoreFrom
store_numeric
store_from "Bit"
if is_complex
store_from "DComplex","dcomplex","m_from_dcomplex"
store_from "SComplex","scomplex","m_from_scomplex"
end
store_from "DFloat","double", "m_from_real"
store_from "SFloat","float", "m_from_real"
store_from "Int64", "int64_t", "m_from_int64"
store_from "Int32", "int32_t", "m_from_int32"
store_from "Int16", "int16_t", "m_from_sint"
store_from "Int8", "int8_t", "m_from_sint"
store_from "UInt64","u_int64_t","m_from_uint64"
store_from "UInt32","u_int32_t","m_from_uint32"
store_from "UInt16","u_int16_t","m_from_sint"
store_from "UInt8", "u_int8_t", "m_from_sint"
store_from "RObject", "VALUE", "m_num_to_data"
store_array
end
def_method "extract_data"
def_method "cast_array"
def_singleton_method "cast"
def_method "aref", op:"[]"
def_method "aset", op:"[]="
def_method "coerce_cast"
def_method "to_a"
def_method "fill"
def_method "format"
def_method "format_to_a"
def_method "inspect"
# Array manipulation
def_method "each"
unary "map" if !is_bit
def_method "each_with_index"
if is_bit
unary "copy"
unary "not", "~"
binary "and", "&"
binary "or" , "|"
binary "xor", "^"
binary "eq"
bit_count "count_true"
def_alias "count_1","count_true"
def_alias "count","count_true"
bit_count "count_false"
def_alias "count_0","count_false"
bit_reduce "all?", 1
bit_reduce "any?", 0
def_method "none?", "none_p"
def_method "where"
def_method "where2"
def_method "mask"
else
def_method "map_with_index"
# Arithmetic
unary2 "abs", "rtype", "cRT"
binary "add", "+"
binary "sub", "-"
binary "mul", "*"
binary "div", "/"
if !is_complex
binary "mod", "%"
binary2 "divmod"
end
if !is_bit
pow
def_id "**","pow"
def_alias "pow","**"
end
unary "minus", "-@"
unary "reciprocal"
unary "sign"
unary "square"
# Complex
if is_complex
unary "conj"
unary "im"
unary2 "real", "rtype", "cRT"
unary2 "imag", "rtype", "cRT"
unary2 "arg", "rtype", "cRT"
def_alias "angle","arg"
set2 "set_imag", "rtype", "cRT"
set2 "set_real", "rtype", "cRT"
def_alias "imag=","set_imag"
def_alias "real=","set_real"
else
def_alias "conj", "view"
def_alias "im", "view"
end
def_alias "conjugate","conj"
# base_cond
cond_binary "eq"
cond_binary "ne"
# nearly_eq : x=~y is true if |x-y| <= (|x|+|y|)*epsilon
if is_float
cond_binary "nearly_eq"
else
def_alias "nearly_eq", "eq"
end
def_alias "close_to", "nearly_eq"
# Integer
if is_int
binary "bit_and", "&"
binary "bit_or" , "|"
binary "bit_xor", "^"
unary "bit_not", "~"
binary "left_shift", "<<"
binary "right_shift", ">>"
if !is_object
def_alias "floor", "view"
def_alias "round", "view"
def_alias "ceil", "view"
def_alias "trunc", "view"
def_alias "rint", "view"
end
end
if is_float
unary "floor"
unary "round"
unary "ceil"
unary "trunc"
if !is_object
unary "rint"
binary "copysign"
if !is_complex
cond_unary "signbit"
def_method "modf", "unary_ret2"
end
end
end
if is_comparable
cond_binary "gt"
cond_binary "ge"
cond_binary "lt"
cond_binary "le"
def_alias ">", "gt"
def_alias ">=","ge"
def_alias "<", "lt"
def_alias "<=","le"
def_method "clip"
end
# Float
if is_float
cond_unary "isnan"
cond_unary "isinf"
cond_unary "isposinf"
cond_unary "isneginf"
cond_unary "isfinite"
end
if is_int && !is_object
if is_unsigned
accum "sum","u_int64_t","numo_cUInt64"
accum "prod","u_int64_t","numo_cUInt64"
else
accum "sum","int64_t","numo_cInt64"
accum "prod","int64_t","numo_cInt64"
end
else
accum "sum","dtype","cT"
accum "prod","dtype","cT"
end
if is_double_precision
accum "kahan_sum","dtype","cT"
end
if is_float
accum "mean","dtype","cT"
accum "stddev","rtype","cRT"
accum "var","rtype","cRT"
accum "rms","rtype","cRT"
end
if is_comparable
accum "min","dtype","cT"
accum "max","dtype","cT"
accum "ptp","dtype","cT"
accum_index "max_index"
accum_index "min_index"
accum_arg "argmax"
accum_arg "argmin"
def_method "minmax"
def_module_function "maximum", "ewcomp", n_arg:2
def_module_function "minimum", "ewcomp", n_arg:2
end
if is_int && !is_object
def_method "bincount"
end
cum "cumsum","add"
cum "cumprod","mul"
# dot
accum_binary "mulsum"
# rmsdev
# prod
# shuffle
# histogram
def_method "seq"
if is_float
def_method "logseq"
end
def_method "eye"
def_alias "indgen", "seq"
def_method "rand"
if is_float && !is_object
def_method "rand_norm"
end
# y = a[0] + a[1]*x + a[2]*x^2 + a[3]*x^3 + ... + a[n]*x^n
def_method "poly"
if is_comparable && !is_object
if is_float
qsort type_name,"dtype","*(dtype*)","_prnan"
qsort type_name,"dtype","*(dtype*)","_ignan"
else
qsort type_name,"dtype","*(dtype*)"
end
def_method "sort"
if is_float
qsort type_name+"_index","dtype*","**(dtype**)","_prnan"
qsort type_name+"_index","dtype*","**(dtype**)","_ignan"
else
qsort type_name+"_index","dtype*","**(dtype**)"
end
def_method "sort_index"
def_method "median"
end
# Math
# histogram
if has_math
fn = get(:full_class_name)
cn = get(:class_name)
nm = get(:name)
st = get(:simd_type)
dp = get(:is_double_precision)
algn = get(:need_align)
is_c = is_complex
def_module do
extend NMathMethod
set ns_var: "cT"
set class_name: cn
set name: "#{nm}_math"
set full_module_name: fn+"::NMath"
set module_name: "Math"
set module_var: "mTM"
set simd_type: st
set is_double_precision: dp
set is_complex: is_c
set need_align: algn
math "sqrt"
math "cbrt"
math "log"
math "log2"
math "log10"
math "exp"
math "exp2"
math "exp10"
math "sin"
math "cos"
math "tan"
math "asin"
math "acos"
math "atan"
math "sinh"
math "cosh"
math "tanh"
math "asinh"
math "acosh"
math "atanh"
math "sinc"
if !is_c
math "atan2",2
math "hypot",2
math "erf"
math "erfc"
math "log1p"
math "expm1"
math "ldexp",2
math "frexp",1,"frexp"
end
end
end
end # other than Bit