11/**
22 * ml - Machine learning tools
3- * @version v0.2.1
3+ * @version v0.2.2
44 * @link https://github.com/mljs/ml
55 * @license MIT
66 */
@@ -2281,10 +2281,18 @@ function inverse(matrix) {
22812281 return solve ( matrix , Matrix . eye ( matrix . rows ) ) ;
22822282}
22832283
2284+ Matrix . prototype . inverse = function ( ) {
2285+ return inverse ( this ) ;
2286+ } ;
2287+
22842288function solve ( leftHandSide , rightHandSide ) {
22852289 return leftHandSide . isSquare ( ) ? new LuDecomposition ( leftHandSide ) . solve ( rightHandSide ) : new QrDecomposition ( leftHandSide ) . solve ( rightHandSide ) ;
22862290}
22872291
2292+ Matrix . prototype . solve = function ( other ) {
2293+ return solve ( this , other ) ;
2294+ } ;
2295+
22882296module . exports = {
22892297 LuDecomposition : LuDecomposition ,
22902298 LU : LuDecomposition ,
@@ -2299,6 +2307,7 @@ module.exports = {
22992307 inverse : inverse ,
23002308 solve : solve
23012309} ;
2310+
23022311} , { "./matrix" :55 } ] , 54 :[ function ( require , module , exports ) {
23032312'use strict' ;
23042313
@@ -2328,10 +2337,6 @@ MatrixError.prototype = Object.create(Error.prototype);
23282337MatrixError . prototype . name = 'MatrixError' ;
23292338MatrixError . prototype . constructor = MatrixError ;
23302339
2331- function throwError ( message ) {
2332- throw new MatrixError ( message ) ;
2333- }
2334-
23352340/**
23362341 * Real matrix.
23372342 * @constructor
@@ -2346,18 +2351,18 @@ function Matrix(nRows, nColumns) {
23462351 nRows = matrix . length ;
23472352 nColumns = matrix [ 0 ] . length ;
23482353 if ( typeof nColumns === 'undefined' ) {
2349- throwError ( 'Data must be a 2D array' ) ;
2354+ throw new MatrixError ( 'Data must be a 2D array' ) ;
23502355 }
23512356 if ( nRows > 0 && nColumns > 0 ) {
23522357 for ( ; i < nRows ; i ++ ) {
23532358 if ( matrix [ i ] . length !== nColumns ) {
2354- throwError ( 'Inconsistent array dimensions' ) ;
2359+ throw new MatrixError ( 'Inconsistent array dimensions' ) ;
23552360 } else if ( newInstance ) {
23562361 matrix [ i ] = slice ( matrix [ i ] ) ;
23572362 }
23582363 }
23592364 } else {
2360- throwError ( 'Invalid dimensions: ' + nRows + 'x' + nColumns ) ;
2365+ throw new MatrixError ( 'Invalid dimensions: ' + nRows + 'x' + nColumns ) ;
23612366 }
23622367 } else if ( typeof nRows === 'number' ) { // Create empty matrix
23632368 if ( nRows > 0 && nColumns > 0 ) {
@@ -2366,10 +2371,10 @@ function Matrix(nRows, nColumns) {
23662371 matrix [ i ] = new Array ( nColumns ) ;
23672372 }
23682373 } else {
2369- throwError ( 'Invalid dimensions: ' + nRows + 'x' + nColumns ) ;
2374+ throw new MatrixError ( 'Invalid dimensions: ' + nRows + 'x' + nColumns ) ;
23702375 }
23712376 } else {
2372- throwError ( 'Invalid arguments' )
2377+ throw new MatrixError ( 'Invalid arguments' )
23732378 }
23742379
23752380 Object . defineProperty ( matrix , 'rows' , { writable : true , value : nRows } ) ;
@@ -2392,7 +2397,7 @@ Matrix.from1DArray = function from1DArray(newRows, newColumns, newData) {
23922397
23932398 length = newRows * newColumns ;
23942399 if ( length !== newData . length )
2395- throwError ( 'Data length does not match given dimensions' ) ;
2400+ throw new MatrixError ( 'Data length does not match given dimensions' ) ;
23962401
23972402 data = new Array ( newRows ) ;
23982403 for ( ; i < newRows ; i ++ ) {
@@ -2556,7 +2561,7 @@ Matrix.expand = function expand(base, count) {
25562561 */
25572562Matrix . checkMatrix = function checkMatrix ( value ) {
25582563 if ( ! value ) {
2559- throwError ( 'Argument has to be a matrix' ) ;
2564+ throw new MatrixError ( 'Argument has to be a matrix' ) ;
25602565 }
25612566 if ( value . klass !== 'Matrix' ) {
25622567 value = new Matrix ( value ) ;
@@ -2598,7 +2603,7 @@ Object.defineProperty(Matrix.prototype, 'size', {
25982603 */
25992604Matrix . prototype . checkRowIndex = function checkRowIndex ( index ) {
26002605 if ( index < 0 || index > this . rows - 1 )
2601- throwError ( 'Row index out of range.' ) ;
2606+ throw new MatrixError ( 'Row index out of range.' ) ;
26022607} ;
26032608
26042609/**
@@ -2608,7 +2613,7 @@ Matrix.prototype.checkRowIndex = function checkRowIndex(index) {
26082613 */
26092614Matrix . prototype . checkColumnIndex = function checkColumnIndex ( index ) {
26102615 if ( index < 0 || index > this . columns - 1 )
2611- throwError ( 'Column index out of range.' ) ;
2616+ throw new MatrixError ( 'Column index out of range.' ) ;
26122617} ;
26132618
26142619/**
@@ -2618,7 +2623,7 @@ Matrix.prototype.checkColumnIndex = function checkColumnIndex(index) {
26182623 */
26192624Matrix . prototype . checkDimensions = function checkDimensions ( otherMatrix ) {
26202625 if ( ( this . rows !== otherMatrix . rows ) || ( this . columns !== otherMatrix . columns ) )
2621- throwError ( 'Matrices dimensions must be equal.' ) ;
2626+ throw new MatrixError ( 'Matrices dimensions must be equal.' ) ;
26222627} ;
26232628
26242629/**
@@ -2947,7 +2952,7 @@ Matrix.prototype.setRow = function setRow(index, array) {
29472952 this . checkRowIndex ( index ) ;
29482953 if ( Matrix . isMatrix ( array ) ) array = array . to1DArray ( ) ;
29492954 if ( array . length !== this . columns )
2950- throwError ( 'Invalid row size' ) ;
2955+ throw new MatrixError ( 'Invalid row size' ) ;
29512956 this [ index ] = slice ( array ) ;
29522957 return this ;
29532958} ;
@@ -2960,7 +2965,7 @@ Matrix.prototype.setRow = function setRow(index, array) {
29602965Matrix . prototype . removeRow = function removeRow ( index ) {
29612966 this . checkRowIndex ( index ) ;
29622967 if ( this . rows === 1 )
2963- throwError ( 'A matrix cannot have less than one row' ) ;
2968+ throw new MatrixError ( 'A matrix cannot have less than one row' ) ;
29642969 Asplice . call ( this , index , 1 ) ;
29652970 this . rows -= 1 ;
29662971 return this ;
@@ -2978,10 +2983,10 @@ Matrix.prototype.addRow = function addRow(index, array) {
29782983 index = this . rows ;
29792984 }
29802985 if ( index < 0 || index > this . rows )
2981- throwError ( 'Row index out of range.' ) ;
2986+ throw new MatrixError ( 'Row index out of range.' ) ;
29822987 if ( Matrix . isMatrix ( array ) ) array = array . to1DArray ( ) ;
29832988 if ( array . length !== this . columns )
2984- throwError ( 'Invalid row size' ) ;
2989+ throw new MatrixError ( 'Invalid row size' ) ;
29852990 Asplice . call ( this , index , 0 , slice ( array ) ) ;
29862991 this . rows += 1 ;
29872992 return this ;
@@ -3027,7 +3032,7 @@ Matrix.prototype.setColumn = function setColumn(index, array) {
30273032 if ( Matrix . isMatrix ( array ) ) array = array . to1DArray ( ) ;
30283033 var l = this . rows ;
30293034 if ( array . length !== l )
3030- throwError ( 'Invalid column size' ) ;
3035+ throw new MatrixError ( 'Invalid column size' ) ;
30313036 for ( var i = 0 ; i < l ; i ++ ) {
30323037 this [ i ] [ index ] = array [ i ] ;
30333038 }
@@ -3042,7 +3047,7 @@ Matrix.prototype.setColumn = function setColumn(index, array) {
30423047Matrix . prototype . removeColumn = function removeColumn ( index ) {
30433048 this . checkColumnIndex ( index ) ;
30443049 if ( this . columns === 1 )
3045- throwError ( 'A matrix cannot have less than one column' ) ;
3050+ throw new MatrixError ( 'A matrix cannot have less than one column' ) ;
30463051 for ( var i = 0 , ii = this . rows ; i < ii ; i ++ ) {
30473052 this [ i ] . splice ( index , 1 ) ;
30483053 }
@@ -3062,11 +3067,11 @@ Matrix.prototype.addColumn = function addColumn(index, array) {
30623067 index = this . columns ;
30633068 }
30643069 if ( index < 0 || index > this . columns )
3065- throwError ( 'Column index out of range.' ) ;
3070+ throw new MatrixError ( 'Column index out of range.' ) ;
30663071 if ( Matrix . isMatrix ( array ) ) array = array . to1DArray ( ) ;
30673072 var l = this . rows ;
30683073 if ( array . length !== l )
3069- throwError ( 'Invalid column size' ) ;
3074+ throw new MatrixError ( 'Invalid column size' ) ;
30703075 for ( var i = 0 ; i < l ; i ++ ) {
30713076 this [ i ] . splice ( index , 0 , array [ i ] ) ;
30723077 }
@@ -3104,7 +3109,7 @@ Matrix.prototype.checkRowVector = function checkRowVector(vector) {
31043109 if ( Matrix . isMatrix ( vector ) )
31053110 vector = vector . to1DArray ( ) ;
31063111 if ( vector . length !== this . columns )
3107- throwError ( 'vector size must be the same as the number of columns' ) ;
3112+ throw new MatrixError ( 'vector size must be the same as the number of columns' ) ;
31083113 return vector ;
31093114} ;
31103115
@@ -3119,7 +3124,7 @@ Matrix.prototype.checkColumnVector = function checkColumnVector(vector) {
31193124 if ( Matrix . isMatrix ( vector ) )
31203125 vector = vector . to1DArray ( ) ;
31213126 if ( vector . length !== this . rows )
3122- throwError ( 'vector size must be the same as the number of rows' ) ;
3127+ throw new MatrixError ( 'vector size must be the same as the number of rows' ) ;
31233128 return vector ;
31243129} ;
31253130
@@ -3514,7 +3519,7 @@ Matrix.prototype.minColumnIndex = function minColumnIndex(index) {
35143519 */
35153520Matrix . prototype . diag = function diag ( ) {
35163521 if ( ! this . isSquare ( ) )
3517- throwError ( 'Only square matrices have a diagonal.' ) ;
3522+ throw new MatrixError ( 'Only square matrices have a diagonal.' ) ;
35183523 var diag = new Array ( this . rows ) ;
35193524 for ( var i = 0 , ii = this . rows ; i < ii ; i ++ ) {
35203525 diag [ i ] = this [ i ] [ i ] ;
@@ -3583,7 +3588,7 @@ Matrix.prototype.cumulativeSum = function cumulativeSum() {
35833588 */
35843589Matrix . prototype . dot = function dot ( other ) {
35853590 if ( this . size !== other . size )
3586- throwError ( 'vectors do not have the same size' ) ;
3591+ throw new MatrixError ( 'vectors do not have the same size' ) ;
35873592 var vector1 = this . to1DArray ( ) ;
35883593 var vector2 = other . to1DArray ( ) ;
35893594 var dot = 0 , l = vector1 . length ;
@@ -3672,7 +3677,7 @@ Matrix.prototype.transpose = function transpose() {
36723677 */
36733678Matrix . prototype . subMatrix = function subMatrix ( startRow , endRow , startColumn , endColumn ) {
36743679 if ( ( startRow > endRow ) || ( startColumn > endColumn ) || ( startRow < 0 ) || ( startRow >= this . rows ) || ( endRow < 0 ) || ( endRow >= this . rows ) || ( startColumn < 0 ) || ( startColumn >= this . columns ) || ( endColumn < 0 ) || ( endColumn >= this . columns ) )
3675- throwError ( 'Argument out of range' ) ;
3680+ throw new MatrixError ( 'Argument out of range' ) ;
36763681 var newMatrix = new Matrix ( endRow - startRow + 1 , endColumn - startColumn + 1 ) ;
36773682 for ( var i = startRow ; i <= endRow ; i ++ ) {
36783683 for ( var j = startColumn ; j <= endColumn ; j ++ ) {
@@ -3697,13 +3702,13 @@ Matrix.prototype.subMatrixRow = function subMatrixRow(indices, startColumn, endC
36973702 endColumn = this . columns - 1 ;
36983703 }
36993704 if ( ( startColumn > endColumn ) || ( startColumn < 0 ) || ( startColumn >= this . columns ) || ( endColumn < 0 ) || ( endColumn >= this . columns ) )
3700- throwError ( 'Argument out of range.' ) ;
3705+ throw new MatrixError ( 'Argument out of range.' ) ;
37013706 var l = indices . length , rows = this . rows ,
37023707 X = new Matrix ( l , endColumn - startColumn + 1 ) ;
37033708 for ( var i = 0 ; i < l ; i ++ ) {
37043709 for ( var j = startColumn ; j <= endColumn ; j ++ ) {
37053710 if ( ( indices [ i ] < 0 ) || ( indices [ i ] >= rows ) )
3706- throwError ( 'Argument out of range.' ) ;
3711+ throw new MatrixError ( 'Argument out of range.' ) ;
37073712 X [ i ] [ j - startColumn ] = this [ indices [ i ] ] [ j ] ;
37083713 }
37093714 }
@@ -3725,13 +3730,13 @@ Matrix.prototype.subMatrixColumn = function subMatrixColumn(indices, startRow, e
37253730 endRow = this . rows - 1 ;
37263731 }
37273732 if ( ( startRow > endRow ) || ( startRow < 0 ) || ( startRow >= this . rows ) || ( endRow < 0 ) || ( endRow >= this . rows ) )
3728- throwError ( 'Argument out of range.' ) ;
3733+ throw new MatrixError ( 'Argument out of range.' ) ;
37293734 var l = indices . length , columns = this . columns ,
37303735 X = new Matrix ( endRow - startRow + 1 , l ) ;
37313736 for ( var i = 0 ; i < l ; i ++ ) {
37323737 for ( var j = startRow ; j <= endRow ; j ++ ) {
37333738 if ( ( indices [ i ] < 0 ) || ( indices [ i ] >= columns ) )
3734- throwError ( 'Argument out of range.' ) ;
3739+ throw new MatrixError ( 'Argument out of range.' ) ;
37353740 X [ j - startRow ] [ i ] = this [ j ] [ indices [ i ] ] ;
37363741 }
37373742 }
@@ -3744,7 +3749,7 @@ Matrix.prototype.subMatrixColumn = function subMatrixColumn(indices, startRow, e
37443749 */
37453750Matrix . prototype . trace = function trace ( ) {
37463751 if ( ! this . isSquare ( ) )
3747- throwError ( 'The matrix is not square' ) ;
3752+ throw new MatrixError ( 'The matrix is not square' ) ;
37483753 var trace = 0 , i = 0 , l = this . rows ;
37493754 for ( ; i < l ; i ++ ) {
37503755 trace += this [ i ] [ i ] ;
0 commit comments