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

matrix-multiplication

v0.5.2

Published

implements row by column multiplication

Downloads

63

Readme

matrix-multiplication

implements row by column multiplication

Installation | API | Examples | License

NPM version Build Status Dependency Status JavaScript Style Guide

Installation

With npm do

npm install matrix-multiplication

API

Optional custom operators are supported.

var operator = matrixMultiplication([customOperator])

  • @param {Object} [customOperator]
  • @param {Function} [customOperator.addition] defaults to common +
  • @param {Function} [customOperator.multiplication] defaults to common *
  • @returns {Function} operator

var mul = operator(middle, leftMatrix, rightMatrix)

The only requirement needed to multiply row by column an a x b matrix by an c x d matrix is that b = c, i.e. the middle indexes are equal. Actually two compatible matrices are n x m and m x l, let's call m the middle.

  • @param {Number} middle
  • @returns {Function} mul

var matrix = mul(leftMatrix, rightMatrix)

Finally we have the matrix multiplication function. Remember that is not a commutative operator.

  • @param {Array} leftMatrix
  • @param {Array} rightMatrix
  • @returns {Array} matrix

matrixMultiplication.error

An object exposing the following error messages:

Examples

All code in the examples below is intended to be contained into a single file.

Square matrices 2x2

var matrixMultiplication = require('matrix-multiplication')

var mul = matrixMultiplication()(2)

var leftMatrix = [2, 3,
                  1, 1]

var rightMatrix = [0, 1,
                  -1, 0]

mul(leftMatrix, rightMatrix) // [-3, 2,
                             //  -1, 1]

Actually, any pair of matrices with middle = 2 can be multiplied with the same mul function, try with a 3x2 by 2x4

var matrix3x2 = [2, 3,
                 1, 1,
                 1, 1]

var matrix2x4 = [0, 1, 1, 1,
                -1, 0, 2, 3]

mul(matrix3x2, matrix2x4) // [-3, 2, 8, 11,
                          //  -1, 1, 3, 4,
                          //  -1, 1, 3, 4])

Matrices are checked for compatibility, for instances the following snippets will throw.

leftMatrixNotCompatible

Since mul was defined as a multiplication with middle index 2, left matrix is not compatible cause it has 3 columns.

mul([1, 2, 3,
     4, 5, 6,
     7, 8, 9], [1, 2,
                3, 4])

rightMatrixNotCompatible

Since mul was defined as a multiplication with middle index 2, right matrix is not compatible cause it has 3 rows.

mul([1, 2,
     3, 4], [1, 2, 3,
             4, 5, 6,
             7, 8, 9])

You can also multiply over a custom field, just provide a customOperator object with an addition and a multiplication function. The following example shows multiplication of two square matrices of booleans.

Matrices of strings are left as an excercise to the reader.

function booleanAdd (a, b) { return a || b }
function booleanMul (a, b) { return a && b }

var customOperators = {
  addition: booleanAdd,
  multiplication: booleanMul
}

var mulB = matrixMultiplication(customOperators)(3)

var y = true
var n = false

var matrix = [n, y, n,
              y, n, y,
              n, y, n]

var identity = [y, n, n
                n, y, n,
                n, n, y]

mulB(matrix, identity) // [n, y, n,
                       //  y, n, y,
                       //  n, y, n]

License

MIT