Overview
The TensorFlow library provides methods for some of the standard matrix manipulation methods of linear algebra.
Creating Matrices
The following demonstrates how to create a matrix. The matrix created represents the matrix
{% \displaystyle
\begin{bmatrix}
1 & 2 \\
3 & 4 \\
\end{bmatrix}
%}
const matrix = tf.tensor([[1,2],[3,4]]);
Matrix Addition
Once a matrix has been created, it can be added to another matrix of the same dimensions.
let sum = matrix1.add(matrix2)
Try it!
Matrix Multiplication
A matrix can be mulitiplied by another matrix if the number of rows of the left matrix equals the number of columns of the right matrix.
let mult = matrix1.matMul(matrix2)
Try it!
Matrices can also be multiplied by a scalar using the mul function.
const a = tf.tensor([[1,2],[3,4]]);
let c = tf.mul(a,5);
Try it!
Converting Matrix Back to Array
Davinci hosts a utility to convert matrix objects used by tensor-flow back to Javascript arrays.
let ut = await import('/lib/tensor-flow/v2.0.0/util.mjs');
let array = await ut.toArray(matrix);