npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-mat

v1.6.0

Published

JavaScript library for Matrix representation

Downloads

29

Readme

js-mat

JavaScript library for representation and mathematical operations using matrix

Usage

Import the package as:

import {mat} from '../mat/Mat'

Create a matrix of random values:

var M: mat.Matrix = mat.rand(3,3); // create a 3x3 matrix

Create a null matrix:

var M: mat.Matrix = mat.zeros(2,5); // create a 2x5 null matrix

Create a matrix of ones:

var M: mat.Matrix = mat.ones(2,2) // create a 2x2 matrix of ones

Create an identity matrix:

var I: mat.Matrix = mat.eye(4); // identity matrix of size 4x4

Create a matrix from a 2D array:

// We show you two ways of doing it

// Using constructor
var M: mat.matrix = new mat.Matrix([
    [1,2,3],
    [4,5,6],
    [7,8,9]
]);

// Using the matrix function
var M: mat.Matrix = mat.matrix([
    [1,2,3],
    [4,5,6],
    [7,8,9]
]);

Create a matrix from another matrix

var M1: mat.Matrix = mat.rand(3,6);
var M2: mat.Matrix = new mat.Matrix(M1); // equal to M1

Operations

Addition:

var M1 = new Matrix([
    [12,7,9],
    [5,-2,3]
]);
var M2 = new Matrix([
    [-3.6, 0, 5.4],
    [-12,-2,7]
]);

var result = M1.add(M2);
// [8.4, 7, 14.4]
// [-7, -4, -10]

Substraction:

var M1 = new Matrix([
    [12,7,9],
    [5,-2,3]
]);
var M2 = new Matrix([
    [-3.6, 0, 5.4],
    [-12,-2,7]
]);

var result = M1.subtract(M2); // M1.diff(M2) also works
// [15.6, 7.0, 3.6]
// [17.0, 0, -4.0]

Multiplication:

// Multiply two matrices
var M1 = new Matrix([
    [1, 2, 9],
    [-3, 7, 1]
]);

var M2 = new Matrix([
    [-5, 1],
    [3, 12],
    [1, 1]
]);

var result = M1.multiply(M2); // M1.dot(M2) also works
// [10, 34]
// [37, 82]
// Multiply a matrix by a constant
var M1 = new Matrix([
    [1, 2, 9],
    [-3, 7, 1]
]);

var result = M1.multiply(5);
// [5, 10, 45]
// [-15, 35, 5]

Determinant:

var M = new Matrix([
    [5, -2, 2, 7],
    [1, 0, 0, 3],
    [-3, 1, 5, 0],
    [3, -1, -9, 4]
]);

M.det(); // returns 88

Inverse:

var M = new Matrix([
    [5, -2, 2, 7],
    [1, 0, 0, 3],
    [-3, 1, 5, 0],
    [3, -1, -9, 4]
]);

M.inv();

// [-0.1364,    0.8636,   -0.6818,   -0.4091]
// [-0.6364,    2.3636,   -0.9318,   -0.6591]
// [0.0455,    0.0455,   -0.0227,   -0.1136]
// [0.0455,    0.0455,    0.2273,    0.1364]

Transpose:

var M = new Matrix([
    [5, -2, 2, 7],
    [1, 0, 0, 3],
    [-3, 1, 5, 0]
]);

M.T; // or also M.transpose()
// [5, 1, -3]
// [-2, 0, 1]
// [2, 0, 5]
// [7, 3, 0]

Cofactor Matrix:

var M = new Matrix([
    [5, -2, 2, 7],
    [1, 0, 0, 3],
    [-3, 1, 5, 0],
    [3, -1, -9, 4]
]);

M.cof();

// [-12, -56, 4, 4]
// [76, 208, 4, 4]
// [-60, -82, -2, 20]
// [-36, -58, -10, 12]

Adjoint:

var M = new Matrix([
    [5, -2, 2, 7],
    [1, 0, 0, 3],
    [-3, 1, 5, 0],
    [3, -1, -9, 4]
]);

M.adj();

//  [ -12, 76, -60, -36 ]
//  [ -56, 208, -82, -58 ]
//  [ 4, 4, -2, -10 ]
//  [ 4, 4, 20, 12 ]

Minor:

// Calculate the determinant when removing the given row and column indexes
var M = new Matrix([
    [5, -2, 2, 7],
    [1, 0, 0, 3],
    [-3, 1, 5, 0],
    [3, -1, -9, 4]
]);

M.minor(0,1); // returns 56

And more matrix operations including:

  • Horizontal concatenation: M1.horzcat(M2)
  • Vetical concatenation: M1.vertcat(M2)
  • Add row: M.addRow(row)
  • Add column: M.addColumn(column)
  • Remove row: M.deleteRow(index)
  • Delete column: M.deleteColumn(index)
  • Compare: M1.equals(M2)
  • map/apply: M.map(x => x**2), M.apply(x => x**2)
  • arange: Matrix.arange(2, 10, 0.5)
  • linspace: Matrix.linspace(0, 10, 100)
  • reshape: M.reshape([2,3])
  • flatten/ravel: M.flatten(), M.ravel()
  • diag: M.diag()

Examples