Class: Numo::RObject

Inherits:
NArray show all
Defined in:
ext/numo/narray/src/t_robject.c,
ext/numo/narray/src/t_robject.c

Overview

Ruby object N-dimensional array class.

Constant Summary collapse

UPCAST =

Upcasting rules of RObject.

hCast
ELEMENT_BIT_SIZE =

Element size of RObject in bits.

INT2FIX(sizeof(dtype) * 8)
ELEMENT_BYTE_SIZE =

Element size of RObject in bytes.

INT2FIX(sizeof(dtype))
CONTIGUOUS_STRIDE =

Stride size of contiguous RObject array.

INT2FIX(sizeof(dtype))

Constants inherited from NArray

NArray::ALTERNATIVE, NArray::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NArray

#==, alternative?, #append, #argsort, array_type, asarray, #at, byte_size, #byte_size, #byte_swapped?, #cast_to, #coerce, #column_major?, column_stack, concatenate, #concatenate, #contiguous?, #cov, debug=, #debug_info, #deg2rad, #delete, #diag, #diag_indices, diag_indices, #diagonal, #diff, #dot, #dsplit, dstack, #each_over_axis, #empty?, #expand_dims, eye, #flatten, #fliplr, #flipud, #fortran_contiguous?, #free, from_binary, #host_order?, #hsplit, hstack, #initialize, #initialize_copy, #inner, #inplace, #inplace!, #inplace?, #insert, inspect_cols, inspect_cols=, inspect_rows, inspect_rows=, #kron, linspace, logspace, #marshal_dump, #marshal_load, #ndim, #new_fill, new_like, #new_narray, #new_ones, #new_zeros, ones, #out_of_place!, #outer, parse, #percentile, profile, profile=, #rad2deg, #repeat, #reshape, #reshape!, #reverse, #rot90, #row_major?, #shape, #size, #split, #store_binary, #swap_byte, #swapaxes, #tile, #to_binary, #to_c, #to_f, #to_host, #to_i, #to_network, #to_swapped, #to_vacs, #trace, #transpose, #tril, #tril!, #tril_indices, tril_indices, #triu, #triu!, #triu_indices, triu_indices, upcast, #view, #vsplit, vstack, zeros

Constructor Details

This class inherits a constructor from Numo::NArray

Class Method Details

.[]Object

.[](elements) ⇒ Object .cast(array) ⇒ Numo::RObject

Cast object to Numo::RObject.

Overloads:

.maximum(a1, a2, nan: false) ⇒ Numo::RObject

Element-wise maximum of two arrays.

Parameters:

  • a1 (Numo::NArray, Numeric)

    The array to be compared.

  • a2 (Numo::NArray, Numeric)

    The array to be compared.

  • nan (Boolean) (defaults to: false)

    If true, apply NaN-aware algorithm (return NaN if exist).

Returns:

.minimum(a1, a2, nan: false) ⇒ Numo::RObject

Element-wise minimum of two arrays.

Parameters:

  • a1 (Numo::NArray, Numeric)

    The array to be compared.

  • a2 (Numo::NArray, Numeric)

    The array to be compared.

  • nan (Boolean) (defaults to: false)

    If true, apply NaN-aware algorithm (return NaN if exist).

Returns:

Instance Method Details

#%(other) ⇒ Numo::NArray

Binary mod.

Returns self % other.

Parameters:

Returns:

#&(other) ⇒ Numo::NArray

Binary bit_and.

Returns self & other.

Parameters:

Returns:

#*(other) ⇒ Numo::NArray

Binary mul.

Returns self * other.

Parameters:

Returns:

#**(other) ⇒ Numo::NArray Also known as: pow

Binary power.

Returns self to the other-th power.

Parameters:

Returns:

#+(other) ⇒ Numo::NArray

Binary add.

Returns self + other.

Parameters:

Returns:

#-(other) ⇒ Numo::NArray

Binary sub.

Returns self - other.

Parameters:

Returns:

#-@Numo::RObject

Unary minus.

Returns minus of self.

Returns:

#/(other) ⇒ Numo::NArray

Binary div.

Returns self / other.

Parameters:

Returns:

#<<(other) ⇒ Numo::NArray

Binary left_shift.

Returns self << other.

Parameters:

Returns:

#>>(other) ⇒ Numo::NArray

Binary right_shift.

Returns self >> other.

Parameters:

Returns:

#[](dim0, ..., dimL) ⇒ Numeric, Numo::RObject

Multi-dimensional element reference.

Returns an element or NArray view.

Parameters:

Returns:

See Also:

#[]=(dim0, ..., dimL, val) ⇒ Numeric, ...

Multi-dimensional element assignment.

Returns ‘val` (last argument).

Parameters:

Returns:

  • (Numeric, Numo::NArray, Array)

    returns ‘val` (last argument).

See Also:

#^(other) ⇒ Numo::NArray

Binary bit_xor.

Returns self ^ other.

Parameters:

Returns:

#absNumo::RObject

abs of self.

Returns abs of self.

Returns:

#allocateObject



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
# File 'ext/numo/narray/src/t_robject.c', line 301

static VALUE robject_allocate(VALUE self) {
  narray_t* na;
  char* ptr;

  GetNArray(self, na);

  switch (NA_TYPE(na)) {
  case NARRAY_DATA_T:
    ptr = NA_DATA_PTR(na);
    if (na->size > 0 && ptr == NULL) {
      ptr = xmalloc(sizeof(dtype) * na->size);

      {
        size_t i;
        VALUE* a = (VALUE*)ptr;
        for (i = na->size; i--;) {
          *a++ = Qnil;
        }
      }

      NA_DATA_PTR(na) = ptr;
      NA_DATA_OWNED(na) = TRUE;
    }
    break;
  case NARRAY_VIEW_T:
    rb_funcall(NA_VIEW_DATA(na), rb_intern("allocate"), 0);
    break;
  case NARRAY_FILEMAP_T:
    // ptr = ((narray_filemap_t*)na)->ptr;
    //  to be implemented
  default:
    rb_bug("invalid narray type : %d", NA_TYPE(na));
  }
  return self;
}

#argmax(axis: nil, nan: false) ⇒ Integer, Numo::Int

Index of the maximum value.

Examples:

a = Numo::NArray[3,4,1,2]
a.argmax  #=> 1

b = Numo::NArray[[3,4,1],[2,0,5]]
b.argmax                       #=> 5
b.argmax(axis:1)               #=> [1, 2]
b.argmax(axis:0)               #=> [0, 0, 1]
b.at(b.argmax(axis:0), 0..-1)  #=> [3, 4, 5]

Returns the result indices.

Parameters:

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (return NaN posision if exist).

  • axis (Numeric, Array, Range) (defaults to: nil)

    Finds maximum values along the axis and returns **indices along the axis**.

Returns:

  • (Integer, Numo::Int)

    returns the result indices.

See Also:

#argmin(axis: nil, nan: false) ⇒ Integer, Numo::Int

Index of the minimum value.

Examples:

a = Numo::NArray[3,4,1,2]
a.argmin  #=> 2

b = Numo::NArray[[3,4,1],[2,0,5]]
b.argmin                       #=> 4
b.argmin(axis:1)               #=> [2, 1]
b.argmin(axis:0)               #=> [1, 1, 0]
b.at(b.argmin(axis:0), 0..-1)  #=> [2, 0, 1]

Returns the result indices.

Parameters:

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (return NaN posision if exist).

  • axis (Numeric, Array, Range) (defaults to: nil)

    Finds minimum values along the axis and returns **indices along the axis**.

Returns:

  • (Integer, Numo::Int)

    returns the result indices.

See Also:

#ceilNumo::RObject

Unary ceil.

Returns ceil of self.

Returns:

#clip(min, max) ⇒ Numo::NArray

Clip array elements by [min,max]. If either of min or max is nil, one side is clipped.

Examples:

a = Numo::Int32.new(10).seq
# => Numo::Int32#shape=[10]
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

a.clip(1,8)
# => Numo::Int32#shape=[10]
# [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]

a.inplace.clip(3,6)
a
# => Numo::Int32#shape=[10]
# [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]

b = Numo::Int32.new(10).seq
# => Numo::Int32#shape=[10]
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

b.clip([3,4,1,1,1,4,4,4,4,4], 8)
# => Numo::Int32#shape=[10]
# [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]

Returns result of clip.

Parameters:

Returns:

#coerce_cast(type) ⇒ nil

return NArray with cast to the type of self.

Returns:

  • (nil)

#cumprod(axis: nil, nan: false) ⇒ Numo::RObject

cumprod of self.

Returns cumprod of self.

Parameters:

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs cumprod along the axis.

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN if exists).

Returns:

#cumsum(axis: nil, nan: false) ⇒ Numo::RObject

cumsum of self.

Returns cumsum of self.

Parameters:

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs cumsum along the axis.

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN if exists).

Returns:

#divmod(other) ⇒ Numo::NArray

Binary divmod.

Returns divmod of self and other.

Parameters:

Returns:

#each {|x| ... } ⇒ Numo::NArray

Calls the given block once for each element in self, passing that element as a parameter.

For a block ‘{|x| … }`,

Yield Parameters:

  • x (Numeric)

    an element of NArray.

Returns:

See Also:

#each_with_index {|x, i,j,...| ... } ⇒ Numo::NArray

Invokes the given block once for each element of self, passing that element and indices along each axis as parameters.

For a block ‘{|x,i,j,…| … }`,

Yield Parameters:

  • x (Numeric)

    an element

  • i,j,... (Integer)

    multitimensional indices

Returns:

See Also:

#eq(other) ⇒ Numo::Bit

Comparison eq other.

Returns result of self eq other.

Parameters:

Returns:

#extractNumeric, Numo::NArray

Extract an element only if self is a dimensionless NArray.

— Extract element value as Ruby Object if self is a dimensionless NArray, otherwise returns self.

Returns:

#eye([element,offset]) ⇒ Numo::RObject

Eye: Set a value to diagonal components, set 0 to non-diagonal components.

Returns eye of self.

Parameters:

  • element (Numeric)

    Diagonal element to be stored. Default is 1.

  • offset (Integer)

    Diagonal offset from the main diagonal. The default is 0. k>0 for diagonals above the main diagonal, and k<0 for diagonals below the main diagonal.

Returns:

#fill(other) ⇒ Numo::RObject

Fill elements with other.

Returns self.

Parameters:

  • other (Numeric)

Returns:

#floorNumo::RObject

Unary floor.

Returns floor of self.

Returns:

#format(format) ⇒ Numo::RObject

Format elements into strings.

Returns array of formatted strings.

Parameters:

  • format (String)

Returns:

#format_to_a(format) ⇒ Array

Format elements into strings.

Returns array of formatted strings.

Parameters:

  • format (String)

Returns:

  • (Array)

    array of formatted strings.

#ge(other) ⇒ Numo::Bit Also known as: >=

Comparison ge other.

Returns result of self ge other.

Parameters:

Returns:

#gt(other) ⇒ Numo::Bit Also known as: >

Comparison gt other.

Returns result of self gt other.

Parameters:

Returns:

#inspectString

Returns a string containing a human-readable representation of NArray.

Returns:

  • (String)

#isfiniteNumo::Bit

Condition of isfinite.

Returns Condition of isfinite.

Returns:

#isinfNumo::Bit

Condition of isinf.

Returns Condition of isinf.

Returns:

#isnanNumo::Bit

Condition of isnan.

Returns Condition of isnan.

Returns:

#isneginfNumo::Bit

Condition of isneginf.

Returns Condition of isneginf.

Returns:

#isposinfNumo::Bit

Condition of isposinf.

Returns Condition of isposinf.

Returns:

#le(other) ⇒ Numo::Bit Also known as: <=

Comparison le other.

Returns result of self le other.

Parameters:

Returns:

#logseq(beg, step, [base]) ⇒ Numo::RObject

Set logarithmic sequence of numbers to self. The sequence is obtained from

`base**(beg+i*step)`

where i is 1-dimensional index. Applicable classes: DFloat, SFloat, DComplex, SCopmplex.

Examples:

Numo::DFloat.new(5).logseq(4,-1,2)
# => Numo::DFloat#shape=[5]
# [16, 8, 4, 2, 1]

Numo::DComplex.new(5).logseq(0,1i*Math::PI/3,Math::E)
# => Numo::DComplex#shape=[5]
# [1+7.26156e-310i, 0.5+0.866025i, -0.5+0.866025i, -1+1.22465e-16i, ...]

Returns self.

Parameters:

  • beg (Numeric)

    The beginning of sequence.

  • step (Numeric)

    The step of sequence.

  • base (Numeric)

    The base of log space. (default=10)

Returns:

#lt(other) ⇒ Numo::Bit Also known as: <

Comparison lt other.

Returns result of self lt other.

Parameters:

Returns:

#mapNumo::RObject

Unary map.

Returns map of self.

Returns:

#map_with_index {|x, i,j,...| ... } ⇒ Numo::NArray

Invokes the given block once for each element of self, passing that element and indices along each axis as parameters. Creates a new NArray containing the values returned by the block. Inplace option is allowed, i.e., ‘nary.inplace.map` overwrites `nary`.

For a block ‘{|x,i,j,…| … }`,

Yield Parameters:

  • x (Numeric)

    an element

  • i,j,... (Integer)

    multitimensional indices

Returns:

See Also:

#max(axis: nil, keepdims: false, nan: false) ⇒ Numo::RObject

max of self.

Returns result of max.

Parameters:

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs max along the axis.

  • keepdims (TrueClass) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

Returns:

#max_index(axis: nil, nan: false) ⇒ Integer, Numo::Int

Index of the maximum value.

Examples:

a = Numo::NArray[3,4,1,2]
a.max_index  #=> 1

b = Numo::NArray[[3,4,1],[2,0,5]]
b.max_index             #=> 5
b.max_index(axis:1)     #=> [1, 5]
b.max_index(axis:0)     #=> [0, 1, 5]
b[b.max_index(axis:0)]  #=> [3, 4, 5]

Returns result indices.

Parameters:

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (return NaN posision if exist).

  • axis (Numeric, Array, Range) (defaults to: nil)

    Finds maximum values along the axis and returns **flat 1-d indices**.

Returns:

  • (Integer, Numo::Int)

    returns result indices.

See Also:

#mean(axis: nil, keepdims: false, nan: false) ⇒ Numo::RObject

mean of self.

Returns result of mean.

Parameters:

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs mean along the axis.

  • keepdims (Boolean) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

  • nan (Boolean) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

Returns:

#min(axis: nil, keepdims: false, nan: false) ⇒ Numo::RObject

min of self.

Returns result of min.

Parameters:

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs min along the axis.

  • keepdims (TrueClass) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

Returns:

#min_index(axis: nil, nan: false) ⇒ Integer, Numo::Int

Index of the minimum value.

Examples:

a = Numo::NArray[3,4,1,2]
a.min_index  #=> 2

b = Numo::NArray[[3,4,1],[2,0,5]]
b.min_index             #=> 4
b.min_index(axis:1)     #=> [2, 4]
b.min_index(axis:0)     #=> [3, 4, 2]
b[b.min_index(axis:0)]  #=> [2, 0, 1]

Returns result indices.

Parameters:

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (returnNaN posision if exist).

  • axis (Numeric, Array, Range) (defaults to: nil)

    Finds minimum values along the axis and returns **flat 1-d indices**.

Returns:

  • (Integer, Numo::Int)

    returns result indices.

See Also:

#minmax(axis: nil, keepdims: false, nan: false) ⇒ Numo::RObject

minmax of self.

Returns min and max of self.

Parameters:

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (return NaN if exist).

  • axis (Numeric, Array, Range) (defaults to: nil)

    Finds min-max along the axis.

  • keepdims (TrueClass) (defaults to: false)

    (keyword) If true, the reduced axes are left in the result array as dimensions with size one.

Returns:

#mulsum(other, axis: nil, keepdims: false, nan: false) ⇒ Numo::NArray

Binary mulsum.

Returns mulsum of self and other.

Parameters:

  • other (Numo::NArray, Numeric)
  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs mulsum along the axis.

  • keepdims (TrueClass) (defaults to: false)

    (keyword) If true, the reduced axes are left in the result array as dimensions with size one.

  • nan (TrueClass) (defaults to: false)

    (keyword) If true, apply NaN-aware algorithm (avoid NaN if exists).

Returns:

#ne(other) ⇒ Numo::Bit

Comparison ne other.

Returns result of self ne other.

Parameters:

Returns:

#nearly_eq(other) ⇒ Numo::Bit Also known as: close_to

Comparison nearly_eq other.

Returns result of self nearly_eq other.

Parameters:

Returns:

  • (Numo::Bit)

    result of self nearly_eq other.

#poly(a0, a1, ..., an) ⇒ Numo::RObject

Calculate polynomial.

`x.poly(a0,a1,a2,...,an) = a0 + a1*x + a2*x**2 + ... + an*x**n`

Parameters:

Returns:

#prod(axis: nil, keepdims: false, nan: false) ⇒ Numo::RObject

prod of self.

Returns result of prod.

Parameters:

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs prod along the axis.

  • keepdims (TrueClass) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

Returns:

#ptp(axis: nil, keepdims: false, nan: false) ⇒ Numo::RObject

ptp of self.

Returns result of ptp.

Parameters:

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs ptp along the axis.

  • keepdims (TrueClass) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

Returns:

#rand([[low],high]) ⇒ Numo::RObject

Generate uniformly distributed random numbers on self narray.

Examples:

Numo::DFloat.new(6).rand
# => Numo::DFloat#shape=[6]
# [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]

Numo::DComplex.new(6).rand(5+5i)
# => Numo::DComplex#shape=[6]
# [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]

Numo::Int32.new(6).rand(2,5)
# => Numo::Int32#shape=[6]
# [4, 3, 3, 2, 4, 2]

Returns self.

Parameters:

  • low (Numeric)

    lower inclusive boundary of random numbers. (default=0)

  • high (Numeric)

    upper exclusive boundary of random numbers. (default=1 or 1+1i for complex types)

Returns:

#reciprocalNumo::RObject

Unary reciprocal.

Returns reciprocal of self.

Returns:

#rms(axis: nil, keepdims: false, nan: false) ⇒ Numo::RObject

rms of self.

Returns result of rms.

Parameters:

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs rms along the axis.

  • keepdims (Boolean) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

  • nan (Boolean) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

Returns:

#roundNumo::RObject

Unary round.

Returns round of self.

Returns:

#seq([beg,[step]]) ⇒ Numo::RObject Also known as: indgen

Set linear sequence of numbers to self. The sequence is obtained from

beg+i*step

where i is 1-dimensional index.

Examples:

Numo::DFloat.new(6).seq(1,-0.2)
# => Numo::DFloat#shape=[6]
# [1, 0.8, 0.6, 0.4, 0.2, 0]

Numo::DComplex.new(6).seq(1,-0.2+0.2i)
# => Numo::DComplex#shape=[6]
# [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]

Returns self.

Parameters:

  • beg (Numeric)

    beginning of sequence. (default=0)

  • step (Numeric)

    step of sequence. (default=1)

Returns:

#signNumo::RObject

Unary sign.

Returns sign of self.

Returns:

#squareNumo::RObject

Unary square.

Returns square of self.

Returns:

#stddev(axis: nil, keepdims: false, nan: false) ⇒ Numo::RObject

stddev of self.

Returns result of stddev.

Parameters:

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs stddev along the axis.

  • keepdims (Boolean) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

  • nan (Boolean) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

Returns:

#store(other) ⇒ Numo::RObject

Store elements to Numo::RObject from other.

Returns self.

Parameters:

  • other (Object)

Returns:

#sum(axis: nil, keepdims: false, nan: false) ⇒ Numo::RObject

sum of self.

Returns result of sum.

Parameters:

  • nan (TrueClass) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs sum along the axis.

  • keepdims (TrueClass) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

Returns:

#to_aArray

Convert self to Array.

Returns:

  • (Array)

#truncNumo::RObject

Unary trunc.

Returns trunc of self.

Returns:

#var(axis: nil, keepdims: false, nan: false) ⇒ Numo::RObject

var of self.

Returns result of var.

Parameters:

  • axis (Numeric, Array, Range) (defaults to: nil)

    Performs var along the axis.

  • keepdims (Boolean) (defaults to: false)

    If true, the reduced axes are left in the result array as dimensions with size one.

  • nan (Boolean) (defaults to: false)

    If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or, return NaN for min/max etc).

Returns:

#|(other) ⇒ Numo::NArray

Binary bit_or.

Returns self | other.

Parameters:

Returns:

#~Numo::RObject

Unary bit_not.

Returns bit_not of self.

Returns: