Comparing version 1.0.0 to 2.0.0
@@ -1,1 +0,8 @@ | ||
exports.Matrix = require('./Matrix'); | ||
/** | ||
* @module matrixmath | ||
* | ||
* @property {Object} Matrix The Matrix class. | ||
*/ | ||
'use strict'; | ||
exports.Matrix = require('./Matrix'); |
448
Matrix.js
@@ -1,51 +0,56 @@ | ||
'use strict'; | ||
/** | ||
* @author Johannes Koggdal | ||
* @module matrixmath/Matrix | ||
*/ | ||
'use strict'; | ||
/** | ||
* A class for representing and working with a mathematical matrix. | ||
* @classdesc A class for representing and working with a mathematical matrix. | ||
* | ||
* @property {number} rows Number of rows. | ||
* @property {number} cols Number of cols. | ||
* @property {number} length Number of values. | ||
* | ||
* @constructor | ||
* | ||
* @param {Array|number=} opt_rowsOrData If an array, it is interpreted | ||
* as data for the matrix. That means the length of the array is the | ||
* number of rows. Each item in the array should be an array where | ||
* the length is interpreted as the number of columns. Example: | ||
* [[1, 0, 0], [0, 1, 0], [0, 0, 1]] | ||
* If a number, it will create a matrix with that number of rows. | ||
* @param {number=} opt_rows The number of rows for the matrix. Default is 0. | ||
* @param {number=} opt_cols The number of columns for the matrix. Default is same | ||
* amount of columns as rows. | ||
* @param {boolean=} opt_setInitial Whether to set the initial data when created. | ||
* The initial data will be set to the identity matrix if it specifies the same | ||
* amount of rows as columns. Default is true. | ||
* | ||
* @param {number=} opt_cols The number of columns for the matrix. If | ||
* an array was passed as the first argument, this is ignored. | ||
* @example | ||
* // Create a 3x3 matrix with data | ||
* var matrix = new Matrix(3, 3); | ||
* matrix.setData( | ||
* 1, 0, 0, | ||
* 0, 1, 0, | ||
* 0, 0, 1 | ||
* ); | ||
* | ||
* @example | ||
* // Create a matrix with data | ||
* var matrix = new Matrix([ | ||
* [1, 0, 0], | ||
* [0, 1, 0], | ||
* [0, 0, 1] | ||
* ]); | ||
* | ||
* // Create a matrix filled with zeros | ||
* // The matrix will be 3 rows and 2 columns | ||
* var matrix = new Matrix(3, 2); | ||
* | ||
* // Create an identity matrix | ||
* // The matrix will be 3 rows and 3 columns | ||
* var matrix = new Matrix(3); | ||
* | ||
* // Create a matrix with no data set | ||
* // The matrix will be 3 rows and 3 columns | ||
* var matrix = new Matrix(3, 3, false); | ||
*/ | ||
function Matrix(opt_rowsOrData, opt_cols) { | ||
if (Array.isArray(opt_rowsOrData)) { | ||
this.rows = opt_rowsOrData; | ||
function Matrix(opt_rows, opt_cols, opt_setInitial) { | ||
this.rows = opt_rows || 0; | ||
this.cols = opt_cols || this.rows; | ||
this.length = this.rows * this.cols; | ||
} else { | ||
var numRows = opt_rowsOrData || 1; | ||
var numCols = opt_cols || 1; | ||
var rows = new Array(numRows); | ||
var setInitial = opt_setInitial === undefined ? true : opt_setInitial; | ||
for (var row = 0; row < numRows; row++) { | ||
var rowData = new Array(numCols); | ||
rows[row] = rowData; | ||
for (var col = 0; col < numCols; col++) { | ||
rowData[col] = 0; | ||
} | ||
if (setInitial) { | ||
if (this.rows === this.cols) { | ||
this.setIdentityData(); | ||
} else { | ||
this.setEmptyData(); | ||
} | ||
this.rows = rows; | ||
} | ||
@@ -55,19 +60,2 @@ } | ||
/** | ||
* Create a new identity matrix for a given size. | ||
* | ||
* @param {number} size Number of rows/columns. | ||
* | ||
* @return {Matrix} A new matrix. | ||
*/ | ||
Matrix.identity = function(size) { | ||
var matrix = new Matrix(size, size); | ||
for (var i = 0; i < size; i++) { | ||
matrix.rows[i][i] = 1; | ||
} | ||
return matrix; | ||
}; | ||
/** | ||
* Add matrices together and return a new matrix. | ||
@@ -149,24 +137,126 @@ * It will clone the first matrix and add to that. | ||
/** | ||
* Clone this matrix to a new instance. | ||
* Set the data for this matrix to be only zeros. | ||
*/ | ||
Matrix.prototype.setEmptyData = function() { | ||
var newData = new Array(this.length); | ||
for (var i = 0, l = this.length; i < l; i++) { | ||
newData[i] = 0; | ||
} | ||
this.setData(newData); | ||
return this; | ||
}; | ||
/** | ||
* Set the data for this matrix to the identity data. | ||
* | ||
* @return {Matrix} A new matrix for the result. | ||
* @return {Matrix} This Matrix instance. | ||
*/ | ||
Matrix.prototype.clone = function() { | ||
var rows = this.rows; | ||
var numRows = rows.length; | ||
var numCols = rows[0].length; | ||
var newRows = new Array(numRows); | ||
Matrix.prototype.setIdentityData = function() { | ||
var newData = new Array(this.length); | ||
for (var row = 0; row < numRows; row++) { | ||
var matrixRow = rows[row]; | ||
newRows[row] = new Array(numCols); | ||
for (var col = 0; col < numCols; col++) { | ||
newRows[row][col] = matrixRow[col]; | ||
for (var i = 0, l = this.length; i < l; i++) { | ||
newData[i] = i % (this.cols + 1) ? 0 : 1; | ||
} | ||
this.setData(newData); | ||
return this; | ||
}; | ||
/** | ||
* Set the data for this matrix. | ||
* | ||
* @param {Array.<number>} data An array of values (numbers). Alternatively, | ||
* the data can be provided as separate arguments, but if so, the size | ||
* must match the current size. | ||
* @param {number=} opt_rows Number of rows in the new data. If not provided, | ||
* the data must match the size of the previous data. | ||
* @param {number=} opt_cols Number of columns in the new data. If not provided, | ||
* the data must match the size of the previous data. | ||
* | ||
* @return {Matrix} This Matrix instance. | ||
*/ | ||
Matrix.prototype.setData = function(data, opt_rows, opt_cols) { | ||
var i, l; | ||
var isArray = Array.isArray(data); | ||
if (!isArray) { | ||
data = arguments; | ||
opt_rows = undefined; | ||
opt_cols = undefined; | ||
} | ||
// If the number of values is different than before, and there was no hint | ||
// provided for the size of the new matrix data, we can't modify the data | ||
// safely, so we do nothing. | ||
if (data.length !== this.length) { | ||
if (opt_rows === undefined || opt_cols === undefined) { | ||
return this; | ||
} else if (opt_rows * opt_cols !== data.length) { | ||
return this; | ||
} | ||
} | ||
return new Matrix(newRows); | ||
// Clean out previous data | ||
for (i = 0, l = this.length; i < l; i++) { | ||
delete this[i]; | ||
} | ||
// Set new data | ||
for (i = 0, l = data.length; i < l; i++) { | ||
this[i] = data[i]; | ||
} | ||
// Set new metadata | ||
this.length = data.length; | ||
this.rows = opt_rows || this.rows; | ||
this.cols = opt_cols || this.cols; | ||
return this; | ||
}; | ||
/** | ||
* Get the data for this matrix as an array of numbers, with additional data | ||
* properties for rows and columns counts. | ||
* | ||
* @return {Array} An array of numbers, representing the data of the matrix. | ||
*/ | ||
Matrix.prototype.getData = function() { | ||
var data = this.toArray(); | ||
data.rows = this.rows; | ||
data.cols = this.cols; | ||
return data; | ||
}; | ||
/** | ||
* Get the data for this matrix as a regular array. | ||
* | ||
* @return {Array} An array of numbers. | ||
*/ | ||
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; | ||
}; | ||
/** | ||
* Clone this matrix to a new instance. | ||
* | ||
* @return {Matrix} A new matrix for the result. | ||
*/ | ||
Matrix.prototype.clone = function() { | ||
var matrix = new Matrix(this.rows, this.cols); | ||
matrix.setData(this.getData()); | ||
return matrix; | ||
}; | ||
/** | ||
* Add matrices together into this matrix. | ||
@@ -180,30 +270,20 @@ * | ||
Matrix.prototype.add = function(var_args) { | ||
var matrices = Array.prototype.slice.call(arguments); | ||
var matrices = arguments; | ||
var numCols = this.rows[0].length; | ||
var numRows = this.rows.length; | ||
var numValues = this.length; | ||
// Loop through all the matrices passed to the method | ||
for (var i = 0, l = matrices.length; i < l; i++) { | ||
var matrix = matrices[i]; | ||
// Get the number of rows and columns for the current matrix | ||
var matrixRows = matrices[i].rows; | ||
var numRowsInput = matrixRows.length; | ||
var numColsInput = matrixRows[0].length; | ||
// The size of the matrices must match | ||
if (numColsInput !== numCols || numRowsInput !== numRows) { | ||
if (matrix.cols !== this.cols || matrix.rows !== this.rows) { | ||
continue; | ||
} | ||
// Loop through all rows | ||
for (var row = 0; row < numRows; row++) { | ||
var matrixRow = matrixRows[row]; | ||
// Loop through all values | ||
for (var n = 0; n < numValues; n++) { | ||
// Loop through all columns in that row | ||
for (var col = 0; col < numCols; col++) { | ||
// Add the number in that position | ||
this.rows[row][col] += matrixRow[col]; | ||
} | ||
// Add the number in that position | ||
this[n] += matrix[n]; | ||
} | ||
@@ -224,30 +304,20 @@ } | ||
Matrix.prototype.subtract = function(var_args) { | ||
var matrices = Array.prototype.slice.call(arguments); | ||
var matrices = arguments; | ||
var numCols = this.rows[0].length; | ||
var numRows = this.rows.length; | ||
var numValues = this.length; | ||
// Loop through all the matrices passed to the method | ||
for (var i = 0, l = matrices.length; i < l; i++) { | ||
var matrix = matrices[i]; | ||
// Get the number of rows and columns for the current matrix | ||
var matrixRows = matrices[i].rows; | ||
var numRowsInput = matrixRows.length; | ||
var numColsInput = matrixRows[0].length; | ||
// The size of the matrices must match | ||
if (numColsInput !== numCols || numRowsInput !== numRows) { | ||
if (matrix.cols !== this.cols || matrix.rows !== this.rows) { | ||
continue; | ||
} | ||
// Loop through all rows | ||
for (var row = 0; row < numRows; row++) { | ||
// Loop through all values | ||
for (var n = 0; n < numValues; n++) { | ||
// Loop through all columns in that row | ||
var matrixRow = matrixRows[row]; | ||
for (var col = 0; col < numCols; col++) { | ||
// Subtract the number in that position | ||
this.rows[row][col] -= matrixRow[col]; | ||
} | ||
// Subtract the number in that position | ||
this[n] -= matrix[n]; | ||
} | ||
@@ -269,5 +339,5 @@ } | ||
Matrix.prototype.multiply = function(var_args) { | ||
var matrices = Array.prototype.slice.call(arguments); | ||
var matrices = arguments; | ||
var newRows = this.rows; | ||
var newRows = this.getData(); | ||
@@ -279,4 +349,5 @@ // Loop through all the matrices passed to the method | ||
// Get the number of rows and columns for the target matrix | ||
var rowsInTarget = newRows.length; | ||
var colsInTarget = newRows[0].length; | ||
var rowsInTarget = newRows.rows; | ||
var colsInTarget = newRows.cols; | ||
var numValuesInTarget = newRows.length; | ||
@@ -286,14 +357,9 @@ // A number means we should do a scalar multiplication. | ||
var scale = matrix; | ||
var factor = 1 / scale; // Used to not get floating point errors | ||
// Loop through all rows | ||
for (var row = 0; row < rowsInTarget; row++) { | ||
var matrixRow = newRows[row]; | ||
// Loop through all values | ||
for (var n = 0; n < numValuesInTarget; n++) { | ||
// Loop through all columns in that row | ||
for (var col = 0; col < colsInTarget; col++) { | ||
// Multiply the number in that position | ||
var factor = 1 / scale; // Used to not get floating point errors | ||
matrixRow[col] = matrixRow[col] * (scale * factor) / factor; | ||
} | ||
// Multiply the number in that position | ||
newRows[n] = newRows[n] * (scale * factor) / factor; | ||
} | ||
@@ -306,5 +372,4 @@ | ||
// Get the number of rows and columns for the current matrix | ||
var matrixRows = matrix.rows; | ||
var rowsInCurrent = matrixRows.length; | ||
var colsInCurrent = matrixRows[0].length; | ||
var rowsInCurrent = matrix.rows; | ||
var colsInCurrent = matrix.cols; | ||
@@ -319,2 +384,4 @@ // The number of rows must match the number of columns in the first matrix | ||
var tempData = new Array(newRows.length); | ||
tempData.rows = newRows.rows; | ||
tempData.cols = matrix.cols; | ||
@@ -329,6 +396,6 @@ // Loop through each row from the first matrix | ||
for (var currentRow = 0; currentRow < rowsInCurrent; currentRow++) { | ||
var outputIndex = row * tempData.cols + currentCol; | ||
// Create initial values when they don't exist | ||
if (!tempData[row]) tempData[row] = new Array(colsInCurrent); | ||
if (!tempData[row][currentCol]) tempData[row][currentCol] = 0; | ||
if (!tempData[outputIndex]) tempData[outputIndex] = 0; | ||
@@ -338,3 +405,3 @@ // Calculate the product of the number at the current position in the first matrix | ||
// value at the current position in the output data array. | ||
tempData[row][currentCol] += newRows[row][currentRow] * matrixRows[currentRow][currentCol]; | ||
tempData[outputIndex] += newRows[row * newRows.cols + currentRow] * matrix[currentRow * matrix.cols + currentCol]; | ||
} | ||
@@ -350,3 +417,3 @@ } | ||
// Set the new data for this Matrix instance | ||
this.rows = newRows; | ||
this.setData(newRows, newRows.rows, newRows.cols); | ||
@@ -374,3 +441,3 @@ return this; | ||
// matrix from the list. | ||
if (matrix.rows.length !== matrix.rows[0].length) { | ||
if (matrix.rows !== matrix.cols) { | ||
matrices.splice(i, 1); | ||
@@ -402,3 +469,3 @@ i--; l--; | ||
// Matrices that are not square can't be raised | ||
if (this.rows.length !== this.rows[0].length) { | ||
if (this.rows !== this.cols) { | ||
return this; | ||
@@ -423,16 +490,14 @@ } | ||
Matrix.prototype.transpose = function() { | ||
var rows = this.rows; | ||
var numRows = rows.length; | ||
var numCols = rows[0].length; | ||
var numRows = this.rows; | ||
var numCols = this.cols; | ||
var newRows = new Array(numCols); | ||
var newData = new Array(this.length); | ||
for (var row = 0; row < numRows; row++) { | ||
for (var col = 0; col < numCols; col++) { | ||
if (!newRows[col]) newRows[col] = new Array(rows); | ||
newRows[col][row] = rows[row][col]; | ||
newData[col * numCols + row] = this[row * numCols + col]; | ||
} | ||
} | ||
this.rows = newRows; | ||
this.setData(newData); | ||
@@ -452,5 +517,4 @@ return this; | ||
Matrix.prototype.invert = function() { | ||
var rows = this.rows; | ||
var numRows = rows.length; | ||
var numCols = rows[0].length; | ||
var numRows = this.rows; | ||
var numCols = this.cols; | ||
@@ -460,9 +524,16 @@ // The matrix must be square | ||
var col; // Used in loop further down | ||
var removeColumn = function(row) { | ||
row.splice(col, 1); return row; | ||
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 matrixOfMinors = new Matrix(numRows, numCols); | ||
var matrixOfMinorsData = matrixOfMinors.rows; | ||
@@ -472,3 +543,3 @@ // Loop through each number in the matrix | ||
for (var row = 0; row < numRows; row++) { | ||
for (col = 0; col < numCols; col++) { | ||
for (var col = 0; col < numCols; col++) { | ||
@@ -478,9 +549,11 @@ // We need to get the determinant of the matrix made by the area | ||
// we remove the first row and the column where the number is. | ||
var matrix = this.clone(); | ||
matrix.rows.splice(row, 1); | ||
matrix.rows.map(removeColumn); | ||
var matrix = new Matrix(this.rows, this.cols, false); | ||
var newData = this.getData(); | ||
newData = removeRow(newData, row, this.cols); | ||
newData = removeColumn(newData, col, this.cols); | ||
matrix.setData(newData, matrix.rows - 1, matrix.cols - 1); | ||
// 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. | ||
matrixOfMinorsData[row][col] = (i % 2 ? -1 : 1) * matrix.getDeterminant(); | ||
matrixOfMinors[row * matrixOfMinors.cols + col] = (i % 2 ? -1 : 1) * matrix.getDeterminant(); | ||
@@ -491,5 +564,2 @@ i++; | ||
// Transpose the cofactor matrix of minors to get the adjugate matrix | ||
matrixOfMinors.transpose(); | ||
// Get the determinant of the original matrix. | ||
@@ -499,3 +569,3 @@ // This could be done with the getDeterminant method, but this is faster. | ||
for (var n = 0; n < numCols; n++) { | ||
originalDeterminant += rows[0][n] * matrixOfMinorsData[0][n]; | ||
originalDeterminant += this[n] * matrixOfMinors[n]; | ||
} | ||
@@ -506,4 +576,10 @@ | ||
this.rows = matrixOfMinors.multiply(1 / originalDeterminant).rows; | ||
// Transpose the cofactor matrix of minors to get the adjugate matrix | ||
matrixOfMinors.transpose(); | ||
// Multiply the matrix of minors with the inverse of the determinant, | ||
// to get the final inverse of the original matrix. | ||
var product = matrixOfMinors.multiply(1 / originalDeterminant); | ||
this.setData(product.getData(), product.rows, product.cols); | ||
return this; | ||
@@ -520,15 +596,15 @@ }; | ||
var rows = this.rows; | ||
var size = rows.length; | ||
var cols = this.cols; | ||
// The matrix must be square | ||
if (size !== rows[0].length) return null; | ||
if (rows !== cols) return null; | ||
// For a 1x1 matrix ( [[a]] ), the determinant is: a | ||
if (size === 1) { | ||
return rows[0][0]; | ||
if (rows === 1) { | ||
return this[0]; | ||
} | ||
// For a 2x2 matrix ( [[a, b], [c, d]] ), the determinant is: a*d - b*c | ||
if (size === 2) { | ||
return rows[0][0] * rows[1][1] - rows[0][1] * rows[1][0]; | ||
if (rows === 2) { | ||
return this[0] * this[3] - this[1] * this[2]; | ||
} | ||
@@ -538,12 +614,12 @@ | ||
// is: a*(e*i - f*h) - b*(d*i - f*g) + c*(d*h - e*g) | ||
if (size === 3) { | ||
var a = rows[0][0]; | ||
var b = rows[0][1]; | ||
var c = rows[0][2]; | ||
var d = rows[1][0]; | ||
var e = rows[1][1]; | ||
var f = rows[1][2]; | ||
var g = rows[2][0]; | ||
var h = rows[2][1]; | ||
var i = rows[2][2]; | ||
if (rows === 3) { | ||
var a = this[0]; | ||
var b = this[1]; | ||
var c = this[2]; | ||
var d = this[3]; | ||
var e = this[4]; | ||
var f = this[5]; | ||
var g = this[6]; | ||
var h = this[7]; | ||
var i = this[8]; | ||
return a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g); | ||
@@ -553,13 +629,21 @@ } | ||
// For 4x4 or larger matrices | ||
if (size >= 4) { | ||
if (rows >= 4) { | ||
var n; // Used in loop further down | ||
var removeColumn = function(row) { | ||
row.splice(n, 1); return row; | ||
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; | ||
// Loop through each number for the first row | ||
for (n = 0; n < size; n++) { | ||
for (var col = 0; col < cols; col++) { | ||
@@ -569,6 +653,9 @@ // We need to get the determinant of the matrix made by the area | ||
// we remove the first row and the column where the number is. | ||
var matrix = this.clone(); | ||
matrix.rows.shift(); | ||
matrix.rows.map(removeColumn); | ||
result += (n % 2 ? -1 : 1) * rows[0][n] * matrix.getDeterminant(); | ||
var matrix = new Matrix(this.rows, this.cols, false); | ||
var newData = this.getData(); | ||
newData = removeRow(newData, 0, this.cols); | ||
newData = removeColumn(newData, col, this.cols); | ||
matrix.setData(newData, matrix.rows - 1, matrix.cols - 1); | ||
result += (col % 2 ? -1 : 1) * this[col] * matrix.getDeterminant(); | ||
} | ||
@@ -583,3 +670,3 @@ | ||
* | ||
* @param {Matrix|Array} input Another Matrix instance or an array of rows. | ||
* @param {Matrix} input Another Matrix instance. | ||
* | ||
@@ -589,11 +676,6 @@ * @return {Boolean} True if it's the same. | ||
Matrix.prototype.equals = function(input) { | ||
var thisRows = this.rows; | ||
var inputRows = input instanceof Matrix ? input.rows : input; | ||
var numRowsThis = thisRows.length; | ||
var numColsThis = thisRows[0].length; | ||
var numRowsInput = inputRows.length; | ||
var numColsInput = inputRows[0].length; | ||
if (!(input instanceof Matrix)) return false; | ||
// If the size does not match, it is not equal | ||
if (numRowsThis !== numRowsInput || numColsThis !== numColsInput) { | ||
if (this.rows !== input.rows || this.cols !== input.cols) { | ||
return false; | ||
@@ -603,6 +685,4 @@ } | ||
// Check each number and return false if something doesn't match | ||
for (var row = 0; row < numRowsThis; row++) { | ||
for (var col = 0; col < numColsThis; col++) { | ||
if (thisRows[row][col] !== inputRows[row][col]) return false; | ||
} | ||
for (var i = 0, l = this.length; i < l; i++) { | ||
if (this[i] !== input[i]) return false; | ||
} | ||
@@ -609,0 +689,0 @@ |
{ | ||
"name": "matrixmath", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"author": "Johannes Koggdal <johannes@koggdal.com>", | ||
@@ -5,0 +5,0 @@ "description": "Library for working with mathematical matrices.", |
300
README.md
@@ -27,4 +27,3 @@ # Matrix Math | ||
* new Matrix(rows) | ||
* new Matrix(numRows, numCols) | ||
* new Matrix(opt_rows, opt_cols, opt_setInitial) | ||
@@ -34,2 +33,4 @@ **Instance properties** | ||
* rows | ||
* cols | ||
* length | ||
@@ -45,2 +46,8 @@ **Static methods** | ||
* setIdentityData () | ||
* setEmptyData () | ||
* setData (data, opt_rows, opt_cols) | ||
* getData () | ||
* toArray () | ||
* clone () | ||
* add (matrix[,…matrixN]) | ||
@@ -54,3 +61,3 @@ * subtract (matrix[,…matrixN]) | ||
* getDeterminant () | ||
* equals () | ||
* equals (input) | ||
@@ -64,25 +71,28 @@ | ||
#### new Matrix(rows) | ||
#### new Matrix() | ||
``` | ||
var matrix = new Matrix([ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
[7, 8, 9] | ||
]); | ||
var matrix = new Matrix(); | ||
``` | ||
#### new Matrix(numRows, numCols) | ||
#### new Matrix(opt_rows, opt_cols) | ||
``` | ||
var matrix = new Matrix(2, 3); | ||
var matrix = new Matrix(3, 3); | ||
``` | ||
``` | ||
> console.log(matrix.rows); | ||
[ | ||
[0, 0, 0], | ||
[0, 0, 0] | ||
] | ||
> console.log(matrix.toArray()); | ||
[1, 0, 0, 0, 1, 0, 0, 0, 1] | ||
``` | ||
#### new Matrix(opt_rows, opt_cols, opt_setInitial) | ||
``` | ||
var matrix = new Matrix(3, 3, false); | ||
``` | ||
``` | ||
> console.log(matrix.toArray()); | ||
[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined] | ||
``` | ||
--- | ||
@@ -94,37 +104,34 @@ | ||
The rows property contains the matrix represented as an array of rows, where each row is an array of numbers. | ||
The rows property contains the number of rows in the matrix. | ||
``` | ||
> console.log(matrix.rows); | ||
[ | ||
[0, 0, 0], | ||
[0, 0, 0] | ||
] | ||
3 | ||
``` | ||
--- | ||
#### matrix.cols | ||
### Static methods | ||
The cols property contains the number of columns in the matrix. | ||
The following methods will return a new Matrix instance. | ||
``` | ||
> console.log(matrix.cols); | ||
3 | ||
``` | ||
#### matrix.length | ||
#### Matrix.identity(size) | ||
The length property contains the number of values in the matrix. | ||
Creates an identity matrix with the given number of rows and columns. | ||
``` | ||
var matrix = Matrix.identity(3); | ||
> console.log(matrix.length); | ||
9 | ||
``` | ||
``` | ||
> console.log(matrix.rows); | ||
[ | ||
[1, 0, 0], | ||
[0, 1, 0], | ||
[0, 0, 1] | ||
] | ||
``` | ||
--- | ||
### Static methods | ||
The following methods will return a new Matrix instance. | ||
#### Matrix.add(matrix1, matrix2[,…matrixN]) | ||
@@ -176,2 +183,119 @@ | ||
#### matrix.setIdentityData() | ||
Set the data in the matrix to the identity matrix. | ||
``` | ||
var matrix = new Matrix(3, 3, false).setIdentityData(); | ||
``` | ||
``` | ||
> console.log(matrix.toArray()); | ||
[ | ||
1, 0, 0 | ||
0, 1, 0 | ||
0, 0, 1 | ||
] | ||
``` | ||
#### matrix.setEmptyData() | ||
Set the data in the matrix to be only zeros. | ||
``` | ||
var matrix = new Matrix(3, 3, false).setEmptyData(); | ||
``` | ||
``` | ||
> console.log(matrix.toArray()); | ||
[ | ||
0, 0, 0 | ||
0, 0, 0 | ||
0, 0, 0 | ||
] | ||
``` | ||
#### matrix.setData(data, opt_rows_ opt_cols) | ||
Set the data in the matrix to the passed in data. | ||
``` | ||
var matrix = new Matrix(3, 3); | ||
matrix.setData([1, 2, 3, 4], 2, 2); | ||
``` | ||
``` | ||
> console.log(matrix.toArray()); | ||
[ | ||
1, 2 | ||
3, 4 | ||
] | ||
``` | ||
``` | ||
var matrix = new Matrix(3, 3); | ||
matrix.setData([1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
``` | ||
``` | ||
> console.log(matrix.toArray()); | ||
[ | ||
1, 2, 3 | ||
4, 5, 6, | ||
7, 8, 9 | ||
] | ||
``` | ||
``` | ||
var matrix = new Matrix(3, 3); | ||
matrix.setData(1, 2, 3, 4, 5, 6, 7, 8, 9); | ||
``` | ||
``` | ||
> console.log(matrix.toArray()); | ||
[ | ||
1, 2, 3 | ||
4, 5, 6, | ||
7, 8, 9 | ||
] | ||
``` | ||
#### matrix.getData() | ||
Get the data in the matrix as an array with extra data. | ||
``` | ||
var matrix = new Matrix(2, 2); | ||
``` | ||
``` | ||
> console.log(matrix.getData()); | ||
[ | ||
1, 0, | ||
0, 1 | ||
] | ||
> console.log(matrix.getData().rows); | ||
2 | ||
> console.log(matrix.getData().cols); | ||
2 | ||
``` | ||
#### matrix.toArray() | ||
Get the data in the matrix as an array. | ||
``` | ||
var matrix = new Matrix(2, 2); | ||
``` | ||
``` | ||
> console.log(matrix.toArray()); | ||
[ | ||
1, 0, | ||
0, 1 | ||
] | ||
``` | ||
#### matrix.clone() | ||
@@ -182,3 +306,3 @@ | ||
``` | ||
var matrix = Matrix.identity(2); | ||
var matrix = new Matrix(2, 2); | ||
var matrix1 = matrix.clone(); | ||
@@ -193,11 +317,9 @@ ``` | ||
``` | ||
var matrix = new Matrix([ [1, 2, 3] ]); | ||
var matrix1 = new Matrix([ [2, 4, 6] ]); | ||
var matrix = new Matrix(1, 3).setData(1, 2, 3); | ||
var matrix1 = new Matrix(1, 3).setData(2, 4, 6); | ||
matrix.add(matrix1); | ||
``` | ||
``` | ||
> console.log(matrix.rows); | ||
[ | ||
[3, 6, 9] | ||
] | ||
> console.log(matrix.toArray()); | ||
[3, 6, 9] | ||
``` | ||
@@ -211,11 +333,9 @@ | ||
``` | ||
var matrix = new Matrix([ [1, 2, 3] ]); | ||
var matrix1 = new Matrix([ [2, 4, 6] ]); | ||
var matrix = new Matrix(1, 3).setData(1, 2, 3); | ||
var matrix1 = new Matrix(1, 3).setData(2, 4, 6); | ||
matrix.subtract(matrix1); | ||
``` | ||
``` | ||
> console.log(matrix.rows); | ||
[ | ||
[-2, -2, -3] | ||
] | ||
> console.log(matrix.toArray()); | ||
[-2, -2, -3] | ||
``` | ||
@@ -231,24 +351,18 @@ | ||
``` | ||
var matrix = new Matrix([ [1, 2], [3, 4] ]); | ||
var matrix1 = new Matrix( [[2, 4], [6, 8] ]); | ||
var matrix = new Matrix(2, 2).setData(1, 2, 3, 4); | ||
var matrix1 = new Matrix(2, 2).setData(2, 4, 6, 8); | ||
matrix.multiply(matrix1); | ||
``` | ||
``` | ||
> console.log(matrix.rows); | ||
[ | ||
[14, 20], | ||
[30, 44] | ||
] | ||
> console.log(matrix.toArray()); | ||
[14, 20, 30, 44] | ||
``` | ||
``` | ||
var matrix = new Matrix([ [1, 2], [3, 4] ]); | ||
var matrix = new Matrix(2, 2).setData(1, 2, 3, 4); | ||
matrix.multiply(3); | ||
``` | ||
``` | ||
> console.log(matrix.rows); | ||
[ | ||
[3, 6], | ||
[9, 12] | ||
] | ||
> console.log(matrix.toArray()); | ||
[3, 6, 9, 12] | ||
``` | ||
@@ -262,12 +376,9 @@ | ||
``` | ||
var matrix = new Matrix([ [1, 0], [0, 1] ]); | ||
var matrix1 = new Matrix( [[2, 0], [0, 2] ]); | ||
var matrix = new Matrix(2, 2).setData(1, 0, 0, 1); | ||
var matrix1 = new Matrix(2, 2).setData(2, 0, 0, 2); | ||
matrix.divide(matrix1); | ||
``` | ||
``` | ||
> console.log(matrix.rows); | ||
[ | ||
[0.5, 0], | ||
[0, -0.5] | ||
] | ||
> console.log(matrix.toArray()); | ||
[0.5, 0, 0, -0.5] | ||
``` | ||
@@ -280,11 +391,8 @@ | ||
``` | ||
var matrix = new Matrix([[1, 2], [4, 1]]); | ||
var matrix = new Matrix(2, 2).setData(1, 2, 4, 1); | ||
matrix.power(3); | ||
``` | ||
``` | ||
> console.log(matrix.rows); | ||
[ | ||
[25, 22], | ||
[44, 25] | ||
] | ||
> console.log(matrix.toArray()); | ||
[25, 22, 44, 25] | ||
``` | ||
@@ -297,14 +405,8 @@ | ||
``` | ||
var matrix = new Matrix([ | ||
[1, 2], | ||
[4, 1] | ||
]); | ||
var matrix = new Matrix(2, 2).setData(1, 2, 4, 1); | ||
matrix.transpose(); | ||
``` | ||
``` | ||
> console.log(matrix.rows); | ||
[ | ||
[1, 4], | ||
[2, 1] | ||
] | ||
> console.log(matrix.toArray()); | ||
[1, 4, 2, 1] | ||
``` | ||
@@ -317,15 +419,15 @@ | ||
``` | ||
var matrix = new Matrix([ | ||
[3, 0, 2], | ||
[2, 0, -2], | ||
[0, 1, 1] | ||
]); | ||
var matrix = new Matrix(3, 3).setData( | ||
3, 0, 2, | ||
2, 0, -2, | ||
0, 1, 1 | ||
); | ||
matrix.invert(); | ||
``` | ||
``` | ||
> console.log(matrix.rows); | ||
> console.log(matrix.toArray()); | ||
[ | ||
[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 | ||
] | ||
@@ -339,3 +441,3 @@ ``` | ||
``` | ||
var matrix = new Matrix([[4, 6], [3, 8]]); | ||
var matrix = new Matrix(2, 2).setData(4, 6, 3, 8); | ||
var determinant = matrix.getDeterminant(); | ||
@@ -353,5 +455,5 @@ ``` | ||
``` | ||
var matrix = new Matrix([[3, 0], [2, 0], [0, 1]]); | ||
var matrix1 = new Matrix([[3, 0], [2, 0], [0, 1]]); | ||
var matrix2 = new Matrix([[3, 0], [2, 0]]); | ||
var matrix = new Matrix(3, 2).setData(3, 0, 2, 0, 0, 1); | ||
var matrix1 = new Matrix(3, 2).setData(3, 0, 2, 0, 0, 1); | ||
var matrix2 = new Matrix(2, 2).setData(3, 0, 2, 0); | ||
@@ -370,3 +472,3 @@ matrix.equals(matrix1); // true | ||
The Matrix class is fully unit tested with over 60 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 with over 70 tests. It uses mocha and expect.js and can be run from node.js in the command line. | ||
@@ -396,2 +498,2 @@ | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
THE SOFTWARE. |
@@ -11,37 +11,30 @@ var expect = require('expect.js'); | ||
expect(matrix instanceof Matrix).to.equal(true); | ||
expect(matrix.rows.length).to.equal(3); | ||
expect(matrix.rows[0].length).to.equal(4); | ||
expect(matrix.rows).to.equal(3); | ||
expect(matrix.cols).to.equal(4); | ||
}); | ||
it('should take an initial array to set up the matrix', function() { | ||
var matrix = new Matrix([ | ||
[1, 0, 0], | ||
[0, 1, 0] | ||
]); | ||
expect(matrix instanceof Matrix).to.equal(true); | ||
expect(matrix.rows.length).to.equal(2); | ||
expect(matrix.rows[0].length).to.equal(3); | ||
expect(matrix.rows[1][1]).to.equal(1); | ||
it('should have defaults for all arguments', function() { | ||
var matrix1 = new Matrix(); | ||
expect(matrix1.rows).to.equal(0); | ||
expect(matrix1.cols).to.equal(0); | ||
var matrix2 = new Matrix(3); | ||
expect(matrix2.rows).to.equal(3); | ||
expect(matrix2.cols).to.equal(3); | ||
}); | ||
}); | ||
it('should set initial data', function() { | ||
var matrix1 = new Matrix(3, 3); | ||
expect(matrix1[0]).to.equal(1); | ||
describe('.identity()', function() { | ||
var matrix2 = new Matrix(3, 2); | ||
expect(matrix2[0]).to.equal(0); | ||
}); | ||
it('should create an identity matrix for a given size', function() { | ||
var matrix = Matrix.identity(3); | ||
it('should not set initial data if option says so', function() { | ||
var matrix1 = new Matrix(3, 3, false); | ||
expect(matrix1[0]).to.equal(undefined); | ||
expect(matrix instanceof Matrix).to.equal(true); | ||
expect(matrix.rows.length).to.equal(3); | ||
expect(matrix.rows[0].length).to.equal(3); | ||
expect(matrix.rows[0][0]).to.equal(1); | ||
expect(matrix.rows[0][1]).to.equal(0); | ||
expect(matrix.rows[0][2]).to.equal(0); | ||
expect(matrix.rows[1][0]).to.equal(0); | ||
expect(matrix.rows[1][1]).to.equal(1); | ||
expect(matrix.rows[1][2]).to.equal(0); | ||
expect(matrix.rows[2][0]).to.equal(0); | ||
expect(matrix.rows[2][1]).to.equal(0); | ||
expect(matrix.rows[2][2]).to.equal(1); | ||
var matrix2 = new Matrix(3, 2, false); | ||
expect(matrix2[0]).to.equal(undefined); | ||
}); | ||
@@ -53,8 +46,8 @@ | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[2, 4, 6]]); | ||
var matrix3 = new Matrix([[8, 10, 12]]); | ||
var matrix4 = new Matrix([[14, 16, 18]]); | ||
var matrix5 = new Matrix([[2, 4, 6], [2, 4, 8]]); | ||
var matrix6 = new Matrix([[4, 6]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(1, 3).setData([2, 4, 6]); | ||
var matrix3 = new Matrix(1, 3).setData([8, 10, 12]); | ||
var matrix4 = new Matrix(1, 3).setData([14, 16, 18]); | ||
var matrix5 = new Matrix(2, 3).setData([2, 4, 6, 2, 4, 8]); | ||
var matrix6 = new Matrix(1, 2).setData([4, 6]); | ||
@@ -68,3 +61,3 @@ it('should return an instance of Matrix', function() { | ||
var sum = Matrix.add(matrix1, matrix2); | ||
expect(sum.rows[0][0]).to.equal(3); | ||
expect(sum[0]).to.equal(3); | ||
}); | ||
@@ -74,3 +67,3 @@ | ||
var sum = Matrix.add(matrix1, matrix5); | ||
expect(sum.rows[0][0]).to.equal(1); | ||
expect(sum[0]).to.equal(1); | ||
}); | ||
@@ -80,3 +73,3 @@ | ||
var sum = Matrix.add(matrix1, matrix6); | ||
expect(sum.rows[0][0]).to.equal(1); | ||
expect(sum[0]).to.equal(1); | ||
}); | ||
@@ -86,3 +79,3 @@ | ||
var sum = Matrix.add(matrix1, matrix2, matrix3, matrix4); | ||
expect(sum.rows[0][0]).to.equal(25); | ||
expect(sum[0]).to.equal(25); | ||
}); | ||
@@ -94,8 +87,8 @@ | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[2, 4, 6]]); | ||
var matrix3 = new Matrix([[8, 10, 12]]); | ||
var matrix4 = new Matrix([[14, 16, 18]]); | ||
var matrix5 = new Matrix([[2, 4, 6], [2, 4, 8]]); | ||
var matrix6 = new Matrix([[4, 6]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(1, 3).setData([2, 4, 6]); | ||
var matrix3 = new Matrix(1, 3).setData([8, 10, 12]); | ||
var matrix4 = new Matrix(1, 3).setData([14, 16, 18]); | ||
var matrix5 = new Matrix(2, 3).setData([2, 4, 6, 2, 4, 8]); | ||
var matrix6 = new Matrix(1, 2).setData([4, 6]); | ||
@@ -109,3 +102,3 @@ it('should return an instance of Matrix', function() { | ||
var difference = Matrix.subtract(matrix1, matrix2); | ||
expect(difference.rows[0][1]).to.equal(-2); | ||
expect(difference[1]).to.equal(-2); | ||
}); | ||
@@ -115,3 +108,3 @@ | ||
var difference = Matrix.subtract(matrix1, matrix5); | ||
expect(difference.rows[0][0]).to.equal(1); | ||
expect(difference[0]).to.equal(1); | ||
}); | ||
@@ -121,3 +114,3 @@ | ||
var difference = Matrix.subtract(matrix1, matrix6); | ||
expect(difference.rows[0][0]).to.equal(1); | ||
expect(difference[0]).to.equal(1); | ||
}); | ||
@@ -127,3 +120,3 @@ | ||
var difference = Matrix.subtract(matrix1, matrix2, matrix3, matrix4); | ||
expect(difference.rows[0][0]).to.equal(-23); | ||
expect(difference[0]).to.equal(-23); | ||
}); | ||
@@ -135,6 +128,6 @@ | ||
var matrix1 = new Matrix([[1, 2], [3, 4]]); | ||
var matrix2 = new Matrix([[2, 4], [6, 8]]); | ||
var matrix3 = new Matrix([[3, 6], [9, 12]]); | ||
var matrix4 = new Matrix([[8, 10, 12]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 2, 3, 4]); | ||
var matrix2 = new Matrix(2, 2).setData([2, 4, 6, 8]); | ||
var matrix3 = new Matrix(2, 2).setData([3, 6, 9, 12]); | ||
var matrix4 = new Matrix(1, 3).setData([8, 10, 12]); | ||
@@ -148,3 +141,3 @@ it('should return an instance of Matrix', function() { | ||
var product = Matrix.multiply(matrix1, matrix2); | ||
expect(product.rows[0][1]).to.equal(20); | ||
expect(product[1]).to.equal(20); | ||
}); | ||
@@ -154,6 +147,6 @@ | ||
var product = Matrix.multiply(matrix1, 3); | ||
expect(product.rows[0][0]).to.equal(3); | ||
expect(product.rows[0][1]).to.equal(6); | ||
expect(product.rows[1][0]).to.equal(9); | ||
expect(product.rows[1][1]).to.equal(12); | ||
expect(product[0]).to.equal(3); | ||
expect(product[1]).to.equal(6); | ||
expect(product[2]).to.equal(9); | ||
expect(product[3]).to.equal(12); | ||
}); | ||
@@ -163,3 +156,3 @@ | ||
var product = Matrix.multiply(matrix1, matrix4); | ||
expect(product.rows[0][1]).to.equal(2); | ||
expect(product[1]).to.equal(2); | ||
}); | ||
@@ -169,3 +162,3 @@ | ||
var product = Matrix.multiply(matrix1, matrix2, matrix3); | ||
expect(product.rows[0][1]).to.equal(324); | ||
expect(product[1]).to.equal(324); | ||
}); | ||
@@ -177,6 +170,6 @@ | ||
var matrix1 = new Matrix([[1, 0], [0, 1]]); | ||
var matrix2 = new Matrix([[2, 0], [0, 2]]); | ||
var matrix3 = new Matrix([[4, 0], [0, 4]]); | ||
var matrix4 = new Matrix([[2, 0, 1], [0, 2, 1]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 0, 0, 1]); | ||
var matrix2 = new Matrix(2, 2).setData([2, 0, 0, 2]); | ||
var matrix3 = new Matrix(2, 2).setData([4, 0, 0, 4]); | ||
var matrix4 = new Matrix(2, 3).setData([2, 0, 1, 0, 2, 1]); | ||
@@ -190,3 +183,3 @@ it('should return an instance of Matrix', function() { | ||
var quotient = Matrix.divide(matrix1, matrix2); | ||
expect(quotient.rows[0][0]).to.equal(0.5); | ||
expect(quotient[0]).to.equal(0.5); | ||
}); | ||
@@ -196,3 +189,3 @@ | ||
var quotient = Matrix.divide(matrix1, matrix4); | ||
expect(quotient.rows[0][0]).to.equal(1); | ||
expect(quotient[0]).to.equal(1); | ||
}); | ||
@@ -202,3 +195,3 @@ | ||
var quotient = Matrix.divide(matrix1, matrix2, matrix3); | ||
expect(quotient.rows[0][0]).to.equal(0.125); | ||
expect(quotient[0]).to.equal(0.125); | ||
}); | ||
@@ -208,5 +201,200 @@ | ||
describe('#setEmptyData()', function() { | ||
var matrix1 = new Matrix(2, 2); | ||
it('should set all values to 0', function() { | ||
expect(matrix1[0]).to.equal(1); | ||
expect(matrix1[1]).to.equal(0); | ||
expect(matrix1[2]).to.equal(0); | ||
expect(matrix1[3]).to.equal(1); | ||
matrix1.setEmptyData(); | ||
expect(matrix1[0]).to.equal(0); | ||
expect(matrix1[1]).to.equal(0); | ||
expect(matrix1[2]).to.equal(0); | ||
expect(matrix1[3]).to.equal(0); | ||
}); | ||
}); | ||
describe('#setIdentityData()', function() { | ||
var matrix1 = new Matrix(2, 2, false); | ||
matrix1.setEmptyData(); | ||
it('should set all values to 0', function() { | ||
expect(matrix1[0]).to.equal(0); | ||
expect(matrix1[1]).to.equal(0); | ||
expect(matrix1[2]).to.equal(0); | ||
expect(matrix1[3]).to.equal(0); | ||
matrix1.setIdentityData(); | ||
expect(matrix1[0]).to.equal(1); | ||
expect(matrix1[1]).to.equal(0); | ||
expect(matrix1[2]).to.equal(0); | ||
expect(matrix1[3]).to.equal(1); | ||
}); | ||
}); | ||
describe('#setData()', function() { | ||
it('should set the passed in values as data', function() { | ||
var matrix1 = new Matrix(2, 2, false); | ||
expect(matrix1[0]).to.equal(undefined); | ||
expect(matrix1[1]).to.equal(undefined); | ||
expect(matrix1[2]).to.equal(undefined); | ||
expect(matrix1[3]).to.equal(undefined); | ||
matrix1.setData([4, 6, 7, 8]); | ||
expect(matrix1[0]).to.equal(4); | ||
expect(matrix1[1]).to.equal(6); | ||
expect(matrix1[2]).to.equal(7); | ||
expect(matrix1[3]).to.equal(8); | ||
}); | ||
it('should set the passed in values as data (values taken from multiple arguments)', function() { | ||
var matrix1 = new Matrix(2, 2, false); | ||
expect(matrix1[0]).to.equal(undefined); | ||
expect(matrix1[1]).to.equal(undefined); | ||
expect(matrix1[2]).to.equal(undefined); | ||
expect(matrix1[3]).to.equal(undefined); | ||
matrix1.setData(4, 6, 7, 8); | ||
expect(matrix1[0]).to.equal(4); | ||
expect(matrix1[1]).to.equal(6); | ||
expect(matrix1[2]).to.equal(7); | ||
expect(matrix1[3]).to.equal(8); | ||
}); | ||
it('should not set the values if the length is different, and the values come from multiple arguments', function() { | ||
var matrix1 = new Matrix(2, 2, false); | ||
expect(matrix1[0]).to.equal(undefined); | ||
expect(matrix1[1]).to.equal(undefined); | ||
expect(matrix1[2]).to.equal(undefined); | ||
expect(matrix1[3]).to.equal(undefined); | ||
matrix1.setData(4, 6, 7, 8, 9, 3); | ||
expect(matrix1[0]).to.equal(undefined); | ||
expect(matrix1[1]).to.equal(undefined); | ||
expect(matrix1[2]).to.equal(undefined); | ||
expect(matrix1[3]).to.equal(undefined); | ||
}); | ||
it('should not set the values if the length is different, but no size hint provided', function() { | ||
var matrix1 = new Matrix(2, 2, false); | ||
expect(matrix1[0]).to.equal(undefined); | ||
expect(matrix1[1]).to.equal(undefined); | ||
expect(matrix1[2]).to.equal(undefined); | ||
expect(matrix1[3]).to.equal(undefined); | ||
matrix1.setData([4, 6, 7, 8, 9, 3]); | ||
expect(matrix1[0]).to.equal(undefined); | ||
expect(matrix1[1]).to.equal(undefined); | ||
expect(matrix1[2]).to.equal(undefined); | ||
expect(matrix1[3]).to.equal(undefined); | ||
}); | ||
it('should set the values if the length is different, and size hint provided', function() { | ||
var matrix1 = new Matrix(2, 2, false); | ||
expect(matrix1[0]).to.equal(undefined); | ||
expect(matrix1[1]).to.equal(undefined); | ||
expect(matrix1[2]).to.equal(undefined); | ||
expect(matrix1[3]).to.equal(undefined); | ||
matrix1.setData([4, 6, 7, 8, 9, 3], 3, 2); | ||
expect(matrix1[0]).to.equal(4); | ||
expect(matrix1[1]).to.equal(6); | ||
expect(matrix1[2]).to.equal(7); | ||
expect(matrix1[3]).to.equal(8); | ||
expect(matrix1[4]).to.equal(9); | ||
expect(matrix1[5]).to.equal(3); | ||
expect(matrix1.length).to.equal(6); | ||
expect(matrix1.rows).to.equal(3); | ||
expect(matrix1.cols).to.equal(2); | ||
}); | ||
it('should clean out previous data', function() { | ||
var matrix1 = new Matrix(3, 3); | ||
expect(matrix1[0]).to.equal(1); | ||
expect(matrix1[1]).to.equal(0); | ||
expect(matrix1[2]).to.equal(0); | ||
expect(matrix1[3]).to.equal(0); | ||
expect(matrix1[4]).to.equal(1); | ||
expect(matrix1[5]).to.equal(0); | ||
expect(matrix1[6]).to.equal(0); | ||
expect(matrix1[7]).to.equal(0); | ||
expect(matrix1[8]).to.equal(1); | ||
matrix1.setData([4, 6, 7, 8], 2, 2); | ||
expect(matrix1[0]).to.equal(4); | ||
expect(matrix1[1]).to.equal(6); | ||
expect(matrix1[2]).to.equal(7); | ||
expect(matrix1[3]).to.equal(8); | ||
expect(matrix1[4]).to.equal(undefined); | ||
expect(matrix1[5]).to.equal(undefined); | ||
expect(matrix1[6]).to.equal(undefined); | ||
expect(matrix1[7]).to.equal(undefined); | ||
expect(matrix1[8]).to.equal(undefined); | ||
expect(matrix1.length).to.equal(4); | ||
expect(matrix1.rows).to.equal(2); | ||
expect(matrix1.cols).to.equal(2); | ||
}); | ||
}); | ||
describe('#getData()', function() { | ||
var matrix1 = new Matrix(2, 2); | ||
it('should get all the values as an array', function() { | ||
var data = matrix1.getData(); | ||
expect(data[0]).to.equal(1); | ||
expect(data[1]).to.equal(0); | ||
expect(data[2]).to.equal(0); | ||
expect(data[3]).to.equal(1); | ||
}); | ||
it('should expose the number of rows and columns on the array', function() { | ||
var data = matrix1.getData(); | ||
expect(data.rows).to.equal(2); | ||
expect(data.cols).to.equal(2); | ||
}); | ||
}); | ||
describe('#toArray()', function() { | ||
var matrix1 = new Matrix(2, 2); | ||
it('should get all the values as an array', function() { | ||
var data = matrix1.toArray(); | ||
expect(data).to.eql([1, 0, 0, 1]); | ||
}); | ||
}); | ||
describe('#clone()', function() { | ||
var matrix1 = new Matrix([[1, 0], [0, 1]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 0, 0, 1]); | ||
var clone = matrix1.clone(); | ||
@@ -222,12 +410,7 @@ | ||
it('should return an instance with new array objects', function() { | ||
expect(clone.rows).to.not.equal(matrix1.rows); | ||
expect(clone.rows[0]).to.not.equal(matrix1.rows[0]); | ||
}); | ||
it('should return an instance with the same data', function() { | ||
expect(clone.rows[0][0]).to.equal(matrix1.rows[0][0]); | ||
expect(clone.rows[0][1]).to.equal(matrix1.rows[0][1]); | ||
expect(clone.rows[1][0]).to.equal(matrix1.rows[1][0]); | ||
expect(clone.rows[1][1]).to.equal(matrix1.rows[1][1]); | ||
expect(clone[0]).to.equal(matrix1[0]); | ||
expect(clone[1]).to.equal(matrix1[1]); | ||
expect(clone[2]).to.equal(matrix1[2]); | ||
expect(clone[3]).to.equal(matrix1[3]); | ||
}); | ||
@@ -240,34 +423,34 @@ | ||
it('should add another matrix of the same size', function() { | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[2, 4, 6]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(1, 3).setData([2, 4, 6]); | ||
matrix1.add(matrix2); | ||
expect(matrix1.rows[0][0]).to.equal(3); | ||
expect(matrix1[0]).to.equal(3); | ||
}); | ||
it('should not add a matrix with a different number of rows', function() { | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[2, 4, 6], [2, 4, 8]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(2, 3).setData([2, 4, 6, 2, 4, 8]); | ||
matrix1.add(matrix2); | ||
expect(matrix1.rows[0][0]).to.equal(1); | ||
expect(matrix1[0]).to.equal(1); | ||
}); | ||
it('should not add a matrix with a different number of columns', function() { | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[4, 6]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(1, 2).setData([4, 6]); | ||
matrix1.add(matrix2); | ||
expect(matrix1.rows[0][0]).to.equal(1); | ||
expect(matrix1[0]).to.equal(1); | ||
}); | ||
it('should add together all matrices passed in to the method (if same size)', function() { | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[2, 4, 6]]); | ||
var matrix3 = new Matrix([[8, 10, 12]]); | ||
var matrix4 = new Matrix([[14, 16, 18]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(1, 3).setData([2, 4, 6]); | ||
var matrix3 = new Matrix(1, 3).setData([8, 10, 12]); | ||
var matrix4 = new Matrix(1, 3).setData([14, 16, 18]); | ||
matrix1.add(matrix2, matrix3, matrix4); | ||
expect(matrix1.rows[0][0]).to.equal(25); | ||
expect(matrix1[0]).to.equal(25); | ||
}); | ||
it('should return the instance', function() { | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[2, 4, 6]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(1, 3).setData([2, 4, 6]); | ||
var returnValue = matrix1.add(matrix2); | ||
@@ -282,34 +465,34 @@ expect(returnValue).to.equal(matrix1); | ||
it('should subtract another matrix of same size', function() { | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[2, 4, 6]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(1, 3).setData([2, 4, 6]); | ||
matrix1.subtract(matrix2); | ||
expect(matrix1.rows[0][1]).to.equal(-2); | ||
expect(matrix1[1]).to.equal(-2); | ||
}); | ||
it('should not subtract a matrix with a different number of rows', function() { | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[2, 4, 6], [2, 4, 8]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(2, 3).setData([2, 4, 6, 2, 4, 8]); | ||
matrix1.subtract(matrix2); | ||
expect(matrix1.rows[0][0]).to.equal(1); | ||
expect(matrix1[0]).to.equal(1); | ||
}); | ||
it('should not subtract a matrix with a different number of columns', function() { | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[4, 6]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(1, 2).setData([4, 6]); | ||
matrix1.subtract(matrix2); | ||
expect(matrix1.rows[0][0]).to.equal(1); | ||
expect(matrix1[0]).to.equal(1); | ||
}); | ||
it('should subtract all matrices passed in to the method (if same size)', function() { | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[2, 4, 6]]); | ||
var matrix3 = new Matrix([[8, 10, 12]]); | ||
var matrix4 = new Matrix([[14, 16, 18]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(1, 3).setData([2, 4, 6]); | ||
var matrix3 = new Matrix(1, 3).setData([8, 10, 12]); | ||
var matrix4 = new Matrix(1, 3).setData([14, 16, 18]); | ||
matrix1.subtract(matrix2, matrix3, matrix4); | ||
expect(matrix1.rows[0][0]).to.equal(-23); | ||
expect(matrix1[0]).to.equal(-23); | ||
}); | ||
it('should return the instance', function() { | ||
var matrix1 = new Matrix([[1, 2, 3]]); | ||
var matrix2 = new Matrix([[2, 4, 6]]); | ||
var matrix1 = new Matrix(1, 3).setData([1, 2, 3]); | ||
var matrix2 = new Matrix(1, 3).setData([2, 4, 6]); | ||
var returnValue = matrix1.subtract(matrix2); | ||
@@ -324,41 +507,41 @@ expect(returnValue).to.equal(matrix1); | ||
it('should multiply another square matrix of the same size', function() { | ||
var matrix1 = new Matrix([[1, 2], [3, 4]]); | ||
var matrix2 = new Matrix([[2, 4], [6, 8]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 2, 3, 4]); | ||
var matrix2 = new Matrix(2, 2).setData([2, 4, 6, 8]); | ||
matrix1.multiply(matrix2); | ||
expect(matrix1.rows[0][1]).to.equal(20); | ||
expect(matrix1[1]).to.equal(20); | ||
}); | ||
it('should multiply the matrix with a number', function() { | ||
var matrix1 = new Matrix([[1, 2], [3, 4]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 2, 3, 4]); | ||
matrix1.multiply(3); | ||
expect(matrix1.rows[0][0]).to.equal(3); | ||
expect(matrix1.rows[0][1]).to.equal(6); | ||
expect(matrix1.rows[1][0]).to.equal(9); | ||
expect(matrix1.rows[1][1]).to.equal(12); | ||
expect(matrix1[0]).to.equal(3); | ||
expect(matrix1[1]).to.equal(6); | ||
expect(matrix1[2]).to.equal(9); | ||
expect(matrix1[3]).to.equal(12); | ||
}); | ||
it('should not multiply a matrix where the number of columns in first one does not match rows in second', function() { | ||
var matrix1 = new Matrix([[1, 2], [3, 4]]); | ||
var matrix2 = new Matrix([[8, 10, 12]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 2, 3, 4]); | ||
var matrix2 = new Matrix(1, 3).setData([8, 10, 12]); | ||
matrix1.multiply(matrix2); | ||
expect(matrix1.rows[0][1]).to.equal(2); | ||
expect(matrix1[1]).to.equal(2); | ||
}); | ||
it('should multiply all matrices passed in to the method', function() { | ||
var matrix1 = new Matrix([[1, 2], [3, 4]]); | ||
var matrix2 = new Matrix([[2, 4], [6, 8]]); | ||
var matrix3 = new Matrix([[3, 6], [9, 12]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 2, 3, 4]); | ||
var matrix2 = new Matrix(2, 2).setData([2, 4, 6, 8]); | ||
var matrix3 = new Matrix(2, 2).setData([3, 6, 9, 12]); | ||
matrix1.multiply(matrix2, matrix3); | ||
expect(matrix1.rows[0][1]).to.equal(324); | ||
expect(matrix1[1]).to.equal(324); | ||
}); | ||
it('should multiply a number', function() { | ||
var matrix1 = new Matrix([[1, 2], [3, 4]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 2, 3, 4]); | ||
matrix1.multiply(3); | ||
expect(matrix1.rows[0][1]).to.equal(6); | ||
expect(matrix1[1]).to.equal(6); | ||
}); | ||
it('should return the instance', function() { | ||
var matrix1 = new Matrix([[1, 2], [3, 4]]); | ||
var matrix2 = new Matrix([[2, 4], [6, 8]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 2, 3, 4]); | ||
var matrix2 = new Matrix(2, 2).setData([2, 4, 6, 8]); | ||
var returnValue = matrix1.multiply(matrix2); | ||
@@ -373,26 +556,26 @@ expect(returnValue).to.equal(matrix1); | ||
it('should divide another square matrix of the same size', function() { | ||
var matrix1 = new Matrix([[1, 0], [0, 1]]); | ||
var matrix2 = new Matrix([[2, 0], [0, 2]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 0, 0, 1]); | ||
var matrix2 = new Matrix(2, 2).setData([2, 0, 0, 2]); | ||
matrix1.divide(matrix2); | ||
expect(matrix1.rows[0][0]).to.equal(0.5); | ||
expect(matrix1[0]).to.equal(0.5); | ||
}); | ||
it('should not divide a matrix that is not square', function() { | ||
var matrix1 = new Matrix([[1, 0], [0, 1]]); | ||
var matrix2 = new Matrix([[2, 0, 1], [0, 2, 1]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 0, 0, 1]); | ||
var matrix2 = new Matrix(2, 3).setData([2, 0, 1, 0, 2, 1]); | ||
matrix1.divide(matrix2); | ||
expect(matrix1.rows[0][0]).to.equal(1); | ||
expect(matrix1[0]).to.equal(1); | ||
}); | ||
it('should divide all matrices passed in to the method', function() { | ||
var matrix1 = new Matrix([[1, 0], [0, 1]]); | ||
var matrix2 = new Matrix([[2, 0], [0, 2]]); | ||
var matrix3 = new Matrix([[4, 0], [0, 4]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 0, 0, 1]); | ||
var matrix2 = new Matrix(2, 2).setData([2, 0, 0, 2]); | ||
var matrix3 = new Matrix(2, 2).setData([4, 0, 0, 4]); | ||
matrix1.divide(matrix2, matrix3); | ||
expect(matrix1.rows[0][0]).to.equal(0.125); | ||
expect(matrix1[0]).to.equal(0.125); | ||
}); | ||
it('should return the instance', function() { | ||
var matrix1 = new Matrix([[1, 0], [0, 1]]); | ||
var matrix2 = new Matrix([[2, 0], [0, 2]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 0, 0, 1]); | ||
var matrix2 = new Matrix(2, 2).setData([2, 0, 0, 2]); | ||
var returnValue = matrix1.divide(matrix2); | ||
@@ -407,23 +590,23 @@ expect(returnValue).to.equal(matrix1); | ||
it('should raise the matrix to a given power', function() { | ||
var matrix1 = new Matrix([[1, 2], [4, 1]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 2, 4, 1]); | ||
matrix1.power(3); | ||
expect(matrix1.rows[0][0]).to.equal(25); | ||
expect(matrix1.rows[0][1]).to.equal(22); | ||
expect(matrix1.rows[1][0]).to.equal(44); | ||
expect(matrix1.rows[1][1]).to.equal(25); | ||
expect(matrix1[0]).to.equal(25); | ||
expect(matrix1[1]).to.equal(22); | ||
expect(matrix1[2]).to.equal(44); | ||
expect(matrix1[3]).to.equal(25); | ||
}); | ||
it('should not raise non-square matrices', function() { | ||
var matrix1 = new Matrix([[1, 2], [4, 1], [3, 2]]); | ||
var matrix1 = new Matrix(3, 2).setData([1, 2, 4, 1, 3, 2]); | ||
matrix1.power(3); | ||
expect(matrix1.rows.length).to.equal(3); | ||
expect(matrix1.rows[0].length).to.equal(2); | ||
expect(matrix1.rows[0][0]).to.equal(1); | ||
expect(matrix1.rows[0][1]).to.equal(2); | ||
expect(matrix1.rows[1][0]).to.equal(4); | ||
expect(matrix1.rows[1][1]).to.equal(1); | ||
expect(matrix1.rows[2][0]).to.equal(3); | ||
expect(matrix1.rows[2][1]).to.equal(2); | ||
expect(matrix1.rows).to.equal(3); | ||
expect(matrix1.cols).to.equal(2); | ||
expect(matrix1[0]).to.equal(1); | ||
expect(matrix1[1]).to.equal(2); | ||
expect(matrix1[2]).to.equal(4); | ||
expect(matrix1[3]).to.equal(1); | ||
expect(matrix1[4]).to.equal(3); | ||
expect(matrix1[5]).to.equal(2); | ||
}); | ||
@@ -435,3 +618,3 @@ | ||
var matrix1 = new Matrix([[1, 2], [4, 1]]); | ||
var matrix1 = new Matrix(2, 2).setData([1, 2, 4, 1]); | ||
@@ -441,6 +624,6 @@ it('should transpose the matrix', function() { | ||
expect(matrix1.rows[0][0]).to.equal(1); | ||
expect(matrix1.rows[0][1]).to.equal(4); | ||
expect(matrix1.rows[1][0]).to.equal(2); | ||
expect(matrix1.rows[1][1]).to.equal(1); | ||
expect(matrix1[0]).to.equal(1); | ||
expect(matrix1[1]).to.equal(4); | ||
expect(matrix1[2]).to.equal(2); | ||
expect(matrix1[3]).to.equal(1); | ||
}); | ||
@@ -458,30 +641,30 @@ | ||
it('should invert a square matrix', function() { | ||
var matrix1 = new Matrix([[3, 0, 2], [2, 0, -2], [0, 1, 1]]); | ||
var matrix1 = new Matrix(3, 3).setData([3, 0, 2, 2, 0, -2, 0, 1, 1]); | ||
matrix1.invert(); | ||
expect(matrix1.rows[0][0]).to.equal(0.2); | ||
expect(matrix1.rows[0][1]).to.equal(0.2); | ||
expect(matrix1.rows[0][2]).to.equal(0); | ||
expect(matrix1.rows[1][0]).to.equal(-0.2); | ||
expect(matrix1.rows[1][1]).to.equal(0.3); | ||
expect(matrix1.rows[1][2]).to.equal(1); | ||
expect(matrix1.rows[2][0]).to.equal(0.2); | ||
expect(matrix1.rows[2][1]).to.equal(-0.3); | ||
expect(matrix1.rows[2][2]).to.equal(0); | ||
expect(matrix1[0]).to.equal(0.2); | ||
expect(matrix1[1]).to.equal(0.2); | ||
expect(matrix1[2]).to.equal(0); | ||
expect(matrix1[3]).to.equal(-0.2); | ||
expect(matrix1[4]).to.equal(0.3); | ||
expect(matrix1[5]).to.equal(1); | ||
expect(matrix1[6]).to.equal(0.2); | ||
expect(matrix1[7]).to.equal(-0.3); | ||
expect(matrix1[8]).to.equal(0); | ||
}); | ||
it('should not invert a non-square matrix', function() { | ||
var matrix1 = new Matrix([[3, 0], [2, 0], [0, 1]]); | ||
var matrix1 = new Matrix(3, 2).setData([3, 0, 2, 0, 0, 1]); | ||
matrix1.invert(); | ||
expect(matrix1.rows[0][0]).to.equal(3); | ||
expect(matrix1.rows[0][1]).to.equal(0); | ||
expect(matrix1.rows[1][0]).to.equal(2); | ||
expect(matrix1.rows[1][1]).to.equal(0); | ||
expect(matrix1.rows[2][0]).to.equal(0); | ||
expect(matrix1.rows[2][1]).to.equal(1); | ||
expect(matrix1[0]).to.equal(3); | ||
expect(matrix1[1]).to.equal(0); | ||
expect(matrix1[2]).to.equal(2); | ||
expect(matrix1[3]).to.equal(0); | ||
expect(matrix1[4]).to.equal(0); | ||
expect(matrix1[5]).to.equal(1); | ||
}); | ||
it('should not invert a matrix whose determinant is zero', function() { | ||
var matrix1 = new Matrix([[3, 4], [6, 8]]); | ||
var matrix1 = new Matrix(2, 2).setData([3, 4, 6, 8]); | ||
var determinant = matrix1.getDeterminant(); | ||
@@ -491,10 +674,10 @@ matrix1.invert(); | ||
expect(determinant).to.equal(0); | ||
expect(matrix1.rows[0][0]).to.equal(3); | ||
expect(matrix1.rows[0][1]).to.equal(4); | ||
expect(matrix1.rows[1][0]).to.equal(6); | ||
expect(matrix1.rows[1][1]).to.equal(8); | ||
expect(matrix1[0]).to.equal(3); | ||
expect(matrix1[1]).to.equal(4); | ||
expect(matrix1[2]).to.equal(6); | ||
expect(matrix1[3]).to.equal(8); | ||
}); | ||
it('should return the instance', function() { | ||
var matrix1 = new Matrix([[3, 0, 2], [2, 0, -2], [0, 1, 1]]); | ||
var matrix1 = new Matrix(3, 3).setData([3, 0, 2, 2, 0, -2, 0, 1, 1]); | ||
var returnValue = matrix1.invert(); | ||
@@ -509,3 +692,3 @@ expect(returnValue).to.equal(matrix1); | ||
it('should return null if the matrix is not square', function() { | ||
var matrix1 = new Matrix([[3, 0], [2, 0], [0, 1]]); | ||
var matrix1 = new Matrix(3, 2).setData([3, 0, 2, 0, 0, 1]); | ||
var determinant = matrix1.getDeterminant(); | ||
@@ -516,3 +699,3 @@ expect(determinant).to.equal(null); | ||
it('should return the determinant of a 1x1 matrix', function() { | ||
var matrix1 = new Matrix([[3]]); | ||
var matrix1 = new Matrix(1, 1).setData([3]); | ||
var determinant = matrix1.getDeterminant(); | ||
@@ -523,3 +706,3 @@ expect(determinant).to.equal(3); | ||
it('should return the determinant of a 2x2 matrix', function() { | ||
var matrix1 = new Matrix([[4, 6], [3, 8]]); | ||
var matrix1 = new Matrix(2, 2).setData([4, 6, 3, 8]); | ||
var determinant = matrix1.getDeterminant(); | ||
@@ -530,3 +713,3 @@ expect(determinant).to.equal(14); | ||
it('should return the determinant of a 3x3 matrix', function() { | ||
var matrix1 = new Matrix([[6, 1, 1], [4, -2, 5], [2, 8, 7]]); | ||
var matrix1 = new Matrix(3, 3).setData([6, 1, 1, 4, -2, 5, 2, 8, 7]); | ||
var determinant = matrix1.getDeterminant(); | ||
@@ -537,3 +720,3 @@ expect(determinant).to.equal(-306); | ||
it('should return the determinant of a 4x4 (or larger) matrix', function() { | ||
var matrix1 = new Matrix([[6, 1, 1, 3], [4, -2, 5, 6], [2, 8, 7, -3], [6, 2, 4, 1]]); | ||
var matrix1 = new Matrix(4, 4).setData([6, 1, 1, 3, 4, -2, 5, 6, 2, 8, 7, -3, 6, 2, 4, 1]); | ||
var determinant = matrix1.getDeterminant(); | ||
@@ -547,15 +730,9 @@ expect(determinant).to.equal(708); | ||
var matrix1 = new Matrix([[3, 0], [2, 0], [0, 1]]); | ||
var matrix2 = new Matrix([[3, 0], [2, 0], [0, 1]]); | ||
var matrix3 = new Matrix([[3, 0], [2, 5], [0, 1]]); | ||
var matrix4 = new Matrix([[3, 0], [2, 0]]); | ||
var matrix1 = new Matrix(3, 2).setData([3, 0, 2, 0, 0, 1]); | ||
var matrix2 = new Matrix(3, 2).setData([3, 0, 2, 0, 0, 1]); | ||
var matrix3 = new Matrix(3, 2).setData([3, 0, 2, 5, 0, 1]); | ||
var matrix4 = new Matrix(2, 2).setData([3, 0, 2, 0]); | ||
var matrix1Data = [[3, 0], [2, 0], [0, 1]]; | ||
var matrix2Data = [[3, 0], [2, 0], [0, 1]]; | ||
var matrix3Data = [[3, 0], [2, 5], [0, 1]]; | ||
var matrix4Data = [[3, 0], [2, 0]]; | ||
it('should return false if the size is not the same', function() { | ||
expect(matrix1.equals(matrix4)).to.equal(false); | ||
expect(matrix1.equals(matrix4Data)).to.equal(false); | ||
}); | ||
@@ -565,3 +742,2 @@ | ||
expect(matrix1.equals(matrix3)).to.equal(false); | ||
expect(matrix1.equals(matrix3Data)).to.equal(false); | ||
}); | ||
@@ -571,3 +747,2 @@ | ||
expect(matrix1.equals(matrix2)).to.equal(true); | ||
expect(matrix1.equals(matrix2Data)).to.equal(true); | ||
}); | ||
@@ -574,0 +749,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53235
8
1106
481