Artificial Intelligence
Tensors
A Tensor is a Ndimensional Matrix:
 A Scalar is a 0dimensional tensor
 A Vector is a 1dimensional tensor
 A Matrix is a 2dimensional tensor
A Tensor is a generalization of Vectors and Matrices to higher dimensions.
Scalar  Vector(s)  
1 


Matrix  Tensor  


Tensor Ranks
The number of simultaneous directions a tensor can have in a Ndimensional space, is called the Rank of the tensor.
The rank is denoted R.
A Scalar is a single number. It has a rank of 0.
 It has 0 Axes
 It has a Rank of 0
 It is a 0dimensional Tensor
A Vector is an array of numbers. It has a rank of 1.
 It has 1 Axis
 It has a Rank of 1
 It is a 1dimensional Tensor
A Matrix is a 2dimensional array. It has a rank of 2.
 It has 2 Axis
 It has a Rank of 2
 It is a 2dimensional Tensor
Real Tensors
Technically, all of the above are tensors, but when we speak of tensors, we generally speak of matrices with a dimension larger than 2 (R > 2).
Linear Algebra in JavaScript
In linear algebra, the most simple math object is the Scalar:
const scalar = 1;
Another simple math object is the Array:
const array = [ 1, 2, 3 ];
Matrices are 2dimensional Arrays:
const matrix = [ [1,2],[3,4],[5,6] ];
Vectors can be written as Matrices with only one column:
const vector = [ [1],[2],[3] ];
Vectors can also be written as Arrays:
const vector = [ 1, 2, 3 ];
Tensors are Ndimensional Arrays:
const tensor = [ [1,2,3],[4,5,6],[7,8,9] ];
JavaScript Tensor Operations
Programming tensor operations in JavaScript, can easily become a spaghetti of loops.
Using a JavScript library will save you a lot of headache.
One of the most common libraries to use for tensor operations is called tensorflow.js.
Tensor Addition
const tA = tf.tensor([[1, 2], [3, 4], [5, 6]]);
const tB = tf.tensor([[1,1], [2,2], [3,3]]);
// Tensor Addition
const tensorAdd = tensor.add(mA, mB);
// Result [ [2, 1], [5, 2], [8, 3] ]
Tensor Subtraction
const tA = tf.tensor([[1, 2], [3, 4], [5, 6]]);
const tB = tf.tensor([[1,1], [2,2], [3,3]]);
// Tensor Subtraction
const tensorAdd = tensor.sub(mA, mB);
// Result [ [0, 3], [1, 6], [2, 9] ]