The Matrix constructor creates a new matrix object that can be used to perform common matrix operations. It can be initialized with a 2D array of numbers or with individual values for a specific size.
To create a new matrix object, use the following syntax:
const { Matrix } = require('mathlib-n');
const matrix = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);
This creates a 3x3 matrix with the values:
| 1 2 3 | | 4 5 6 | | 7 8 9 |
Alternatively, you can create a new matrix with specific dimensions using the following syntax:
const matrix = new Matrix(3, 4);
This creates a new 3x4 matrix with all values initialized to zero.
| 0 0 0 | | 0 0 0 | | 0 0 0 |
The following functions are available for manipulating matrix objects:
det
Calculate the determinant of the matrix.
const determinant = matrix.det();
inv
Calculate the inverse of the matrix.
const inverse = matrix.inv();
add
Add two matrices together and return the result.
const result = matrix1.add(matrix2);
sub
Subtract one matrix from another and return the result.
const result = matrix1.sub(matrix2);
div
Divide the matrix by a scalar value.
const result = matrix.div(scalar);
mul
Multiply two matrices together and return the result.
const result = matrix1.mul(matrix2);
trnsp
Transpose the matrix (swap rows with columns).
const transposed = matrix.trnsp();
isSquare
Check if the matrix is square (equal number of rows and columns).
const isSquareMatrix = matrix.isSquare();
isSymmetric
Check if the matrix is symmetric.
const isSymmetricMatrix = matrix.isSymmetric();
isSkewSymmetric
Check if the matrix is skew-symmetric.
const isSkewSymmetricMatrix = matrix.isSkewSymmetric();
isIdentity
Check if the matrix is an identity matrix.
const isIdentityMatrix = matrix.isIdentity();
isUpperTriangular
Check if the matrix is upper triangular.
const isUpperTriangularMatrix = matrix.isUpperTriangular();
isLowerTriangular
Check if the matrix is lower triangular.
const isLowerTriangularMatrix = matrix.isLowerTriangular();
isDiagonal
Check if the matrix is diagonal.
const isDiagonalMatrix = matrix.isDiagonal();
isOrthogonal
Check if the matrix is orthogonal.
const isOrthogonalMatrix = matrix.isOrthogonal();
isUnitary
Check if the matrix is unitary.
const isUnitaryMatrix = matrix.isUnitary();
isZero
Check if the matrix is a zero matrix (all elements are zero).
const isZeroMatrix = matrix.isZero();
isScalar
Check if the matrix is a scalar matrix (diagonal with equal elements).
const isScalarMatrix = matrix.isScalar();
isRowEchelon
Check if the matrix is in row-echelon form.
const isRowEchelonMatrix = matrix.isRowEchelon();
rank
Calculate the rank of the matrix.
const rank = matrix.rank();
adjoint
Calculate the adjoint (adjugate) of the matrix.
const adjointMatrix = matrix.adjoint();
magicSquare
Generate a magic square or return false.
const magicSquare = Matrix.magicSquare(size);
trace
Calculate the trace (sum of diagonal elements) of the matrix.
const traceValue = matrix.trace();
// Create matrices
const matrix1 = new Matrix([[1, 2], [3, 4]]);
const matrix2 = new Matrix([[5, 6], [7, 8]]);
const scalar = 2;
// Perform matrix operations
const determinant = matrix1.det();
const inverse = matrix1.inv();
const sum = matrix1.add(matrix2);
const difference = matrix1.sub(matrix2);
const divided = matrix1.div(scalar);
const product = matrix1.mul(matrix2);
const transposed = matrix1.trnsp();
const isSquareMatrix = matrix1.isSquare();
const isSymmetricMatrix = matrix1.isSymmetric();
const isSkewSymmetricMatrix = matrix1.isSkewSymmetric();
const isIdentityMatrix = matrix1.isIdentity();
const isUpperTriangularMatrix = matrix1.isUpperTriangular();
const isLowerTriangularMatrix = matrix1.isLowerTriangular();
const isDiagonalMatrix = matrix1.isDiagonal();
const isOrthogonalMatrix = matrix1.isOrthogonal();
const isUnitaryMatrix = matrix1.isUnitary();
const isZeroMatrix = matrix1.isZero();
const isScalarMatrix = matrix1.isScalar();
const isRowEchelonMatrix = matrix1.isRowEchelon();
const rankValue = matrix1.rank();
const adjointMatrix = matrix1.adjoint();
const magicSquareMatrix = Matrix.magicSquare();
const traceValue = matrix1.trace();
// Print results
console.log('Matrix 1:', matrix1);
console.log('Matrix 2:', matrix2);
console.log('Scalar:', scalar);
console.log('Size:', size);
console.log('Determinant:', determinant);
console.log('Inverse:', inverse);
console.log('Sum:', sum);
console.log('Difference:', difference);
console.log('Divided:', divided);
console.log('Product:', product);
console.log('Transposed:', transposed);
console.log('Is Square:', isSquareMatrix);
console.log('Is Symmetric:', isSymmetricMatrix);
console.log('Is Skew Symmetric:', isSkewSymmetricMatrix);
console.log('Is Identity:', isIdentityMatrix);
console.log('Is Upper Triangular:', isUpperTriangularMatrix);
console.log('Is Lower Triangular:', isLowerTriangularMatrix);
console.log('Is Diagonal:', isDiagonalMatrix);
console.log('Is Orthogonal:', isOrthogonalMatrix);
console.log('Is Unitary:', isUnitaryMatrix);
console.log('Is Zero:', isZeroMatrix);
console.log('Is Scalar:', isScalarMatrix);
console.log('Is Row Echelon:', isRowEchelonMatrix);
console.log('Rank:', rankValue);
console.log('Adjoint:', adjointMatrix);
console.log('Magic Square:', magicSquareMatrix);
console.log('Trace:', traceValue);
Matrix 1: Matrix { matrix: [ [ 1, 2 ], [ 3, 4 ] ], row: 2, column: 2 } Matrix 2: Matrix { matrix: [ [ 5, 6 ], [ 7, 8 ] ], row: 2, column: 2 } Scalar: 2 Determinant: -2 Inverse: Matrix { matrix: [ [ -0.5, 1 ], [ 1.5, -2 ] ], row: 2, column: 2 } Sum: Matrix { matrix: [ [ 6, 8 ], [ 10, 12 ] ], row: 2, column: 2 } Difference: Matrix { matrix: [ [ -4, -4 ], [ -4, -4 ] ], row: 2, column: 2 } Divided: Matrix { matrix: [ [ 0.5, 1 ], [ 1.5, 2 ] ], row: 2, column: 2 } Product: Matrix { matrix: [ [ 19, 22 ], [ 43, 50 ] ], row: 2, column: 2 } Transposed: Matrix { matrix: [ [ 1, 3 ], [ 2, 4 ] ], row: 2, column: 2 } Is Square: true Is Symmetric: false Is Skew Symmetric: false Is Identity: false Is Upper Triangular: false Is Lower Triangular: false Is Diagonal: false Is Orthogonal: true Is Unitary: true Is Zero: false Is Scalar: false Is Row Echelon: true Rank: 2 Adjoint: Matrix { matrix: [ [ 1, 2 ], [ 3, 4 ] ], row: 2, column: 2 } Magic Square: false Trace: 5