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.

Hierarchy

  • Tensor

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

    the absolute value of each tensor element

    Returns Tensor

  • 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

    the arc cosine of each tensor element

    Returns Tensor

  • Adds two tensors element-wise.

    Returns

    the sum of the tensors

    Parameters

    • b: number | Vector | Tensor

      the tensor to be added

    Returns Tensor

  • Adds two rows of a matrix.

    Returns

    the resulting 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

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

    Returns

    the (possibly nested) array of values

    Returns Promise<any>

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

    Returns

    the (possibly nested) array of values

    Returns any

  • 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

    the arc sine of each tensor element

    Returns Tensor

  • 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

    the arc tangent of each tensor element

    Returns Tensor

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

    Returns

    the arc tangent of each tensor element

    Parameters

    • b: number | Tensor

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

    Returns Tensor

  • 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

    each tensor element rounded up

    Returns Tensor

  • Equality check against a complex tensor.

    Returns

    whether the objects are equals

    Parameters

    • b: Tensor

      the tensor to be compared

    Returns boolean

  • Concatenates two or more tensors.

    Returns

    the concatenated tensor

    Parameters

    • b: Tensor | Tensor[]

      the tensor(s) to be concatenated

    • Optional axis: number

      (optional) the axis to concatenate along

    Returns Tensor

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

    Returns

    each tensor element constrained to the given range

    Parameters

    • low: number

      the minimum value

    • high: number

      the maximum value

    Returns Tensor

  • Gets a copy of the tensor, returns a Tensor object.

    Returns

    a copy of the tensor

    Returns Tensor

  • 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

    the cosine of each tensor element

    Returns Tensor

  • Disposes the tensor from memory.

    Returns void

  • Divides two tensors element-wise.

    Returns

    the quotient of the tensors

    Parameters

    • b: number | Vector | Tensor

      the tensor to be divided by

    Returns Tensor

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

    Returns

    the dot product of the tensors

    Parameters

    • b: Vector | Tensor

      the matrix or vector to be dotted

    Returns Tensor

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

    Returns

    whether the objects are equals

    Parameters

    • b: number | Vector | Tensor

      the object to be compared

    Returns boolean

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

    Returns

    e^n for each tensor element

    Returns Tensor

  • Flattens this tensor to one dimension.

    Returns

    the flattened tensor

    Returns 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

    each tensor element rounded down

    Returns Tensor

  • Gets the imaginary component of a complex tensor.

    Returns

    the imaginary component(s) of the tensor

    Returns 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

    the natural logarithm of each tensor element

    Returns Tensor

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

    Returns

    the maximum number in the tensor

    Returns number

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

    Returns

    the minimum number in the tensor

    Returns number

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

    Returns

    the remainder(s)

    Parameters

    • b: number | Tensor

      the tensor to be divided by

    Returns Tensor

  • Multiplies a row of a matrix by a constant.

    Returns

    the resulting matrix

    Parameters

    • r1: number

      the index of the row being multiplied

    • c: number

      the constant multiplier

    Returns Tensor

  • Multiplies two tensors element-wise.

    Returns

    the product of the tensors

    Parameters

    • b: number | Vector | Tensor

      the tensor to be multiplied

    Returns Tensor

  • Pads a tensor with a given value and paddings.

    Returns

    the padded tensor

    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

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

    Returns

    the exponentiated tensor

    Parameters

    • b: number | Tensor

      the power by which to raise each tensor element

    Returns Tensor

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

    Returns void

  • Gets the real component of a complex tensor.

    Returns

    the real component(s) of the tensor

    Returns Tensor

  • Reshapes a tensor to a given shape.

    Returns

    the reshaped tensor

    Parameters

    • shape: number[]

      an array of integers defining the output tensor shape

    Returns Tensor

  • Reverses the tensor along a specificed axis.

    Returns

    the reversed tensor

    Parameters

    • Optional axis: number | number[]

      (optional) the axis to reverse along

    Returns Tensor

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

    Returns

    each tensor element rounded

    Returns Tensor

  • 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

    the sine of each tensor element

    Returns Tensor

  • Extracts a slice from a tensor.

    Returns

    the tensor slice

    Parameters

    • begin: number | number[]

      the coordinates to start the slice from

    • Optional size: number | number[]

      (optional) the size of the slice

    Returns Tensor

  • Splits a tensor into sub tensors.

    Returns

    the split tensor

    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[]

  • 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

    the square of each tensor element

    Returns Tensor

  • 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

    the square root of each tensor element

    Returns Tensor

  • Subtracts two tensors element-wise.

    Returns

    the difference of the tensors

    Parameters

    • b: number | Vector | Tensor

      the tensor to be subtracted

    Returns Tensor

  • Subtracts two rows of a matrix.

    Returns

    the resulting 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

  • Calculates the sum of tensor elements along an axis.

    Returns

    the sum

    Parameters

    • Optional axis: number | number[]

      (optional) the axis to sum along

    Returns Tensor

  • Swaps two rows of a matrix.

    Returns

    the resulting matrix

    Parameters

    • r1: number

      the index of the first row being swapped

    • r2: number

      the index of the second row being swapped

    Returns Tensor

  • 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

    the tangent of each tensor element

    Returns Tensor

  • Returns a representation of this tensor as a scalar.

    Returns

    the scalar

    Returns number

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

    Returns

    a human-readable description of the tensor

    Returns string

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

    Returns

    the p5.Vector

    Returns Vector

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

    Returns

    the array of tensors

    Parameters

    • Optional axis: number

      (optional) the axis to unstack along

    Returns Tensor[]

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

    Returns

    the complex tensor

    Parameters

    • real: number | number[] | Tensor

      the real component(s)

    • imag: number | number[] | Tensor

      the imaginary component(s)

    Returns Tensor

  • Creates an identity matrix with the given dimensions.

    Returns

    the identity matrix

    Parameters

    • numRows: number

      the number of rows

    • Optional numCols: number

      (optional) the number of columns

    Returns Tensor

  • Creates a tensor filled with a given value.

    Returns

    the filled tensor

    Parameters

    • shape: number[]

      the shape of the tensor

    • value: number

      the value to fill the tensor with

    Returns Tensor

  • Creates a tensor filled with evenly spaced values.

    Returns

    the filled tensor

    Parameters

    • min: number

      the lower bound (inclusive)

    • max: number

      the upper bound (inclusive)

    • num: number

      the number of values to generate

    Returns Tensor

  • Creates a tensor filled with ones.

    Returns

    the filled tensor

    Parameters

    • shape: number[]

      the shape of the tensor

    Returns Tensor

  • Creates a tensor filled with uniformly distributed random numbers.

    Returns

    the filled tensor

    Parameters

    • shape: number[]

      the shape of the tensor

    Returns Tensor

  • Creates a tensor filled with normally distributed random numbers.

    Returns

    the filled tensor

    Parameters

    • shape: number[]

      the shape of tensor

    • Optional mean: number

      (optional) the mean

    • Optional sd: number

      (optional) the standard deviation

    Returns Tensor

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

    Returns

    the filled tensor

    Parameters

    • min: number

      the lower bound (inclusive)

    • max: number

      the upper bound (exclusive)

    • Optional step: number

      (optional) the integer spacing between values

    Returns Tensor

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

    Returns

    the stacked tensor

    Parameters

    • tensors: Tensor[]

      the tensors to be stacked

    • Optional axis: number

      (optional) the axis to stack along

    Returns Tensor

  • Creates a tensor filled with zeros.

    Returns

    the filled tensor

    Parameters

    • shape: number[]

      the shape of the tensor

    Returns Tensor

Generated using TypeDoc