Starter code using the HTML5 Canvas API
Code includes file matrix.js
used to create matrices
Matrix(m, n)
Creates a new m×n matrix (m
rows and n
columns) with 1's on the diagonal and 0's everywhere else
Example:
let t = new Matrix(3, 3);
Result:
rows
-- read only
number of rows in matrix
columns
-- read only
number of columns in matrix
values
2D array of matrix values (can also set values using 1D array of length m×n)
Example:
t.values = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]];
t.values = [1, 2, 3, 4, 5, 6, 7, 8, 9];
Result:
multiply(matrices)
Mutliplies matrices (where matrices
is an array of two or more Matrix
objects). Returns null
if matrices cannot be multiplied.
Example:
let t1 = new Matrix(3, 3);
t1.values = [[1, 0, -5],
[0, 1, 10],
[0, 0, 1]];
let t2 = new Matrix(3, 3);
t2.values = [[2, 0, 0],
[0, 4, 0],
[0, 0, 1]];
let t = Matrix.multiply([t2, t1]);
Result:
determinant()
Returns the determinant of the matrix (result is null
if the determinant cannot be calculated)
Example:
let t = new Matrix(3, 3);
t.values = [[2, 0, -5],
[0, 4, 10],
[0, 0, 1]];
let det = t.determinant()
Result:
transpose()
Returns the transpose of the matrix
Example:
let t = new Matrix(3, 3);
t.values = [[2, 0, -5],
[0, 4, 10],
[0, 0, 1]];
let t_tran = t.transpose()
Result:
inverse()
Returns the inverse of the matrix (result is null
if the inverse cannot be calculated)
Example:
let t = new Matrix(3, 3);
t.values = [[2, 0, -5],
[0, 4, 10],
[0, 0, 1]];
let t_inv = t.inverse()
Result: