Numerous Packages Multiply
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

## @argonic/tensor

1.1.0 • Public • Published

# Tensor

### Introduction

Tensors are multidimensional vector spaces that maps in a (multi-)linear manner to vectors, scalars, and other tensors to a resulting tensor.

A tensor of type (p, q) is an assignment of a multidimensional array $$T^{i_1 \dots i_p}_{j_1 \dots j_q} [f]$$ to each basis $$f = (e_1, \dots ,e_n)$$ of an n-dimensional vector space such that, if we apply the change of basis. $$f \mapsto f . R = (e_i R^i_1, \dots , e_i R^i_n)$$

Where $$i$$ is the $$i$$ th dimension, ranging from one to $$n$$ rank. the number $$j$$ is being the depth of the $$i$$ th dimension, ranging from one to $$q$$ dimension. A tensor is composed of subsequent tensors with the $$n$$ th tensors being composed of scalars. Scalars or "scaled" numbers, called scalars. Scalars are tensor's primitive elements and often taken to be real numbers, but there are also vector spaces with scalar multiplication by complex numbers, rational numbers, or generally any field.

#### Features

• Browser and server support out of the box, thanks to Webpack
• Full integration within the Typescript ecosystem
• Fully tested with 100% code coverage
• Lightweight with production zero-dependencies
• Optimized for performance
• Learning purposes
• Tensor class:
• The creation of tensors of $$p$$ rank/dimensions
• TypedArray as the flat tensor vector
• Calculate tensor's length $$L = q_1 \times q_2 \dots q_p$$
• Calculate strides $$S^i = 1 \times q_{p-i} \times q_{p-i+1} \dots q_i$$
• Calculate indices out of coordinates $$I = (C_1 \times S_1) + (C_2 \times S_2) \dots C_i \times S_i$$
• Calculate coordinates out of indices $$C_i(I) = r_i(I)/S_i$$ such as $$r_i(I) = I - (C_{i-1} \times S_{i-1})$$
• Calculate transpose
• Just in time tensor filling using scalars or closure evaluation
• Get indice or coordinates values
• Update indice or coordinates values
• Transfer tensor's content to another tensor with/out instantiating another Tensor object

1. Installation
2. Getting started
3. Documentation
4. The API
5. Examples

### Installation

The easiest way to use Tensor, is by adding it to your local package by runing this command:

\$ npm i @argonic/tensor --save


Note: To run the preceding command, Node.js and npm must be installed.

### Getting started

To use Tensor as a node module, add this code to your .js file:

or using ECMA6 syntax:

To use Tensor in the browser, add the script to your HTML file:

And you can access the Tensor class using .default property:

### Documentation

#### Constructor

###### new Tensor(TypedArrayConstructor, shape, ...shape)

The main function in Tensor class, It creates an n-dimensional tensor and maps it to 1D array view.

• TypedArrayConstructor: the constructor you wich to use in the storage buffer. e.g.: new Tensor(Uint8Array, 1); which creates a 1D tensor over a Uint8Array typed array.
• shape: a natural number where shape > 0, it is the depth of each dimension.

Note: tensors are per default filled with zeros.

return: a Tensor object.

### Properties

• tensor.min: number: returns the tensor's min value.
• tensor.max: number: returns the tensor's max value.
• tensor.length: number: returns the tensor's length.
• tensor.dimensions: number: returns the tensor's dimensions/rank.
• tensor.type: TypedArrayConstructor: returns the tensor's type as TypedArrayConstructor.
• tensor.strides: number[]: returns the tensor's strides per dimension.
• tensor.T: Tensor: returns the tensor's tensor as a new Tensor object.
• tensor.shape: number[]: returns the tensor's shape array.
• tensor.flat: TypedArray: returns the tensor's flat TypedArray.
• tensor.array: any[]: returns the tensor's N-dimensional array.

### Methods

#### Tensor.fill

###### Tensor.fill(value: (() => number) | number): Tensor;

Fills the array with a scalar value or a closure evaluation that returns a scalar.

• value: either a scalar value generally a number, or a closure that evaluates to a scalar.

return: Tensor object.

#### Tensor.flat

###### Tensor.flat: TypedArray = flat;

Replaces the flat view using another TypedArray from the same type.

• flat: TypedArray from the same type, e.g.: tensor.flat = new Uint8Array(1) to replace a 1D Uint8Array tensor's flat array.

#### Tensor.transpose

###### Tensor.transpose(...axes: number[]): Tensor;

Permute the dimensions of an array. Transposing a 1-D array returns an unchanged view of the original array and Transposing a transpose return the same tensor.

• axes: if axes.length === 0 it reverse the dimensions indices, otherwise permute the axes according to the values given.

return: a new Tensor with permuted dimensions.

#### Tensor.coordinates

###### Tensor.coordinates(index: number): number[];

Takes an index as argument and returns the corresponding coordinates.

• index: an integer between 0 and (tensor.length-1).

return: coordinates array.

#### Tensor.index

###### Tensor.index(...coordinates: number[]): number;

Takes coordinates as argument and returns the corresponding index.

• coordinates: an integer between 0 and (tensor.shape[i]-1).

return: corresponding index.

#### Tensor.get

###### Tensor.get(...coordinates: number[]): number;

Takes coordinates as argument, or an index and returns the corresponding index value.

• coordinates: an integer between 0 and (tensor.shape[i]-1).
• or one index: an integer between 0 and (tensor.length-1).

return: corresponding index value.

#### Tensor.set

###### Tensor.set(value, ...coordinates: number[]): Tensor;

Takes coordinates as argument, or an index and a value to update the corresponding index value.

• value: the new value.
• coordinates: an integer between 0 and (tensor.shape[i]-1).
• or one index: an integer between 0 and (tensor.length-1).

return: Tensor object.

#### Tensor.copy

###### Tensor.copy(tensor, instantiate?: boolean): Tensor;

Copys data and metadata from the target tensor with or without instantiating a new tensor.

• tensor: the target tensor to copy data from.
• instantiate: boolean with default to false.

return: the same Tensor object if instantiate is false, or a new Tensor object if instantiate is set to true.

### Licence

This code is released under the MIT license.

## Keywords

### install

npm i @argonic/tensor

0

1.1.0

MIT

github.com

github