Class: Numo::Int16

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

Overview

16-bit signed integer N-dimensional array class.

Constant Summary collapse

UPCAST =

Upcasting rules of Int16.

hCast
ELEMENT_BIT_SIZE =

Element size of Int16 in bits.

INT2FIX(sizeof(dtype) * 8)
ELEMENT_BYTE_SIZE =

Element size of Int16 in bytes.

INT2FIX(sizeof(dtype))
CONTIGUOUS_STRIDE =

Stride size of contiguous Int16 array.

INT2FIX(sizeof(dtype))
MAX =

The largest representable value of Int16.

M_MAX
MIN =

The smallest representable value of Int16.

M_MIN

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::Int16

Cast object to Numo::Int16.

Overloads:

.maximum(a1, a2) ⇒ Numo::Int16

Element-wise maximum of two arrays.

Parameters:

  • a1,a2 (Numo::NArray, Numeric)

    The arrays holding the elements to be compared.

Returns:

.minimum(a1, a2) ⇒ Numo::Int16

Element-wise minimum of two arrays.

Parameters:

  • a1,a2 (Numo::NArray, Numeric)

    The arrays holding the elements to be compared.

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::Int16

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::Int16

Multi-dimensional element reference.

Returns an element or NArray view.

Parameters:

Returns:

  • (Numeric, Numo::Int16)

    an element or NArray view.

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::Int16

abs of self.

Returns abs of self.

Returns:

#allocateObject



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

static VALUE int16_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);

      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) ⇒ 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:

  • 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) ⇒ 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:

  • 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:

#bincount([weight], minlength: nil) ⇒ UInt32 or UInt64 or SFloat or DFloat

Count the number of occurrences of each non-negative integer value. Only Integer-types has this method.

Examples:

Numo::Int32[0..4].bincount
# => Numo::UInt32#shape=[5]
# [1, 1, 1, 1, 1]

Numo::Int32[0, 1, 1, 3, 2, 1, 7].bincount
# => Numo::UInt32#shape=[8]
# [1, 3, 1, 1, 0, 0, 0, 1]

x = Numo::Int32[0, 1, 1, 3, 2, 1, 7, 23]
x.bincount.size == x.max+1
# => true

w = Numo::DFloat[0.3, 0.5, 0.2, 0.7, 1.0, -0.6]
x = Numo::Int32[0, 1, 1, 2, 2, 2]
x.bincount(w)
# => Numo::DFloat#shape=[3]
# [0.3, 0.7, 1.1]

Returns Float NArray if weight array is supplied, otherwise returns UInt32 or UInt64 depending on the size along last axis.

Parameters:

  • weight (SFloat or DFloat or Array)

    (optional) Array of float values. Its size along last axis should be same as that of self.

  • minlength (Integer) (defaults to: nil)

    (keyword, optional) Minimum size along last axis for the output array.

Returns:

  • (UInt32 or UInt64 or SFloat or DFloat)

    Returns Float NArray if weight array is supplied, otherwise returns UInt32 or UInt64 depending on the size along last axis.

#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::Int16

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::Int16

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 Also known as: nearly_eq

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::Int16

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::Int16

Fill elements with other.

Returns self.

Parameters:

  • other (Numeric)

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)

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

Comparison le other.

Returns result of self le other.

Parameters:

Returns:

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

Comparison lt other.

Returns result of self lt other.

Parameters:

Returns:

#mapNumo::Int16

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) ⇒ Numo::Int16

max of self.

Returns result of max.

Parameters:

  • 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) ⇒ 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:

  • 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::DFloat

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:

#median(axis: nil, keepdims: false) ⇒ Numo::Int16

median of self.

dimensions with size one.

Parameters:

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

    Finds median along the axis.

  • keepdims (TrueClass) (defaults to: false)

    If true, the reduced axes are left in the result array as

Returns:

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

min of self.

Returns result of min.

Parameters:

  • 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) ⇒ 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:

  • 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) ⇒ Numo::Int16

minmax of self.

Returns min and max of self.

Parameters:

  • 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) ⇒ 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.

Returns:

#ne(other) ⇒ Numo::Bit

Comparison ne other.

Returns result of self ne other.

Parameters:

Returns:

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

Calculate polynomial.

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

Parameters:

Returns:

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

prod of self.

Returns result of prod.

Parameters:

  • 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) ⇒ Numo::Int16

ptp of self.

Returns result of ptp.

Parameters:

  • 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::Int16

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::Int16

Unary reciprocal.

Returns reciprocal of self.

Returns:

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

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:

#seq([beg,[step]]) ⇒ Numo::Int16 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::Int16

Unary sign.

Returns sign of self.

Returns:

#sort(axis: nil) ⇒ Numo::Int16

sort of self.

Examples:

Numo::DFloat[3,4,1,2].sort #=> Numo::DFloat[1,2,3,4]

Returns result of sort.

Parameters:

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

    Performs sort along the axis.

Returns:

#sort_index(axis: nil) ⇒ Integer, Numo::Int

sort_index. Returns an index array of sort result.

Examples:

Numo::NArray[3,4,1,2].sort_index #=> Numo::Int32[2,3,0,1]

Returns result index of sort_index.

Parameters:

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

    Performs sort_index along the axis.

Returns:

  • (Integer, Numo::Int)

    returns result index of sort_index.

#squareNumo::Int16

Unary square.

Returns square of self.

Returns:

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

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::Int16

Store elements to Numo::Int16 from other.

Returns self.

Parameters:

  • other (Object)

Returns:

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

sum of self.

Returns result of sum.

Parameters:

  • 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)

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

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::Int16

Unary bit_not.

Returns bit_not of self.

Returns: