class Narray::Array(T)

Overview

Main class for multi-dimensional arrays.

This class represents a multi-dimensional array with elements of type T. It provides methods for accessing and manipulating array elements, as well as various mathematical operations.

Defined in:

narray.cr
narray/broadcast.cr
narray/math.cr
narray/operations.cr:9
narray/operations.cr:230
narray/operations.cr:639

Constructors

Instance Method Summary

Constructor Detail

def self.new(shape : ::Array(Int32), data : ::Array(T)) #

Creates a new NArray with the given shape and data.

The data array must have the same number of elements as the product of the shape dimensions. Elements are stored in row-major order (C-style).

arr = Narray::Array(Int32).new([2, 2], [1, 2, 3, 4])
arr.shape # => [2, 2]
arr.data  # => [1, 2, 3, 4]

Raises ArgumentError if the data size does not match the shape.


[View source]

Instance Method Detail

def !=(other : Array(T)) : Array(Bool) #

Inequality operator (!=)


[View source]
def !=(value : T) : Array(Bool) #

Inequality operator (!=) with scalar


[View source]
def *(other : Array(T)) : Array(T) #

Performs element-wise multiplication of two arrays.

If the shapes match exactly, multiplies corresponding elements. If the shapes are compatible for broadcasting, broadcasts the arrays to a common shape and then multiplies corresponding elements.

a = Narray.array([2, 2], [1, 2, 3, 4])
b = Narray.array([2, 2], [5, 6, 7, 8])
c = a * b
c.shape # => [2, 2]
c.data  # => [5, 12, 21, 32]

# Broadcasting example
a = Narray.array([2, 1], [2, 3])
b = Narray.array([1, 3], [1, 2, 3])
c = a * b
c.shape # => [2, 3]
c.data  # => [2, 4, 6, 3, 6, 9]

Raises ArgumentError if the shapes are not compatible for broadcasting.

See also: Array#multiply!, Array#/.


[View source]
def *(scalar : Number) : Array(T) #

Performs element-wise multiplication of an array and a scalar.

Multiplies each element of the array by the scalar.

a = Narray.array([2, 2], [1, 2, 3, 4])
b = a * 2
b.shape # => [2, 2]
b.data  # => [2, 4, 6, 8]

See also: Array#multiply!, Array#/.


[View source]
def +(other : Array(T)) : Array(T) #

Performs element-wise addition of two arrays.

If the shapes match exactly, adds corresponding elements. If the shapes are compatible for broadcasting, broadcasts the arrays to a common shape and then adds corresponding elements.

a = Narray.array([2, 2], [1, 2, 3, 4])
b = Narray.array([2, 2], [5, 6, 7, 8])
c = a + b
c.shape # => [2, 2]
c.data  # => [6, 8, 10, 12]

# Broadcasting example
a = Narray.array([2, 1], [1, 2])
b = Narray.array([1, 3], [3, 4, 5])
c = a + b
c.shape # => [2, 3]
c.data  # => [4, 5, 6, 5, 6, 7]

Raises ArgumentError if the shapes are not compatible for broadcasting.

See also: Array#add!, Array#-.


[View source]
def +(scalar : Number) : Array(T) #

Performs element-wise addition of an array and a scalar.

Adds the scalar to each element of the array.

a = Narray.array([2, 2], [1, 2, 3, 4])
b = a + 5
b.shape # => [2, 2]
b.data  # => [6, 7, 8, 9]

See also: Array#add!, Array#-.


[View source]
def -(other : Array(T)) : Array(T) #

Performs element-wise subtraction of two arrays.

If the shapes match exactly, subtracts corresponding elements. If the shapes are compatible for broadcasting, broadcasts the arrays to a common shape and then subtracts corresponding elements.

a = Narray.array([2, 2], [5, 6, 7, 8])
b = Narray.array([2, 2], [1, 2, 3, 4])
c = a - b
c.shape # => [2, 2]
c.data  # => [4, 4, 4, 4]

# Broadcasting example
a = Narray.array([2, 1], [5, 10])
b = Narray.array([1, 3], [1, 2, 3])
c = a - b
c.shape # => [2, 3]
c.data  # => [4, 3, 2, 9, 8, 7]

Raises ArgumentError if the shapes are not compatible for broadcasting.

See also: Array#subtract!, Array#+.


[View source]
def -(scalar : Number) : Array(T) #

Performs element-wise subtraction of an array and a scalar.

Subtracts the scalar from each element of the array.

a = Narray.array([2, 2], [5, 6, 7, 8])
b = a - 3
b.shape # => [2, 2]
b.data  # => [2, 3, 4, 5]

See also: Array#subtract!, Array#+.


[View source]
def - #

Performs element-wise negation of an array.

Returns a new array with each element negated.

a = Narray.array([2, 2], [1, 2, 3, 4])
b = -a
b.shape # => [2, 2]
b.data  # => [-1, -2, -3, -4]

[View source]
def /(other : Array(T)) : Array(T) #

Performs element-wise division of two arrays.

If the shapes match exactly, divides corresponding elements. If the shapes are compatible for broadcasting, broadcasts the arrays to a common shape and then divides corresponding elements.

a = Narray.array([2, 2], [10, 12, 14, 16])
b = Narray.array([2, 2], [2, 3, 2, 4])
c = a / b
c.shape # => [2, 2]
c.data  # => [5, 4, 7, 4]

# Broadcasting example with floating-point division
a = Narray.array([2, 1], [6.0, 9.0])
b = Narray.array([1, 3], [1.0, 2.0, 3.0])
c = a / b
c.shape # => [2, 3]
c.data  # => [6.0, 3.0, 2.0, 9.0, 4.5, 3.0]

Raises ArgumentError if the shapes are not compatible for broadcasting.

See also: Array#divide!, Array#*.


[View source]
def /(scalar : Number) : Array(T) #

Performs element-wise division of an array and a scalar.

Divides each element of the array by the scalar.

a = Narray.array([2, 2], [2, 4, 6, 8])
b = a / 2
b.shape # => [2, 2]
b.data  # => [1, 2, 3, 4]

See also: Array#divide!, Array#*.


[View source]
def <(other : Array(T)) : Array(Bool) #

Less than operator (<)


[View source]
def <(value : T) : Array(Bool) #

Less than operator (<) with scalar


[View source]
def <=(other : Array(T)) : Array(Bool) #

Less than or equal to operator (<=)


[View source]
def <=(value : T) : Array(Bool) #

Less than or equal to operator (<=) with scalar


[View source]
def ==(other : Array(T)) : Array(Bool) #

Equality operator (==)


[View source]
def ==(value : T) : Array(Bool) #

Equality operator (==) with scalar


[View source]
def >(other : Array(T)) : Array(Bool) #

Greater than operator (>)


[View source]
def >(value : T) : Array(Bool) #

Greater than operator (>) with scalar


[View source]
def >=(other : Array(T)) : Array(Bool) #

Greater than or equal to operator (>=)


[View source]
def >=(value : T) : Array(Bool) #

Greater than or equal to operator (>=) with scalar


[View source]
def [](indices : ::Array(Int32)) : T #

Returns the element at the given indices.

arr = Narray.array([2, 3], [1, 2, 3, 4, 5, 6])
arr[[0, 0]] # => 1
arr[[0, 1]] # => 2
arr[[1, 2]] # => 6

Raises IndexError if the number of indices does not match the number of dimensions. Raises IndexError if any index is out of bounds.


[View source]
def []=(indices : ::Array(Int32), value : T) #

Sets the element at the given indices.

arr = Narray.zeros([2, 3], Int32)
arr[[0, 0]] = 1
arr[[0, 1]] = 2
arr[[1, 2]] = 3
arr.data # => [1, 2, 0, 0, 0, 3]

Raises IndexError if the number of indices does not match the number of dimensions. Raises IndexError if any index is out of bounds.


[View source]
def []=(indices : ::Array(SliceIndex), value : Array(T)) #

Sets a slice of the array using bracket notation with slice indices.

arr = Narray.array([3, 4], (1..12).to_a)
sub_arr = Narray.array([2, 2], [100, 200, 300, 400])
arr[[0..1, 0..1]] = sub_arr # Replace the top-left 2x2 submatrix

See #slice_set for more details.


[View source]
def add!(other : Array(T)) : self #

Performs element-wise addition of two arrays in-place.

If the shapes match exactly, adds corresponding elements in-place. If the shapes are compatible for broadcasting, broadcasts the arrays to a common shape and then adds corresponding elements in-place.

a = Narray.array([2, 2], [1, 2, 3, 4])
b = Narray.array([2, 2], [5, 6, 7, 8])
a.add!(b)
a.data # => [6, 8, 10, 12]

# Broadcasting example
a = Narray.array([2, 1], [1, 2])
b = Narray.array([1, 3], [3, 4, 5])
a.add!(b)
a.shape # => [2, 3]
a.data  # => [4, 5, 6, 5, 6, 7]

Raises ArgumentError if the shapes are not compatible for broadcasting.

See also: Array#+, Array#subtract!.


[View source]
def add!(scalar : Number) : self #

Performs element-wise addition of an array and a scalar in-place.

Adds the scalar to each element of the array in-place.

a = Narray.array([2, 2], [1, 2, 3, 4])
a.add!(5)
a.data # => [6, 7, 8, 9]

See also: Array#+, Array#subtract!.


[View source]
def at(indices : ::Array(Int32)) : T #

Returns the element at the given indices.

arr = Narray.array([2, 3], [1, 2, 3, 4, 5, 6])
arr.at([0, 0]) # => 1
arr.at([0, 1]) # => 2
arr.at([1, 2]) # => 6

Raises IndexError if the number of indices does not match the number of dimensions. Raises IndexError if any index is out of bounds.


[View source]
def at(*indices : Int32) : T #

Convenience method for accessing elements with variadic indices.

arr = Narray.array([2, 3, 4], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])
arr.at(0, 1, 2) # => 7

Raises IndexError if the number of indices does not match the number of dimensions. Raises IndexError if any index is out of bounds.


[View source]
def at_set(indices : ::Array(Int32), value : T) : self #

Sets the element at the given indices.

arr = Narray.zeros([2, 3], Int32)
arr.at_set([0, 0], 1)
arr.at_set([0, 1], 2)
arr.at_set([1, 2], 3)
arr.data # => [1, 2, 0, 0, 0, 3]

Raises IndexError if the number of indices does not match the number of dimensions. Raises IndexError if any index is out of bounds.


[View source]
def at_set(*args : Int32) : self #

Convenience method for setting elements with variadic indices.

arr = Narray.zeros([2, 3], Int32)
arr.at_set(0, 0, 1)
arr.at_set(0, 1, 2)
arr.at_set(1, 2, 3)
arr.data # => [1, 2, 0, 0, 0, 3]

Raises IndexError if the number of indices does not match the number of dimensions. Raises IndexError if any index is out of bounds.


[View source]
def broadcast_to(new_shape : ::Array(Int32)) : Array(T) #

Broadcasts this array to a new shape.

arr = Narray.array([3], [1, 2, 3])
result = arr.broadcast_to([2, 3])
result.shape # => [2, 3]
result.data  # => [1, 2, 3, 1, 2, 3]

Raises ArgumentError if the shapes are incompatible for broadcasting.

See also: Narray.broadcast.


[View source]
def data : ::Array(T) #

The underlying data storage.

Returns a flat array containing all elements of the multi-dimensional array. Elements are stored in row-major order (C-style).


[View source]
def divide!(other : Array(T)) : self #

Performs element-wise division of two arrays in-place.

If the shapes match exactly, divides corresponding elements in-place. If the shapes are compatible for broadcasting, broadcasts the arrays to a common shape and then divides corresponding elements in-place.

a = Narray.array([2, 2], [10, 12, 14, 16])
b = Narray.array([2, 2], [2, 3, 2, 4])
a.divide!(b)
a.data # => [5, 4, 7, 4]

# Broadcasting example with floating-point division
a = Narray.array([2, 1], [6.0, 9.0])
b = Narray.array([1, 3], [1.0, 2.0, 3.0])
a.divide!(b)
a.shape # => [2, 3]
a.data  # => [6.0, 3.0, 2.0, 9.0, 4.5, 3.0]

Raises ArgumentError if the shapes are not compatible for broadcasting.

See also: Array#/, Array#multiply!.


[View source]
def divide!(scalar : Number) : self #

Performs element-wise division of an array and a scalar in-place.

Divides each element of the array by the scalar in-place.

a = Narray.array([2, 2], [2, 4, 6, 8])
a.divide!(2)
a.data # => [1, 2, 3, 4]

See also: Array#/, Array#multiply!.


[View source]
def eq(other : Array(T)) : Array(Bool) #

Returns a boolean mask for equality with another array.

The arrays must be broadcast compatible.

a = Narray.array([3], [1, 2, 3])
b = Narray.array([3], [1, 3, 3])
mask = a.eq(b)
mask.data # => [true, false, true]

Raises ArgumentError if the arrays cannot be broadcast together.

See also: Array#ne, Array#==.


[View source]
def eq(value : T) : Array(Bool) #

Returns a boolean mask for equality with a value.

arr = Narray.array([5], [1, 2, 3, 2, 1])
mask = arr.eq(2)
mask.data # => [false, true, false, true, false]

See also: Array#ne, Array#==.


[View source]
def ge(other : Array(T)) : Array(Bool) #

Returns a boolean mask for greater than or equal to another array.

The arrays must be broadcast compatible.

a = Narray.array([3], [1, 2, 3])
b = Narray.array([3], [0, 2, 4])
mask = a.ge(b)
mask.data # => [true, true, false]

Raises ArgumentError if the arrays cannot be broadcast together.

See also: Array#gt, Array#>=.


[View source]
def ge(value : T) : Array(Bool) #

Returns a boolean mask for greater than or equal to a value.

arr = Narray.array([5], [1, 2, 3, 4, 5])
mask = arr.ge(3)
mask.data # => [false, false, true, true, true]

See also: Array#gt, Array#>=.


[View source]
def gt(other : Array(T)) : Array(Bool) #

Returns a boolean mask for greater than another array.

The arrays must be broadcast compatible.

a = Narray.array([3], [1, 2, 3])
b = Narray.array([3], [0, 2, 4])
mask = a.gt(b)
mask.data # => [true, false, false]

Raises ArgumentError if the arrays cannot be broadcast together.

See also: Array#ge, Array#>.


[View source]
def gt(value : T) : Array(Bool) #

Returns a boolean mask for greater than a value.

arr = Narray.array([5], [1, 2, 3, 4, 5])
mask = arr.gt(3)
mask.data # => [false, false, false, true, true]

See also: Array#ge, Array#>.


[View source]
def inspect(io : IO) : Nil #

Returns a detailed string representation of the array for debugging.

For small arrays (size <= 20), all elements are shown. For large arrays, only the first 10 and last 10 elements are shown.

arr = Narray.array([2, 2], [1, 2, 3, 4])
arr.inspect # => "Narray::Array(Int32)[shape=[2, 2], ndim=2, size=4, data=[1, 2, 3, 4]]"

[View source]
def le(other : Array(T)) : Array(Bool) #

Returns a boolean mask for less than or equal to another array.

The arrays must be broadcast compatible.

a = Narray.array([3], [1, 2, 3])
b = Narray.array([3], [0, 2, 4])
mask = a.le(b)
mask.data # => [false, true, true]

Raises ArgumentError if the arrays cannot be broadcast together.

See also: Array#lt, Array#<=.


[View source]
def le(value : T) : Array(Bool) #

Returns a boolean mask for less than or equal to a value.

arr = Narray.array([5], [1, 2, 3, 4, 5])
mask = arr.le(3)
mask.data # => [true, true, true, false, false]

See also: Array#lt, Array#<=.


[View source]
def lt(other : Array(T)) : Array(Bool) #

Returns a boolean mask for less than another array.

The arrays must be broadcast compatible.

a = Narray.array([3], [1, 2, 3])
b = Narray.array([3], [0, 2, 4])
mask = a.lt(b)
mask.data # => [false, false, true]

Raises ArgumentError if the arrays cannot be broadcast together.

See also: Array#le, Array#<.


[View source]
def lt(value : T) : Array(Bool) #

Returns a boolean mask for less than a value.

arr = Narray.array([5], [1, 2, 3, 4, 5])
mask = arr.lt(3)
mask.data # => [true, true, false, false, false]

See also: Array#le, Array#<.


[View source]
def mask(mask : Array(Bool)) : Array(T) #

Returns a new array containing only the elements where the mask is true.

The mask must be a boolean array with the same shape as the original array. The result is a 1D array containing only the elements where the mask is true.

arr = Narray.array([5], [1, 2, 3, 4, 5])
mask = Narray.array([5], [true, false, true, false, true])
result = arr.mask(mask)
result.shape # => [3]
result.data  # => [1, 3, 5]

Raises ArgumentError if the mask shape does not match the array shape.

See also: Array#mask_set, Array#mask(&block).


[View source]
def mask(&block : T -> Bool) : Array(T) #

Returns a new array containing only the elements that satisfy the given condition.

The condition is specified as a block that takes an element and returns a boolean. The result is a 1D array containing only the elements where the block returns true.

arr = Narray.array([5], [1, 2, 3, 4, 5])
result = arr.mask { |x| x > 2 }
result.shape # => [3]
result.data  # => [3, 4, 5]

See also: Array#mask(mask), Array#mask_set.


[View source]
def mask_set(mask : Array(Bool), values : Array(T)) : self #

Updates elements in the array where the mask is true with values from another array.

The mask must be a boolean array with the same shape as the original array. The values array must have the same number of elements as there are true values in the mask. The array is modified in-place.

arr = Narray.array([5], [1, 2, 3, 4, 5])
mask = Narray.array([5], [true, false, true, false, true])
values = Narray.array([3], [10, 20, 30])
arr.mask_set(mask, values)
arr.data # => [10, 2, 20, 4, 30]

Raises ArgumentError if the mask shape does not match the array shape. Raises ArgumentError if the values array does not have the correct number of elements.

See also: Array#mask, Array#mask_set(mask, value).


[View source]
def mask_set(mask : Array(Bool), value : T) : self #

Updates elements in the array where the mask is true with the given value.

The mask must be a boolean array with the same shape as the original array. The array is modified in-place.

arr = Narray.array([5], [1, 2, 3, 4, 5])
mask = Narray.array([5], [true, false, true, false, true])
arr.mask_set(mask, 0)
arr.data # => [0, 2, 0, 4, 0]

Raises ArgumentError if the mask shape does not match the array shape.

See also: Array#mask, Array#mask_set(&block).


[View source]
def mask_set(value : T, &block : T -> Bool) : self #

Updates elements in the array that satisfy the given condition with the given value.

The condition is specified as a block that takes an element and returns a boolean. The array is modified in-place.

arr = Narray.array([5], [1, 2, 3, 4, 5])
arr.mask_set(0) { |x| x > 2 }
arr.data # => [1, 2, 0, 0, 0]

See also: Array#mask, Array#mask_set(mask, value).


[View source]
def max : T #

Returns the maximum value in the array.

a = Narray.array([2, 2], [3, 1, 4, 2])
a.max # => 4

See also: Array#min, Array#sum.


[View source]
def mean : Float64 #

Computes the mean (average) of all elements in the array.

a = Narray.array([2, 2], [1, 2, 3, 4])
a.mean # => 2.5

See also: Array#sum, Array#std.


[View source]
def min : T #

Returns the minimum value in the array.

a = Narray.array([2, 2], [3, 1, 4, 2])
a.min # => 1

See also: Array#max, Array#sum.


[View source]
def multiply!(other : Array(T)) : self #

Performs element-wise multiplication of two arrays in-place.

If the shapes match exactly, multiplies corresponding elements in-place. If the shapes are compatible for broadcasting, broadcasts the arrays to a common shape and then multiplies corresponding elements in-place.

a = Narray.array([2, 2], [1, 2, 3, 4])
b = Narray.array([2, 2], [5, 6, 7, 8])
a.multiply!(b)
a.data # => [5, 12, 21, 32]

# Broadcasting example
a = Narray.array([2, 1], [2, 3])
b = Narray.array([1, 3], [1, 2, 3])
a.multiply!(b)
a.shape # => [2, 3]
a.data  # => [2, 4, 6, 3, 6, 9]

Raises ArgumentError if the shapes are not compatible for broadcasting.

See also: Array#*, Array#divide!.


[View source]
def multiply!(scalar : Number) : self #

Performs element-wise multiplication of an array and a scalar in-place.

Multiplies each element of the array by the scalar in-place.

a = Narray.array([2, 2], [1, 2, 3, 4])
a.multiply!(2)
a.data # => [2, 4, 6, 8]

See also: Array#*, Array#divide!.


[View source]
def ndim : Int32 #

Returns the number of dimensions of the array.

arr = Narray.array([2, 3], [1, 2, 3, 4, 5, 6])
arr.ndim # => 2

[View source]
def ne(other : Array(T)) : Array(Bool) #

Returns a boolean mask for inequality with another array.

The arrays must be broadcast compatible.

a = Narray.array([3], [1, 2, 3])
b = Narray.array([3], [1, 3, 3])
mask = a.ne(b)
mask.data # => [false, true, false]

Raises ArgumentError if the arrays cannot be broadcast together.

See also: Array#eq, Array#!=.


[View source]
def ne(value : T) : Array(Bool) #

Returns a boolean mask for inequality with a value.

arr = Narray.array([5], [1, 2, 3, 2, 1])
mask = arr.ne(2)
mask.data # => [true, false, true, false, true]

See also: Array#eq, Array#!=.


[View source]
def reshape(new_shape : ::Array(Int32)) : Array(T) #

Reshapes the array to the new shape.

The total number of elements must remain the same. Returns a new array with the same data but new shape.

arr = Narray.arange(0, 6)
reshaped = arr.reshape([2, 3])
reshaped.shape # => [2, 3]
reshaped.ndim  # => 2
reshaped.size  # => 6
reshaped.data  # => [0, 1, 2, 3, 4, 5]

arr2 = Narray.array([2, 3], [1, 2, 3, 4, 5, 6])
reshaped2 = arr2.reshape([6])
reshaped2.shape # => [6]
reshaped2.ndim  # => 1

Raises ArgumentError if the new shape has a different number of elements.

See also: Array#reshape!.


[View source]
def reshape!(new_shape : ::Array(Int32)) : self #

Reshapes the array to the new shape in-place.

The total number of elements must remain the same. Modifies the array's shape in-place, keeping the same data.

arr = Narray.arange(0, 6)
arr.reshape!([2, 3])
arr.shape # => [2, 3]
arr.ndim  # => 2
arr.size  # => 6
arr.data  # => [0, 1, 2, 3, 4, 5]

Raises ArgumentError if the new shape has a different number of elements.

See also: Array#reshape.


[View source]
def shape : ::Array(Int32) #

The shape of the array (dimensions).

Returns an array of integers representing the size of each dimension. For example, a 2x3 array would have a shape of [2, 3].


[View source]
def size : Int32 #

Returns the total number of elements in the array.

arr = Narray.array([2, 3], [1, 2, 3, 4, 5, 6])
arr.size # => 6

[View source]
def slice(indices : ::Array(SliceIndex)) : Array(T) #

Returns a slice of the array based on the given indices.

Each index can be:

  • An integer: selects a single element along that dimension
  • A range: selects a range of elements along that dimension
  • A boolean (true): selects all elements along that dimension
arr = Narray.array([3, 4], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
arr.slice([0..1, 1..2]) # => 2D array with elements at positions (0,1), (0,2), (1,1), (1,2)
arr.slice([0, true])    # => 1D array with all elements in the first row
arr.slice([true, 2])    # => 1D array with elements at column index 2

Raises IndexError if the number of indices does not match the number of dimensions. Raises IndexError if any index is out of bounds.


[View source]
def slice_set(indices : ::Array(SliceIndex), value : Array(T)) : self #

Sets a slice of the array to the given value.

Each index can be:

  • An integer: selects a single element along that dimension
  • A range: selects a range of elements along that dimension
  • A boolean (true): selects all elements along that dimension
arr = Narray.array([3, 4], (1..12).to_a)
sub_arr = Narray.array([2, 2], [100, 200, 300, 400])
arr.slice_set([0..1, 0..1], sub_arr) # Replace the top-left 2x2 submatrix

Raises IndexError if the number of indices does not match the number of dimensions. Raises IndexError if any index is out of bounds. Raises ArgumentError if the shape of the value does not match the shape of the slice.


[View source]
def std : Float64 #

Computes the standard deviation of all elements in the array.

The standard deviation is a measure of the amount of variation or dispersion of a set of values. A low standard deviation indicates that the values tend to be close to the mean, while a high standard deviation indicates that the values are spread out over a wider range.

a = Narray.array([2, 2], [1, 2, 3, 4])
a.std # => 1.118... (approximately)

See also: Array#mean, Array#sum.


[View source]
def subtract!(other : Array(T)) : self #

Performs element-wise subtraction of two arrays in-place.

If the shapes match exactly, subtracts corresponding elements in-place. If the shapes are compatible for broadcasting, broadcasts the arrays to a common shape and then subtracts corresponding elements in-place.

a = Narray.array([2, 2], [5, 6, 7, 8])
b = Narray.array([2, 2], [1, 2, 3, 4])
a.subtract!(b)
a.data # => [4, 4, 4, 4]

# Broadcasting example
a = Narray.array([2, 1], [5, 10])
b = Narray.array([1, 3], [1, 2, 3])
a.subtract!(b)
a.shape # => [2, 3]
a.data  # => [4, 3, 2, 9, 8, 7]

Raises ArgumentError if the shapes are not compatible for broadcasting.

See also: Array#-, Array#add!.


[View source]
def subtract!(scalar : Number) : self #

Performs element-wise subtraction of an array and a scalar in-place.

Subtracts the scalar from each element of the array in-place.

a = Narray.array([2, 2], [5, 6, 7, 8])
a.subtract!(3)
a.data # => [2, 3, 4, 5]

See also: Array#-, Array#add!.


[View source]
def sum : T #

Computes the sum of all elements in the array.

a = Narray.array([2, 2], [1, 2, 3, 4])
a.sum # => 10

See also: Array#mean, Array#min, Array#max.


[View source]
def to_s(io : IO) #

Returns a string representation of the array.

arr = Narray.array([2, 2], [1, 2, 3, 4])
arr.to_s # => "Narray.array([2, 2], [1, 2, 3, 4])"

[View source]
def transpose : Array(T) #

Returns the transpose of the array.

For 1D arrays, this returns a copy of the array. For 2D arrays, this swaps rows and columns. For higher dimensions, this reverses the order of dimensions.

# 1D array
arr = Narray.arange(0, 3)
transposed = arr.transpose
transposed.shape # => [3]
transposed.data  # => [0, 1, 2]

# 2D array
arr = Narray.array([2, 3], [1, 2, 3, 4, 5, 6])
transposed = arr.transpose
transposed.shape # => [3, 2]
transposed.data  # => [1, 4, 2, 5, 3, 6]

# 3D array
arr = Narray.array([2, 2, 2], [1, 2, 3, 4, 5, 6, 7, 8])
transposed = arr.transpose
transposed.shape # => [2, 2, 2]
transposed.data  # => [1, 5, 3, 7, 2, 6, 4, 8]

See also: Array#transpose!.


[View source]
def transpose! : self #

Transposes the array in-place.

For 1D arrays, this does nothing. For 2D arrays, this swaps rows and columns. For higher dimensions, this reverses the order of dimensions. Note: This method creates a new data array and updates the shape.

# 1D array - no change
arr = Narray.arange(0, 3)
arr.transpose!
arr.shape # => [3]
arr.data  # => [0, 1, 2]

# 2D array
arr = Narray.array([2, 3], [1, 2, 3, 4, 5, 6])
arr.transpose!
arr.shape # => [3, 2]
arr.data  # => [1, 4, 2, 5, 3, 6]

See also: Array#transpose.


[View source]