Class: Numo::NArray
- Inherits:
-
Object
- Object
- Numo::NArray
- Defined in:
- ext/numo/narray/narray.c
Defined Under Namespace
Classes: CastError, DimensionError, OperationError, ShapeError, Step
Constant Summary collapse
- VERSION =
rb_str_new2(NARRAY_VERSION)
Class Method Summary collapse
-
.[](elements) ⇒ NArray
Generate NArray object.
- .array_type(ary) ⇒ Object
-
.byte_size ⇒ Numeric
Returns byte size of one element of NArray.
- .debug=(flag) ⇒ Object
-
.eye(n) ⇒ Numo::NArray
Returns a NArray with shape=(n,n) whose diagonal elements are 1, otherwise 0.
-
.from_string(string, [shape]) ⇒ Numo::NArray
Returns a new 1-D array initialized from binary raw data in a string.
-
.inspect_cols ⇒ Integer or nil
Returns the number of cols used for NArray#inspect.
-
.inspect_cols=(cols) ⇒ nil
Set the number of cols used for NArray#inspect.
-
.inspect_rows ⇒ Integer or nil
Returns the number of rows used for NArray#inspect.
-
.inspect_rows=(rows) ⇒ nil
Set the number of rows used for NArray#inspect.
-
.linspace(x1, x2, [n]) ⇒ Numo::NArray
Returns an array of N linearly spaced points between x1 and x2.
-
.logspace(a, b, [n, base]) ⇒ Numo::NArray
Returns an array of N logarithmically spaced points between 10^a and 10^b.
-
.ones(*args) ⇒ Object
Returns a one-filled narray with shape.
- .profile ⇒ Object
- .profile=(val) ⇒ Object
- .step(*args) ⇒ Object
-
.upcast(type2) ⇒ Object
———————————————————————-.
-
.zeros(*args) ⇒ Object
Returns a zero-filled narray with shape.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality of self and other in view of numerical array.
-
#[]=(*args) ⇒ Object
method: []=(idx1,idx2,…,idxN,val).
-
#byte_size ⇒ Integer
Returns total byte size of NArray.
-
#byte_swapped? ⇒ Boolean
(also: #network_order?)
Return true if byte swapped.
-
#cast_to(datatype) ⇒ Numo::NArray
Cast self to another NArray datatype.
-
#coerce(other) ⇒ Array
Returns an array containing other and self, both are converted to upcasted type of NArray.
-
#column_major? ⇒ Boolean
Return true if column major.
- #debug_info ⇒ Object
-
#diagonal([offset,axes]) ⇒ Numo::NArray
Returns a diagonal view of NArray.
-
#dot(other) ⇒ Object
Returns dot product.
-
#empty? ⇒ Boolean
Returns true if self.size == 0.
-
#expand_dims(dim) ⇒ Numo::NArray
Expand the shape of an array.
- #flatten ⇒ Object
-
#host_order? ⇒ Boolean
(also: #little_endian?, #vacs_order?)
Return true if not byte swapped.
-
#initialize(args) ⇒ Object
constructor
Constructs a narray using the given DataType and shape or sizes.
-
#initialize_copy(other) ⇒ Numo::NArray
Replaces the contents of self with the contents of other narray.
-
#inplace ⇒ Numo::NArray
Returns view of narray with inplace flagged.
-
#inplace! ⇒ Numo::NArray
Set inplace flag to self.
-
#inplace? ⇒ Boolean
Return true if inplace flagged.
-
#ndim ⇒ Object
(also: #rank)
method: size() – returns the total number of typeents.
-
#out_of_place! ⇒ Numo::NArray
(also: #not_inplace!)
Unset inplace flag to self.
-
#reshape(*args) ⇒ Object
private function for reshape.
-
#reverse([dim0,dim1,..]) ⇒ Object
Return reversed view along specified dimeinsion.
-
#row_major? ⇒ Boolean
Return true if row major.
-
#shape ⇒ Object
method: shape() – returns shape, array of the size of dimensions.
-
#size ⇒ Object
(also: #length, #total)
method: size() – returns the total number of typeents.
-
#slice(*args) ⇒ Object
method: slice(idx1,idx2,…,idxN).
- #swap_byte ⇒ Object (also: #hton)
- #to_host ⇒ Object
- #to_network ⇒ Object
-
#to_string ⇒ String
Returns string containing the raw data bytes in NArray.
- #to_swapped ⇒ Object
- #to_vacs ⇒ Object
- #transpose(*args) ⇒ Object
-
#view ⇒ Object
Return view of NArray.
Constructor Details
#Numo::DataType.new(shape) ⇒ Object #Numo::DataType.new(size1, size2, ...) ⇒ Object
Constructs a narray using the given DataType and shape or sizes.
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 |
# File 'ext/numo/narray/narray.c', line 323
static VALUE
na_initialize(VALUE self, VALUE args)
{
VALUE v;
size_t *shape=NULL;
int ndim;
if (RARRAY_LEN(args) == 1) {
v = RARRAY_AREF(args,0);
if (TYPE(v) != T_ARRAY) {
v = args;
}
} else {
v = args;
}
ndim = RARRAY_LEN(v);
if (ndim > NA_MAX_DIMENSION) {
rb_raise(rb_eArgError,"ndim=%d exceeds maximum dimension",ndim);
}
shape = ALLOCA_N(size_t, ndim);
// setup size_t shape[] from VALUE shape argument
na_array_to_internal_shape(self, v, shape);
na_setup(self, ndim, shape);
return self;
}
|
Class Method Details
.[](elements) ⇒ NArray
Generate NArray object. NArray datatype is automatically selected.
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'ext/numo/narray/array.c', line 393
static VALUE
nary_s_bracket(VALUE klass, VALUE ary)
{
VALUE dtype=Qnil;
if (TYPE(ary)!=T_ARRAY) {
rb_bug("Argument is not array");
}
dtype = na_ary_composition_dtype(ary);
if (RTEST(rb_obj_is_kind_of(dtype,rb_cClass))) {
if (RTEST(rb_funcall(dtype,rb_intern("<="),1,cNArray))) {
return rb_funcall(dtype,rb_intern("cast"),1,ary);
}
}
rb_raise(nary_eCastError, "cannot convert to NArray");
return Qnil;
}
|
.array_type(ary) ⇒ Object
379 380 381 382 383 |
# File 'ext/numo/narray/array.c', line 379
static VALUE
na_s_array_type(VALUE mod, VALUE ary)
{
return na_ary_composition_dtype(ary);
}
|
.byte_size ⇒ Numeric
Returns byte size of one element of NArray.
1162 1163 1164 1165 1166 |
# File 'ext/numo/narray/narray.c', line 1162
static VALUE
nary_s_byte_size(VALUE type)
{
return rb_const_get(type, rb_intern(ELEMENT_BYTE_SIZE));
}
|
.debug=(flag) ⇒ Object
1511 1512 1513 1514 1515 |
# File 'ext/numo/narray/narray.c', line 1511
VALUE na_debug_set(VALUE mod, VALUE flag)
{
na_debug_flag = RTEST(flag);
return Qnil;
}
|
.eye(n) ⇒ Numo::NArray
Returns a NArray with shape=(n,n) whose diagonal elements are 1, otherwise 0.
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'ext/numo/narray/narray.c', line 536
static VALUE
na_s_eye(int argc, VALUE *argv, VALUE klass)
{
VALUE obj;
VALUE tmp[2];
if (argc==0) {
rb_raise(rb_eArgError,"No argument");
}
else if (argc==1) {
tmp[0] = tmp[1] = argv[0];
argv = tmp;
argc = 2;
}
obj = rb_class_new_instance(argc, argv, klass);
return rb_funcall(obj, rb_intern("eye"), 0);
}
|
.from_string(string, [shape]) ⇒ Numo::NArray
Returns a new 1-D array initialized from binary raw data in a string.
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 |
# File 'ext/numo/narray/narray.c', line 1176
static VALUE
nary_s_from_string(int argc, VALUE *argv, VALUE type)
{
size_t len, str_len, byte_size;
size_t *shape;
char *ptr;
int i, nd, narg;
VALUE vstr, vshape, vna;
VALUE velmsz;
narg = rb_scan_args(argc,argv,"11",&vstr,&vshape);
str_len = RSTRING_LEN(vstr);
velmsz = rb_const_get(type, rb_intern(ELEMENT_BYTE_SIZE));
if (narg==2) {
switch(TYPE(vshape)) {
case T_FIXNUM:
nd = 1;
len = NUM2SIZET(vshape);
shape = &len;
break;
case T_ARRAY:
nd = RARRAY_LEN(vshape);
if (nd == 0 || nd > NA_MAX_DIMENSION) {
rb_raise(nary_eDimensionError,"too long or empty shape (%d)", nd);
}
shape = ALLOCA_N(size_t,nd);
len = 1;
for (i=0; i<nd; ++i) {
len *= shape[i] = NUM2SIZET(RARRAY_AREF(vshape,i));
}
break;
default:
rb_raise(rb_eArgError,"second argument must be size or shape");
}
if (FIXNUM_P(velmsz)) {
byte_size = len * NUM2SIZET(velmsz);
} else {
byte_size = ceil(len * NUM2DBL(velmsz));
}
if (byte_size > str_len) {
rb_raise(rb_eArgError, "specified size is too large");
}
} else {
nd = 1;
if (FIXNUM_P(velmsz)) {
len = str_len / NUM2SIZET(velmsz);
byte_size = len * NUM2SIZET(velmsz);
} else {
len = floor(str_len / NUM2DBL(velmsz));
byte_size = str_len;
}
if (len == 0) {
rb_raise(rb_eArgError, "string is empty or too short");
}
shape = ALLOCA_N(size_t,nd);
shape[0] = len;
}
vna = rb_narray_new(type, nd, shape);
ptr = na_get_pointer_for_write(vna);
memcpy(ptr, RSTRING_PTR(vstr), byte_size);
return vna;
}
|
.inspect_cols ⇒ Integer or nil
Returns the number of cols used for NArray#inspect
1566 1567 1568 1569 1570 1571 1572 1573 |
# File 'ext/numo/narray/narray.c', line 1566
static VALUE na_inspect_cols(VALUE mod)
{
if (numo_na_inspect_cols > 0) {
return INT2NUM(numo_na_inspect_cols);
} else {
return Qnil;
}
}
|
.inspect_cols=(cols) ⇒ nil
Set the number of cols used for NArray#inspect
1581 1582 1583 1584 1585 1586 1587 1588 1589 |
# File 'ext/numo/narray/narray.c', line 1581
static VALUE na_inspect_cols_set(VALUE mod, VALUE num)
{
if (RTEST(num)) {
numo_na_inspect_cols = NUM2INT(num);
} else {
numo_na_inspect_cols = 0;
}
return Qnil;
}
|
.inspect_rows ⇒ Integer or nil
Returns the number of rows used for NArray#inspect
1536 1537 1538 1539 1540 1541 1542 1543 |
# File 'ext/numo/narray/narray.c', line 1536
static VALUE na_inspect_rows(VALUE mod)
{
if (numo_na_inspect_rows > 0) {
return INT2NUM(numo_na_inspect_rows);
} else {
return Qnil;
}
}
|
.inspect_rows=(rows) ⇒ nil
Set the number of rows used for NArray#inspect
1551 1552 1553 1554 1555 1556 1557 1558 1559 |
# File 'ext/numo/narray/narray.c', line 1551
static VALUE na_inspect_rows_set(VALUE mod, VALUE num)
{
if (RTEST(num)) {
numo_na_inspect_rows = NUM2INT(num);
} else {
numo_na_inspect_rows = 0;
}
return Qnil;
}
|
.linspace(x1, x2, [n]) ⇒ Numo::NArray
Returns an array of N linearly spaced points between x1 and x2. This singleton method is valid not for NArray class itself but for typed NArray subclasses, e.g., DFloat, Int64.
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'ext/numo/narray/narray.c', line 457
static VALUE
na_s_linspace(int argc, VALUE *argv, VALUE klass)
{
VALUE obj, vx1, vx2, vstep, vsize;
double n;
int narg;
narg = rb_scan_args(argc,argv,"21",&vx1,&vx2,&vsize);
if (narg==3) {
n = NUM2DBL(vsize);
} else {
n = 100;
vsize = INT2FIX(100);
}
obj = rb_funcall(vx2, '-', 1, vx1);
vstep = rb_funcall(obj, '/', 1, DBL2NUM(n-1));
obj = rb_class_new_instance(1, &vsize, klass);
return rb_funcall(obj, rb_intern("seq"), 2, vx1, vstep);
}
|
.logspace(a, b, [n, base]) ⇒ Numo::NArray
Returns an array of N logarithmically spaced points between 10^a and 10^b. This singleton method is valid not for NArray having logseq method, i.e., DFloat, SFloat, DComplex, and SComplex.
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'ext/numo/narray/narray.c', line 499
static VALUE
na_s_logspace(int argc, VALUE *argv, VALUE klass)
{
VALUE obj, vx1, vx2, vstep, vsize, vbase;
double n;
rb_scan_args(argc,argv,"22",&vx1,&vx2,&vsize,&vbase);
if (vsize == Qnil) {
vsize = INT2FIX(50);
n = 50;
} else {
n = NUM2DBL(vsize);
}
if (vbase == Qnil) {
vbase = DBL2NUM(10);
}
obj = rb_funcall(vx2, '-', 1, vx1);
vstep = rb_funcall(obj, '/', 1, DBL2NUM(n-1));
obj = rb_class_new_instance(1, &vsize, klass);
return rb_funcall(obj, rb_intern("logseq"), 3, vx1, vstep, vbase);
}
|
.ones(shape) ⇒ Object .ones(size1, size2, ...) ⇒ Object
Returns a one-filled narray with shape. This singleton method is valid not for NArray class itself but for typed NArray subclasses, e.g., DFloat, Int64.
432 433 434 435 436 437 438 |
# File 'ext/numo/narray/narray.c', line 432
static VALUE
na_s_ones(int argc, VALUE *argv, VALUE klass)
{
VALUE obj;
obj = rb_class_new_instance(argc, argv, klass);
return rb_funcall(obj, rb_intern("fill"), 1, INT2FIX(1));
}
|
.profile ⇒ Object
1519 1520 1521 1522 |
# File 'ext/numo/narray/narray.c', line 1519
VALUE na_profile(VALUE mod)
{
return rb_float_new(na_profile_value);
}
|
.profile=(val) ⇒ Object
1524 1525 1526 1527 1528 |
# File 'ext/numo/narray/narray.c', line 1524
VALUE na_profile_set(VALUE mod, VALUE val)
{
na_profile_value = NUM2DBL(val);
return val;
}
|
.step(*args) ⇒ Object
454 455 456 457 458 459 460 |
# File 'ext/numo/narray/step.c', line 454
static VALUE
nary_s_step( int argc, VALUE *argv, VALUE mod )
{
VALUE self = rb_obj_alloc(na_cStep);
step_initialize(argc, argv, self);
return self;
}
|
.upcast(type2) ⇒ Object
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 |
# File 'ext/numo/narray/narray.c', line 1098
VALUE
numo_na_upcast(VALUE type1, VALUE type2)
{
VALUE upcast_hash;
VALUE result_type;
if (type1==type2) {
return type1;
}
upcast_hash = rb_const_get(type1, rb_intern("UPCAST"));
result_type = rb_hash_aref(upcast_hash, type2);
if (NIL_P(result_type)) {
if (TYPE(type2)==T_CLASS) {
if (RTEST(rb_class_inherited_p(type2,cNArray))) {
upcast_hash = rb_const_get(type2, rb_intern("UPCAST"));
result_type = rb_hash_aref(upcast_hash, type1);
}
}
}
return result_type;
}
|
.zeros(shape) ⇒ Object .zeros(size1, size2, ...) ⇒ Object
Returns a zero-filled narray with shape. This singleton method is valid not for NArray class itself but for typed NArray subclasses, e.g., DFloat, Int64.
408 409 410 411 412 413 414 |
# File 'ext/numo/narray/narray.c', line 408
static VALUE
na_s_zeros(int argc, VALUE *argv, VALUE klass)
{
VALUE obj;
obj = rb_class_new_instance(argc, argv, klass);
return rb_funcall(obj, rb_intern("fill"), 1, INT2FIX(0));
}
|
Instance Method Details
#==(other) ⇒ Boolean
Equality of self and other in view of numerical array. i.e., both arrays have same shape and corresponding elements are equal.
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 |
# File 'ext/numo/narray/narray.c', line 1599
VALUE
na_equal(VALUE self, volatile VALUE other)
{
volatile VALUE vbool;
narray_t *na1, *na2;
int i;
GetNArray(self,na1);
if (!rb_obj_is_kind_of(other,cNArray)) {
other = rb_funcall(CLASS_OF(self), rb_intern("cast"), 1, other);
}
GetNArray(other,na2);
if (na1->ndim != na2->ndim) {
return Qfalse;
}
for (i=0; i<na1->ndim; i++) {
if (na1->shape[i] != na2->shape[i]) {
return Qfalse;
}
}
vbool = rb_funcall(self, rb_intern("eq"), 1, other);
return (rb_funcall(vbool, rb_intern("count_false"), 0)==INT2FIX(0)) ? Qtrue : Qfalse;
}
|
#[]=(*args) ⇒ Object
method: []=(idx1,idx2,…,idxN,val)
671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
# File 'ext/numo/narray/index.c', line 671
static VALUE
na_aset(int argc, VALUE *argv, VALUE self)
{
VALUE a;
argc--;
if (argc==0)
na_store(self, argv[argc]);
else {
a = na_aref_main(argc, argv, self, 0);
na_store(a, argv[argc]);
}
return argv[argc];
}
|
#byte_size ⇒ Integer
Returns total byte size of NArray.
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 |
# File 'ext/numo/narray/narray.c', line 1144
static VALUE
nary_byte_size(VALUE self)
{
VALUE velmsz;
narray_t *na;
GetNArray(self,na);
velmsz = rb_const_get(CLASS_OF(self), rb_intern(ELEMENT_BYTE_SIZE));
if (FIXNUM_P(velmsz)) {
return SIZET2NUM(NUM2SIZET(velmsz) * na->size);
}
return SIZET2NUM(ceil(NUM2DBL(velmsz) * na->size));
}
|
#byte_swapped? ⇒ Boolean Also known as: network_order?
Return true if byte swapped.
1440 1441 1442 1443 1444 1445 |
# File 'ext/numo/narray/narray.c', line 1440
VALUE na_byte_swapped_p( VALUE self )
{
if (TEST_BYTE_SWAPPED(self))
return Qtrue;
return Qfalse;
}
|
#cast_to(datatype) ⇒ Numo::NArray
Cast self to another NArray datatype.
1273 1274 1275 1276 1277 |
# File 'ext/numo/narray/narray.c', line 1273
static VALUE
nary_cast_to(VALUE obj, VALUE type)
{
return rb_funcall(type, rb_intern("cast"), 1, obj);
}
|
#coerce(other) ⇒ Array
Returns an array containing other and self, both are converted to upcasted type of NArray. Note that NArray has distinct UPCAST mechanism. Coerce is used for operation between non-NArray and NArray.
1129 1130 1131 1132 1133 1134 1135 1136 1137 |
# File 'ext/numo/narray/narray.c', line 1129
static VALUE
nary_coerce(VALUE x, VALUE y)
{
VALUE type;
type = numo_na_upcast(CLASS_OF(x), CLASS_OF(y));
y = rb_funcall(type,rb_intern("cast"),1,y);
return rb_assoc_new(y , x);
}
|
#column_major? ⇒ Boolean
Return true if column major.
1417 1418 1419 1420 1421 1422 1423 |
# File 'ext/numo/narray/narray.c', line 1417
VALUE na_column_major_p( VALUE self )
{
if (TEST_COLUMN_MAJOR(self))
return Qtrue;
else
return Qfalse;
}
|
#debug_info ⇒ Object
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 |
# File 'ext/numo/narray/narray.c', line 113
VALUE
rb_narray_debug_info(VALUE self)
{
int i;
narray_t *na;
GetNArray(self,na);
printf("%s:\n",rb_class2name(CLASS_OF(self)));
printf(" id = 0x%"PRI_VALUE_PREFIX"x\n", self);
printf(" type = %d\n", na->type);
printf(" flag = [%d,%d]\n", na->flag[0], na->flag[1]);
printf(" size = %"SZF"d\n", na->size);
printf(" ndim = %d\n", na->ndim);
printf(" shape = 0x%"SZF"x\n", (size_t)na->shape);
if (na->shape) {
printf(" shape = [");
for (i=0;i<na->ndim;i++)
printf(" %"SZF"d", na->shape[i]);
printf(" ]\n");
}
switch(na->type) {
case NARRAY_DATA_T:
case NARRAY_FILEMAP_T:
rb_narray_debug_info_nadata(self);
break;
case NARRAY_VIEW_T:
rb_narray_debug_info_naview(self);
break;
}
return Qnil;
}
|
#diagonal([offset,axes]) ⇒ Numo::NArray
Returns a diagonal view of NArray
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 |
# File 'ext/numo/narray/data.c', line 561
VALUE
na_diagonal(int argc, VALUE *argv, VALUE self)
{
int i, k, nd;
size_t j;
size_t *idx0, *idx1, *diag_idx;
size_t *shape;
size_t diag_size;
ssize_t stride, stride0, stride1;
narray_t *na;
narray_view_t *na1, *na2;
VALUE view;
VALUE vofs=0, vaxes=0;
ssize_t kofs;
size_t k0, k1;
int ax[2];
// check arguments
if (argc>2) {
rb_raise(rb_eArgError,"too many arguments (%d for 0..2)",argc);
}
for (i=0; i<argc; i++) {
switch(TYPE(argv[i])) {
case T_FIXNUM:
if (vofs) {
rb_raise(rb_eArgError,"offset is given twice");
}
vofs = argv[i];
break;
case T_ARRAY:
if (vaxes) {
rb_raise(rb_eArgError,"axes-array is given twice");
}
vaxes = argv[i];
break;
}
}
if (vofs) {
kofs = NUM2SSIZET(vofs);
} else {
kofs = 0;
}
GetNArray(self,na);
nd = na->ndim;
if (nd < 2) {
rb_raise(nary_eDimensionError,"less than 2-d array");
}
if (vaxes) {
if (RARRAY_LEN(vaxes) != 2) {
rb_raise(rb_eArgError,"axes must be 2-element array");
}
ax[0] = NUM2INT(RARRAY_AREF(vaxes,0));
ax[1] = NUM2INT(RARRAY_AREF(vaxes,1));
if (ax[0]<-nd || ax[0]>=nd || ax[1]<-nd || ax[1]>=nd) {
rb_raise(rb_eArgError,"axis out of range:[%d,%d]",ax[0],ax[1]);
}
if (ax[0]<0) {ax[0] += nd;}
if (ax[1]<0) {ax[1] += nd;}
if (ax[0]==ax[1]) {
rb_raise(rb_eArgError,"same axes:[%d,%d]",ax[0],ax[1]);
}
} else {
ax[0] = nd-2;
ax[1] = nd-1;
}
// Diagonal offset from the main diagonal.
if (kofs >= 0) {
k0 = 0;
k1 = kofs;
if (k1 >= na->shape[ax[1]]) {
rb_raise(rb_eArgError,"invalid diagonal offset(%"SZF"d) for "
"last dimension size(%"SZF"d)",kofs,na->shape[ax[1]]);
}
} else {
k0 = -kofs;
k1 = 0;
if (k0 >= na->shape[ax[0]]) {
rb_raise(rb_eArgError,"invalid diagonal offset(=%"SZF"d) for "
"last-1 dimension size(%"SZF"d)",kofs,na->shape[ax[0]]);
}
}
diag_size = MIN(na->shape[ax[0]]-k0,na->shape[ax[1]]-k1);
// new shape
shape = ALLOCA_N(size_t,nd-1);
for (i=k=0; i<nd; i++) {
if (i != ax[0] && i != ax[1]) {
shape[k++] = na->shape[i];
}
}
shape[k] = diag_size;
// new object
view = na_s_allocate_view(CLASS_OF(self));
na_copy_flags(self, view);
GetNArrayView(view, na2);
// new stride
na_setup_shape((narray_t*)na2, nd-1, shape);
na2->stridx = ALLOC_N(stridx_t, nd-1);
switch(na->type) {
case NARRAY_DATA_T:
case NARRAY_FILEMAP_T:
na2->offset = 0;
na2->data = self;
stride = stride0 = stride1 = na_get_elmsz(self);
for (i=nd,k=nd-2; i--; ) {
if (i==ax[1]) {
stride1 = stride;
if (kofs > 0) {
na2->offset = kofs*stride;
}
} else if (i==ax[0]) {
stride0 = stride;
if (kofs < 0) {
na2->offset = (-kofs)*stride;
}
} else {
SDX_SET_STRIDE(na2->stridx[--k],stride);
}
stride *= na->shape[i];
}
SDX_SET_STRIDE(na2->stridx[nd-2],stride0+stride1);
break;
case NARRAY_VIEW_T:
GetNArrayView(self, na1);
na2->data = na1->data;
na2->offset = na1->offset;
for (i=k=0; i<nd; i++) {
if (i != ax[0] && i != ax[1]) {
if (SDX_IS_INDEX(na1->stridx[i])) {
idx0 = SDX_GET_INDEX(na1->stridx[i]);
idx1 = ALLOC_N(size_t, na->shape[i]);
for (j=0; j<na->shape[i]; j++) {
idx1[j] = idx0[j];
}
SDX_SET_INDEX(na2->stridx[k],idx1);
} else {
na2->stridx[k] = na1->stridx[i];
}
k++;
}
}
if (SDX_IS_INDEX(na1->stridx[ax[0]])) {
idx0 = SDX_GET_INDEX(na1->stridx[ax[0]]);
diag_idx = ALLOC_N(size_t, diag_size);
if (SDX_IS_INDEX(na1->stridx[ax[1]])) {
idx1 = SDX_GET_INDEX(na1->stridx[ax[1]]);
for (j=0; j<diag_size; j++) {
diag_idx[j] = idx0[j+k0] + idx1[j+k1];
}
} else {
stride1 = SDX_GET_STRIDE(na1->stridx[ax[1]]);
for (j=0; j<diag_size; j++) {
diag_idx[j] = idx0[j+k0] + stride1*(j+k1);
}
}
SDX_SET_INDEX(na2->stridx[nd-2],diag_idx);
} else {
stride0 = SDX_GET_STRIDE(na1->stridx[ax[0]]);
if (SDX_IS_INDEX(na1->stridx[ax[1]])) {
idx1 = SDX_GET_INDEX(na1->stridx[ax[1]]);
diag_idx = ALLOC_N(size_t, diag_size);
for (j=0; j<diag_size; j++) {
diag_idx[j] = stride0*(j+k0) + idx1[j+k1];
}
SDX_SET_INDEX(na2->stridx[nd-2],diag_idx);
} else {
stride1 = SDX_GET_STRIDE(na1->stridx[ax[1]]);
na2->offset += stride0*k0 + stride1*k1;
SDX_SET_STRIDE(na2->stridx[nd-2],stride0+stride1);
}
}
break;
}
return view;
}
|
#dot(other) ⇒ Object
Returns dot product.
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 |
# File 'ext/numo/narray/data.c', line 863
static VALUE
numo_na_dot(VALUE self, VALUE other)
{
VALUE test, sym_mulsum;
volatile VALUE a1=self, a2=other;
ID id_mulsum;
narray_t *na1, *na2;
id_mulsum = rb_intern("mulsum");
sym_mulsum = ID2SYM(id_mulsum);
test = rb_funcall(a1, rb_intern("respond_to?"), 1, sym_mulsum);
if (!RTEST(test)) {
rb_raise(rb_eNoMethodError,"requires mulsum method for dot method");
}
GetNArray(a1,na1);
GetNArray(a2,na2);
if (na2->ndim > 1) {
// insert new axis [ ..., last-1-dim, newaxis*other.ndim, last-dim ]
a1 = na_new_dimension_for_dot(a1, na1->ndim-1, na2->ndim-1, 0);
// insert & transpose [ newaxis*self.ndim, ..., last-dim, last-1-dim ]
a2 = na_new_dimension_for_dot(a2, 0, na1->ndim-1, 1);
}
return rb_funcall(a1,rb_intern("mulsum"),2,a2,INT2FIX(-1));
}
|
#empty? ⇒ Boolean
Returns true if self.size == 0.
767 768 769 770 771 772 773 774 775 776 |
# File 'ext/numo/narray/narray.c', line 767
static VALUE
na_empty_p(VALUE self)
{
narray_t *na;
GetNArray(self,na);
if (NA_SIZE(na)==0) {
return Qtrue;
}
return Qfalse;
}
|
#expand_dims(dim) ⇒ Numo::NArray
Expand the shape of an array. Insert a new axis with size=1 at a given dimension.
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
# File 'ext/numo/narray/narray.c', line 956
VALUE
na_expand_dims(VALUE self, VALUE vdim)
{
int i, j, nd, dim;
size_t *shape, *na_shape;
stridx_t *stridx, *na_stridx;
narray_t *na;
narray_view_t *na2;
VALUE view;
GetNArray(self,na);
nd = na->ndim;
dim = NUM2INT(vdim);
if (dim < -nd-1 || dim > nd) {
rb_raise(nary_eDimensionError,"invalid axis (%d for %dD NArray)",
dim,nd);
}
if (dim < 0) {
dim += nd+1;
}
view = na_make_view(self);
GetNArrayView(view, na2);
shape = ALLOC_N(size_t,nd+1);
stridx = ALLOC_N(stridx_t,nd+1);
na_shape = na2->base.shape;
na_stridx = na2->stridx;
for (i=j=0; i<=nd; i++) {
if (i==dim) {
shape[i] = 1;
SDX_SET_STRIDE(stridx[i],0);
} else {
shape[i] = na_shape[j];
stridx[i] = na_stridx[j];
j++;
}
}
na2->stridx = stridx;
xfree(na_stridx);
na2->base.shape = shape;
if (na_shape != &(na2->base.size)) {
xfree(na_shape);
}
na2->base.ndim++;
return view;
}
|
#flatten ⇒ Object
437 438 439 440 441 |
# File 'ext/numo/narray/data.c', line 437
VALUE
na_flatten(VALUE self)
{
return na_flatten_dim(self,0);
}
|
#host_order? ⇒ Boolean Also known as: little_endian?, vacs_order?
Return true if not byte swapped.
1450 1451 1452 1453 1454 1455 |
# File 'ext/numo/narray/narray.c', line 1450
VALUE na_host_order_p( VALUE self )
{
if (TEST_BYTE_SWAPPED(self))
return Qfalse;
return Qtrue;
}
|
#initialize_copy(other) ⇒ Numo::NArray
Replaces the contents of self with the contents of other narray. Used in dup and clone method.
380 381 382 383 384 385 386 387 388 389 390 |
# File 'ext/numo/narray/narray.c', line 380
static VALUE
na_initialize_copy(VALUE self, VALUE orig)
{
narray_t *na;
GetNArray(orig,na);
na_setup(self,NA_NDIM(na),NA_SHAPE(na));
na_store(self,orig);
na_copy_flags(orig,self);
return self;
}
|
#inplace ⇒ Numo::NArray
Returns view of narray with inplace flagged.
1462 1463 1464 1465 1466 1467 1468 |
# File 'ext/numo/narray/narray.c', line 1462
VALUE na_inplace( VALUE self )
{
VALUE view = self;
//view = na_clone(self);
SET_INPLACE(view);
return view;
}
|
#inplace! ⇒ Numo::NArray
Set inplace flag to self.
1474 1475 1476 1477 1478 |
# File 'ext/numo/narray/narray.c', line 1474
VALUE na_inplace_bang( VALUE self )
{
SET_INPLACE(self);
return self;
}
|
#inplace? ⇒ Boolean
Return true if inplace flagged.
1491 1492 1493 1494 1495 1496 1497 |
# File 'ext/numo/narray/narray.c', line 1491
VALUE na_inplace_p( VALUE self )
{
if (TEST_INPLACE(self))
return Qtrue;
else
return Qfalse;
}
|
#ndim ⇒ Object Also known as: rank
method: size() – returns the total number of typeents
754 755 756 757 758 759 760 |
# File 'ext/numo/narray/narray.c', line 754
static VALUE
na_ndim(VALUE self)
{
narray_t *na;
GetNArray(self,na);
return INT2NUM(na->ndim);
}
|
#out_of_place! ⇒ Numo::NArray Also known as: not_inplace!
Unset inplace flag to self.
1503 1504 1505 1506 1507 |
# File 'ext/numo/narray/narray.c', line 1503
VALUE na_out_of_place_bang( VALUE self )
{
UNSET_INPLACE(self);
return self;
}
|
#reshape(*args) ⇒ Object
private function for reshape
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 |
# File 'ext/numo/narray/data.c', line 271
static VALUE
na_reshape(int argc, VALUE *argv, VALUE self)
{
int i, unfixed=-1;
size_t total=1;
size_t *shape; //, *shape_save;
narray_t *na;
VALUE copy;
if (argc == 0) {
rb_raise(rb_eRuntimeError, "No argrument");
}
GetNArray(self,na);
if (NA_SIZE(na) == 0) {
rb_raise(rb_eRuntimeError, "cannot reshape empty array");
}
/* get shape from argument */
shape = ALLOCA_N(size_t,argc);
for (i=0; i<argc; ++i) {
switch(TYPE(argv[i])) {
case T_FIXNUM:
total *= shape[i] = NUM2INT(argv[i]);
break;
case T_NIL:
case T_TRUE:
unfixed = i;
break;
default:
rb_raise(rb_eArgError,"illegal type");
}
}
if (unfixed>=0) {
if (NA_SIZE(na) % total != 0)
rb_raise(rb_eArgError, "Total size size must be divisor");
shape[unfixed] = NA_SIZE(na) / total;
}
else if (total != NA_SIZE(na)) {
rb_raise(rb_eArgError, "Total size must be same");
}
copy = rb_funcall(self,rb_intern("copy"),0);
GetNArray(copy,na);
//shape_save = NA_SHAPE(na);
na_setup_shape(na,argc,shape);
//if (NA_SHAPE(na) != shape_save) {
// xfree(shape_save);
//}
return copy;
}
|
#reverse([dim0,dim1,..]) ⇒ Object
Return reversed view along specified dimeinsion
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 |
# File 'ext/numo/narray/narray.c', line 1015
VALUE
nary_reverse(int argc, VALUE *argv, VALUE self)
{
int i, nd;
size_t j, n;
size_t offset;
size_t *idx1, *idx2;
ssize_t stride;
ssize_t sign;
narray_t *na;
narray_view_t *na1, *na2;
VALUE view;
VALUE reduce;
reduce = na_reduce_dimension(argc, argv, 1, &self);
GetNArray(self,na);
nd = na->ndim;
view = na_s_allocate_view(CLASS_OF(self));
na_copy_flags(self, view);
GetNArrayView(view, na2);
na_setup_shape((narray_t*)na2, nd, na->shape);
na2->stridx = ALLOC_N(stridx_t,nd);
switch(na->type) {
case NARRAY_DATA_T:
case NARRAY_FILEMAP_T:
stride = na_get_elmsz(self);
offset = 0;
for (i=nd; i--;) {
if (na_test_reduce(reduce,i)) {
offset += (na->shape[i]-1)*stride;
sign = -1;
} else {
sign = 1;
}
SDX_SET_STRIDE(na2->stridx[i],stride*sign);
stride *= na->shape[i];
}
na2->offset = offset;
na2->data = self;
break;
case NARRAY_VIEW_T:
GetNArrayView(self, na1);
offset = na1->offset;
for (i=0; i<nd; i++) {
n = na1->base.shape[i];
if (SDX_IS_INDEX(na1->stridx[i])) {
idx1 = SDX_GET_INDEX(na1->stridx[i]);
idx2 = ALLOC_N(size_t,n);
if (na_test_reduce(reduce,i)) {
for (j=0; j<n; j++) {
idx2[n-1-j] = idx1[j];
}
} else {
for (j=0; j<n; j++) {
idx2[j] = idx1[j];
}
}
SDX_SET_INDEX(na2->stridx[i],idx2);
} else {
stride = SDX_GET_STRIDE(na1->stridx[i]);
if (na_test_reduce(reduce,i)) {
offset += (n-1)*stride;
SDX_SET_STRIDE(na2->stridx[i],-stride);
} else {
na2->stridx[i] = na1->stridx[i];
}
}
}
na2->offset = offset;
na2->data = na1->data;
break;
}
return view;
}
|
#row_major? ⇒ Boolean
Return true if row major.
1428 1429 1430 1431 1432 1433 1434 |
# File 'ext/numo/narray/narray.c', line 1428
VALUE na_row_major_p( VALUE self )
{
if (TEST_ROW_MAJOR(self))
return Qtrue;
else
return Qfalse;
}
|
#shape ⇒ Object
method: shape() – returns shape, array of the size of dimensions
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 |
# File 'ext/numo/narray/narray.c', line 780
static VALUE
na_shape(VALUE self)
{
volatile VALUE v;
narray_t *na;
size_t i, n, c, s;
GetNArray(self,na);
n = NA_NDIM(na);
if (TEST_COLUMN_MAJOR(na)) {
c = n-1;
s = -1;
} else {
c = 0;
s = 1;
}
v = rb_ary_new2(n);
for (i=0; i<n; i++) {
rb_ary_push(v, SIZET2NUM(na->shape[c]));
c += s;
}
return v;
}
|
#size ⇒ Object Also known as: length, total
method: size() – returns the total number of typeents
744 745 746 747 748 749 750 |
# File 'ext/numo/narray/narray.c', line 744
static VALUE
na_size(VALUE self)
{
narray_t *na;
GetNArray(self,na);
return SIZET2NUM(na->size);
}
|
#slice(*args) ⇒ Object
method: slice(idx1,idx2,…,idxN)
662 663 664 665 |
# File 'ext/numo/narray/index.c', line 662
static VALUE na_slice(int argc, VALUE *argv, VALUE self)
{
return na_aref_main(argc, argv, self, 1);
}
|
#swap_byte ⇒ Object Also known as: hton
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'ext/numo/narray/data.c', line 109
static VALUE
nary_swap_byte(VALUE self)
{
VALUE v;
ndfunc_arg_in_t ain[1] = {{Qnil,0}};
ndfunc_arg_out_t aout[1] = {{INT2FIX(0),0}};
ndfunc_t ndf = { iter_swap_byte, FULL_LOOP|NDF_ACCEPT_BYTESWAP,
1, 1, ain, aout };
v = na_ndloop(&ndf, 1, self);
if (self!=v) {
na_copy_flags(self, v);
}
REVERSE_BYTE_SWAPPED(v);
return v;
}
|
#to_host ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'ext/numo/narray/data.c', line 145
static VALUE
nary_to_host(VALUE self)
{
if (TEST_HOST_ORDER(self)) {
return self;
}
return rb_funcall(self, rb_intern("swap_byte"), 0);
}
|
#to_network ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'ext/numo/narray/data.c', line 127
static VALUE
nary_to_network(VALUE self)
{
if (TEST_NETWORK_ORDER(self)) {
return self;
}
return rb_funcall(self, rb_intern("swap_byte"), 0);
}
|
#to_string ⇒ String
Returns string containing the raw data bytes in NArray.
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 |
# File 'ext/numo/narray/narray.c', line 1247
static VALUE
nary_to_string(VALUE self)
{
size_t len;
char *ptr;
VALUE str;
narray_t *na;
GetNArray(self,na);
if (na->type == NARRAY_VIEW_T) {
self = rb_funcall(self,rb_intern("copy"),0);
}
len = NUM2SIZET(nary_byte_size(self));
ptr = na_get_pointer_for_read(self);
str = rb_usascii_str_new(ptr,len);
RB_GC_GUARD(self);
return str;
}
|
#to_swapped ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'ext/numo/narray/data.c', line 154
static VALUE
nary_to_swapped(VALUE self)
{
if (TEST_BYTE_SWAPPED(self)) {
return self;
}
return rb_funcall(self, rb_intern("swap_byte"), 0);
}
|
#to_vacs ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'ext/numo/narray/data.c', line 136
static VALUE
nary_to_vacs(VALUE self)
{
if (TEST_VACS_ORDER(self)) {
return self;
}
return rb_funcall(self, rb_intern("swap_byte"), 0);
}
|
#transpose(*args) ⇒ Object
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 |
# File 'ext/numo/narray/data.c', line 197
VALUE
na_transpose(int argc, VALUE *argv, VALUE self)
{
int ndim, *map, *permute;
int i, d;
bool is_positive, is_negative;
narray_t *na1;
GetNArray(self,na1);
ndim = na1->ndim;
if (ndim < 2) {
if (argc > 0) {
rb_raise(rb_eArgError, "unnecessary argument for 1-d array");
}
return na_make_view(self);
}
map = ALLOCA_N(int,ndim);
if (argc == 0) {
for (i=0; i < ndim; i++) {
map[i] = ndim-1-i;
}
return na_transpose_map(self,map);
}
// with argument
if (argc > ndim) {
rb_raise(rb_eArgError, "more arguments than ndim");
}
for (i=0; i < ndim; i++) {
map[i] = i;
}
permute = ALLOCA_N(int,argc);
for (i=0; i < argc; i++) {
permute[i] = 0;
}
is_positive = is_negative = 0;
for (i=0; i < argc; i++) {
if (TYPE(argv[i]) != T_FIXNUM) {
rb_raise(rb_eArgError, "invalid argument");
}
d = FIX2INT(argv[i]);
if (d >= 0) {
if (d >= argc) {
rb_raise(rb_eArgError, "out of dimension range");
}
if (is_negative) {
rb_raise(rb_eArgError, "dimension must be non-negative only or negative only");
}
if (permute[d]) {
rb_raise(rb_eArgError, "not permutation");
}
map[i] = d;
permute[d] = 1;
is_positive = 1;
} else {
if (d < -argc) {
rb_raise(rb_eArgError, "out of dimension range");
}
if (is_positive) {
rb_raise(rb_eArgError, "dimension must be non-negative only or negative only");
}
if (permute[argc+d]) {
rb_raise(rb_eArgError, "not permutation");
}
map[ndim-argc+i] = ndim+d;
permute[argc+d] = 1;
is_negative = 1;
}
}
return na_transpose_map(self,map);
}
|
#view ⇒ Object
Return view of NArray
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 |
# File 'ext/numo/narray/narray.c', line 889
VALUE
na_make_view(VALUE self)
{
int i, nd;
size_t j;
size_t *idx1, *idx2;
ssize_t stride;
narray_t *na;
narray_view_t *na1, *na2;
volatile VALUE view;
GetNArray(self,na);
nd = na->ndim;
view = na_s_allocate_view(CLASS_OF(self));
na_copy_flags(self, view);
GetNArrayView(view, na2);
na_setup_shape((narray_t*)na2, nd, na->shape);
na2->stridx = ALLOC_N(stridx_t,nd);
switch(na->type) {
case NARRAY_DATA_T:
case NARRAY_FILEMAP_T:
stride = na_get_elmsz(self);
for (i=nd; i--;) {
SDX_SET_STRIDE(na2->stridx[i],stride);
stride *= na->shape[i];
}
na2->offset = 0;
na2->data = self;
break;
case NARRAY_VIEW_T:
GetNArrayView(self, na1);
for (i=0; i<nd; i++) {
if (SDX_IS_INDEX(na1->stridx[i])) {
idx1 = SDX_GET_INDEX(na1->stridx[i]);
idx2 = ALLOC_N(size_t,na1->base.shape[i]);
for (j=0; j<na1->base.shape[i]; j++) {
idx2[j] = idx1[j];
}
SDX_SET_INDEX(na2->stridx[i],idx2);
} else {
na2->stridx[i] = na1->stridx[i];
}
}
na2->offset = na1->offset;
na2->data = na1->data;
break;
}
return view;
}
|