ext/numo/narray/narray.c in numo-narray-0.9.1.4 vs ext/numo/narray/narray.c in numo-narray-0.9.1.5

- old
+ new

@@ -1,9 +1,9 @@ /* narray.c - Numerical Array Extension for Ruby - (C) Copyright 1999-2017 by Masahiro TANAKA + Ruby/Numo::NArray - Numerical Array class for Ruby + Copyright (C) 1999-2019 Masahiro TANAKA */ #define NARRAY_C #include <ruby.h> #include <assert.h> @@ -40,21 +40,25 @@ VALUE sym_reduce; VALUE sym_option; VALUE sym_loop_opt; VALUE sym_init; -VALUE na_cStep; #ifndef HAVE_RB_CCOMPLEX VALUE rb_cComplex; #endif +#ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT +VALUE rb_cArithSeq; +#endif int numo_na_inspect_rows=20; int numo_na_inspect_cols=80; void Init_nary_data(); void Init_nary_ndloop(); +#ifndef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT void Init_nary_step(); +#endif void Init_nary_index(); void Init_numo_bit(); void Init_numo_int8(); void Init_numo_int16(); void Init_numo_int32(); @@ -346,23 +350,23 @@ This method does not allocate memory for array data. Memory is allocated on write method such as #fill, #store, #seq, etc. @example i = Numo::Int64.new([2,4,3]) - #=> Numo::Int64#shape=[2,4,3](empty) + # => Numo::Int64#shape=[2,4,3](empty) f = Numo::DFloat.new(3,4) - #=> Numo::DFloat#shape=[3,4](empty) + # => Numo::DFloat#shape=[3,4](empty) f.fill(2) - #=> Numo::DFloat#shape=[3,4] + # => Numo::DFloat#shape=[3,4] # [[2, 2, 2, 2], # [2, 2, 2, 2], # [2, 2, 2, 2]] x = Numo::NArray.new(5) - #=> in `new': allocator undefined for Numo::NArray (TypeError) + # => in `new': allocator undefined for Numo::NArray (TypeError) # from t.rb:9:in `<main>' */ static VALUE na_initialize(VALUE self, VALUE args) @@ -442,14 +446,14 @@ * Returns a zero-filled narray with <i>shape</i>. * This singleton method is valid not for NArray class itself * but for typed NArray subclasses, e.g., DFloat, Int64. * @example * a = Numo::DFloat.zeros(3,5) - * => Numo::DFloat#shape=[3,5] - * [[0, 0, 0, 0, 0], - * [0, 0, 0, 0, 0], - * [0, 0, 0, 0, 0]] + * # => Numo::DFloat#shape=[3,5] + * # [[0, 0, 0, 0, 0], + * # [0, 0, 0, 0, 0], + * # [0, 0, 0, 0, 0]] */ static VALUE na_s_zeros(int argc, VALUE *argv, VALUE klass) { VALUE obj; @@ -466,14 +470,14 @@ * Returns a one-filled narray with <i>shape</i>. * This singleton method is valid not for NArray class itself * but for typed NArray subclasses, e.g., DFloat, Int64. * @example * a = Numo::DFloat.ones(3,5) - * => Numo::DFloat#shape=[3,5] - * [[1, 1, 1, 1, 1], - * [1, 1, 1, 1, 1], - * [1, 1, 1, 1, 1]] + * # => Numo::DFloat#shape=[3,5] + * # [[1, 1, 1, 1, 1], + * # [1, 1, 1, 1, 1], + * # [1, 1, 1, 1, 1]] */ static VALUE na_s_ones(int argc, VALUE *argv, VALUE klass) { VALUE obj; @@ -493,12 +497,12 @@ @param [Integer] n The number of elements. (default is 100). @return [Numo::NArray] result array. @example a = Numo::DFloat.linspace(-5,5,7) - => Numo::DFloat#shape=[7] - [-5, -3.33333, -1.66667, 0, 1.66667, 3.33333, 5] + # => Numo::DFloat#shape=[7] + # [-5, -3.33333, -1.66667, 0, 1.66667, 3.33333, 5] */ static VALUE na_s_linspace(int argc, VALUE *argv, VALUE klass) { VALUE obj, vx1, vx2, vstep, vsize; @@ -532,15 +536,16 @@ @param [Numeric] base The base of log space. (default is 10) @return [Numo::NArray] result array. @example Numo::DFloat.logspace(4,0,5,2) - => Numo::DFloat#shape=[5] - [16, 8, 4, 2, 1] + # => Numo::DFloat#shape=[5] + # [16, 8, 4, 2, 1] + Numo::DComplex.logspace(0,1i*Math::PI,5,Math::E) - => Numo::DComplex#shape=[5] - [1+4.44659e-323i, 0.707107+0.707107i, 6.12323e-17+1i, -0.707107+0.707107i, ...] + # => Numo::DComplex#shape=[5] + # [1+4.44659e-323i, 0.707107+0.707107i, 6.12323e-17+1i, -0.707107+0.707107i, ...] */ static VALUE na_s_logspace(int argc, VALUE *argv, VALUE klass) { VALUE obj, vx1, vx2, vstep, vsize, vbase; @@ -570,14 +575,14 @@ @overload eye(n) @param [Integer] n Size of NArray. Creates 2-D NArray with shape=(n,n) @return [Numo::NArray] created NArray. @example a = Numo::DFloat.eye(3) - => Numo::DFloat#shape=[3,3] - [[1, 0, 0], - [0, 1, 0], - [0, 0, 1]] + # => Numo::DFloat#shape=[3,3] + # [[1, 0, 0], + # [0, 1, 0], + # [0, 0, 1]] */ static VALUE na_s_eye(int argc, VALUE *argv, VALUE klass) { VALUE obj; @@ -1581,12 +1586,17 @@ rb_raise(nary_eDimensionError,"dimension is out of range"); } len = 1; step = 0; //printf("beg=%d step=%d len=%d\n",beg,step,len); - } else if (rb_obj_is_kind_of(v,rb_cRange) || - rb_obj_is_kind_of(v,na_cStep)) { + } else if (rb_obj_is_kind_of(v,rb_cRange) +#ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT + || rb_obj_is_kind_of(v,rb_cArithSeq) +#else + || rb_obj_is_kind_of(v,rb_cEnumerator) +#endif + ) { nary_step_array_index( v, ndim, &len, &beg, &step ); } else { rb_raise(nary_eDimensionError, "invalid dimension argument %s", rb_obj_classname(v)); } @@ -1868,10 +1878,13 @@ for (i=0; i<na1->ndim; i++) { if (na1->shape[i] != na2->shape[i]) { return Qfalse; } } + if (na1->size == 0) { + return Qtrue; + } vbool = rb_funcall(self, id_eq, 1, other); return (rb_funcall(vbool, id_count_false, 0)==INT2FIX(0)) ? Qtrue : Qfalse; } @@ -1894,10 +1907,13 @@ #ifndef HAVE_RB_CCOMPLEX rb_require("complex"); rb_cComplex = rb_const_get(rb_cObject, rb_intern("Complex")); #endif +#ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT + rb_cArithSeq = rb_path2class("Enumerator::ArithmeticSequence"); +#endif rb_define_const(cNArray, "VERSION", rb_str_new2(NARRAY_VERSION)); nary_eCastError = rb_define_class_under(cNArray, "CastError", rb_eStandardError); nary_eShapeError = rb_define_class_under(cNArray, "ShapeError", rb_eStandardError); @@ -1997,10 +2013,12 @@ sym_reduce = ID2SYM(rb_intern("reduce")); sym_option = ID2SYM(rb_intern("option")); sym_loop_opt = ID2SYM(rb_intern("loop_opt")); sym_init = ID2SYM(rb_intern("init")); +#ifndef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT Init_nary_step(); +#endif Init_nary_index(); Init_nary_data(); Init_nary_ndloop();