numo-narray-alt 0.9.13 → 0.9.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/numo/narray/numo/narray.h +2 -2
- data/ext/numo/narray/src/mh/abs.h +56 -0
- data/ext/numo/narray/src/mh/aref.h +28 -0
- data/ext/numo/narray/src/mh/arg.h +56 -0
- data/ext/numo/narray/src/mh/conj.h +71 -0
- data/ext/numo/narray/src/mh/copysign.h +97 -0
- data/ext/numo/narray/src/mh/each.h +71 -0
- data/ext/numo/narray/src/mh/each_with_index.h +98 -0
- data/ext/numo/narray/src/mh/extract.h +36 -0
- data/ext/numo/narray/src/mh/im.h +71 -0
- data/ext/numo/narray/src/mh/imag.h +56 -0
- data/ext/numo/narray/src/mh/kahan_sum.h +39 -0
- data/ext/numo/narray/src/mh/map.h +126 -0
- data/ext/numo/narray/src/mh/map_with_index.h +76 -0
- data/ext/numo/narray/src/mh/modf.h +35 -0
- data/ext/numo/narray/src/mh/poly.h +42 -0
- data/ext/numo/narray/src/mh/real.h +56 -0
- data/ext/numo/narray/src/mh/set_imag.h +60 -0
- data/ext/numo/narray/src/mh/set_real.h +60 -0
- data/ext/numo/narray/src/mh/signbit.h +42 -0
- data/ext/numo/narray/src/t_bit.c +63 -176
- data/ext/numo/narray/src/t_dcomplex.c +142 -1001
- data/ext/numo/narray/src/t_dfloat.c +24 -560
- data/ext/numo/narray/src/t_int16.c +83 -417
- data/ext/numo/narray/src/t_int32.c +83 -417
- data/ext/numo/narray/src/t_int64.c +83 -417
- data/ext/numo/narray/src/t_int8.c +83 -400
- data/ext/numo/narray/src/t_robject.c +83 -400
- data/ext/numo/narray/src/t_scomplex.c +130 -953
- data/ext/numo/narray/src/t_sfloat.c +22 -524
- data/ext/numo/narray/src/t_uint16.c +83 -417
- data/ext/numo/narray/src/t_uint32.c +83 -417
- data/ext/numo/narray/src/t_uint64.c +83 -417
- data/ext/numo/narray/src/t_uint8.c +83 -400
- metadata +21 -2
|
@@ -43,12 +43,19 @@ static ID id_to_a;
|
|
|
43
43
|
VALUE cT;
|
|
44
44
|
extern VALUE cRT;
|
|
45
45
|
|
|
46
|
+
#include "mh/extract.h"
|
|
47
|
+
#include "mh/aref.h"
|
|
46
48
|
#include "mh/coerce_cast.h"
|
|
47
49
|
#include "mh/to_a.h"
|
|
48
50
|
#include "mh/fill.h"
|
|
49
51
|
#include "mh/format.h"
|
|
50
52
|
#include "mh/format_to_a.h"
|
|
51
53
|
#include "mh/inspect.h"
|
|
54
|
+
#include "mh/each.h"
|
|
55
|
+
#include "mh/map.h"
|
|
56
|
+
#include "mh/each_with_index.h"
|
|
57
|
+
#include "mh/map_with_index.h"
|
|
58
|
+
#include "mh/abs.h"
|
|
52
59
|
#include "mh/op/add.h"
|
|
53
60
|
#include "mh/op/sub.h"
|
|
54
61
|
#include "mh/op/mul.h"
|
|
@@ -92,6 +99,7 @@ extern VALUE cRT;
|
|
|
92
99
|
#include "mh/seq.h"
|
|
93
100
|
#include "mh/eye.h"
|
|
94
101
|
#include "mh/rand.h"
|
|
102
|
+
#include "mh/poly.h"
|
|
95
103
|
#include "mh/cumprod.h"
|
|
96
104
|
#include "mh/mean.h"
|
|
97
105
|
#include "mh/var.h"
|
|
@@ -100,12 +108,19 @@ extern VALUE cRT;
|
|
|
100
108
|
|
|
101
109
|
typedef int32_t int32; // Type aliases for shorter notation
|
|
102
110
|
// following the codebase naming convention.
|
|
111
|
+
DEF_NARRAY_EXTRACT_METHOD_FUNC(int32)
|
|
112
|
+
DEF_NARRAY_AREF_METHOD_FUNC(int32)
|
|
103
113
|
DEF_NARRAY_COERCE_CAST_METHOD_FUNC(int32)
|
|
104
114
|
DEF_NARRAY_TO_A_METHOD_FUNC(int32)
|
|
105
115
|
DEF_NARRAY_FILL_METHOD_FUNC(int32)
|
|
106
116
|
DEF_NARRAY_FORMAT_METHOD_FUNC(int32)
|
|
107
117
|
DEF_NARRAY_FORMAT_TO_A_METHOD_FUNC(int32)
|
|
108
118
|
DEF_NARRAY_INSPECT_METHOD_FUNC(int32)
|
|
119
|
+
DEF_NARRAY_EACH_METHOD_FUNC(int32)
|
|
120
|
+
DEF_NARRAY_MAP_METHOD_FUNC(int32, numo_cInt32)
|
|
121
|
+
DEF_NARRAY_EACH_WITH_INDEX_METHOD_FUNC(int32)
|
|
122
|
+
DEF_NARRAY_MAP_WITH_INDEX_METHOD_FUNC(int32, numo_cInt32)
|
|
123
|
+
DEF_NARRAY_ABS_METHOD_FUNC(int32, numo_cInt32, int32, numo_cInt32)
|
|
109
124
|
DEF_NARRAY_ADD_METHOD_FUNC(int32, numo_cInt32)
|
|
110
125
|
DEF_NARRAY_SUB_METHOD_FUNC(int32, numo_cInt32)
|
|
111
126
|
DEF_NARRAY_MUL_METHOD_FUNC(int32, numo_cInt32)
|
|
@@ -149,6 +164,7 @@ DEF_NARRAY_INT_MULSUM_METHOD_FUNC(int32, numo_cInt32)
|
|
|
149
164
|
DEF_NARRAY_INT_SEQ_METHOD_FUNC(int32)
|
|
150
165
|
DEF_NARRAY_EYE_METHOD_FUNC(int32)
|
|
151
166
|
DEF_NARRAY_INT_RAND_METHOD_FUNC(int32)
|
|
167
|
+
DEF_NARRAY_POLY_METHOD_FUNC(int32, numo_cInt32)
|
|
152
168
|
DEF_NARRAY_INT_MEAN_METHOD_FUNC(int32, numo_cInt32)
|
|
153
169
|
DEF_NARRAY_INT_VAR_METHOD_FUNC(int32, numo_cInt32)
|
|
154
170
|
DEF_NARRAY_INT_STDDEV_METHOD_FUNC(int32, numo_cInt32)
|
|
@@ -257,28 +273,6 @@ static VALUE int32_allocate(VALUE self) {
|
|
|
257
273
|
return self;
|
|
258
274
|
}
|
|
259
275
|
|
|
260
|
-
/*
|
|
261
|
-
Extract an element only if self is a dimensionless NArray.
|
|
262
|
-
@overload extract
|
|
263
|
-
@return [Numeric,Numo::NArray]
|
|
264
|
-
--- Extract element value as Ruby Object if self is a dimensionless NArray,
|
|
265
|
-
otherwise returns self.
|
|
266
|
-
*/
|
|
267
|
-
static VALUE int32_extract(VALUE self) {
|
|
268
|
-
volatile VALUE v;
|
|
269
|
-
char* ptr;
|
|
270
|
-
narray_t* na;
|
|
271
|
-
GetNArray(self, na);
|
|
272
|
-
|
|
273
|
-
if (na->ndim == 0) {
|
|
274
|
-
ptr = na_get_pointer_for_read(self) + na_get_offset(self);
|
|
275
|
-
v = m_extract(ptr);
|
|
276
|
-
na_release_lock(self);
|
|
277
|
-
return v;
|
|
278
|
-
}
|
|
279
|
-
return self;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
276
|
static VALUE int32_new_dim0(dtype x) {
|
|
283
277
|
VALUE v;
|
|
284
278
|
dtype* ptr;
|
|
@@ -1256,29 +1250,6 @@ static VALUE int32_s_cast(VALUE type, VALUE obj) {
|
|
|
1256
1250
|
return Qnil;
|
|
1257
1251
|
}
|
|
1258
1252
|
|
|
1259
|
-
/*
|
|
1260
|
-
Multi-dimensional element reference.
|
|
1261
|
-
@overload [](dim0,...,dimL)
|
|
1262
|
-
@param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
|
|
1263
|
-
dim0,...,dimL multi-dimensional indices.
|
|
1264
|
-
@return [Numeric,Numo::Int32] an element or NArray view.
|
|
1265
|
-
@see Numo::NArray#[]
|
|
1266
|
-
@see #[]=
|
|
1267
|
-
*/
|
|
1268
|
-
static VALUE int32_aref(int argc, VALUE* argv, VALUE self) {
|
|
1269
|
-
int nd;
|
|
1270
|
-
size_t pos;
|
|
1271
|
-
char* ptr;
|
|
1272
|
-
|
|
1273
|
-
nd = na_get_result_dimension(self, argc, argv, sizeof(dtype), &pos);
|
|
1274
|
-
if (nd) {
|
|
1275
|
-
return na_aref_main(argc, argv, self, 0, nd);
|
|
1276
|
-
} else {
|
|
1277
|
-
ptr = na_get_pointer_for_read(self) + pos;
|
|
1278
|
-
return m_extract(ptr);
|
|
1279
|
-
}
|
|
1280
|
-
}
|
|
1281
|
-
|
|
1282
1253
|
/*
|
|
1283
1254
|
Multi-dimensional element assignment.
|
|
1284
1255
|
@overload []=(dim0,...,dimL,val)
|
|
@@ -1313,378 +1284,6 @@ static VALUE int32_aset(int argc, VALUE* argv, VALUE self) {
|
|
|
1313
1284
|
return argv[argc];
|
|
1314
1285
|
}
|
|
1315
1286
|
|
|
1316
|
-
static void iter_int32_each(na_loop_t* const lp) {
|
|
1317
|
-
size_t i, s1;
|
|
1318
|
-
char* p1;
|
|
1319
|
-
size_t* idx1;
|
|
1320
|
-
dtype x;
|
|
1321
|
-
VALUE y;
|
|
1322
|
-
|
|
1323
|
-
INIT_COUNTER(lp, i);
|
|
1324
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1325
|
-
if (idx1) {
|
|
1326
|
-
for (; i--;) {
|
|
1327
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1328
|
-
y = m_data_to_num(x);
|
|
1329
|
-
rb_yield(y);
|
|
1330
|
-
}
|
|
1331
|
-
} else {
|
|
1332
|
-
for (; i--;) {
|
|
1333
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1334
|
-
y = m_data_to_num(x);
|
|
1335
|
-
rb_yield(y);
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
}
|
|
1339
|
-
|
|
1340
|
-
/*
|
|
1341
|
-
Calls the given block once for each element in self,
|
|
1342
|
-
passing that element as a parameter.
|
|
1343
|
-
@overload each
|
|
1344
|
-
@return [Numo::NArray] self
|
|
1345
|
-
For a block `{|x| ... }`,
|
|
1346
|
-
@yieldparam [Numeric] x an element of NArray.
|
|
1347
|
-
@see #each_with_index
|
|
1348
|
-
@see #map
|
|
1349
|
-
*/
|
|
1350
|
-
static VALUE int32_each(VALUE self) {
|
|
1351
|
-
ndfunc_arg_in_t ain[1] = { { Qnil, 0 } };
|
|
1352
|
-
ndfunc_t ndf = { iter_int32_each, FULL_LOOP_NIP, 1, 0, ain, 0 };
|
|
1353
|
-
|
|
1354
|
-
na_ndloop(&ndf, 1, self);
|
|
1355
|
-
return self;
|
|
1356
|
-
}
|
|
1357
|
-
|
|
1358
|
-
static void iter_int32_map(na_loop_t* const lp) {
|
|
1359
|
-
size_t i, n;
|
|
1360
|
-
char *p1, *p2;
|
|
1361
|
-
ssize_t s1, s2;
|
|
1362
|
-
size_t *idx1, *idx2;
|
|
1363
|
-
dtype x;
|
|
1364
|
-
|
|
1365
|
-
INIT_COUNTER(lp, n);
|
|
1366
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1367
|
-
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
1368
|
-
|
|
1369
|
-
if (idx1) {
|
|
1370
|
-
if (idx2) {
|
|
1371
|
-
for (i = 0; i < n; i++) {
|
|
1372
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1373
|
-
x = m_map(x);
|
|
1374
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
1375
|
-
}
|
|
1376
|
-
} else {
|
|
1377
|
-
for (i = 0; i < n; i++) {
|
|
1378
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1379
|
-
x = m_map(x);
|
|
1380
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
1381
|
-
}
|
|
1382
|
-
}
|
|
1383
|
-
} else {
|
|
1384
|
-
if (idx2) {
|
|
1385
|
-
for (i = 0; i < n; i++) {
|
|
1386
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1387
|
-
x = m_map(x);
|
|
1388
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
1389
|
-
}
|
|
1390
|
-
} else {
|
|
1391
|
-
//
|
|
1392
|
-
if (is_aligned(p1, sizeof(dtype)) && is_aligned(p2, sizeof(dtype))) {
|
|
1393
|
-
if (s1 == sizeof(dtype) && s2 == sizeof(dtype)) {
|
|
1394
|
-
for (i = 0; i < n; i++) {
|
|
1395
|
-
((dtype*)p2)[i] = m_map(((dtype*)p1)[i]);
|
|
1396
|
-
}
|
|
1397
|
-
return;
|
|
1398
|
-
}
|
|
1399
|
-
if (is_aligned_step(s1, sizeof(dtype)) && is_aligned_step(s2, sizeof(dtype))) {
|
|
1400
|
-
//
|
|
1401
|
-
for (i = 0; i < n; i++) {
|
|
1402
|
-
*(dtype*)p2 = m_map(*(dtype*)p1);
|
|
1403
|
-
p1 += s1;
|
|
1404
|
-
p2 += s2;
|
|
1405
|
-
}
|
|
1406
|
-
return;
|
|
1407
|
-
//
|
|
1408
|
-
}
|
|
1409
|
-
}
|
|
1410
|
-
for (i = 0; i < n; i++) {
|
|
1411
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1412
|
-
x = m_map(x);
|
|
1413
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
1414
|
-
}
|
|
1415
|
-
//
|
|
1416
|
-
}
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1419
|
-
|
|
1420
|
-
/*
|
|
1421
|
-
Unary map.
|
|
1422
|
-
@overload map
|
|
1423
|
-
@return [Numo::Int32] map of self.
|
|
1424
|
-
*/
|
|
1425
|
-
static VALUE int32_map(VALUE self) {
|
|
1426
|
-
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
1427
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
1428
|
-
ndfunc_t ndf = { iter_int32_map, FULL_LOOP, 1, 1, ain, aout };
|
|
1429
|
-
|
|
1430
|
-
return na_ndloop(&ndf, 1, self);
|
|
1431
|
-
}
|
|
1432
|
-
|
|
1433
|
-
static inline void yield_each_with_index(dtype x, size_t* c, VALUE* a, int nd, int md) {
|
|
1434
|
-
int j;
|
|
1435
|
-
|
|
1436
|
-
a[0] = m_data_to_num(x);
|
|
1437
|
-
for (j = 0; j <= nd; j++) {
|
|
1438
|
-
a[j + 1] = SIZET2NUM(c[j]);
|
|
1439
|
-
}
|
|
1440
|
-
rb_yield(rb_ary_new4(md, a));
|
|
1441
|
-
}
|
|
1442
|
-
|
|
1443
|
-
static void iter_int32_each_with_index(na_loop_t* const lp) {
|
|
1444
|
-
size_t i, s1;
|
|
1445
|
-
char* p1;
|
|
1446
|
-
size_t* idx1;
|
|
1447
|
-
dtype x;
|
|
1448
|
-
VALUE* a;
|
|
1449
|
-
size_t* c;
|
|
1450
|
-
int nd, md;
|
|
1451
|
-
|
|
1452
|
-
c = (size_t*)(lp->opt_ptr);
|
|
1453
|
-
nd = lp->ndim;
|
|
1454
|
-
if (nd > 0) {
|
|
1455
|
-
nd--;
|
|
1456
|
-
}
|
|
1457
|
-
md = nd + 2;
|
|
1458
|
-
a = ALLOCA_N(VALUE, md);
|
|
1459
|
-
|
|
1460
|
-
INIT_COUNTER(lp, i);
|
|
1461
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1462
|
-
c[nd] = 0;
|
|
1463
|
-
if (idx1) {
|
|
1464
|
-
for (; i--;) {
|
|
1465
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1466
|
-
yield_each_with_index(x, c, a, nd, md);
|
|
1467
|
-
c[nd]++;
|
|
1468
|
-
}
|
|
1469
|
-
} else {
|
|
1470
|
-
for (; i--;) {
|
|
1471
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1472
|
-
yield_each_with_index(x, c, a, nd, md);
|
|
1473
|
-
c[nd]++;
|
|
1474
|
-
}
|
|
1475
|
-
}
|
|
1476
|
-
}
|
|
1477
|
-
|
|
1478
|
-
/*
|
|
1479
|
-
Invokes the given block once for each element of self,
|
|
1480
|
-
passing that element and indices along each axis as parameters.
|
|
1481
|
-
@overload each_with_index
|
|
1482
|
-
For a block `{|x,i,j,...| ... }`,
|
|
1483
|
-
@yieldparam [Numeric] x an element
|
|
1484
|
-
@yieldparam [Integer] i,j,... multitimensional indices
|
|
1485
|
-
@return [Numo::NArray] self
|
|
1486
|
-
@see #each
|
|
1487
|
-
@see #map_with_index
|
|
1488
|
-
*/
|
|
1489
|
-
static VALUE int32_each_with_index(VALUE self) {
|
|
1490
|
-
ndfunc_arg_in_t ain[1] = { { Qnil, 0 } };
|
|
1491
|
-
ndfunc_t ndf = { iter_int32_each_with_index, FULL_LOOP_NIP, 1, 0, ain, 0 };
|
|
1492
|
-
|
|
1493
|
-
na_ndloop_with_index(&ndf, 1, self);
|
|
1494
|
-
return self;
|
|
1495
|
-
}
|
|
1496
|
-
|
|
1497
|
-
static inline dtype yield_map_with_index(dtype x, size_t* c, VALUE* a, int nd, int md) {
|
|
1498
|
-
int j;
|
|
1499
|
-
VALUE y;
|
|
1500
|
-
|
|
1501
|
-
a[0] = m_data_to_num(x);
|
|
1502
|
-
for (j = 0; j <= nd; j++) {
|
|
1503
|
-
a[j + 1] = SIZET2NUM(c[j]);
|
|
1504
|
-
}
|
|
1505
|
-
y = rb_yield(rb_ary_new4(md, a));
|
|
1506
|
-
return m_num_to_data(y);
|
|
1507
|
-
}
|
|
1508
|
-
|
|
1509
|
-
static void iter_int32_map_with_index(na_loop_t* const lp) {
|
|
1510
|
-
size_t i;
|
|
1511
|
-
char *p1, *p2;
|
|
1512
|
-
ssize_t s1, s2;
|
|
1513
|
-
size_t *idx1, *idx2;
|
|
1514
|
-
dtype x;
|
|
1515
|
-
VALUE* a;
|
|
1516
|
-
size_t* c;
|
|
1517
|
-
int nd, md;
|
|
1518
|
-
|
|
1519
|
-
c = (size_t*)(lp->opt_ptr);
|
|
1520
|
-
nd = lp->ndim;
|
|
1521
|
-
if (nd > 0) {
|
|
1522
|
-
nd--;
|
|
1523
|
-
}
|
|
1524
|
-
md = nd + 2;
|
|
1525
|
-
a = ALLOCA_N(VALUE, md);
|
|
1526
|
-
|
|
1527
|
-
INIT_COUNTER(lp, i);
|
|
1528
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1529
|
-
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
1530
|
-
|
|
1531
|
-
c[nd] = 0;
|
|
1532
|
-
if (idx1) {
|
|
1533
|
-
if (idx2) {
|
|
1534
|
-
for (; i--;) {
|
|
1535
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1536
|
-
x = yield_map_with_index(x, c, a, nd, md);
|
|
1537
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
1538
|
-
c[nd]++;
|
|
1539
|
-
}
|
|
1540
|
-
} else {
|
|
1541
|
-
for (; i--;) {
|
|
1542
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1543
|
-
x = yield_map_with_index(x, c, a, nd, md);
|
|
1544
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
1545
|
-
c[nd]++;
|
|
1546
|
-
}
|
|
1547
|
-
}
|
|
1548
|
-
} else {
|
|
1549
|
-
if (idx2) {
|
|
1550
|
-
for (; i--;) {
|
|
1551
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1552
|
-
x = yield_map_with_index(x, c, a, nd, md);
|
|
1553
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
1554
|
-
c[nd]++;
|
|
1555
|
-
}
|
|
1556
|
-
} else {
|
|
1557
|
-
for (; i--;) {
|
|
1558
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1559
|
-
x = yield_map_with_index(x, c, a, nd, md);
|
|
1560
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
1561
|
-
c[nd]++;
|
|
1562
|
-
}
|
|
1563
|
-
}
|
|
1564
|
-
}
|
|
1565
|
-
}
|
|
1566
|
-
|
|
1567
|
-
/*
|
|
1568
|
-
Invokes the given block once for each element of self,
|
|
1569
|
-
passing that element and indices along each axis as parameters.
|
|
1570
|
-
Creates a new NArray containing the values returned by the block.
|
|
1571
|
-
Inplace option is allowed, i.e., `nary.inplace.map` overwrites `nary`.
|
|
1572
|
-
@overload map_with_index
|
|
1573
|
-
For a block `{|x,i,j,...| ... }`,
|
|
1574
|
-
@yieldparam [Numeric] x an element
|
|
1575
|
-
@yieldparam [Integer] i,j,... multitimensional indices
|
|
1576
|
-
@return [Numo::NArray] mapped array
|
|
1577
|
-
@see #map
|
|
1578
|
-
@see #each_with_index
|
|
1579
|
-
*/
|
|
1580
|
-
static VALUE int32_map_with_index(VALUE self) {
|
|
1581
|
-
ndfunc_arg_in_t ain[1] = { { Qnil, 0 } };
|
|
1582
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
1583
|
-
ndfunc_t ndf = { iter_int32_map_with_index, FULL_LOOP, 1, 1, ain, aout };
|
|
1584
|
-
|
|
1585
|
-
return na_ndloop_with_index(&ndf, 1, self);
|
|
1586
|
-
}
|
|
1587
|
-
|
|
1588
|
-
static void iter_int32_abs(na_loop_t* const lp) {
|
|
1589
|
-
size_t i;
|
|
1590
|
-
char *p1, *p2;
|
|
1591
|
-
ssize_t s1, s2;
|
|
1592
|
-
size_t *idx1, *idx2;
|
|
1593
|
-
dtype x;
|
|
1594
|
-
rtype y;
|
|
1595
|
-
INIT_COUNTER(lp, i);
|
|
1596
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1597
|
-
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
1598
|
-
if (idx1) {
|
|
1599
|
-
if (idx2) {
|
|
1600
|
-
for (; i--;) {
|
|
1601
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1602
|
-
y = m_abs(x);
|
|
1603
|
-
SET_DATA_INDEX(p2, idx2, rtype, y);
|
|
1604
|
-
}
|
|
1605
|
-
} else {
|
|
1606
|
-
for (; i--;) {
|
|
1607
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1608
|
-
y = m_abs(x);
|
|
1609
|
-
SET_DATA_STRIDE(p2, s2, rtype, y);
|
|
1610
|
-
}
|
|
1611
|
-
}
|
|
1612
|
-
} else {
|
|
1613
|
-
if (idx2) {
|
|
1614
|
-
for (; i--;) {
|
|
1615
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1616
|
-
y = m_abs(x);
|
|
1617
|
-
SET_DATA_INDEX(p2, idx2, rtype, y);
|
|
1618
|
-
}
|
|
1619
|
-
} else {
|
|
1620
|
-
for (; i--;) {
|
|
1621
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1622
|
-
y = m_abs(x);
|
|
1623
|
-
SET_DATA_STRIDE(p2, s2, rtype, y);
|
|
1624
|
-
}
|
|
1625
|
-
}
|
|
1626
|
-
}
|
|
1627
|
-
}
|
|
1628
|
-
|
|
1629
|
-
/*
|
|
1630
|
-
abs of self.
|
|
1631
|
-
@overload abs
|
|
1632
|
-
@return [Numo::Int32] abs of self.
|
|
1633
|
-
*/
|
|
1634
|
-
static VALUE int32_abs(VALUE self) {
|
|
1635
|
-
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
1636
|
-
ndfunc_arg_out_t aout[1] = { { cRT, 0 } };
|
|
1637
|
-
ndfunc_t ndf = { iter_int32_abs, FULL_LOOP, 1, 1, ain, aout };
|
|
1638
|
-
|
|
1639
|
-
return na_ndloop(&ndf, 1, self);
|
|
1640
|
-
}
|
|
1641
|
-
|
|
1642
|
-
static void iter_int32_poly(na_loop_t* const lp) {
|
|
1643
|
-
size_t i;
|
|
1644
|
-
dtype x, y, a;
|
|
1645
|
-
|
|
1646
|
-
x = *(dtype*)(lp->args[0].ptr + lp->args[0].iter[0].pos);
|
|
1647
|
-
i = lp->narg - 2;
|
|
1648
|
-
y = *(dtype*)(lp->args[i].ptr + lp->args[i].iter[0].pos);
|
|
1649
|
-
for (; --i;) {
|
|
1650
|
-
y = m_mul(x, y);
|
|
1651
|
-
a = *(dtype*)(lp->args[i].ptr + lp->args[i].iter[0].pos);
|
|
1652
|
-
y = m_add(y, a);
|
|
1653
|
-
}
|
|
1654
|
-
i = lp->narg - 1;
|
|
1655
|
-
*(dtype*)(lp->args[i].ptr + lp->args[i].iter[0].pos) = y;
|
|
1656
|
-
}
|
|
1657
|
-
|
|
1658
|
-
/*
|
|
1659
|
-
Calculate polynomial.
|
|
1660
|
-
`x.poly(a0,a1,a2,...,an) = a0 + a1*x + a2*x**2 + ... + an*x**n`
|
|
1661
|
-
@overload poly a0, a1, ..., an
|
|
1662
|
-
@param [Numo::NArray,Numeric] a0,a1,...,an
|
|
1663
|
-
@return [Numo::Int32]
|
|
1664
|
-
*/
|
|
1665
|
-
static VALUE int32_poly(VALUE self, VALUE args) {
|
|
1666
|
-
int argc, i;
|
|
1667
|
-
VALUE* argv;
|
|
1668
|
-
volatile VALUE v, a;
|
|
1669
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
1670
|
-
ndfunc_t ndf = { iter_int32_poly, NO_LOOP, 0, 1, 0, aout };
|
|
1671
|
-
|
|
1672
|
-
argc = (int)RARRAY_LEN(args);
|
|
1673
|
-
ndf.nin = argc + 1;
|
|
1674
|
-
ndf.ain = ALLOCA_N(ndfunc_arg_in_t, argc + 1);
|
|
1675
|
-
for (i = 0; i < argc + 1; i++) {
|
|
1676
|
-
ndf.ain[i].type = cT;
|
|
1677
|
-
}
|
|
1678
|
-
argv = ALLOCA_N(VALUE, argc + 1);
|
|
1679
|
-
argv[0] = self;
|
|
1680
|
-
for (i = 0; i < argc; i++) {
|
|
1681
|
-
argv[i + 1] = RARRAY_PTR(args)[i];
|
|
1682
|
-
}
|
|
1683
|
-
a = rb_ary_new4(argc + 1, argv);
|
|
1684
|
-
v = na_ndloop2(&ndf, a);
|
|
1685
|
-
return int32_extract(v);
|
|
1686
|
-
}
|
|
1687
|
-
|
|
1688
1287
|
/*
|
|
1689
1288
|
qsort.c
|
|
1690
1289
|
Ruby/Numo::NArray - Numerical Array class for Ruby
|
|
@@ -2255,11 +1854,27 @@ void Init_numo_int32(void) {
|
|
|
2255
1854
|
rb_define_const(cT, "MIN", M_MIN);
|
|
2256
1855
|
rb_define_alloc_func(cT, int32_s_alloc_func);
|
|
2257
1856
|
rb_define_method(cT, "allocate", int32_allocate, 0);
|
|
1857
|
+
/**
|
|
1858
|
+
* Extract an element only if self is a dimensionless NArray.
|
|
1859
|
+
* @overload extract
|
|
1860
|
+
* @return [Numeric,Numo::NArray]
|
|
1861
|
+
* --- Extract element value as Ruby Object if self is a dimensionless NArray,
|
|
1862
|
+
* otherwise returns self.
|
|
1863
|
+
*/
|
|
2258
1864
|
rb_define_method(cT, "extract", int32_extract, 0);
|
|
2259
1865
|
|
|
2260
1866
|
rb_define_method(cT, "store", int32_store, 1);
|
|
2261
1867
|
|
|
2262
1868
|
rb_define_singleton_method(cT, "cast", int32_s_cast, 1);
|
|
1869
|
+
/**
|
|
1870
|
+
* Multi-dimensional element reference.
|
|
1871
|
+
* @overload [](dim0,...,dimL)
|
|
1872
|
+
* @param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,
|
|
1873
|
+
* Symbol] dim0,...,dimL multi-dimensional indices.
|
|
1874
|
+
* @return [Numeric,Numo::Int32] an element or NArray view.
|
|
1875
|
+
* @see Numo::NArray#[]
|
|
1876
|
+
* @see #[]=
|
|
1877
|
+
*/
|
|
2263
1878
|
rb_define_method(cT, "[]", int32_aref, -1);
|
|
2264
1879
|
rb_define_method(cT, "[]=", int32_aset, -1);
|
|
2265
1880
|
/**
|
|
@@ -2301,10 +1916,54 @@ void Init_numo_int32(void) {
|
|
|
2301
1916
|
* @return [String]
|
|
2302
1917
|
*/
|
|
2303
1918
|
rb_define_method(cT, "inspect", int32_inspect, 0);
|
|
1919
|
+
/**
|
|
1920
|
+
* Calls the given block once for each element in self,
|
|
1921
|
+
* passing that element as a parameter.
|
|
1922
|
+
* @overload each
|
|
1923
|
+
* @return [Numo::NArray] self
|
|
1924
|
+
* For a block `{|x| ... }`,
|
|
1925
|
+
* @yieldparam [Numeric] x an element of NArray.
|
|
1926
|
+
* @see #each_with_index
|
|
1927
|
+
* @see #map
|
|
1928
|
+
*/
|
|
2304
1929
|
rb_define_method(cT, "each", int32_each, 0);
|
|
1930
|
+
/**
|
|
1931
|
+
* Unary map.
|
|
1932
|
+
* @overload map
|
|
1933
|
+
* @return [Numo::Int32] map of self.
|
|
1934
|
+
*/
|
|
2305
1935
|
rb_define_method(cT, "map", int32_map, 0);
|
|
1936
|
+
/**
|
|
1937
|
+
* Invokes the given block once for each element of self,
|
|
1938
|
+
* passing that element and indices along each axis as parameters.
|
|
1939
|
+
* @overload each_with_index
|
|
1940
|
+
* For a block `{|x,i,j,...| ... }`,
|
|
1941
|
+
* @yieldparam [Numeric] x an element
|
|
1942
|
+
* @yieldparam [Integer] i,j,... multitimensional indices
|
|
1943
|
+
* @return [Numo::NArray] self
|
|
1944
|
+
* @see #each
|
|
1945
|
+
* @see #map_with_index
|
|
1946
|
+
*/
|
|
2306
1947
|
rb_define_method(cT, "each_with_index", int32_each_with_index, 0);
|
|
1948
|
+
/**
|
|
1949
|
+
* Invokes the given block once for each element of self,
|
|
1950
|
+
* passing that element and indices along each axis as parameters.
|
|
1951
|
+
* Creates a new NArray containing the values returned by the block.
|
|
1952
|
+
* Inplace option is allowed, i.e., `nary.inplace.map` overwrites `nary`.
|
|
1953
|
+
* @overload map_with_index
|
|
1954
|
+
* For a block `{|x,i,j,...| ... }`,
|
|
1955
|
+
* @yieldparam [Numeric] x an element
|
|
1956
|
+
* @yieldparam [Integer] i,j,... multitimensional indices
|
|
1957
|
+
* @return [Numo::NArray] mapped array
|
|
1958
|
+
* @see #map
|
|
1959
|
+
* @see #each_with_index
|
|
1960
|
+
*/
|
|
2307
1961
|
rb_define_method(cT, "map_with_index", int32_map_with_index, 0);
|
|
1962
|
+
/**
|
|
1963
|
+
* abs of self.
|
|
1964
|
+
* @overload abs
|
|
1965
|
+
* @return [Numo::Int32] abs of self.
|
|
1966
|
+
*/
|
|
2308
1967
|
rb_define_method(cT, "abs", int32_abs, 0);
|
|
2309
1968
|
/**
|
|
2310
1969
|
* Binary add.
|
|
@@ -2765,6 +2424,13 @@ void Init_numo_int32(void) {
|
|
|
2765
2424
|
* # [4, 3, 3, 2, 4, 2]
|
|
2766
2425
|
*/
|
|
2767
2426
|
rb_define_method(cT, "rand", int32_rand, -1);
|
|
2427
|
+
/**
|
|
2428
|
+
* Calculate polynomial.
|
|
2429
|
+
* `x.poly(a0,a1,a2,...,an) = a0 + a1*x + a2*x**2 + ... + an*x**n`
|
|
2430
|
+
* @overload poly a0, a1, ..., an
|
|
2431
|
+
* @param [Numo::NArray,Numeric] a0,a1,...,an
|
|
2432
|
+
* @return [Numo::Int32]
|
|
2433
|
+
*/
|
|
2768
2434
|
rb_define_method(cT, "poly", int32_poly, -2);
|
|
2769
2435
|
|
|
2770
2436
|
rb_define_method(cT, "sort", int32_sort, -1);
|