NDArray#

class NDArray(data, options)#

Constructs or copies an NDArray instance.

Arguments:
  • options (Object)

  • options.shape (Array.<Number>)

  • options.length (Number)

  • options.strides (Array.<Number>)

  • options.dtype (string)

Examples:

import { NDArray } from 'vectorious';

new NDArray() // => array([], dtype=float64)
new NDArray([]) // => array([], dtype=float64)
new NDArray([1, 2, 3]) // => array([1, 2, 3], dtype=float64)
new NDArray([[1, 2], [3, 4]]) // => array([ [ 1, 2 ], [ 3, 4 ] ], dtype=float64)
new NDArray(new Int32Array([1, 2, 3])) // => array([ 1, 2, 3 ], dtype=int32)
new NDArray([1, 2, 3, 4], {
  shape: [2, 2],
  dtype: 'uint32'
}) // => array([ [ 1, 2 ], [ 3, 4 ] ], dtype=uint32)
NDArray.T#

type: NDArray

Short for this.copy().transpose()

NDArray.data#

type: TypedArray

NDArray.dtype#

type: String

NDArray.length#

type: Number

NDArray.shape#

type: Array.<Number>

NDArray.strides#

type: Array.<Number>

NDArray.w#

type: Number

Gets or sets the value at index 3

NDArray.x#

type: Number

Gets or sets the value at index 0

NDArray.y#

type: Number

Gets or sets the value at index 1

NDArray.z#

type: Number

Gets or sets the value at index 2

NDArray.abs()#

Returns the absolute value of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([-1, -2, -3]).abs() // <=> array([1, 2, 3])
NDArray.acos()#

Returns the arccosine of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([-1, 0, 1]).acos(); // <=> array([3.141592653589793, 1.5707963267948966, 0])
NDArray.acosh(x)#

Returns the hyperbolic arccosine of each element of current array.

Arguments:
Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).acosh(); // <=> array([0, 1.316957950592041, 1.7627471685409546])
NDArray.add(x, 1)#

Adds x multiplied by alpha to the current array. Accelerated with BLAS ?axpy.

Arguments:
Returns:

NDArray

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).add([4, 5, 6]); // <=> array([5, 7, 9])
NDArray.angle(x)#

Determines the angle between the current vector and x.

Arguments:
Returns:

number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).angle([4, 5, 6]); // <=> 0.22572622788897287
NDArray.asin()#

Returns the arcsine of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([-1, 0, 1]).asin() // <=> array([-1.5707963705062866, 0, 1.5707963705062866])
NDArray.asinh()#

Returns the hyperbolic arcsine of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([0, 1, 2]).asinh() // <=> array([0, 0.8813735842704773, 1.4436354637145996])
NDArray.atan()#

Returns the arctangent of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).atan() // <=> array([0.7853981852531433, 1.1071487665176392, 1.249045729637146])
NDArray.atanh()#

Returns the hyperbolic arctangent of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([0, -0.5]).atanh(); // <=> array([0, -0.5493061542510986])
NDArray.augment(x)#

Augments x with current matrix.

Arguments:
Returns:

this

Examples:

import { array } from 'vectorious';

array([[1, 2], [3, 4]]).augment(array([[1], [2]])); // <=> array([[1, 2, 1], [3, 4, 2]])
NDArray.binOp(x)#

Perform binary operation f on x in the current array.

Arguments:
Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).binOp([4, 5, 6], (a, b) => a + b); // => array([[5, 7, 9])
NDArray.cbrt()#

Returns the cube root of each element of current array.

Returns:

this

Examples:

import { cbrt } from 'vectorious';

cbrt([1, 8, 27]); // => array([1, 2, 3])
NDArray.ceil()#

Returns smallest integer greater than or equal to of each element of current array.

Returns:

NDArray

Examples:

import { array } from 'vectorious';

array([0.5, 1.5, 2.5]).ceil(); // <=> array([1, 2, 3])
NDArray.check()#

Asserts if indices i, j, …, n are within the bounds of current array

Arguments:
  • ...indices (Array.<Number>)

Throws:

Error – index out of bounds

Examples:

import { array } from 'vectorious';

array([0.5, 1.5, 2.5]).check(3); // Error: index out of bounds
NDArray.combine(x)#

Combines the current vector with x

Arguments:
Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).combine([4, 5, 6]); // => array([1, 2, 3, 4, 5, 6])
NDArray.copy()#

Makes a copy of the class and underlying data

Returns:

NDArray

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).copy(); // => array([1, 2, 3])
NDArray.cos()#

Returns the cosine of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([0, Math.PI / 2, Math.PI]).cos(); // => array([1, 0, -1])
NDArray.cosh()#

Returns the hyperbolic cosine of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([0, 1, 2]).cosh(); // => array([1, 1.5430806875228882, 3.762195587158203])
NDArray.cross(x)#

Computes the cross product of the current vector and the vector x This operation can only calculated for vectors with three components. Otherwise it throws an exception.

Arguments:
Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).cross([4, 5, 6]); // <=> array([-3, 6, -3])
NDArray.det()#

Gets the determinant of current matrix using LU factorization.

Returns:

Number

Examples:

import { array } from 'vectorious';

array([[0, 1], [2, 3]]).det(); // => -2
NDArray.diagonal()#

Gets the diagonal of current matrix.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).diagonal(); // => array([1, 4])
NDArray.dot(x)#

Performs dot multiplication with x and current array Accelerated with BLAS ?dot.

Arguments:
Returns:

Number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).dot([4, 5, 6]); // => 32
NDArray.eig()#

Gets eigenvalues and eigenvectors of the current matrix using the Jacobi method. Accelerated with LAPACK ?geev.

Returns:

Array.<NDArray>

Examples:

import { array } from 'vectorious';

array([[1, 0, 0], [0, 2, 0], [0, 0, 3]]).eig(); // => [array([1, 2, 3]), array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])]
NDArray.equals(x, tolerance)#

Checks if current array and x are equal.

Arguments:
Returns:

Boolean

Examples:

import { equals } from 'vectorious';

array([1, 2, 3]).equals([1, 2, 3]); // => true
NDArray.equidimensional(x)#

Note

Deprecated.

Asserts if current array and x have the same shape

Arguments:
Throws:

Error – shapes x and y do not match

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).equidimensional([1, 2]); // Error: shapes 3 and 2 do not match
NDArray.equilateral(x)#

Note

Deprecated.

Asserts if current array and x have the same length

Arguments:
Throws:

Error – lengths x and y do not match

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).equilateral([1, 2]); // Error: lengths 3 and 2 do not match
NDArray.exp()#

Returns e^x of each element of current array, where x is the argument, and e is Euler’s constant (2.718…), the base of the natural logarithm.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).exp(); // <=> array([2.7182817459106445, 7.389056205749512, 20.08553695678711])
NDArray.expm1()#

Returns subtracting 1 from exp(x) of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).expm1(); // <=> array([1.7182817459106445, 6.389056205749512, 19.08553695678711])
NDArray.fill(value)#

Fills the current array with a scalar value

Arguments:
  • value (Number)

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).fill(0); // <=> array([0, 0, 0])
NDArray.floor()#

Returns the largest integer less than or equal to a number of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1.5, 2.5, 3.5]).floor(); // <=> array([1, 2, 3])
NDArray.forEach(f)#

Equivalent to TypedArray.prototype.forEach.

Arguments:
  • f (function)

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).forEach(console.log);
// 1 0 [ 1, 2, 3 ]
// 2 1 [ 1, 2, 3 ]
// 3 2 [ 1, 2, 3 ]
NDArray.fround()#

Returns the nearest single precision float representation of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([-5.05, 5.05]).fround(); // <=> array([-5.050000190734863, 5.050000190734863])
NDArray.gauss()#

Gauss-Jordan elimination (i.e. returns the reduced row echelon form) of the current matrix.

Returns:

NDArray

Examples:

import { array } from 'vectorious';

array([[1, 2, 3], [4, 5, 6]]).gauss(); // <=> array([[1, 0, -1], [-0, 1, 2]])
NDArray.get()#

Gets the element at i, j, …, n from current vector.

Arguments:
  • ...indices (Array.<Number>)

Returns:

Number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).get(2); // 3
NDArray.inv()#

Determines the inverse of current matrix using Gaussian elimination. Accelerated with LAPACK ?getri.

Returns:

this

Examples:

import { array } from 'vectorious';

array([[2, -1, 0], [-1, 2, -1], [0, -1, 2]]).inv(); // <=> array([[0.75, 0.5, 0.25], [0.5, 1, 0.5], [0.25, 0.5, 0.75]])
NDArray.log()#

Returns the natural logarithm (log_e, also ln) of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).log(); // <=> array([0, 0.6931471824645996, 1.0986123085021973])
NDArray.log10()#

Returns the base 10 logarithm of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([10, 100, 1000]).log10(); // <=> array([1, 2, 3])
NDArray.log1p()#

Returns the natural logarithm (log_e, also ln) of 1 + x for each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]); // <=> array([0.6931471824645996, 1.0986123085021973, 1.3862943649291992])
NDArray.log2()#

Returns the base 2 logarithm of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 4]).log2(); // => array([0, 1, 2])
NDArray.lu()#

Performs full LU decomposition on current matrix. Accelerated with LAPACK ?getrf.

Returns:

Array.<(NDArray|Int32Array)>

Examples:

import { array } from 'vectorious';

array([[1, 3, 5], [2, 4, 7], [1, 1, 0]]).lu(); // => [array([[1, 0, 0], [0.5, 1, 0], [0.5, -1, 1]]), array([[2, 4, 7], [0, 1, 1.5], [0, 0, -2]]), Int32Array([2, 2, 3])]
NDArray.lu_factor()#

Performs LU factorization on current matrix. Accelerated with LAPACK ?getrf.

Returns:

Array.<(NDArray|Int32Array)>

Examples:

import { array } from 'vectorious';

array([[1, 3, 5], [2, 4, 7], [1, 1, 0]]).lu_factor(); // <=> [array([[2, 4, 7], [0.5, 1, 1.5], [0.5, -1, -2]]), Int32Array([2, 2, 3])]
NDArray.map()#

Equivalent to TypedArray.prototype.map.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).map(value => -value); // => array([-1, -2, -3])
NDArray.max()#

Gets the maximum value (smallest) element of current array. Accelerated with BLAS i?amax.

Returns:

Number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).max(); // => 3
NDArray.mean()#

Gets the arithmetic mean of current array.

Returns:

Number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).mean(); // => 2
NDArray.min()#

Gets the minimum value (smallest) element of current array.

Returns:

Number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).min(); // 1
NDArray.multiply(x)#

Multiplies current matrix with x. Accelerated with BLAS ?gemm.

Arguments:
Returns:

NDArray

Examples:

import { array } from 'vectorious';

array([[1, 2]]).multiply([[1], [2]]); // <=> array([[5]])
NDArray.norm()#

Calculates the norm of current array (also called L2 norm or Euclidean length). Accelerated with BLAS ?nrm2.

Returns:

Number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).norm(); // => 3.7416574954986572
NDArray.normalize()#

Normalizes current vector.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).normalize(); // => array([0.26726123690605164, 0.5345224738121033, 0.8017836809158325])
NDArray.pow(exponent)#

Returns each element of current array to the exponent power, that is, element^exponent.

Arguments:
  • exponent (Number)

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).pow(2); // <=> array([1, 4, 9])
NDArray.prod()#

Product of all elements of current array

Returns:

Number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).prod(); // => 6
NDArray.product()#

Hadamard product of current matrix and x

Returns:

NDArray

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).product([4, 5, 6]); // <=> array([4, 10, 18])
NDArray.project()#

Projects the current vector onto x using the projection formula (y * (x * y / y * y)).

Returns:

NDArray

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).project([4, 5, 6]); // <=> array([1.6623376607894897, 2.0779221057891846, 2.49350643157959])
NDArray.push(value)#

Pushes a new value into current vector.

Arguments:
  • value (Number)

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).push(4); // => array([1, 2, 3, 4])
NDArray.rank(tolerance)#

Finds the rank of current matrix using gaussian elimination.

Arguments:
  • tolerance (Number)

Returns:

Number

Examples:

import { array } from 'vectorious';

array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]).rank(); // => 1
NDArray.reciprocal()#

Gets the element-wise reciprocal of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]); // => array([1, 0.5, 0.3333333432674408])
NDArray.reduce(f, initialValue)#

Equivalent to TypedArray.prototype.reduce.

Arguments:
  • f (function)

  • initialValue (Number)

Returns:

Number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).reduce((a, b) => a + b, 0); // => 6
NDArray.reshape()#

Reshapes current array

Arguments:
  • ...shape (Array.<Number>)

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3, 4]).reshape(2, 2); // <=> array([[1, 2], [3, 4]])
NDArray.round()#

Returns the value of each element of current array rounded to the nearest integer.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1.2, 2.8, 3.5]).round(); // <=> array([1, 3, 4])
NDArray.row_add(dest, source, scalar)#

Adds a multiple of one row multiplied by scalar to another inside current matrix.

Arguments:
  • dest (Number)

  • source (Number)

  • scalar (Number)

Returns:

this

Examples:

import { array } from 'vectorious';

array([[1, 2], [3, 4]]).row_add(1, 0, 2); // <=> array([[1, 2], [5, 8]])
NDArray.scale(scalar)#

Multiplies all elements of current array with a specified scalar. Accelerated with BLAS ?scal.

Arguments:
  • scalar (Number)

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).scale(2); // <=> array([2, 4, 6])
NDArray.set(value)#

Sets the element at i, j, …, n to value.

Arguments:
  • ...indices (Array.<Number>)

  • value (Number)

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).set(1, 0); // <=> array([1, 0, 3])
NDArray.sign()#

Returns the sign of each element of current array, indicating whether it is positive, negative or zero.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).sign(); // <=> array([1, 1, 1])
NDArray.sin()#

Returns the sine of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([0, Math.PI / 2, Math.PI]).sin(); // <=> array([0, 1, 0])
NDArray.sinh()#

Returns the hyperbolic sine of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).sinh(); // <=> array([1.175201177597046, 3.6268603801727295, 10.017874717712402])
NDArray.slice(begin, end, step)#

Slices the current array along the leading dimension

Arguments:
  • begin (Number)

  • end (Number)

  • step (Number)

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3, 4]).slice(0, 4, 2); // => array([1, 3])
NDArray.solve(x)#

Solves the equation AX = B (where A is current matrix and B is x). Accelerated with LAPACK ?gesv.

Arguments:
Returns:

NDArray

Examples:

import { array } from 'vectorious';

array([[1, 3, 5], [2, 4, 7], [1, 1, 0]]).solve([[1], [3], [5]]); // => array([[3.25], [1.75], [-1.5]])
NDArray.sqrt()#

Returns the positive square root of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 4, 9]); // <=> array([1, 2, 3])
NDArray.square()#

Asserts if current matrix is square.

Throws:

Error – matrix is not square

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).square(); // Error: matrix is not square
NDArray.subtract()#

Subtracts x from the current array. Accelerated with BLAS ?axpy.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).subtract([1, 1, 1]); // <=> array([0, 1, 2])
NDArray.sum()#

Sum of array elements

Returns:

Number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).sum(); // => 6
NDArray.swap(i, j)#

Swaps two rows i and j in current matrix

Arguments:
  • i (Number)

  • j (Number)

Returns:

this

Examples:

import { array } from 'vectorious';

array([[1, 2], [3, 4]]); // <=> array([[3, 4], [1, 2]])
NDArray.tan()#

Returns the tangent of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).tan(); // <=> array([1.5574077367782593, -2.185039758682251, -0.14254654943943024])
NDArray.tanh()#

Returns the hyperbolic tangent of each element of current array.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).tanh(); // <=> array([0.7615941762924194, 0.9640275835990906, 0.9950547814369202])
NDArray.toArray(index, dim)#

Converts current vector into a JavaScript array.

Arguments:
  • index (Number)

  • dim (Number)

Returns:

Array

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).toArray(); // => [1, 2, 3]
NDArray.toString()#

Converts current vector into a readable formatted string.

Returns:

String

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).toString(); // => '1,2,3'
NDArray.trace()#

Gets the trace of the matrix (the sum of all diagonal elements).

Returns:

Number

Examples:

import { array } from 'vectorious';

array([1, 2, 3]).trace(); // => 5
NDArray.transpose()#

Transposes current matrix (mirror across the diagonal).

Returns:

this

Examples:

import { array } from 'vectorious';

array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // <=> array([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
NDArray.trunc()#

Returns the integer part of each element of current array, removing any fractional digits.

Returns:

this

Examples:

import { array } from 'vectorious';

array([1.2, 2.8, 3.5]).trunc(); // => array([1, 2, 3])