ndarray_backend_numpy

NDArray numpy backend

source

Array

 Array (size)

Initialize self. See help(type(self)) for accurate signature.

to_numpy

/opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/fastcore/docscrape.py:225: UserWarning: Unknown section Examples
  else: warn(msg)

source

to_numpy

 to_numpy (a, shape, strides, offset)

Converts a contiguous 1D array into an N-dimensional array using numpy stride tricks.

This function creates a ‘view’ of a given 1D array as an N-dimensional array with the specified shape and strides, starting from the offset index.

Type Details
a numpy.ndarray Input 1D array which should be converted.
shape tuple of ints The desired shape of the output array.
strides tuple of ints The number of bytes to step in each dimension when traversing the array.
offset int The index from which the new ‘view’ of the array should start.
Returns numpy.ndarray N-dimensional array ‘view’ of the input 1D array.
array_1D = Array(6)
array_1D
array([1.2611686e-44, 2.1250000e+00,           nan, 1.9999999e+00,
       1.8216880e-44, 1.8750000e+00], dtype=float32)
np_arr = to_numpy(array_1D, (2, 3), (3, 1), 0)
np_arr
array([[1.2611686e-44, 2.1250000e+00,           nan],
       [1.9999999e+00, 1.8216880e-44, 1.8750000e+00]], dtype=float32)

from_numpy


source

from_numpy

 from_numpy (a:numpy.ndarray, out)

Assigns a flattened version of the input N-dimensional array to another array.

This function takes an input N-dimensional numpy array, flattens it, and assigns the result to the specified output array.

Type Details
a ndarray The input array to be flattened.
out object The output object. Its ‘array’ attribute should be an array-like
object that will receive the flattened input array.
Returns None
array_nd = np.array([[1, 2, 3], [4, 5, 6]])
array_nd
array([[1, 2, 3],
       [4, 5, 6]])
array_nd.flatten()
array([1, 2, 3, 4, 5, 6])
out = Array(6)
out.array[:] = array_nd.flatten()
out
array([1., 2., 3., 4., 5., 6.], dtype=float32)
from_numpy(array_nd, out)
out.array
array([1., 2., 3., 4., 5., 6.], dtype=float32)

fill


source

fill

 fill (out:__main__.Array, val)

Fills an Array object with a specific value.

This function replaces all the elements of the underlying numpy array of the out Array object with the specified value.

Type Details
out Array The Array object whose underlying numpy array is to be filled with the specified value.
val scalar The value to fill the Array with.
Returns None

compact


source

compact

 compact (a, out:__main__.Array, shape, strides, offset)

Transforms a 1D array into an N-dimensional array, flattens it, and assigns it to an Array object.

This function uses the to_numpy function to create an N-dimensional view of the input array a with the specified shape and strides, starting from the offset index. The result is then flattened and assigned to the underlying numpy array of the out Array object.

Type Details
a numpy.ndarray The input 1D array to be transformed.
out Array The Array object whose underlying numpy array is to be assigned the flattened N-dimensional array.
shape tuple of ints The desired shape of the N-dimensional array.
strides tuple of ints The number of bytes to step in each dimension when traversing the array.
offset int The index from which the N-dimensional view of the array should start.
Returns None

ewise_setitem


source

ewise_setitem

 ewise_setitem (a:__main__.Array, out:__main__.Array, shape, strides,
                offset)

Modifies a section of an Array object to be equivalent to another reshaped array, on an element-wise basis.

This function uses the to_numpy function to create a view of a section of the underlying numpy array of the out Array object, determined by shape, strides, and offset. It then assigns the reshaped a.array to this section, performing the operation element-wise.

Type Details
a Array The Array object whose reshaped underlying numpy array is to be assigned.
out Array The Array object whose underlying numpy array is to be modified.
shape tuple of ints The shape to reshape a.array to, and the shape of the section in out.array.
strides tuple of ints The number of bytes to step in each dimension when traversing the out array.
offset int The index from which the view of the out array should start.
Returns None
shape = (2, 3)
a = Array(6)
a.array[:] = np.array([1, 2, 3, 4, 5, 6])
a
array([1., 2., 3., 4., 5., 6.], dtype=float32)
out = Array(9)
out.array[:] = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0])
out
array([0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
a.array.reshape(shape)
array([[1., 2., 3.],
       [4., 5., 6.]], dtype=float32)
to_numpy(out, shape, strides=(3, 1), offset=0)
array([[0., 0., 0.],
       [0., 0., 0.]], dtype=float32)
res = to_numpy(out, shape, strides=(3, 1), offset=0)[:] = a.array.reshape(shape)
res
array([[1., 2., 3.],
       [4., 5., 6.]], dtype=float32)
ewise_setitem(a, out, (2, 3), (3, 1), 0)
out
array([1., 2., 3., 4., 5., 6., 0., 0., 0.], dtype=float32)

scalar_setitem


source

scalar_setitem

 scalar_setitem (val, out:__main__.Array, shape, strides, offset)

Fills a section of an Array object with a specific scalar value.

This function uses the to_numpy function to create a view of a section of the underlying numpy array of the out Array object, determined by shape, strides, and offset. It then assigns the scalar value val to this section.

Type Details
val scalar The scalar value to be assigned to the section of out.array.
out Array The Array object whose underlying numpy array is to be modified.
shape tuple of ints The shape of the section in out.array.
strides tuple of ints The number of bytes to step in each dimension when traversing the out array.
offset int The index from which the view of the out array should start.
Returns None

ewise_add


source

ewise_add

 ewise_add (a:__main__.Array, b:__main__.Array, out:__main__.Array)

Performs an element-wise addition of two Array objects and assigns the result to a third Array object.

This function adds the underlying numpy arrays of a and b on an element-wise basis, and assigns the result to the underlying numpy array of out.

Type Details
a Array
b Array
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

scalar_add


source

scalar_add

 scalar_add (a:__main__.Array, val, out:__main__.Array)

Adds a scalar value to an Array object and assigns the result to another Array object.

This function adds a scalar value val to the underlying numpy array of a, and assigns the result to the underlying numpy array of out.

Type Details
a Array The Array object to be added to the scalar value.
val scalar The scalar value to be added to a.array.
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

ewise_mul


source

ewise_mul

 ewise_mul (a:__main__.Array, b:__main__.Array, out:__main__.Array)

Performs an element-wise multiplication of two Array objects and assigns the result to a third Array object.

This function multiplies the underlying numpy arrays of a and b on an element-wise basis, and assigns the result to the underlying numpy array of out.

Type Details
a Array
b Array
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

scalar_mul


source

scalar_mul

 scalar_mul (a:__main__.Array, val, out:__main__.Array)

Multiplies an Array object by a scalar value and assigns the result to another Array object.

This function multiplies a scalar value val with the underlying numpy array of a, and assigns the result to the underlying numpy array of out.

Type Details
a Array The Array object to be multiplied by the scalar value.
val scalar The scalar value to be multiplied by a.array.
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

ewise_div


source

ewise_div

 ewise_div (a:__main__.Array, b:__main__.Array, out:__main__.Array)

Performs an element-wise division of two Array objects and assigns the result to a third Array object.

This function divides the underlying numpy array of a by that of b on an element-wise basis, and assigns the result to the underlying numpy array of out.

Type Details
a Array
b Array
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

scalar_div


source

scalar_div

 scalar_div (a:__main__.Array, val, out:__main__.Array)

Divides an Array object by a scalar value and assigns the result to another Array object.

This function divides the underlying numpy array of a by a scalar value val, and assigns the result to the underlying numpy array of out.

Type Details
a Array The Array object to be divided by the scalar value.
val scalar The scalar value by which a.array is to be divided.
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

scalar_power


source

scalar_power

 scalar_power (a:__main__.Array, val, out:__main__.Array)

Raises an Array object to the power of a scalar value and assigns the result to another Array object.

This function raises each element in the underlying numpy array of a to the power of a scalar value val, and assigns the result to the underlying numpy array of out.

Type Details
a Array The Array object to be raised to the power of the scalar value.
val scalar The scalar value which is the exponent for a.array.
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

ewise_maximum


source

ewise_maximum

 ewise_maximum (a:__main__.Array, b:__main__.Array, out:__main__.Array)

Computes the element-wise maximum of two Array objects and assigns the result to a third Array object.

This function compares the underlying numpy arrays of a and b on an element-wise basis, and assigns the maximum value at each position to the underlying numpy array of out.

Type Details
a Array
b Array
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

scalar_maximum


source

scalar_maximum

 scalar_maximum (a:__main__.Array, val, out:__main__.Array)

Computes the maximum of an Array object and a scalar value, and assigns the result to another Array object.

This function compares a scalar value val with the underlying numpy array of a, and assigns the maximum value at each position to the underlying numpy array of out.

Type Details
a Array The Array object whose maximum with val is to be computed.
val scalar The scalar value to be compared with a.array.
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

ewise_eq


source

ewise_eq

 ewise_eq (a:__main__.Array, b:__main__.Array, out:__main__.Array)

Performs an element-wise comparison for equality between two Array objects and assigns the result to a third Array object.

This function compares the underlying numpy arrays of a and b on an element-wise basis for equality, and assigns the boolean result (converted to float32) to the underlying numpy array of out.

Type Details
a Array
b Array
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

scalar_eq


source

scalar_eq

 scalar_eq (a:__main__.Array, val, out:__main__.Array)

Compares an Array object with a scalar value for equality and assigns the result to another Array object.

This function compares a scalar value val with the underlying numpy array of a for equality, and assigns the boolean result (converted to float32) to the underlying numpy array of out.

Type Details
a Array The Array object to be compared with the scalar value.
val scalar The scalar value to be compared with a.array.
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

ewise_ge


source

ewise_ge

 ewise_ge (a:__main__.Array, b:__main__.Array, out:__main__.Array)

Performs an element-wise comparison to check if elements of one Array object are greater than or equal to those of another Array object. The result is assigned to a third Array object.

This function compares the underlying numpy arrays of a and b on an element-wise basis to check if elements in a are greater than or equal to those in b. The boolean result (converted to float32) is assigned to the underlying numpy array of out.

Type Details
a Array
b Array
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

scalar_ge


source

scalar_ge

 scalar_ge (a:__main__.Array, val, out:__main__.Array)

Compares an Array object with a scalar value to check if elements in the Array object are greater than or equal to the scalar. The result is assigned to another Array object.

This function compares a scalar value val with the underlying numpy array of a to check if elements in a are greater than or equal to val. The boolean result (converted to float32) is assigned to the underlying numpy array of out.

Type Details
a Array The Array object to be compared with the scalar value.
val scalar The scalar value to be compared with a.array.
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

ewise_log


source

ewise_log

 ewise_log (a:__main__.Array, out:__main__.Array)

Computes the natural logarithm of each element in an Array object and assigns the result to another Array object.

This function applies the natural logarithm (base e) to each element in the underlying numpy array of a, and assigns the result to the underlying numpy array of out.

Type Details
a Array The Array object whose natural logarithm is to be computed.
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

ewise_exp


source

ewise_exp

 ewise_exp (a:__main__.Array, out:__main__.Array)

Computes the exponential of each element in an Array object and assigns the result to another Array object.

This function applies the exponential (base e) to each element in the underlying numpy array of a, and assigns the result to the underlying numpy array of out.

Type Details
a Array The Array object whose exponential is to be computed.
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None
x = np.arange(24).reshape(2,3,4)
x
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
y = np.empty((2,3,4))
y
array([[[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]],

       [[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]]])
y[:] = np.exp(x)
y
array([[[1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01],
        [5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03],
        [2.98095799e+03, 8.10308393e+03, 2.20264658e+04, 5.98741417e+04]],

       [[1.62754791e+05, 4.42413392e+05, 1.20260428e+06, 3.26901737e+06],
        [8.88611052e+06, 2.41549528e+07, 6.56599691e+07, 1.78482301e+08],
        [4.85165195e+08, 1.31881573e+09, 3.58491285e+09, 9.74480345e+09]]])
a = Array(3)
a.array[:] = np.array([0, 1, 2])
out = Array(3)
ewise_exp(a, out)
out
array([1.       , 2.7182817, 7.389056 ], dtype=float32)

ewise_tanh


source

ewise_tanh

 ewise_tanh (a:__main__.Array, out:__main__.Array)

Computes the hyperbolic tangent of each element in an Array object and assigns the result to another Array object.

This function applies the hyperbolic tangent function to each element in the underlying numpy array of a, and assigns the result to the underlying numpy array of out.

Type Details
a Array The Array object whose hyperbolic tangent is to be computed.
out Array The Array object whose underlying numpy array is to be assigned the result.
Returns None

reduce_max

x = np.array([1, 2, 3, 4, 5, 6])
x
array([1, 2, 3, 4, 5, 6])
x[:].reshape(-1, 3)
array([[1, 2, 3],
       [4, 5, 6]])
x[:].reshape(-1, 3).max(1)
array([3, 6])

source

reduce_max

 reduce_max (a:__main__.Array, out:__main__.Array, reduce_size:int)

Computes the maximum of every reduce_size elements in an Array object and assigns the result to another Array object.

This function reshapes the underlying numpy array of a into a 2D array with reduce_size columns, computes the maximum of every reduce_size elements (along the second axis), and assigns the results to the underlying numpy array of out.

Type Details
a Array The Array object whose maximum of every reduce_size elements is to be computed.
out Array The Array object whose underlying numpy array is to be assigned the result.
reduce_size int The size of elements over which the maximum is to be computed.
Returns None

reduce_sum

x.reshape(-1, 3).sum(1)
array([ 6, 15])
x.reshape(-1, 3).sum(0)
array([5, 7, 9])

source

reduce_sum

 reduce_sum (a:__main__.Array, out:__main__.Array, reduce_size:int)

Computes the sum of every reduce_size elements in an Array object and assigns the result to another Array object.

This function reshapes the underlying numpy array of a into a 2D array with reduce_size columns, computes the sum of every reduce_size elements (along the second axis), and assigns the results to the underlying numpy array of out.

Type Details
a Array The Array object whose sum of every reduce_size elements is to be computed.
out Array The Array object whose underlying numpy array is to be assigned the result.
reduce_size int The size of elements over which the sum is to be computed.
Returns None

matmul

m,n,p = 2, 3, 2
a = np.array([1, 2, 3, 4, 5, 6])
b = np.array([7, 8, 9, 10, 11, 12])
out = Array(4)
res = a.reshape(m, n) @ b.reshape(n, p)
res
array([[ 58,  64],
       [139, 154]])
res.reshape(-1)
array([ 58,  64, 139, 154])

source

matmul

 matmul (a:__main__.Array, b:__main__.Array, out:__main__.Array, m:int,
         n:int, p:int)

Performs matrix multiplication between two Array objects and assigns the result to another Array object.

This function reshapes the underlying numpy arrays of a and b to matrices of dimensions (m, n) and (n, p) respectively. It then performs matrix multiplication (a @ b), reshapes the result back into a 1D array, and assigns it to the underlying numpy array of out.

Type Details
a Array
b Array
out Array The Array object whose underlying numpy array is to be assigned the result.
m int
n int
p int
Returns None
a = Array(6)
a.array[:] = np.array([1, 2, 3, 4, 5, 6])
b = Array(6)
b.array[:] = np.array([7, 8, 9, 10, 11, 12])
out = Array(4)
matmul(a, b, out, 2, 3, 2)
print(out)
array([ 58.,  64., 139., 154.], dtype=float32)

Export

import nbdev; nbdev.nbdev_export()