Class Tensor

A class to describe a tensor, a generalization of vectors and matrices. Tensors can be thought of as (possibly) multidimensional arrays that support many useful operations. They are applied throughout mathematics, physics, engineering, and computer science.

Constructors

  • Constructs a new tensor object.

    Parameters

    • x: number | number[] | Vector | Tensor<Rank> | Tensor

      the numerical object used to create the tensor

    Returns Tensor

Properties

isComplex: boolean = false
shape: number[]
tensor: Tensor<Rank>

Methods

  • Calculates the absolute value (magniutde) of each tensor element. The absolute value of a number is always positive.

    Returns Tensor

    the absolute value of each tensor element

  • The inverse of cos(), returns the arc cosine of each tensor element. This function expects the values in the range of -1 to 1 and values are returned in the range 0 to PI (3.1415927).

    Returns Tensor

    the arc cosine of each tensor element

  • Adds two rows of a matrix.

    Parameters

    • r1: number

      the index of the row being added to

    • r2: number

      the index of the row being added to the other row

    • c: number = 1

      (optional) the constant multiplier for r1

    Returns Tensor

    the resulting matrix

  • Returns a representation of this tensor as a float array. The data transfer is done asynchronously.

    Returns Promise<any>

    the (possibly nested) array of values

  • Returns a representation of this tensor as a float array. The data transfer is done synchronously.

    Returns any

    the (possibly nested) array of values

  • The inverse of sin(), returns the arc sine of a each tensor element. This function expects the values in the range of -1 to 1 and values are returned in the range -PI/2 to PI/2.

    Returns Tensor

    the arc sine of each tensor element

  • The inverse of tan(), returns the arc tangent of each tensor element. This function expects the values in the range of -Infinity to Infinity (exclusive) and values are returned in the range -PI/2 to PI/2.

    Returns Tensor

    the arc tangent of each tensor element

  • Calculates the angle (in radians) from a specified point to the coordinate origin as measured from the positive x-axis. Values are returned as a float in the range from PI to -PI.

    Parameters

    • b: number | Tensor

      the x-coordinate(s) used for computing the arc tangent

    Returns Tensor

    the arc tangent of each tensor element

  • Calculates the closest int value that is greater than or equal to the value of each tensor element. For example, ceil(9.03) returns the value 10.

    Returns Tensor

    each tensor element rounded up

  • Equality check against a complex tensor.

    Parameters

    • b: Tensor

      the tensor to be compared

    Returns boolean

    whether the objects are equals

  • Concatenates two or more tensors.

    Parameters

    • b: Tensor | Tensor[]

      the tensor(s) to be concatenated

    • Optional axis: number

      (optional) the axis to concatenate along

    Returns Tensor

    the concatenated tensor

  • Constrains the value of each tensor element between a minimum and maximum value.

    Parameters

    • low: number

      the minimum value

    • high: number

      the maximum value

    Returns Tensor

    each tensor element constrained to the given range

  • Calculates the cosine of each tensor element. This function does not yet take into account the current angleMode. Values are returned in the range -1 to 1.

    Returns Tensor

    the cosine of each tensor element

  • Disposes the tensor from memory.

    Returns void

  • Divides two tensors element-wise.

    Parameters

    • b: number | Vector | Tensor

      the tensor to be divided by

    Returns Tensor

    the quotient of the tensors

  • Calculates the dot product of two matrices and/or vectors. Note: Only works when both operands are rank 1 or 2.

    Parameters

    • b: Vector | Tensor

      the matrix or vector to be dotted

    Returns Tensor

    the dot product of the tensors

  • Equality check against a Number, p5.Vector, or Tensor.

    Parameters

    • b: number | Vector | Tensor

      the object to be compared

    Returns boolean

    whether the objects are equals

  • Raise Euler's number e (2.71828...) to the power of each tensor element.

    Returns Tensor

    e^n for each tensor element

  • Computes the 1-dimensional discrete Fourier transform over the inner-most dimension of input.

    Returns Tensor

    the transformed tensor

  • Calculates the closest int value that is less than or equal to the value of each tensor element. For example, floor(9.97) returns the value 9.

    Returns Tensor

    each tensor element rounded down

  • Gets the imaginary component of a complex tensor.

    Returns Tensor

    the imaginary component(s) of the tensor

  • Calculates the natural logarithm (the base-e logarithm) of each tensor element. This function expects each tensor element to be a value greater than 0.0.

    Returns Tensor

    the natural logarithm of each tensor element

  • Determines the largest value in a tensor, and then returns that value.

    Returns number

    the maximum number in the tensor

  • Determines the smallest value in a tensor, and then returns that value.

    Returns number

    the minimum number in the tensor

  • Performs modular (remainder) division on two tensors element-wise.

    Parameters

    • b: number | Tensor

      the tensor to be divided by

    Returns Tensor

    the remainder(s)

  • Multiplies two tensors element-wise.

    Parameters

    • b: number | Vector | Tensor

      the tensor to be multiplied

    Returns Tensor

    the product of the tensors

  • Multiplies a row of a matrix by a constant.

    Parameters

    • r1: number

      the index of the row being multiplied

    • c: number

      the constant multiplier

    Returns Tensor

    the resulting matrix

  • Pads a tensor with a given value and paddings.

    Parameters

    • paddings: [number, number][]

      an array prescribing how much to pad [before, after] along each tensor axis

    • Optional constantValue: number

      (optional) the pad value to use

    Returns Tensor

    the padded tensor

  • Performs exponentiation. The pow() method is an efficient way of multiplying tensors by themselves (or their reciprocals) in large quantities.

    Parameters

    • b: number | Tensor

      the power by which to raise each tensor element

    Returns Tensor

    the exponentiated tensor

  • Prints the string representation of the tensor to the console.

    Returns void

  • Reshapes a tensor to a given shape.

    Parameters

    • shape: number[]

      an array of integers defining the output tensor shape

    Returns Tensor

    the reshaped tensor

  • Reverses the tensor along a specificed axis.

    Parameters

    • Optional axis: number | number[]

      (optional) the axis to reverse along

    Returns Tensor

    the reversed tensor

  • Calculates the integer closest to each tensor element. For example, round(133.8) returns the value 134.

    Returns Tensor

    each tensor element rounded

  • Calculates the sine of each tensor element. This function does not yet take into account the current angleMode. Values are returned in the range -1 to 1.

    Returns Tensor

    the sine of each tensor element

  • Extracts a slice from a tensor.

    Parameters

    • begin: number | number[]

      the coordinates to start the slice from

    • Optional size: number | number[]

      (optional) the size of the slice

    Returns Tensor

    the tensor slice

  • Splits a tensor into sub tensors.

    Parameters

    • numOrSizeSplits: number | number[]

      either an integer indicating the number of splits along the axis or an array of integers containing the sizes of each output tensor along the axis. If a number then it must evenly divide the axis length; otherwise the sum of sizes must match axis length.

    • Optional axis: number

      (optional) the dimension along which to split

    Returns Tensor[]

    the split tensor

  • Squares each tensor element (multiplies a number by itself). The result is always a positive number, as multiplying two negative numbers always yields a positive result. For example, -1 * -1 = 1.

    Returns Tensor

    the square of each tensor element

  • Calculates the square root of each tensor element. The square root of a number is always positive, even though there may be a valid negative root. The square root s of number a is such that s*s = a. It is the opposite of squaring.

    Returns Tensor

    the square root of each tensor element

  • Subtracts two tensors element-wise.

    Parameters

    • b: number | Vector | Tensor

      the tensor to be subtracted

    Returns Tensor

    the difference of the tensors

  • Subtracts two rows of a matrix.

    Parameters

    • r1: number

      the index of the row being subtracted from

    • r2: number

      the index of the row being subtracted from the other row

    • c: number = 1

      (optional) the constant multiplier for r1

    Returns Tensor

    the resulting matrix

  • Calculates the sum of tensor elements along an axis.

    Parameters

    • Optional axis: number | number[]

      (optional) the axis to sum along

    Returns Tensor

    the sum

  • Swaps two rows of a matrix.

    Parameters

    • r1: number

      the index of the first row being swapped

    • r2: number

      the index of the second row being swapped

    Returns Tensor

    the resulting matrix

  • Calculates the tangent of each tensor element. This function does not yet take into account the current angleMode. Values are returned in the range of all real numbers.

    Returns Tensor

    the tangent of each tensor element

  • Returns a representation of this tensor as a scalar.

    Returns number

    the scalar

  • Returns a string representation of this tensor. This method is useful for logging tensors to the console.

    Returns string

    a human-readable description of the tensor

  • Returns a representation of this tensor as a p5.Vector.

    Returns Vector

    the p5.Vector

  • Unstacks a rank-R tensor into an array of rank-(R-1) tensors.

    Parameters

    • Optional axis: number

      (optional) the axis to unstack along

    Returns Tensor[]

    the array of tensors

  • Creates a complex tensor with the given real and imaginary components.

    Parameters

    • real: number | number[] | Tensor

      the real component(s)

    • imag: number | number[] | Tensor

      the imaginary component(s)

    Returns Tensor

    the complex tensor

  • Creates an identity matrix with the given dimensions.

    Parameters

    • numRows: number

      the number of rows

    • Optional numCols: number

      (optional) the number of columns

    Returns Tensor

    the identity matrix

  • Creates a tensor filled with a given value.

    Parameters

    • shape: number[]

      the shape of the tensor

    • value: number

      the value to fill the tensor with

    Returns Tensor

    the filled tensor

  • Creates a tensor filled with evenly spaced values.

    Parameters

    • min: number

      the lower bound (inclusive)

    • max: number

      the upper bound (inclusive)

    • num: number

      the number of values to generate

    Returns Tensor

    the filled tensor

  • Creates a tensor filled with ones.

    Parameters

    • shape: number[]

      the shape of the tensor

    Returns Tensor

    the filled tensor

  • Creates a tensor filled with uniformly distributed random numbers.

    Parameters

    • shape: number[]

      the shape of the tensor

    Returns Tensor

    the filled tensor

  • Creates a tensor filled with normally distributed random numbers.

    Parameters

    • shape: number[]

      the shape of tensor

    • Optional mean: number

      (optional) the mean

    • Optional sd: number

      (optional) the standard deviation

    Returns Tensor

    the filled tensor

  • Creates a tensor filled with numbers in the range provided.

    Parameters

    • min: number

      the lower bound (inclusive)

    • max: number

      the upper bound (exclusive)

    • Optional step: number

      (optional) the integer spacing between values

    Returns Tensor

    the filled tensor

  • Stacks an array of tensors along an axis. Tensors must have the same rank.

    Parameters

    • tensors: Tensor[]

      the tensors to be stacked

    • Optional axis: number

      (optional) the axis to stack along

    Returns Tensor

    the stacked tensor

  • Creates a tensor filled with zeros.

    Parameters

    • shape: number[]

      the shape of the tensor

    Returns Tensor

    the filled tensor