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.1.0 to 2.2.0

.jshintrc

281

Matrix.js

@@ -6,2 +6,4 @@ /**

var arrays = require('./arrays');
/**

@@ -139,12 +141,10 @@ * @classdesc A class for representing and working with a mathematical matrix.

* Set the data for this matrix to be only zeros.
*
* @return {Matrix} This Matrix instance.
*/
Matrix.prototype.setEmptyData = function() {
var newData = new Array(this.length);
for (var i = 0, l = this.length; i < l; i++) {
newData[i] = 0;
this[i] = 0;
}
this.setData(newData);
return this;

@@ -159,10 +159,6 @@ };

Matrix.prototype.setIdentityData = function() {
var newData = new Array(this.length);
for (var i = 0, l = this.length; i < l; i++) {
newData[i] = i % (this.cols + 1) ? 0 : 1;
this[i] = i % (this.cols + 1) ? 0 : 1;
}
this.setData(newData);
return this;

@@ -187,7 +183,9 @@ };

var isArray = Array.isArray(data);
var rows = opt_rows;
var cols = opt_cols;
if (!isArray) {
data = arguments;
opt_rows = undefined;
opt_cols = undefined;
rows = undefined;
cols = undefined;
}

@@ -199,5 +197,5 @@

if (data.length !== this.length) {
if (opt_rows === undefined || opt_cols === undefined) {
if (rows === undefined || cols === undefined) {
return this;
} else if (opt_rows * opt_cols !== data.length) {
} else if (rows * cols !== data.length) {
return this;

@@ -208,3 +206,3 @@ }

// Clean out previous data
for (i = 0, l = this.length; i < l; i++) {
for (i = data.length, l = this.length; i < l; i++) {
delete this[i];

@@ -220,4 +218,4 @@ }

this.length = data.length;
this.rows = opt_rows || this.rows;
this.cols = opt_cols || this.cols;
this.rows = rows || this.rows;
this.cols = cols || this.cols;

@@ -234,6 +232,3 @@ return this;

Matrix.prototype.getData = function() {
var data = this.toArray();
data.rows = this.rows;
data.cols = this.cols;
return data;
return getData(this, new Array(this.length));
};

@@ -247,9 +242,3 @@

Matrix.prototype.toArray = function() {
var data = new Array(this.length);
for (var i = 0, l = this.length; i < l; i++) {
data[i] = this[i];
}
return data;
return toArray(this, new Array(this.length));
};

@@ -303,2 +292,34 @@

/**
* Copy data from the input matrix to this matrix.
*
* @param {Matrix} matrix Input matrix to copy from.
*
* @return {Matrix} This Matrix instance.
*/
Matrix.prototype.copy = function(matrix) {
var i, l;
// If the input matrix is smaller, clear out the values not needed anymore
if (matrix.length < this.length) {
for (i = matrix.length, l = this.length; i < l; i++) {
delete this[i];
}
}
// Set new metadata if the matrices are of different size
if (matrix.length !== this.length) {
this.length = matrix.length;
this.rows = matrix.rows;
this.cols = matrix.cols;
}
// Copy the data from the input matrix to this matrix
for (i = 0, l = this.length; i < l; i++) {
this[i] = matrix[i];
}
return this;
};
/**
* Clone this matrix to a new instance.

@@ -309,6 +330,3 @@ *

Matrix.prototype.clone = function() {
var matrix = new Matrix(this.rows, this.cols);
matrix.setData(this.getData());
return matrix;
return new Matrix(this.rows, this.cols, false).copy(this);
};

@@ -393,7 +411,48 @@

var matrices = arguments;
var startIndex = 0;
var newRows = this.getData();
// If this matrix is an identity matrix, multiplying it with anything will
// just result in this matrix having the exact same data as the matrix to
// multiply by. We can avoid one step of multiplication if we make a shortcut
// and just copy the data from the next matrix.
if (this.isIdentity()) {
var next;
while ((next = matrices[startIndex]) !== undefined) {
// If a number was found, we must break out and start the multiplication
// with this number. Special case is the number 1 though, as that will
// result in the same as well.
if (typeof next === 'number') {
if (next === 1) {
startIndex++;
continue;
} else {
break;
}
}
// If a matrix was found, we can safely skip the matrix (either it's an
// identity matrix and we'll continue looking for a matrix that isn't an
// identity matrix, or it's not an identity matrix and we'll just copy
// its data and start multiplying by the next matrix in line).
startIndex++;
if (!next.isIdentity()) break;
}
// No matrix was found in line, meaning we are only dealing with identity
// matrices, so it's fine to bail out early, as it will just result in an
// identity matrix.
if (!next) return this;
// If we did find a matrix, we will copy the data from that matrix into this
// one and start multiplying by the next matrix in line.
if (typeof next !== 'number') {
this.copy(next);
}
}
var newRows = getData(this, arrays.getWithLength(this.length));
// Loop through all the matrices passed to the method
for (var i = 0, l = matrices.length; i < l; i++) {
for (var i = startIndex, l = matrices.length; i < l; i++) {
var matrix = matrices[i];

@@ -422,2 +481,5 @@

// Multiplying with an identity matrix will not make any changes
if (matrix.isIdentity()) continue;
// Get the number of rows and columns for the current matrix

@@ -434,3 +496,3 @@ var rowsInCurrent = matrix.rows;

// This will be used to store values in while reading from newRows.
var tempData = new Array(newRows.length);
var tempData = arrays.getWithLength(newRows.length);
tempData.rows = newRows.rows;

@@ -459,2 +521,3 @@ tempData.cols = matrix.cols;

}
arrays.giveBack(newRows);

@@ -469,2 +532,4 @@ // Save the temporary data array in newRows, so that the next matrix can be applied

arrays.giveBack(newRows);
return this;

@@ -541,3 +606,3 @@ };

var newData = new Array(this.length);
var newData = arrays.getWithLength(this.length);

@@ -552,2 +617,4 @@ for (var row = 0; row < numRows; row++) {

arrays.giveBack(newData);
return this;

@@ -572,15 +639,2 @@ };

var removeRow = function(values, row, colsPerRow) {
values.splice(row * colsPerRow, colsPerRow);
return values;
};
var removeColumn = function(values, col, colsPerRow) {
var newData = [];
for (var i = 0, l = values.length; i < l; i++) {
if (i % colsPerRow !== col) newData.push(values[i]);
}
return newData;
};
// By using a cache, only the first call to invert will cause a memory increase.

@@ -596,10 +650,18 @@ var cache = this._cache || (this._cache = {});

// We need to get a temporary copy of the matrix data in an array
var newData = arrays.getWithLength(this.length);
for (var d = this.length; d--;) {
newData[d] = this[d];
}
// We need to get the determinant of the matrix made by the area
// that is not in the current number's row or column. To do this,
// we remove the first row and the column where the number is.
var newData = this.getData();
newData = removeRow(newData, row, this.cols);
newData = removeColumn(newData, col, this.cols);
removeRow(newData, row, this.cols);
removeColumn(newData, col, this.cols);
matrix.setData(newData, this.rows - 1, this.cols - 1);
// We're now done with the temporary copy of the matrix data
arrays.giveBack(newData);
// Set the determinant in the correct position in the matrix of minors.

@@ -629,4 +691,8 @@ // Every other position is multiplied by -1 to get a matrix of cofactors.

var product = matrixOfMinors.multiply(1 / originalDeterminant);
this.setData(product.getData(), product.rows, product.cols);
// Copy the data from the inverted temp matrix to this matrix
for (var x = 0, y = product.length; x < y; x++) {
this[x] = product[x];
}
return this;

@@ -676,15 +742,2 @@ };

var removeRow = function(values, row, colsPerRow) {
values.splice(row * colsPerRow, colsPerRow);
return values;
};
var removeColumn = function(values, col, colsPerRow) {
var newData = [];
for (var i = 0, l = values.length; i < l; i++) {
if (i % colsPerRow !== col) newData.push(values[i]);
}
return newData;
};
var result = 0;

@@ -699,10 +752,18 @@

// We need to get a temporary copy of the matrix data in an array
var newData = arrays.getWithLength(this.length);
for (var d = this.length; d--;) {
newData[d] = this[d];
}
// We need to get the determinant of the matrix made by the area
// that is not in the current number's row or column. To do this,
// we remove the first row and the column where the number is.
var newData = this.getData();
newData = removeRow(newData, 0, this.cols);
newData = removeColumn(newData, col, this.cols);
removeRow(newData, 0, this.cols);
removeColumn(newData, col, this.cols);
matrix.setData(newData, this.rows - 1, this.cols - 1);
// We're now done with the temporary copy of the matrix data
arrays.giveBack(newData);
result += (col % 2 ? -1 : 1) * this[col] * matrix.getDeterminant();

@@ -739,2 +800,84 @@ }

/**
* Tests if the data of the matrix represents the identity matrix.
*
* @return {boolean} True if it is the identity matrix, false otherwise.
*/
Matrix.prototype.isIdentity = function() {
for (var i = 0, l = this.length; i < l; i++) {
if (this[i] !== (i % (this.cols + 1) ? 0 : 1)) {
return false;
}
}
return true;
};
/**
* Remove a row from the values array.
*
* @param {Array} values Array of values.
* @param {number} row Index of the row.
* @param {number} colsPerRow Number of columns per row.
*
* @private
*/
function removeRow(values, row, colsPerRow) {
values.splice(row * colsPerRow, colsPerRow);
}
/**
* Remove a column from the values array.
*
* @param {Array} values Array of values.
* @param {number} col Index of the column.
* @param {number} colsPerRow Number of columns per row.
*
* @private
*/
function removeColumn(values, col, colsPerRow) {
var n = 0;
for (var i = 0, l = values.length; i < l; i++) {
if (i % colsPerRow !== col) values[n++] = values[i];
}
values.length = n;
}
/**
* Convert a matrix to an array with the values.
*
* @param {Matrix} matrix The matrix instance.
* @param {Array} array The array to use.
*
* @return {Array} The array.
*
* @private
*/
function toArray(matrix, array) {
for (var i = 0, l = matrix.length; i < l; i++) {
array[i] = matrix[i];
}
return array;
}
/**
* Get the matrix data as an array with properties for rows and cols.
*
* @param {Matrix} matrix The matrix instance.
* @param {Array} array The array to use.
*
* @return {Array} The array.
*
* @private
*/
function getData(matrix, array) {
toArray(matrix, array);
array.rows = matrix.rows;
array.cols = matrix.cols;
return array;
}
module.exports = Matrix;
{
"name": "matrixmath",
"version": "2.1.0",
"version": "2.2.0",
"author": "Johannes Koggdal <johannes@koggdal.com>",

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

@@ -50,2 +50,3 @@ # Matrix Math

* toLogString ()
* copy (matrix)
* clone ()

@@ -61,2 +62,3 @@ * add (matrix[,…matrixN])

* equals (input)
* isIdentity ()

@@ -82,4 +84,8 @@

```
> console.log(matrix.toArray());
[1, 0, 0, 0, 1, 0, 0, 0, 1]
> console.log(matrix.toLogString());
[
1 0 0
0 1 0
0 0 1
]
```

@@ -93,4 +99,8 @@

```
> console.log(matrix.toArray());
[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
> console.log(matrix.toLogString());
[
undefined undefined undefined
undefined undefined undefined
undefined undefined undefined
]
```

@@ -191,7 +201,7 @@

```
> console.log(matrix.toArray());
> console.log(matrix.toLogString());
[
1, 0, 0
0, 1, 0
0, 0, 1
1 0 0
0 1 0
0 0 1
]

@@ -210,7 +220,7 @@ ```

```
> console.log(matrix.toArray());
> console.log(matrix.toLogString());
[
0, 0, 0
0, 0, 0
0, 0, 0
0 0 0
0 0 0
0 0 0
]

@@ -230,6 +240,6 @@ ```

```
> console.log(matrix.toArray());
> console.log(matrix.toLogString());
[
1, 2
3, 4
1 2
3 4
]

@@ -243,7 +253,7 @@ ```

```
> console.log(matrix.toArray());
> console.log(matrix.toLogString());
[
1, 2, 3
4, 5, 6,
7, 8, 9
1 2 3
4 5 6
7 8 9
]

@@ -257,7 +267,7 @@ ```

```
> console.log(matrix.toArray());
> console.log(matrix.toLogString());
[
1, 2, 3
4, 5, 6,
7, 8, 9
1 2 3
4 5 6
7 8 9
]

@@ -277,6 +287,3 @@ ```

> console.log(matrix.getData());
[
1, 0,
0, 1
]
[1, 0, 0, 1]
> console.log(matrix.getData().rows);

@@ -299,6 +306,3 @@ 2

> console.log(matrix.toArray());
[
1, 0,
0, 1
]
[1, 0, 0, 1]
```

@@ -352,2 +356,20 @@

#### matrix.copy(matrix1)
Copies the data from another matrix into the original matrix.
```
var matrix = new Matrix(2, 2);
var matrix1 = new Matrix(2, 2, false).setData(1, 2, 3, 4);
matrix.copy(matrix1);
```
```
> console.log(matrix.toLogString());
[
1 2
3 4
]
```
#### matrix.clone()

@@ -373,4 +395,6 @@

```
> console.log(matrix.toArray());
[3, 6, 9]
> console.log(matrix.toLogString());
[
3 6 9
]
```

@@ -389,4 +413,6 @@

```
> console.log(matrix.toArray());
[-2, -2, -3]
> console.log(matrix.toLogString());
[
-2 -2 -3
]
```

@@ -407,4 +433,7 @@

```
> console.log(matrix.toArray());
[14, 20, 30, 44]
> console.log(matrix.toLogString());
[
14 20
30 44
]
```

@@ -417,4 +446,7 @@

```
> console.log(matrix.toArray());
[3, 6, 9, 12]
> console.log(matrix.toLogString());
[
3 6
9 12
]
```

@@ -433,4 +465,7 @@

```
> console.log(matrix.toArray());
[0.5, 0, 0, -0.5]
> console.log(matrix.toLogString());
[
0.5 0
0 -0.5
]
```

@@ -447,4 +482,7 @@

```
> console.log(matrix.toArray());
[25, 22, 44, 25]
> console.log(matrix.toLogString());
[
25 22
44 25
]
```

@@ -461,4 +499,7 @@

```
> console.log(matrix.toArray());
[1, 4, 2, 1]
> console.log(matrix.toLogString());
[
1 4
2 1
]
```

@@ -479,7 +520,7 @@

```
> console.log(matrix.toArray());
> console.log(matrix.toLogString());
[
0.2, 0.2, 0,
-0.2, 0.3, 1,
0.2, -0.3, 0
0.2 0.2 0
-0.2 0.3 1
0.2 -0.3 0
]

@@ -514,3 +555,17 @@ ```

#### matrix.isIdentity()
Tests if the data of the matrix represents the identity matrix. Returns `true` if it is, `false` otherwise.
```
var matrix = new Matrix(3, 3);
var matrix1 = new Matrix(3, 3, false).setData(1, 0, 0, 0, 1, 0, 0, 0, 1);
var matrix2 = new Matrix(3, 3, false).setData(1, 2, 3, 4, 5, 6, 7, 8, 9);
matrix.isIdentity(); // true
matrix1.isIdentity(); // true
matrix2.isIdentity(); // false
```
## Versioning

@@ -523,3 +578,3 @@

The Matrix class is fully unit tested with over 70 tests. It uses mocha and expect.js and can be run from node.js in the command line.
The Matrix class is fully unit tested. It uses mocha and expect.js and can be run from node.js in the command line.

@@ -531,3 +586,3 @@

Copyright (c) 2013 Johannes Koggdal
Copyright (c) 2014 Johannes Koggdal

@@ -534,0 +589,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

@@ -519,2 +519,67 @@ var expect = require('expect.js');

describe('#copy()', function() {
it('should return the same Matrix instance', function() {
var matrix1 = new Matrix(2, 2).setData([1, 0, 0, 1]);
var matrix2 = new Matrix(2, 2).setData([1, 2, 3, 4]);
expect(matrix1.copy(matrix2)).to.equal(matrix1);
});
it('should set the data from the input matrix on this matrix', function() {
var matrix1 = new Matrix(2, 2).setData([1, 0, 0, 1]);
var matrix2 = new Matrix(2, 2).setData([1, 2, 3, 4]);
matrix1.copy(matrix2);
expect(matrix1.toArray()).to.eql([1, 2, 3, 4]);
});
it('should resize this matrix to the size of the input matrix (larger)', function() {
var matrix1 = new Matrix(2, 2).setData([1, 0, 0, 1]);
var matrix2 = new Matrix(4, 3).setData([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
matrix1.copy(matrix2);
expect(matrix1.length).to.equal(12);
expect(matrix1.rows).to.equal(4);
expect(matrix1.cols).to.equal(3);
expect(matrix1.toArray()).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
});
it('should resize this matrix to the size of the input matrix (smaller)', function() {
var matrix1 = new Matrix(4, 3).setData([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
var matrix2 = new Matrix(2, 2).setData([1, 0, 0, 1]);
matrix1.copy(matrix2);
expect(matrix1.length).to.equal(4);
expect(matrix1.rows).to.equal(2);
expect(matrix1.cols).to.equal(2);
expect(matrix1.toArray()).to.eql([1, 0, 0, 1]);
});
it('should clear out values not needed anymore if the input matrix is smaller', function() {
var matrix1 = new Matrix(4, 3).setData([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
var matrix2 = new Matrix(2, 2).setData([1, 0, 0, 1]);
matrix1.copy(matrix2);
expect(matrix1.length).to.equal(4);
expect(matrix1.rows).to.equal(2);
expect(matrix1.cols).to.equal(2);
expect(matrix1.toArray()).to.eql([1, 0, 0, 1]);
expect('4' in matrix1).to.equal(false);
expect('5' in matrix1).to.equal(false);
expect('6' in matrix1).to.equal(false);
expect('7' in matrix1).to.equal(false);
expect('8' in matrix1).to.equal(false);
expect('9' in matrix1).to.equal(false);
expect('10' in matrix1).to.equal(false);
expect('11' in matrix1).to.equal(false);
});
});
describe('#clone()', function() {

@@ -657,2 +722,30 @@

it('should handle identity matrices', function() {
var matrix1 = new Matrix(2, 2);
var matrix2 = new Matrix(2, 2);
var matrix3 = new Matrix(2, 2).setData([1, 2, 3, 4]);
var matrix4 = new Matrix(2, 2);
var matrix5 = new Matrix(2, 2).setData([2, 4, 6, 8]);
var matrix6 = new Matrix(2, 2);
var matrix7 = new Matrix(2, 2).setData([3, 6, 9, 12]);
matrix1.multiply(matrix2, matrix3, matrix4, matrix5, matrix6, matrix7);
expect(matrix1[1]).to.equal(324);
});
it('should handle identity matrices and scalar multiplications', function() {
var matrix1 = new Matrix(2, 2);
var matrix2 = new Matrix(2, 2);
var scalar1 = 1;
var scalar2 = 2;
var matrix3 = new Matrix(2, 2).setData([1, 2, 3, 4]);
var matrix4 = new Matrix(2, 2);
var scalar3 = 1;
var scalar4 = 0.5;
var matrix5 = new Matrix(2, 2).setData([2, 4, 6, 8]);
var matrix6 = new Matrix(2, 2);
var matrix7 = new Matrix(2, 2).setData([3, 6, 9, 12]);
matrix1.multiply(matrix2, scalar1, scalar2, matrix3, matrix4, scalar3, scalar4, matrix5, matrix6, matrix7);
expect(matrix1[1]).to.equal(324);
});
it('should multiply a number', function() {

@@ -858,2 +951,16 @@ var matrix1 = new Matrix(2, 2).setData([1, 2, 3, 4]);

describe('#isIdentity()', function() {
it('should return false for a matrix that is not an identity matrix', function() {
var matrix = new Matrix(3, 3, false).setData([1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(matrix.isIdentity()).to.equal(false);
});
it('should return true for a matrix that is an identity matrix', function() {
var matrix = new Matrix(3, 3, false).setData([1, 0, 0, 0, 1, 0, 0, 0, 1]);
expect(matrix.isIdentity()).to.equal(true);
});
});
});

Sorry, the diff of this file is not supported yet

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