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

matrixops

v1.2.5

Published

A package for doing matrix arithmetic with JavaScript arrays

Downloads

40

Readme

#MatrixOps

MatrixOps is an npm package that makes performing matrix operations on 2 dimensional JavaScript arrays simple. It also provides some convenience methods similar to what might be found in a statistical language like MATLAB or Octave.

Usage

To install matrixops, simply run:

  npm install matrixops

#add

Accepts 2-dimensional arrays, 1-dimensional arrays, and numbers, and adds them together, returning a new array.

  const MatrixOps = require('matrixops');

  MatrixOps.add([[1, 2], [3, 4]], 5);
  // => [[6, 7], [8, 9]]

  MatrixOps.add([[1, 2], [3, 4]], [[1, 2], [3, 4]]);
  // => [[2, 4], [6, 8]]

#subtract

Accepts 2-dimensional arrays, 1-dimensional arrays, and numbers, and subtracts the second argument from the first, returning a new array.

  MatrixOps.subtract([[1, 2], [3, 4]], [[1, 2], [3, 4]]);
  // => [[0, 0], [0, 0]]

  MatrixOps.subtract([3, 4], [1, 2]);
  // => [2, 2]

#multiply

Accepts 2-dimensional arrays, 1-dimensional arrays, and numbers, and multiplies them together, returning a new array.

  MatrixOps.multiply([[1, 2, 3], [4, 5, 6]], [[1,2],[3,4],[5,6]]);
  // => [[22, 28], [49, 64]]

  MatrixOps.multiply([[1, 2, 3], [4, 5, 6]], [[1],[2],[3]]);
  // => [[14], [32]]

  MatrixOps.multiply([[1,2],[3,4]], 2);
  // => [[2, 4], [6, 8]]

#transpose

Accepts a 2-dimensional or 1-dimensional array and returns its transpose.

  MatrixOps.transpose([1, 2, 3]);
  // => [[1], [2], [3]]

  MatrixOps.transpose([[[1, 2], [3, 4], [5, 6]]);
  // => [[1, 3, 5], [2, 4, 6]]

#elementTransform

Accepts a 1 or 2-dimensional array and a callback function, and applies the callback to every element of the array. The callback function accepts up to 3 arguments: the element, the row index, and the column index.

  MatrixOps.elementTransform([[1, 2], [3, 4]], el => el * 2);
  // => [[2, 4], [6, 8]]

  MatrixOps.elementTransform([[1, 2], [3, 4]], el => Math.pow(el, 2));
  // => [[1, 4], [9, 16]]

  MatrixOps.elementTransform([[1, 2], [3, 4]], (el, row, col) => {
    return el * 2 + col + row;
  });
  // => [[2, 5], [7, 10]]

#elByElCalc

Accepts 2 arrays with the same dimensions and a callback, and applies the callback to pairs of elements at the same indices in the two arrays. The callback function accepts up to 4 arguments: the matrix 1 element at the position, the matrix 2 element at the position, the row index, and the column index.

  MatrixOps.elByElCalc([[1, 2], [3, 4]], [[1, 2], [3, 4]], (el1, el2) => el1 * el2);
  // => [[1, 4], [9, 16]]

  MatrixOps.elByElCalc([1, 2, 3], [1, 2, 3], (el1, el2) => el1 * el2);
  // => [[1, 4, 9]

  MatrixOps.elByElCalc([[1, 2], [3, 4]], [[1, 2], [3, 4]], (el1, el2, row, col) => {
    return el1 * el2 + row + col;
  });
  // => [[1, 5], [10, 18]]

#zeroes

Accepts 1 or 2 numbers and returns an array of zeroes with the specified dimensions. The second dimension defaults to 1 if no second number is given.

  MatrixOps.zeroes(2, 3);
  // => [[0, 0, 0], [0, 0, 0]]

  MatrixOps.zeroes(2));
  // => [[0], [0]]

#ones

Accepts 1 or 2 numbers and returns an array of ones with the specified dimensions. The second dimension defaults to 1 if no second number is given.

  MatrixOps.ones(2, 3);
  // => [[1,1,1], [1,1,1]]

  MatrixOps.ones(2));
  // => [[1], [1]]

#identity

Accepts a number n and returns an n x n identity matrix

  MatrixOps.identity(3);
  // => [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

#equals

Accepts two matrices or vectors, and returns true if all elements at the same position are equal

  const a = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ]

  const b = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ]

  MatrixOps.equals(a, b)
  // => true

  const c = [1, 2, 3]
  const d = [1, 2, 4]

  MatrixOps.equals(c, d)
  // => false

rowMeans

Accepts a matrix and returns the mean of each row as an array.

  const a = [
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3]
  ]

  MatrixOps.rowMeans(a);
  // => [ 2, 2, 2 ]

colMeans

Accepts a matrix and returns the mean of each column as an array.

  const a = [
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3],
  ]

  MatrixOps.colMeans(a);
  // => [1, 2, 3]

rowStdDevs

Accepts a matrix and returns the standard deviation of each row as an array.

  const a = [
    [1, 5],
    [9, 13]
  ]

  MatrixOps.rowStdDevs(a);
  // => [2, 2]

colStdDevs

Accepts a matrix and returns the standard deviation of each column as an array.

const a = [
  [1, 9],
  [7, 15]
]

  MatrixOps.colStdDevs(a);
  // => [3, 3]