Skip to content

ladunjexa/matrixlab.ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

matrixlab.ts

A TypeScript matrix math library

ladunjexa GitHub license Deploy Status npm version matrixlab Maintained PRs Welcome

matrixlab.ts is a matrix math library written in TypeScript with no dependencies.

How to install it

Using npm

npm install --save matrixlab.ts

Or add the unpkg cdn link to your html

<script src="https://unpkg.com/matrixlab.ts">

Usage

Import the module, from TypeScript or ES6 JavaScript

import { Matrix } from "matrixlab.ts";

The library is built as an ECMAScript module (.mjs file), but it also exports a UMD version if needed.

Then use the methods as you want

const m1 = new Matrix(2, 2, [
  [1, 2],
  [3, 4],
]);
m1.multiply(2);
// ==>
// Matrix {
//   rows: 2,
//   cols: 2,
//   values: [ [ 2, 4 ], [ 6, 8 ] ]
// }
m1.set(0, 0, 5);
m1.at(0, 0); // ==> 5

Documentation

Constructor

constructor description
Matrix() Creates a 1x1 matrix with zero value
Matrix(n) Creates an nxn matrix with zeroes
Matrix(data) Creates a matrix from the specified 2D array
Matrix(rows, columns) Creates an mxn matrix with zeroes
Matrix(rows, columns, data) Creates a matrix from the specified 2D array (with the specified dimensions)
Matrix(n, data) Creates an nxn matrix from the specified 2D array

Accessors and Mutators

member description
rows Retrieves the number of rows in the matrix
columns Retrieves the number of columns in the matrix
data Gets the data of the matrix

Public Methods

method description
shape Gets the shape of the matrix
size Gets the size of the matrix
set(row, column, value) Sets the value at the specified row and column indices
at(row, column) Retrieves the value at the specified row and column indices
reset() Sets all matrix values to 0
clone() Returns a new instance of the matrix
setData(array) Copies data from the specified 2D array to the matrix
add(matrix) Adds the specified matrix to the current matrix
subtract(matrix) Subtracts the specified matrix from the current matrix
multiply(Matrix / Scalar) Multiplies the current matrix by the specified matrix or scalar
modulo(modulus) Modulos the current matrix by the specified modulus
isSquare() Checks if the matrix is square
isSymmetric() Checks if the matrix is symmetric
isIdentity() Checks if the matrix is identity
setShape(array) Sets the shape of the matrix
setAsIdentity() Sets the matrix as an identity matrix
transpose() Transposes the matrix
determinant() Calculates the determinant of the matrix
getCofactor(row, column) Calculates the cofactor of the matrix for the specified row and column
getMinor(row, column) Calculates the minor of the matrix for the specified row and column
trace() Calculates the trace of the matrix
inverse() Calculates the inverse of the matrix
rowSpace() Calculates the row space of the matrix
colSpace() Calculates the column space of the matrix
isSameOrder(matrix) Checks if the matrix has the same order as the specified matrix
equals() Checks if the matrix is equal to the specified matrix

Private Methods

method description
_isOutOfMatrix(row, col) Checks if the specified row and column indices are out of the matrix's bounds
_initializeMatrix() Initializes the matrix with the specified dimensions and data

For more information, please refer to the Matrix Class documentation

Contributing

Contributions are always welcome!

See contributing.md for ways to get started.

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

The MatrixLab is open source and distributed under the MIT License.

Contact

If you want to contact me, you can reach me at @ladunjexa.