Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

matrixmath

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

matrixmath - npm Package Compare versions

Comparing version 2.2.0 to 2.2.1

44

Matrix.js

@@ -621,5 +621,23 @@ /**

// Simple solution for 2x2 matrices
if (numRows === 2) {
var determinant = this.getDeterminant();
if (determinant === 0) return this;
var invertedDeterminant = 1 / determinant;
var m0 = invertedDeterminant * this[3];
var m1 = invertedDeterminant * -this[1];
var m2 = invertedDeterminant * -this[2];
var m3 = invertedDeterminant * this[0];
this[0] = m0;
this[1] = m1;
this[2] = m2;
this[3] = m3;
return this;
}
// By using a cache, only the first call to invert will cause a memory increase.
var cache = this._cache || (this._cache = {});
var matrixOfMinors = cache.matrixOfMinors || (cache.matrixOfMinors = new Matrix(numRows, numCols, false));
var matrixOfCoFactors = cache.matrixOfCoFactors || (cache.matrixOfCoFactors = new Matrix(numRows, numCols, false));
var matrix = cache.tempMatrix || (cache.tempMatrix = new Matrix(this.rows, this.cols, false));

@@ -648,6 +666,14 @@

// Set the determinant in the correct position in the matrix of minors.
// Every other position is multiplied by -1 to get a matrix of cofactors.
matrixOfMinors[row * matrixOfMinors.cols + col] = (i % 2 ? -1 : 1) * matrix.getDeterminant();
// Some of the determinants need to change sign to become the cofactor.
// This is applied as a checkerboard to the matrix.
var coFactor = matrix.getDeterminant();
var rowAlternate = row % 2 === 1;
var colAlternate = col % 2 === 1;
if ((rowAlternate && !colAlternate) || (colAlternate && !rowAlternate)) {
coFactor *= -1;
}
// Set the cofactor in the correct position in the matrix of cofactors.
matrixOfCoFactors[row * matrixOfCoFactors.cols + col] = coFactor;
i++;

@@ -661,3 +687,3 @@ }

for (var n = 0; n < numCols; n++) {
originalDeterminant += this[n] * matrixOfMinors[n];
originalDeterminant += this[n] * matrixOfCoFactors[n];
}

@@ -668,8 +694,8 @@

// Transpose the cofactor matrix of minors to get the adjugate matrix
matrixOfMinors.transpose();
// Transpose the cofactor of cofactors to get the adjugate matrix
matrixOfCoFactors.transpose();
// Multiply the matrix of minors with the inverse of the determinant,
// Multiply the matrix of cofactors with the inverse of the determinant,
// to get the final inverse of the original matrix.
var product = matrixOfMinors.multiply(1 / originalDeterminant);
var product = matrixOfCoFactors.multiply(1 / originalDeterminant);

@@ -676,0 +702,0 @@ // Copy the data from the inverted temp matrix to this matrix

{
"name": "matrixmath",
"version": "2.2.0",
"version": "2.2.1",
"author": "Johannes Koggdal <johannes@koggdal.com>",

@@ -5,0 +5,0 @@ "description": "Library for working with mathematical matrices.",

@@ -847,3 +847,13 @@ var expect = require('expect.js');

it('should invert a square matrix', function() {
it('should invert a square 2x2 matrix', function() {
var matrix1 = new Matrix(2, 2).setData([0, -600, 240, 240]);
matrix1.invert();
expect(matrix1[0]).to.equal(1/600);
expect(matrix1[1]).to.equal(1/240);
expect(matrix1[2]).to.equal(-1/600);
expect(matrix1[3]).to.equal(0);
});
it('should invert a square 3x3 matrix', function() {
var matrix1 = new Matrix(3, 3).setData([3, 0, 2, 2, 0, -2, 0, 1, 1]);

@@ -863,2 +873,24 @@ matrix1.invert();

it('should invert a square 4x4 matrix', function() {
var matrix1 = new Matrix(4, 4).setData([4, 0, 0, 0, 0, 0, 2, 0, 0, 1, 2, 0, 1, 0, 0, 1]);
matrix1.invert();
expect(matrix1[0]).to.equal(0.25);
expect(matrix1[1]).to.equal(0);
expect(matrix1[2]).to.equal(0);
expect(matrix1[3]).to.equal(0);
expect(matrix1[4]).to.equal(0);
expect(matrix1[5]).to.equal(-1);
expect(matrix1[6]).to.equal(1);
expect(matrix1[7]).to.equal(0);
expect(matrix1[8]).to.equal(0);
expect(matrix1[9]).to.equal(0.5);
expect(matrix1[10]).to.equal(0);
expect(matrix1[11]).to.equal(0);
expect(matrix1[12]).to.equal(-0.25);
expect(matrix1[13]).to.equal(0);
expect(matrix1[14]).to.equal(0);
expect(matrix1[15]).to.equal(1);
});
it('should not invert a non-square matrix', function() {

@@ -865,0 +897,0 @@ var matrix1 = new Matrix(3, 2).setData([3, 0, 2, 0, 0, 1]);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc