linear-algebra
Advanced tools
Comparing version
{ | ||
"name": "linear-algebra", | ||
"main": "dist/linear-algebra.js", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"authors": [ | ||
"Ramesh Nair <ram@hiddentao.com>" | ||
], | ||
"description": "Efficient, high-precision linear algebra library", | ||
"description": "Efficient, high-performance linear algebra library", | ||
"moduleType": [ | ||
@@ -10,0 +10,0 @@ "amd", |
@@ -26,19 +26,2 @@ (function (root, factory) { | ||
/** | ||
* OOP inheritance. | ||
* @param {Class} ctor child | ||
* @param {Class} superCtor parent | ||
*/ | ||
var _inherits = function (ctor, superCtor) { | ||
ctor.super_ = superCtor; | ||
ctor.prototype = Object.create(superCtor.prototype, { | ||
constructor: { | ||
value: ctor, | ||
enumerable: false | ||
} | ||
}); | ||
}; | ||
/** | ||
* Initialise the linear algebra library. | ||
@@ -74,40 +57,4 @@ * | ||
// ------------------------------ | ||
// NumArray - base class for vectors and matrices | ||
// ------------------------------ | ||
/** | ||
* Construct a vector. | ||
* | ||
* @param {Array} data Array of values representing vector. | ||
* | ||
* @constructor | ||
*/ | ||
var NumArray = function(data) { | ||
this._data = data; | ||
}; | ||
/** | ||
* Size. | ||
*/ | ||
NumArray.prototype.size = function() { | ||
return this._dim; | ||
}; | ||
/** | ||
* Raw data. | ||
*/ | ||
NumArray.prototype.data = function() { | ||
return this._data; | ||
}; | ||
// ------------------------------ | ||
@@ -126,6 +73,5 @@ // Vectors | ||
var Vector = LinAlg.Vector = function(data) { | ||
NumArray.call(this, data); | ||
this._dim = data.length; | ||
this.data = data; | ||
this.size = data.length; | ||
}; | ||
_inherits(Vector, NumArray); | ||
@@ -146,6 +92,6 @@ | ||
Vector.prototype.scale = function(scale) { | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i = 0; i<this._dim; ++i) { | ||
a[i] = this._data[i] * scale; | ||
for (var i = 0; i<this.size; ++i) { | ||
a[i] = this.data[i] * scale; | ||
} | ||
@@ -164,4 +110,4 @@ | ||
Vector.prototype.scaleP = function(scale) { | ||
for (var i = 0; i<this._dim; ++i) { | ||
this._data[i] *= scale; | ||
for (var i = 0; i<this.size; ++i) { | ||
this.data[i] *= scale; | ||
} | ||
@@ -190,8 +136,7 @@ return this; | ||
var Matrix = LinAlg.Matrix = function(data) { | ||
NumArray.call(this, data); | ||
this._rows = data.length; | ||
this._cols = data[0].length; | ||
this._dim = [this._rows, this._cols]; | ||
this.data = data; | ||
this.rows = data.length; | ||
this.cols = data[0].length; | ||
this.size = [this.rows, this.cols]; | ||
}; | ||
_inherits(Matrix, NumArray); | ||
@@ -213,9 +158,9 @@ | ||
Matrix.prototype.scale = function(scale) { | ||
var a = new Array(this._rows); | ||
var a = new Array(this.rows); | ||
for (var i = 0; i<this._rows; ++i) { | ||
a[i] = new Array(this._cols); | ||
for (var i = 0; i<this.rows; ++i) { | ||
a[i] = new Array(this.cols); | ||
for (var j = 0; j<this._cols; ++j) { | ||
a[i][j] = this._data[i][j] * scale; | ||
for (var j = 0; j<this.cols; ++j) { | ||
a[i][j] = this.data[i][j] * scale; | ||
} | ||
@@ -236,9 +181,9 @@ } | ||
Matrix.prototype.scale = function(scale) { | ||
var a = new Array(this._rows); | ||
var a = new Array(this.rows); | ||
for (var i = 0; i<this._rows; ++i) { | ||
a[i] = new Array(this._cols); | ||
for (var i = 0; i<this.rows; ++i) { | ||
a[i] = new Array(this.cols); | ||
for (var j = 0; j<this._cols; ++j) { | ||
a[i][j] = this._data[i][j] * scale; | ||
for (var j = 0; j<this.cols; ++j) { | ||
a[i][j] = this.data[i][j] * scale; | ||
} | ||
@@ -258,5 +203,5 @@ } | ||
Matrix.prototype.scaleP = function(scale) { | ||
for (var i = 0; i<this._rows; ++i) { | ||
for (var j = 0; j<this._cols; ++j) { | ||
this._data[i][j] *= scale; | ||
for (var i = 0; i<this.rows; ++i) { | ||
for (var j = 0; j<this.cols; ++j) { | ||
this.data[i][j] *= scale; | ||
} | ||
@@ -277,10 +222,10 @@ } | ||
Matrix.prototype.transpose = function() { | ||
var result = new Array(this._cols), | ||
var result = new Array(this.cols), | ||
i, j; | ||
for (j=0; j<this._cols; ++j) { | ||
result[j] = new Array(this._rows); | ||
for (j=0; j<this.cols; ++j) { | ||
result[j] = new Array(this.rows); | ||
for (i=0; i<this._rows; ++i) { | ||
result[j][i] = this._data[i][j]; | ||
for (i=0; i<this.rows; ++i) { | ||
result[j][i] = this.data[i][j]; | ||
} | ||
@@ -342,3 +287,3 @@ } | ||
Vector.prototype.dot = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector dot product requires vectors to have same size'); | ||
@@ -349,4 +294,4 @@ } | ||
for (var i=0; i<this._dim; ++i) { | ||
a += this._data[i] * vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
a += this.data[i] * vector.data[i]; | ||
} | ||
@@ -365,10 +310,10 @@ | ||
Vector.prototype.minus = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector subtraction requires vectors to have same size'); | ||
} | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i=0; i<this._dim; ++i) { | ||
a[i] = this._data[i] - vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
a[i] = this.data[i] - vector.data[i]; | ||
} | ||
@@ -387,8 +332,8 @@ | ||
Vector.prototype.minusP = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector subtraction requires vectors to have same size'); | ||
} | ||
for (var i=0; i<this._dim; ++i) { | ||
this._data[i] = this._data[i] - vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
this.data[i] = this.data[i] - vector.data[i]; | ||
} | ||
@@ -409,10 +354,10 @@ | ||
Vector.prototype.plus = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector addition requires vectors to have same size'); | ||
} | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i=0; i<this._dim; ++i) { | ||
a[i] = this._data[i] + vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
a[i] = this.data[i] + vector.data[i]; | ||
} | ||
@@ -432,8 +377,8 @@ | ||
Vector.prototype.plusP = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector addition requires vectors to have same size'); | ||
} | ||
for (var i=0; i<this._dim; ++i) { | ||
this._data[i] = this._data[i] + vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
this.data[i] = this.data[i] + vector.data[i]; | ||
} | ||
@@ -462,3 +407,3 @@ | ||
Matrix.prototype.dot = function(rowNum, vector) { | ||
if (this._cols !== vector._dim) { | ||
if (this.cols !== vector.size) { | ||
_throwError('Vector dot product requires this.columns = vector.size'); | ||
@@ -469,4 +414,4 @@ } | ||
for (var j=0; j<this._cols; ++j) { | ||
a += this._data[rowNum][j] * vector._data[j]; | ||
for (var j=0; j<this.cols; ++j) { | ||
a += this.data[rowNum][j] * vector.data[j]; | ||
} | ||
@@ -490,16 +435,16 @@ | ||
if (arg.isMatrix) { | ||
if (this._cols !== arg._rows) { | ||
if (this.cols !== arg.rows) { | ||
_throwError('Multiplying by matrix requires this.columns = matrix.rows'); | ||
} | ||
result = new Array(this._rows); | ||
result = new Array(this.rows); | ||
for (i=0; i<this._rows; ++i) { | ||
result[i] = new Array(arg._cols); | ||
for (i=0; i<this.rows; ++i) { | ||
result[i] = new Array(arg.cols); | ||
for (k=0; k<arg._cols; ++k) { | ||
for (k=0; k<arg.cols; ++k) { | ||
result[i][k] = 0; | ||
for (j=0; j<this._cols; ++j) { | ||
result[i][k] += this._data[i][j] * arg._data[j][k]; | ||
for (j=0; j<this.cols; ++j) { | ||
result[i][k] += this.data[i][j] * arg.data[j][k]; | ||
} | ||
@@ -513,14 +458,14 @@ } | ||
else if (arg.isVector) { | ||
if (this._cols !== arg._dim) { | ||
if (this.cols !== arg.size) { | ||
_throwError('Multiplying by vector requires this.columns = vector.size'); | ||
} | ||
result = new Array(this._rows); | ||
result = new Array(this.rows); | ||
for (i=0; i<this._rows; ++i) { | ||
for (i=0; i<this.rows; ++i) { | ||
result[i] = 0; | ||
for (j=0; j<this._cols; ++j) { | ||
for (j=0; j<this.cols; ++j) { | ||
// store values to add in temporary array | ||
result[i] += this._data[i][j] * arg._data[j]; | ||
result[i] += this.data[i][j] * arg.data[j]; | ||
} | ||
@@ -527,0 +472,0 @@ } |
@@ -1,1 +0,1 @@ | ||
!function(t,r){"use strict";"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?module.exports=r():t.linearAlgebra=r()}(this,function(){"use strict";var t=function(t){throw new Error("linear-algebra: "+t)},r=function(t,r){t.super_=r,t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:!1}})};return function(i){i=i||{},i.add&&console.warn("linear-algebra: adder (options.add) will not be used in non-precision version");var o={},e=function(t){this._data=t};e.prototype.size=function(){return this._dim},e.prototype.data=function(){return this._data};var s=o.Vector=function(t){e.call(this,t),this._dim=t.length};r(s,e),Object.defineProperty(s.prototype,"isVector",{value:!0}),s.prototype.scale=function(t){for(var r=new Array(this._dim),i=0;i<this._dim;++i)r[i]=this._data[i]*t;return new s(r)},s.prototype.scaleP=function(t){for(var r=0;r<this._dim;++r)this._data[r]*=t;return this};var a=o.Matrix=function(t){e.call(this,t),this._rows=t.length,this._cols=t[0].length,this._dim=[this._rows,this._cols]};return r(a,e),Object.defineProperty(a.prototype,"isMatrix",{value:!0}),a.prototype.scale=function(t){for(var r=new Array(this._rows),i=0;i<this._rows;++i){r[i]=new Array(this._cols);for(var o=0;o<this._cols;++o)r[i][o]=this._data[i][o]*t}return new a(r)},a.prototype.scale=function(t){for(var r=new Array(this._rows),i=0;i<this._rows;++i){r[i]=new Array(this._cols);for(var o=0;o<this._cols;++o)r[i][o]=this._data[i][o]*t}return new a(r)},a.prototype.scaleP=function(t){for(var r=0;r<this._rows;++r)for(var i=0;i<this._cols;++i)this._data[r][i]*=t;return this},a.prototype.transpose=function(){var t,r,i=new Array(this._cols);for(r=0;r<this._cols;++r)for(i[r]=new Array(this._rows),t=0;t<this._rows;++t)i[r][t]=this._data[t][r];return new a(i)},a.identity=function(t){return a.scalar(t,1)},a.scalar=function(t,r){var i,o,e=new Array(t);for(i=0;t>i;++i){for(e[i]=new Array(t),o=0;t>o;++o)e[i][o]=0;e[i][i]=r}return new a(e)},s.prototype.dot=function(r){this._dim!==r._dim&&t("Vector dot product requires vectors to have same size");for(var i=0,o=0;o<this._dim;++o)i+=this._data[o]*r._data[o];return i},s.prototype.minus=function(r){this._dim!==r._dim&&t("Vector subtraction requires vectors to have same size");for(var i=new Array(this._dim),o=0;o<this._dim;++o)i[o]=this._data[o]-r._data[o];return new s(i)},s.prototype.minusP=function(r){this._dim!==r._dim&&t("Vector subtraction requires vectors to have same size");for(var i=0;i<this._dim;++i)this._data[i]=this._data[i]-r._data[i];return this},s.prototype.plus=function(r){this._dim!==r._dim&&t("Vector addition requires vectors to have same size");for(var i=new Array(this._dim),o=0;o<this._dim;++o)i[o]=this._data[o]+r._data[o];return new s(i)},s.prototype.plusP=function(r){this._dim!==r._dim&&t("Vector addition requires vectors to have same size");for(var i=0;i<this._dim;++i)this._data[i]=this._data[i]+r._data[i];return this},a.prototype.dot=function(r,i){this._cols!==i._dim&&t("Vector dot product requires this.columns = vector.size");for(var o=0,e=0;e<this._cols;++e)o+=this._data[r][e]*i._data[e];return o},a.prototype.mul=function(r){var i,o,e,n;if(r.isMatrix){for(this._cols!==r._rows&&t("Multiplying by matrix requires this.columns = matrix.rows"),i=new Array(this._rows),o=0;o<this._rows;++o)for(i[o]=new Array(r._cols),n=0;n<r._cols;++n)for(i[o][n]=0,e=0;e<this._cols;++e)i[o][n]+=this._data[o][e]*r._data[e][n];return new a(i)}if(r.isVector){for(this._cols!==r._dim&&t("Multiplying by vector requires this.columns = vector.size"),i=new Array(this._rows),o=0;o<this._rows;++o)for(i[o]=0,e=0;e<this._cols;++e)i[o]+=this._data[o][e]*r._data[e];return new s(i)}},o}}); | ||
!function(t,r){"use strict";"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?module.exports=r():t.linearAlgebra=r()}(this,function(){"use strict";var t=function(t){throw new Error("linear-algebra: "+t)};return function(r){r=r||{},r.add&&console.warn("linear-algebra: adder (options.add) will not be used in non-precision version");var s={},e=s.Vector=function(t){this.data=t,this.size=t.length};Object.defineProperty(e.prototype,"isVector",{value:!0}),e.prototype.scale=function(t){for(var r=new Array(this.size),s=0;s<this.size;++s)r[s]=this.data[s]*t;return new e(r)},e.prototype.scaleP=function(t){for(var r=0;r<this.size;++r)this.data[r]*=t;return this};var i=s.Matrix=function(t){this.data=t,this.rows=t.length,this.cols=t[0].length,this.size=[this.rows,this.cols]};return Object.defineProperty(i.prototype,"isMatrix",{value:!0}),i.prototype.scale=function(t){for(var r=new Array(this.rows),s=0;s<this.rows;++s){r[s]=new Array(this.cols);for(var e=0;e<this.cols;++e)r[s][e]=this.data[s][e]*t}return new i(r)},i.prototype.scale=function(t){for(var r=new Array(this.rows),s=0;s<this.rows;++s){r[s]=new Array(this.cols);for(var e=0;e<this.cols;++e)r[s][e]=this.data[s][e]*t}return new i(r)},i.prototype.scaleP=function(t){for(var r=0;r<this.rows;++r)for(var s=0;s<this.cols;++s)this.data[r][s]*=t;return this},i.prototype.transpose=function(){var t,r,s=new Array(this.cols);for(r=0;r<this.cols;++r)for(s[r]=new Array(this.rows),t=0;t<this.rows;++t)s[r][t]=this.data[t][r];return new i(s)},i.identity=function(t){return i.scalar(t,1)},i.scalar=function(t,r){var s,e,o=new Array(t);for(s=0;t>s;++s){for(o[s]=new Array(t),e=0;t>e;++e)o[s][e]=0;o[s][s]=r}return new i(o)},e.prototype.dot=function(r){this.size!==r.size&&t("Vector dot product requires vectors to have same size");for(var s=0,e=0;e<this.size;++e)s+=this.data[e]*r.data[e];return s},e.prototype.minus=function(r){this.size!==r.size&&t("Vector subtraction requires vectors to have same size");for(var s=new Array(this.size),i=0;i<this.size;++i)s[i]=this.data[i]-r.data[i];return new e(s)},e.prototype.minusP=function(r){this.size!==r.size&&t("Vector subtraction requires vectors to have same size");for(var s=0;s<this.size;++s)this.data[s]=this.data[s]-r.data[s];return this},e.prototype.plus=function(r){this.size!==r.size&&t("Vector addition requires vectors to have same size");for(var s=new Array(this.size),i=0;i<this.size;++i)s[i]=this.data[i]+r.data[i];return new e(s)},e.prototype.plusP=function(r){this.size!==r.size&&t("Vector addition requires vectors to have same size");for(var s=0;s<this.size;++s)this.data[s]=this.data[s]+r.data[s];return this},i.prototype.dot=function(r,s){this.cols!==s.size&&t("Vector dot product requires this.columns = vector.size");for(var e=0,i=0;i<this.cols;++i)e+=this.data[r][i]*s.data[i];return e},i.prototype.mul=function(r){var s,o,a,n;if(r.isMatrix){for(this.cols!==r.rows&&t("Multiplying by matrix requires this.columns = matrix.rows"),s=new Array(this.rows),o=0;o<this.rows;++o)for(s[o]=new Array(r.cols),n=0;n<r.cols;++n)for(s[o][n]=0,a=0;a<this.cols;++a)s[o][n]+=this.data[o][a]*r.data[a][n];return new i(s)}if(r.isVector){for(this.cols!==r.size&&t("Multiplying by vector requires this.columns = vector.size"),s=new Array(this.rows),o=0;o<this.rows;++o)for(s[o]=0,a=0;a<this.cols;++a)s[o]+=this.data[o][a]*r.data[a];return new e(s)}},s}}); |
@@ -26,19 +26,2 @@ (function (root, factory) { | ||
/** | ||
* OOP inheritance. | ||
* @param {Class} ctor child | ||
* @param {Class} superCtor parent | ||
*/ | ||
var _inherits = function (ctor, superCtor) { | ||
ctor.super_ = superCtor; | ||
ctor.prototype = Object.create(superCtor.prototype, { | ||
constructor: { | ||
value: ctor, | ||
enumerable: false | ||
} | ||
}); | ||
}; | ||
/** | ||
* Initialise the linear algebra library. | ||
@@ -76,40 +59,4 @@ * | ||
// ------------------------------ | ||
// NumArray - base class for vectors and matrices | ||
// ------------------------------ | ||
/** | ||
* Construct a vector. | ||
* | ||
* @param {Array} data Array of values representing vector. | ||
* | ||
* @constructor | ||
*/ | ||
var NumArray = function(data) { | ||
this._data = data; | ||
}; | ||
/** | ||
* Size. | ||
*/ | ||
NumArray.prototype.size = function() { | ||
return this._dim; | ||
}; | ||
/** | ||
* Raw data. | ||
*/ | ||
NumArray.prototype.data = function() { | ||
return this._data; | ||
}; | ||
// ------------------------------ | ||
@@ -128,6 +75,5 @@ // Vectors | ||
var Vector = LinAlg.Vector = function(data) { | ||
NumArray.call(this, data); | ||
this._dim = data.length; | ||
this.data = data; | ||
this.size = data.length; | ||
}; | ||
_inherits(Vector, NumArray); | ||
@@ -148,6 +94,6 @@ | ||
Vector.prototype.scale = function(scale) { | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i = 0; i<this._dim; ++i) { | ||
a[i] = this._data[i] * scale; | ||
for (var i = 0; i<this.size; ++i) { | ||
a[i] = this.data[i] * scale; | ||
} | ||
@@ -166,4 +112,4 @@ | ||
Vector.prototype.scaleP = function(scale) { | ||
for (var i = 0; i<this._dim; ++i) { | ||
this._data[i] *= scale; | ||
for (var i = 0; i<this.size; ++i) { | ||
this.data[i] *= scale; | ||
} | ||
@@ -192,8 +138,7 @@ return this; | ||
var Matrix = LinAlg.Matrix = function(data) { | ||
NumArray.call(this, data); | ||
this._rows = data.length; | ||
this._cols = data[0].length; | ||
this._dim = [this._rows, this._cols]; | ||
this.data = data; | ||
this.rows = data.length; | ||
this.cols = data[0].length; | ||
this.size = [this.rows, this.cols]; | ||
}; | ||
_inherits(Matrix, NumArray); | ||
@@ -215,9 +160,9 @@ | ||
Matrix.prototype.scale = function(scale) { | ||
var a = new Array(this._rows); | ||
var a = new Array(this.rows); | ||
for (var i = 0; i<this._rows; ++i) { | ||
a[i] = new Array(this._cols); | ||
for (var i = 0; i<this.rows; ++i) { | ||
a[i] = new Array(this.cols); | ||
for (var j = 0; j<this._cols; ++j) { | ||
a[i][j] = this._data[i][j] * scale; | ||
for (var j = 0; j<this.cols; ++j) { | ||
a[i][j] = this.data[i][j] * scale; | ||
} | ||
@@ -238,9 +183,9 @@ } | ||
Matrix.prototype.scale = function(scale) { | ||
var a = new Array(this._rows); | ||
var a = new Array(this.rows); | ||
for (var i = 0; i<this._rows; ++i) { | ||
a[i] = new Array(this._cols); | ||
for (var i = 0; i<this.rows; ++i) { | ||
a[i] = new Array(this.cols); | ||
for (var j = 0; j<this._cols; ++j) { | ||
a[i][j] = this._data[i][j] * scale; | ||
for (var j = 0; j<this.cols; ++j) { | ||
a[i][j] = this.data[i][j] * scale; | ||
} | ||
@@ -260,5 +205,5 @@ } | ||
Matrix.prototype.scaleP = function(scale) { | ||
for (var i = 0; i<this._rows; ++i) { | ||
for (var j = 0; j<this._cols; ++j) { | ||
this._data[i][j] *= scale; | ||
for (var i = 0; i<this.rows; ++i) { | ||
for (var j = 0; j<this.cols; ++j) { | ||
this.data[i][j] *= scale; | ||
} | ||
@@ -279,10 +224,10 @@ } | ||
Matrix.prototype.transpose = function() { | ||
var result = new Array(this._cols), | ||
var result = new Array(this.cols), | ||
i, j; | ||
for (j=0; j<this._cols; ++j) { | ||
result[j] = new Array(this._rows); | ||
for (j=0; j<this.cols; ++j) { | ||
result[j] = new Array(this.rows); | ||
for (i=0; i<this._rows; ++i) { | ||
result[j][i] = this._data[i][j]; | ||
for (i=0; i<this.rows; ++i) { | ||
result[j][i] = this.data[i][j]; | ||
} | ||
@@ -344,10 +289,10 @@ } | ||
Vector.prototype.dot = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector dot product requires vectors to have same size'); | ||
} | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i=0; i<this._dim; ++i) { | ||
a[i] = this._data[i] * vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
a[i] = this.data[i] * vector.data[i]; | ||
} | ||
@@ -366,10 +311,10 @@ | ||
Vector.prototype.minus = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector subtraction requires vectors to have same size'); | ||
} | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i=0; i<this._dim; ++i) { | ||
a[i] = adder([this._data[i], -vector._data[i]]); | ||
for (var i=0; i<this.size; ++i) { | ||
a[i] = adder([this.data[i], -vector.data[i]]); | ||
} | ||
@@ -388,8 +333,8 @@ | ||
Vector.prototype.minusP = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector subtraction requires vectors to have same size'); | ||
} | ||
for (var i=0; i<this._dim; ++i) { | ||
this._data[i] = adder([this._data[i], -vector._data[i]]); | ||
for (var i=0; i<this.size; ++i) { | ||
this.data[i] = adder([this.data[i], -vector.data[i]]); | ||
} | ||
@@ -410,10 +355,10 @@ | ||
Vector.prototype.plus = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector addition requires vectors to have same size'); | ||
} | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i=0; i<this._dim; ++i) { | ||
a[i] = adder([ this._data[i], vector._data[i] ]); | ||
for (var i=0; i<this.size; ++i) { | ||
a[i] = adder([ this.data[i], vector.data[i] ]); | ||
} | ||
@@ -433,8 +378,8 @@ | ||
Vector.prototype.plusP = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector addition requires vectors to have same size'); | ||
} | ||
for (var i=0; i<this._dim; ++i) { | ||
this._data[i] = adder([ this._data[i], vector._data[i] ]); | ||
for (var i=0; i<this.size; ++i) { | ||
this.data[i] = adder([ this.data[i], vector.data[i] ]); | ||
} | ||
@@ -464,10 +409,10 @@ | ||
Matrix.prototype.dot = function(rowNum, vector) { | ||
if (this._cols !== vector._dim) { | ||
if (this.cols !== vector.size) { | ||
_throwError('Vector dot product requires this.columns = vector.size'); | ||
} | ||
var a = new Array(this._cols); | ||
var a = new Array(this.cols); | ||
for (var j=0; j<this._cols; ++j) { | ||
a[j] = this._data[rowNum][j] * vector._data[j]; | ||
for (var j=0; j<this.cols; ++j) { | ||
a[j] = this.data[rowNum][j] * vector.data[j]; | ||
} | ||
@@ -492,16 +437,16 @@ | ||
if (arg.isMatrix) { | ||
if (this._cols !== arg._rows) { | ||
if (this.cols !== arg.rows) { | ||
_throwError('Multiplying by matrix requires this.columns = matrix.rows'); | ||
} | ||
result = new Array(this._rows); | ||
tmp = new Array(this._cols); | ||
result = new Array(this.rows); | ||
tmp = new Array(this.cols); | ||
for (i=0; i<this._rows; ++i) { | ||
result[i] = new Array(arg._cols); | ||
for (i=0; i<this.rows; ++i) { | ||
result[i] = new Array(arg.cols); | ||
for (k=0; k<arg._cols; ++k) { | ||
for (k=0; k<arg.cols; ++k) { | ||
for (j=0; j<this._cols; ++j) { | ||
tmp[j] = this._data[i][j] * arg._data[j][k]; | ||
for (j=0; j<this.cols; ++j) { | ||
tmp[j] = this.data[i][j] * arg.data[j][k]; | ||
} | ||
@@ -517,14 +462,14 @@ | ||
else if (arg.isVector) { | ||
if (this._cols !== arg._dim) { | ||
if (this.cols !== arg.size) { | ||
_throwError('Multiplying by vector requires this.columns = vector.size'); | ||
} | ||
result = new Array(this._rows); | ||
tmp = new Array(arg._dim); | ||
result = new Array(this.rows); | ||
tmp = new Array(arg.size); | ||
for (i=0; i<this._rows; ++i) { | ||
for (i=0; i<this.rows; ++i) { | ||
for (j=0; j<this._cols; ++j) { | ||
for (j=0; j<this.cols; ++j) { | ||
// store values to add in temporary array | ||
tmp[j] = this._data[i][j] * arg._data[j]; | ||
tmp[j] = this.data[i][j] * arg.data[j]; | ||
} | ||
@@ -531,0 +476,0 @@ // add up the values |
@@ -1,1 +0,1 @@ | ||
!function(t,r){"use strict";"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?module.exports=r():t.linearAlgebra=r()}(this,function(){"use strict";var t=function(t){throw new Error("linear-algebra: "+t)},r=function(t,r){t.super_=r,t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:!1}})};return function(i){i=i||{};var o=i.add;o||t("options.add must be set for precision calculation");var s={},e=function(t){this._data=t};e.prototype.size=function(){return this._dim},e.prototype.data=function(){return this._data};var a=s.Vector=function(t){e.call(this,t),this._dim=t.length};r(a,e),Object.defineProperty(a.prototype,"isVector",{value:!0}),a.prototype.scale=function(t){for(var r=new Array(this._dim),i=0;i<this._dim;++i)r[i]=this._data[i]*t;return new a(r)},a.prototype.scaleP=function(t){for(var r=0;r<this._dim;++r)this._data[r]*=t;return this};var n=s.Matrix=function(t){e.call(this,t),this._rows=t.length,this._cols=t[0].length,this._dim=[this._rows,this._cols]};return r(n,e),Object.defineProperty(n.prototype,"isMatrix",{value:!0}),n.prototype.scale=function(t){for(var r=new Array(this._rows),i=0;i<this._rows;++i){r[i]=new Array(this._cols);for(var o=0;o<this._cols;++o)r[i][o]=this._data[i][o]*t}return new n(r)},n.prototype.scale=function(t){for(var r=new Array(this._rows),i=0;i<this._rows;++i){r[i]=new Array(this._cols);for(var o=0;o<this._cols;++o)r[i][o]=this._data[i][o]*t}return new n(r)},n.prototype.scaleP=function(t){for(var r=0;r<this._rows;++r)for(var i=0;i<this._cols;++i)this._data[r][i]*=t;return this},n.prototype.transpose=function(){var t,r,i=new Array(this._cols);for(r=0;r<this._cols;++r)for(i[r]=new Array(this._rows),t=0;t<this._rows;++t)i[r][t]=this._data[t][r];return new n(i)},n.identity=function(t){return n.scalar(t,1)},n.scalar=function(t,r){var i,o,s=new Array(t);for(i=0;t>i;++i){for(s[i]=new Array(t),o=0;t>o;++o)s[i][o]=0;s[i][i]=r}return new n(s)},a.prototype.dot=function(r){this._dim!==r._dim&&t("Vector dot product requires vectors to have same size");for(var i=new Array(this._dim),s=0;s<this._dim;++s)i[s]=this._data[s]*r._data[s];return o(i)},a.prototype.minus=function(r){this._dim!==r._dim&&t("Vector subtraction requires vectors to have same size");for(var i=new Array(this._dim),s=0;s<this._dim;++s)i[s]=o([this._data[s],-r._data[s]]);return new a(i)},a.prototype.minusP=function(r){this._dim!==r._dim&&t("Vector subtraction requires vectors to have same size");for(var i=0;i<this._dim;++i)this._data[i]=o([this._data[i],-r._data[i]]);return this},a.prototype.plus=function(r){this._dim!==r._dim&&t("Vector addition requires vectors to have same size");for(var i=new Array(this._dim),s=0;s<this._dim;++s)i[s]=o([this._data[s],r._data[s]]);return new a(i)},a.prototype.plusP=function(r){this._dim!==r._dim&&t("Vector addition requires vectors to have same size");for(var i=0;i<this._dim;++i)this._data[i]=o([this._data[i],r._data[i]]);return this},n.prototype.dot=function(r,i){this._cols!==i._dim&&t("Vector dot product requires this.columns = vector.size");for(var s=new Array(this._cols),e=0;e<this._cols;++e)s[e]=this._data[r][e]*i._data[e];return o(s)},n.prototype.mul=function(r){var i,s,e,c,_;if(r.isMatrix){for(this._cols!==r._rows&&t("Multiplying by matrix requires this.columns = matrix.rows"),i=new Array(this._rows),s=new Array(this._cols),e=0;e<this._rows;++e)for(i[e]=new Array(r._cols),_=0;_<r._cols;++_){for(c=0;c<this._cols;++c)s[c]=this._data[e][c]*r._data[c][_];i[e][_]=o(s)}return new n(i)}if(r.isVector){for(this._cols!==r._dim&&t("Multiplying by vector requires this.columns = vector.size"),i=new Array(this._rows),s=new Array(r._dim),e=0;e<this._rows;++e){for(c=0;c<this._cols;++c)s[c]=this._data[e][c]*r._data[c];i[e]=o(s)}return new a(i)}},s}}); | ||
!function(t,r){"use strict";"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?module.exports=r():t.linearAlgebra=r()}(this,function(){"use strict";var t=function(t){throw new Error("linear-algebra: "+t)};return function(r){r=r||{};var s=r.add;s||t("options.add must be set for precision calculation");var e={},i=e.Vector=function(t){this.data=t,this.size=t.length};Object.defineProperty(i.prototype,"isVector",{value:!0}),i.prototype.scale=function(t){for(var r=new Array(this.size),s=0;s<this.size;++s)r[s]=this.data[s]*t;return new i(r)},i.prototype.scaleP=function(t){for(var r=0;r<this.size;++r)this.data[r]*=t;return this};var o=e.Matrix=function(t){this.data=t,this.rows=t.length,this.cols=t[0].length,this.size=[this.rows,this.cols]};return Object.defineProperty(o.prototype,"isMatrix",{value:!0}),o.prototype.scale=function(t){for(var r=new Array(this.rows),s=0;s<this.rows;++s){r[s]=new Array(this.cols);for(var e=0;e<this.cols;++e)r[s][e]=this.data[s][e]*t}return new o(r)},o.prototype.scale=function(t){for(var r=new Array(this.rows),s=0;s<this.rows;++s){r[s]=new Array(this.cols);for(var e=0;e<this.cols;++e)r[s][e]=this.data[s][e]*t}return new o(r)},o.prototype.scaleP=function(t){for(var r=0;r<this.rows;++r)for(var s=0;s<this.cols;++s)this.data[r][s]*=t;return this},o.prototype.transpose=function(){var t,r,s=new Array(this.cols);for(r=0;r<this.cols;++r)for(s[r]=new Array(this.rows),t=0;t<this.rows;++t)s[r][t]=this.data[t][r];return new o(s)},o.identity=function(t){return o.scalar(t,1)},o.scalar=function(t,r){var s,e,i=new Array(t);for(s=0;t>s;++s){for(i[s]=new Array(t),e=0;t>e;++e)i[s][e]=0;i[s][s]=r}return new o(i)},i.prototype.dot=function(r){this.size!==r.size&&t("Vector dot product requires vectors to have same size");for(var e=new Array(this.size),i=0;i<this.size;++i)e[i]=this.data[i]*r.data[i];return s(e)},i.prototype.minus=function(r){this.size!==r.size&&t("Vector subtraction requires vectors to have same size");for(var e=new Array(this.size),o=0;o<this.size;++o)e[o]=s([this.data[o],-r.data[o]]);return new i(e)},i.prototype.minusP=function(r){this.size!==r.size&&t("Vector subtraction requires vectors to have same size");for(var e=0;e<this.size;++e)this.data[e]=s([this.data[e],-r.data[e]]);return this},i.prototype.plus=function(r){this.size!==r.size&&t("Vector addition requires vectors to have same size");for(var e=new Array(this.size),o=0;o<this.size;++o)e[o]=s([this.data[o],r.data[o]]);return new i(e)},i.prototype.plusP=function(r){this.size!==r.size&&t("Vector addition requires vectors to have same size");for(var e=0;e<this.size;++e)this.data[e]=s([this.data[e],r.data[e]]);return this},o.prototype.dot=function(r,e){this.cols!==e.size&&t("Vector dot product requires this.columns = vector.size");for(var i=new Array(this.cols),o=0;o<this.cols;++o)i[o]=this.data[r][o]*e.data[o];return s(i)},o.prototype.mul=function(r){var e,a,n,h,c;if(r.isMatrix){for(this.cols!==r.rows&&t("Multiplying by matrix requires this.columns = matrix.rows"),e=new Array(this.rows),a=new Array(this.cols),n=0;n<this.rows;++n)for(e[n]=new Array(r.cols),c=0;c<r.cols;++c){for(h=0;h<this.cols;++h)a[h]=this.data[n][h]*r.data[h][c];e[n][c]=s(a)}return new o(e)}if(r.isVector){for(this.cols!==r.size&&t("Multiplying by vector requires this.columns = vector.size"),e=new Array(this.rows),a=new Array(r.size),n=0;n<this.rows;++n){for(h=0;h<this.cols;++h)a[h]=this.data[n][h]*r.data[h];e[n]=s(a)}return new i(e)}},e}}); |
{ | ||
"name": "linear-algebra", | ||
"version": "1.1.0", | ||
"description": "Efficient, high-precision linear algebra library", | ||
"version": "2.0.0", | ||
"description": "Efficient, high-performance linear algebra library", | ||
"main": "index.js", | ||
@@ -43,4 +43,5 @@ "scripts": { | ||
"sinon-chai": "~2.5.0", | ||
"add": "~2.0.6" | ||
"add": "~2.0.6", | ||
"gulp-bench": "~1.0.3" | ||
} | ||
} |
@@ -11,6 +11,5 @@ # linear-algebra | ||
* Provides `Vector` and `Matrix` objects. | ||
* Uses arrays internally for high performance. | ||
* Objects are just thin wrappers around arrays for max. performance. | ||
* Can perform basic arithmetic in-place for more efficiency. | ||
* Enhanced [floating point precision](#increased-precision) | ||
* Enhanced [floating point precision](#higher-precision) | ||
* Comprehensive unit tests. | ||
@@ -52,4 +51,4 @@ * Works in node.js and in browsers. | ||
console.log( v.isVector ); // true | ||
console.log( v.size() ); // 3 | ||
console.log( v.data() ); // [1, 2, 3] | ||
console.log( v.size ); // 3 | ||
console.log( v.data ); // [1, 2, 3] | ||
@@ -59,3 +58,3 @@ // Scaling | ||
var vScaled = v.scale(2); | ||
console.log(vScaled.data()); // [2, 4, 6] | ||
console.log(vScaled.data); // [2, 4, 6] | ||
@@ -65,3 +64,3 @@ // Scaling in-place | ||
v.scaleP(2); | ||
console.log(v.data()); // [2, 4, 6] | ||
console.log(v.data); // [2, 4, 6] | ||
@@ -72,3 +71,3 @@ // Addition | ||
var v3 = v1.plus(v2); | ||
console.log( v3.data() ); // [ 1.1, 0, 7 ] | ||
console.log( v3.data ); // [ 1.1, 0, 7 ] | ||
@@ -79,3 +78,3 @@ // Addition in-place | ||
v1.plusP(v2); | ||
console.log( v1.data() ); // [ 1.1, 0, 7 ] | ||
console.log( v1.data ); // [ 1.1, 0, 7 ] | ||
@@ -86,3 +85,3 @@ // Subtraction | ||
var v3 = v1.minus(v2); | ||
console.log( v3.data() ); // [ 0.9, 4, -1 ] | ||
console.log( v3.data ); // [ 0.9, 4, -1 ] | ||
@@ -93,3 +92,3 @@ // Subtraction in-place | ||
v1.minusP(v2); | ||
console.log( v1.data() ); // [ 0.9, 4, -1 ] | ||
console.log( v1.data ); // [ 0.9, 4, -1 ] | ||
@@ -108,4 +107,6 @@ // Dot-product | ||
console.log( m.isMatrix ); // true | ||
console.log( m.size() ); // [2, 3] | ||
console.log( m.data() ); // [ [1, 2, 3], [4, 5, 6] ] | ||
console.log( m.size ); // [2, 3] | ||
console.log( m.rows ); // 2 | ||
console.log( m.cols ); // 3 | ||
console.log( m.data ); // [ [1, 2, 3], [4, 5, 6] ] | ||
@@ -115,3 +116,3 @@ // Scaling | ||
var mScaled = m.scale(2); | ||
console.log(mScaled.data()); // [ [2, 4, 6], [8, 10, 12] ] | ||
console.log(mScaled.data); // [ [2, 4, 6], [8, 10, 12] ] | ||
@@ -121,3 +122,3 @@ // Scaling in-place | ||
m.scaleP(2); | ||
console.log(m.data()); // [ [2, 4, 6], [8, 10, 12] ] | ||
console.log(m.data); // [ [2, 4, 6], [8, 10, 12] ] | ||
@@ -127,3 +128,3 @@ // Transpose | ||
var mTranspose = m.transpose(); | ||
console.log(mTranspose.data()); // [ [1, 4], [2, 5], [3, 6] ] | ||
console.log(mTranspose.data); // [ [1, 4], [2, 5], [3, 6] ] | ||
@@ -134,3 +135,3 @@ // Multiplication with a vector | ||
var m2 = m1.mul(v1); | ||
console.log(m2.data()); // [13, 32] | ||
console.log(m2.data); // [13, 32] | ||
@@ -141,3 +142,3 @@ // Multiplication with a matrix | ||
var m3 = m1.mul(m2); | ||
console.log(m3.data()); // [ [0, -6], [2, -3] ] | ||
console.log(m3.data); // [ [0, -6], [2, -3] ] | ||
@@ -156,12 +157,12 @@ // Dot-product of a specific row with a vector | ||
var m = Matrix.scalar(3, 5); | ||
console.log(m.size()); // 3 | ||
console.log(m.data()); // [ [5, 0, 0], [0, 5, 0], [0, 0, 5] ] | ||
console.log(m.size); // 3 | ||
console.log(m.data); // [ [5, 0, 0], [0, 5, 0], [0, 0, 5] ] | ||
// Create an identity matrix | ||
m = Matrix.identity(3); | ||
console.log(m.size()); // 3 | ||
console.log(m.data()); // [ [1, 0, 0], [0, 1, 0], [0, 0, 1] ] | ||
console.log(m.size); // 3 | ||
console.log(m.data); // [ [1, 0, 0], [0, 1, 0], [0, 0, 1] ] | ||
``` | ||
### Increased precision | ||
### Higher precision | ||
@@ -200,2 +201,23 @@ When adding floating point numbers together the end result is sometimes off by a minor decimal point (to see this try `0.1 + 0.2` in your JS console). | ||
## Performance | ||
To run the performance benchmarks: | ||
```bash | ||
$ npm install -g gulp | ||
$ npm install | ||
$ gulp benchmark | ||
``` | ||
Here is sample benchmark output: | ||
```bash | ||
[16:44:02] Running suite Normal vs High precision - matrix multiplication [/Users/home/dev/js/linear-algebra/benchmark/nvh-matrix-mul.js]... | ||
[16:44:08] Normal precision (5x5 matrix) x 1,156,332 ops/sec ±4.39% (88 runs sampled) | ||
[16:44:13] Normal precision (30x30 matrix) x 9,826 ops/sec ±2.15% (93 runs sampled) | ||
[16:44:19] High precision (5x5 matrix) x 167,466 ops/sec ±2.60% (91 runs sampled) | ||
[16:44:24] High precision (30x30 matrix) x 604 ops/sec ±0.67% (80 runs sampled) | ||
[16:44:24] Fastest test is Normal precision (5x5 matrix) at 6.9x faster than High precision (5x5 matrix) | ||
``` | ||
## Building | ||
@@ -202,0 +224,0 @@ |
@@ -26,19 +26,2 @@ (function (root, factory) { | ||
/** | ||
* OOP inheritance. | ||
* @param {Class} ctor child | ||
* @param {Class} superCtor parent | ||
*/ | ||
var _inherits = function (ctor, superCtor) { | ||
ctor.super_ = superCtor; | ||
ctor.prototype = Object.create(superCtor.prototype, { | ||
constructor: { | ||
value: ctor, | ||
enumerable: false | ||
} | ||
}); | ||
}; | ||
/** | ||
* Initialise the linear algebra library. | ||
@@ -45,0 +28,0 @@ * |
@@ -10,40 +10,4 @@ /** | ||
// ------------------------------ | ||
// NumArray - base class for vectors and matrices | ||
// ------------------------------ | ||
/** | ||
* Construct a vector. | ||
* | ||
* @param {Array} data Array of values representing vector. | ||
* | ||
* @constructor | ||
*/ | ||
var NumArray = function(data) { | ||
this._data = data; | ||
}; | ||
/** | ||
* Size. | ||
*/ | ||
NumArray.prototype.size = function() { | ||
return this._dim; | ||
}; | ||
/** | ||
* Raw data. | ||
*/ | ||
NumArray.prototype.data = function() { | ||
return this._data; | ||
}; | ||
// ------------------------------ | ||
@@ -62,6 +26,5 @@ // Vectors | ||
var Vector = LinAlg.Vector = function(data) { | ||
NumArray.call(this, data); | ||
this._dim = data.length; | ||
this.data = data; | ||
this.size = data.length; | ||
}; | ||
_inherits(Vector, NumArray); | ||
@@ -82,6 +45,6 @@ | ||
Vector.prototype.scale = function(scale) { | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i = 0; i<this._dim; ++i) { | ||
a[i] = this._data[i] * scale; | ||
for (var i = 0; i<this.size; ++i) { | ||
a[i] = this.data[i] * scale; | ||
} | ||
@@ -100,4 +63,4 @@ | ||
Vector.prototype.scaleP = function(scale) { | ||
for (var i = 0; i<this._dim; ++i) { | ||
this._data[i] *= scale; | ||
for (var i = 0; i<this.size; ++i) { | ||
this.data[i] *= scale; | ||
} | ||
@@ -126,8 +89,7 @@ return this; | ||
var Matrix = LinAlg.Matrix = function(data) { | ||
NumArray.call(this, data); | ||
this._rows = data.length; | ||
this._cols = data[0].length; | ||
this._dim = [this._rows, this._cols]; | ||
this.data = data; | ||
this.rows = data.length; | ||
this.cols = data[0].length; | ||
this.size = [this.rows, this.cols]; | ||
}; | ||
_inherits(Matrix, NumArray); | ||
@@ -149,9 +111,9 @@ | ||
Matrix.prototype.scale = function(scale) { | ||
var a = new Array(this._rows); | ||
var a = new Array(this.rows); | ||
for (var i = 0; i<this._rows; ++i) { | ||
a[i] = new Array(this._cols); | ||
for (var i = 0; i<this.rows; ++i) { | ||
a[i] = new Array(this.cols); | ||
for (var j = 0; j<this._cols; ++j) { | ||
a[i][j] = this._data[i][j] * scale; | ||
for (var j = 0; j<this.cols; ++j) { | ||
a[i][j] = this.data[i][j] * scale; | ||
} | ||
@@ -172,9 +134,9 @@ } | ||
Matrix.prototype.scale = function(scale) { | ||
var a = new Array(this._rows); | ||
var a = new Array(this.rows); | ||
for (var i = 0; i<this._rows; ++i) { | ||
a[i] = new Array(this._cols); | ||
for (var i = 0; i<this.rows; ++i) { | ||
a[i] = new Array(this.cols); | ||
for (var j = 0; j<this._cols; ++j) { | ||
a[i][j] = this._data[i][j] * scale; | ||
for (var j = 0; j<this.cols; ++j) { | ||
a[i][j] = this.data[i][j] * scale; | ||
} | ||
@@ -194,5 +156,5 @@ } | ||
Matrix.prototype.scaleP = function(scale) { | ||
for (var i = 0; i<this._rows; ++i) { | ||
for (var j = 0; j<this._cols; ++j) { | ||
this._data[i][j] *= scale; | ||
for (var i = 0; i<this.rows; ++i) { | ||
for (var j = 0; j<this.cols; ++j) { | ||
this.data[i][j] *= scale; | ||
} | ||
@@ -213,10 +175,10 @@ } | ||
Matrix.prototype.transpose = function() { | ||
var result = new Array(this._cols), | ||
var result = new Array(this.cols), | ||
i, j; | ||
for (j=0; j<this._cols; ++j) { | ||
result[j] = new Array(this._rows); | ||
for (j=0; j<this.cols; ++j) { | ||
result[j] = new Array(this.rows); | ||
for (i=0; i<this._rows; ++i) { | ||
result[j][i] = this._data[i][j]; | ||
for (i=0; i<this.rows; ++i) { | ||
result[j][i] = this.data[i][j]; | ||
} | ||
@@ -223,0 +185,0 @@ } |
@@ -19,3 +19,3 @@ /** | ||
Matrix.prototype.dot = function(rowNum, vector) { | ||
if (this._cols !== vector._dim) { | ||
if (this.cols !== vector.size) { | ||
_throwError('Vector dot product requires this.columns = vector.size'); | ||
@@ -26,4 +26,4 @@ } | ||
for (var j=0; j<this._cols; ++j) { | ||
a += this._data[rowNum][j] * vector._data[j]; | ||
for (var j=0; j<this.cols; ++j) { | ||
a += this.data[rowNum][j] * vector.data[j]; | ||
} | ||
@@ -47,16 +47,16 @@ | ||
if (arg.isMatrix) { | ||
if (this._cols !== arg._rows) { | ||
if (this.cols !== arg.rows) { | ||
_throwError('Multiplying by matrix requires this.columns = matrix.rows'); | ||
} | ||
result = new Array(this._rows); | ||
result = new Array(this.rows); | ||
for (i=0; i<this._rows; ++i) { | ||
result[i] = new Array(arg._cols); | ||
for (i=0; i<this.rows; ++i) { | ||
result[i] = new Array(arg.cols); | ||
for (k=0; k<arg._cols; ++k) { | ||
for (k=0; k<arg.cols; ++k) { | ||
result[i][k] = 0; | ||
for (j=0; j<this._cols; ++j) { | ||
result[i][k] += this._data[i][j] * arg._data[j][k]; | ||
for (j=0; j<this.cols; ++j) { | ||
result[i][k] += this.data[i][j] * arg.data[j][k]; | ||
} | ||
@@ -70,14 +70,14 @@ } | ||
else if (arg.isVector) { | ||
if (this._cols !== arg._dim) { | ||
if (this.cols !== arg.size) { | ||
_throwError('Multiplying by vector requires this.columns = vector.size'); | ||
} | ||
result = new Array(this._rows); | ||
result = new Array(this.rows); | ||
for (i=0; i<this._rows; ++i) { | ||
for (i=0; i<this.rows; ++i) { | ||
result[i] = 0; | ||
for (j=0; j<this._cols; ++j) { | ||
for (j=0; j<this.cols; ++j) { | ||
// store values to add in temporary array | ||
result[i] += this._data[i][j] * arg._data[j]; | ||
result[i] += this.data[i][j] * arg.data[j]; | ||
} | ||
@@ -84,0 +84,0 @@ } |
@@ -18,10 +18,10 @@ /** | ||
Matrix.prototype.dot = function(rowNum, vector) { | ||
if (this._cols !== vector._dim) { | ||
if (this.cols !== vector.size) { | ||
_throwError('Vector dot product requires this.columns = vector.size'); | ||
} | ||
var a = new Array(this._cols); | ||
var a = new Array(this.cols); | ||
for (var j=0; j<this._cols; ++j) { | ||
a[j] = this._data[rowNum][j] * vector._data[j]; | ||
for (var j=0; j<this.cols; ++j) { | ||
a[j] = this.data[rowNum][j] * vector.data[j]; | ||
} | ||
@@ -46,16 +46,16 @@ | ||
if (arg.isMatrix) { | ||
if (this._cols !== arg._rows) { | ||
if (this.cols !== arg.rows) { | ||
_throwError('Multiplying by matrix requires this.columns = matrix.rows'); | ||
} | ||
result = new Array(this._rows); | ||
tmp = new Array(this._cols); | ||
result = new Array(this.rows); | ||
tmp = new Array(this.cols); | ||
for (i=0; i<this._rows; ++i) { | ||
result[i] = new Array(arg._cols); | ||
for (i=0; i<this.rows; ++i) { | ||
result[i] = new Array(arg.cols); | ||
for (k=0; k<arg._cols; ++k) { | ||
for (k=0; k<arg.cols; ++k) { | ||
for (j=0; j<this._cols; ++j) { | ||
tmp[j] = this._data[i][j] * arg._data[j][k]; | ||
for (j=0; j<this.cols; ++j) { | ||
tmp[j] = this.data[i][j] * arg.data[j][k]; | ||
} | ||
@@ -71,14 +71,14 @@ | ||
else if (arg.isVector) { | ||
if (this._cols !== arg._dim) { | ||
if (this.cols !== arg.size) { | ||
_throwError('Multiplying by vector requires this.columns = vector.size'); | ||
} | ||
result = new Array(this._rows); | ||
tmp = new Array(arg._dim); | ||
result = new Array(this.rows); | ||
tmp = new Array(arg.size); | ||
for (i=0; i<this._rows; ++i) { | ||
for (i=0; i<this.rows; ++i) { | ||
for (j=0; j<this._cols; ++j) { | ||
for (j=0; j<this.cols; ++j) { | ||
// store values to add in temporary array | ||
tmp[j] = this._data[i][j] * arg._data[j]; | ||
tmp[j] = this.data[i][j] * arg.data[j]; | ||
} | ||
@@ -85,0 +85,0 @@ // add up the values |
@@ -13,3 +13,3 @@ /** | ||
Vector.prototype.dot = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector dot product requires vectors to have same size'); | ||
@@ -20,4 +20,4 @@ } | ||
for (var i=0; i<this._dim; ++i) { | ||
a += this._data[i] * vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
a += this.data[i] * vector.data[i]; | ||
} | ||
@@ -36,10 +36,10 @@ | ||
Vector.prototype.minus = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector subtraction requires vectors to have same size'); | ||
} | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i=0; i<this._dim; ++i) { | ||
a[i] = this._data[i] - vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
a[i] = this.data[i] - vector.data[i]; | ||
} | ||
@@ -58,8 +58,8 @@ | ||
Vector.prototype.minusP = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector subtraction requires vectors to have same size'); | ||
} | ||
for (var i=0; i<this._dim; ++i) { | ||
this._data[i] = this._data[i] - vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
this.data[i] = this.data[i] - vector.data[i]; | ||
} | ||
@@ -80,10 +80,10 @@ | ||
Vector.prototype.plus = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector addition requires vectors to have same size'); | ||
} | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i=0; i<this._dim; ++i) { | ||
a[i] = this._data[i] + vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
a[i] = this.data[i] + vector.data[i]; | ||
} | ||
@@ -103,8 +103,8 @@ | ||
Vector.prototype.plusP = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector addition requires vectors to have same size'); | ||
} | ||
for (var i=0; i<this._dim; ++i) { | ||
this._data[i] = this._data[i] + vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
this.data[i] = this.data[i] + vector.data[i]; | ||
} | ||
@@ -111,0 +111,0 @@ |
@@ -13,10 +13,10 @@ /** | ||
Vector.prototype.dot = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector dot product requires vectors to have same size'); | ||
} | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i=0; i<this._dim; ++i) { | ||
a[i] = this._data[i] * vector._data[i]; | ||
for (var i=0; i<this.size; ++i) { | ||
a[i] = this.data[i] * vector.data[i]; | ||
} | ||
@@ -35,10 +35,10 @@ | ||
Vector.prototype.minus = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector subtraction requires vectors to have same size'); | ||
} | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i=0; i<this._dim; ++i) { | ||
a[i] = adder([this._data[i], -vector._data[i]]); | ||
for (var i=0; i<this.size; ++i) { | ||
a[i] = adder([this.data[i], -vector.data[i]]); | ||
} | ||
@@ -57,8 +57,8 @@ | ||
Vector.prototype.minusP = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector subtraction requires vectors to have same size'); | ||
} | ||
for (var i=0; i<this._dim; ++i) { | ||
this._data[i] = adder([this._data[i], -vector._data[i]]); | ||
for (var i=0; i<this.size; ++i) { | ||
this.data[i] = adder([this.data[i], -vector.data[i]]); | ||
} | ||
@@ -79,10 +79,10 @@ | ||
Vector.prototype.plus = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector addition requires vectors to have same size'); | ||
} | ||
var a = new Array(this._dim); | ||
var a = new Array(this.size); | ||
for (var i=0; i<this._dim; ++i) { | ||
a[i] = adder([ this._data[i], vector._data[i] ]); | ||
for (var i=0; i<this.size; ++i) { | ||
a[i] = adder([ this.data[i], vector.data[i] ]); | ||
} | ||
@@ -102,8 +102,8 @@ | ||
Vector.prototype.plusP = function(vector) { | ||
if (this._dim !== vector._dim) { | ||
if (this.size !== vector.size) { | ||
_throwError('Vector addition requires vectors to have same size'); | ||
} | ||
for (var i=0; i<this._dim; ++i) { | ||
this._data[i] = adder([ this._data[i], vector._data[i] ]); | ||
for (var i=0; i<this.size; ++i) { | ||
this.data[i] = adder([ this.data[i], vector.data[i] ]); | ||
} | ||
@@ -110,0 +110,0 @@ |
@@ -25,12 +25,12 @@ var chai = require('chai'), | ||
'dimensions': function() { | ||
new this.Vector([]).size().should.eql(0); | ||
new this.Vector([1]).size().should.eql(1); | ||
new this.Vector([1,2]).size().should.eql(2); | ||
new this.Vector([]).size.should.eql(0); | ||
new this.Vector([1]).size.should.eql(1); | ||
new this.Vector([1,2]).size.should.eql(2); | ||
}, | ||
'data': function() { | ||
var a = []; | ||
new this.Vector(a).data().should.eql(a); | ||
new this.Vector(a).data.should.eql(a); | ||
var b = [1,2]; | ||
new this.Vector(b).data().should.eql(b); | ||
new this.Vector(b).data.should.eql(b); | ||
}, | ||
@@ -42,3 +42,3 @@ 'scale': function() { | ||
v2.should.be.instanceOf(this.Vector); | ||
v2.data().should.eql([6, 12, 18]); | ||
v2.data.should.eql([6, 12, 18]); | ||
}, | ||
@@ -48,3 +48,3 @@ 'scale in-place': function() { | ||
v.scaleP(6).should.eql(v); | ||
v.data().should.eql([6, 12, 18]); | ||
v.data.should.eql([6, 12, 18]); | ||
}, | ||
@@ -66,3 +66,3 @@ 'minus': { | ||
v3.should.be.instanceOf(this.Vector); | ||
v3.data().should.eql([ 1.1-4, 2 - 0.2 ]); | ||
v3.data.should.eql([ 1.1-4, 2 - 0.2 ]); | ||
(v3 === v1 || v3 === v2).should.be.false; | ||
@@ -86,3 +86,3 @@ } | ||
v3.should.eql(v1); | ||
v1.data().should.eql([ 1.1-4, 2 - 0.2 ]); | ||
v1.data.should.eql([ 1.1-4, 2 - 0.2 ]); | ||
} | ||
@@ -105,3 +105,3 @@ }, | ||
v3.should.be.instanceOf(this.Vector); | ||
v3.data().should.eql([ 1.1+4, 2+0.2 ]); | ||
v3.data.should.eql([ 1.1+4, 2+0.2 ]); | ||
(v3 === v1 || v3 === v2).should.be.false; | ||
@@ -125,3 +125,3 @@ } | ||
v3.should.eql(v1); | ||
v1.data().should.eql([ 1.1+4, 2+0.2 ]); | ||
v1.data.should.eql([ 1.1+4, 2+0.2 ]); | ||
} | ||
@@ -173,10 +173,13 @@ }, | ||
var a = [[]]; | ||
new this.Matrix(a).size().should.eql([1,0]); | ||
new this.Matrix(a).size.should.eql([1,0]); | ||
a = [ [1,2], [3,4], [5,6] ]; | ||
new this.Matrix(a).size().should.eql([3,2]); | ||
var m = new this.Matrix(a); | ||
m.size.should.eql([3,2]); | ||
m.rows.should.eql(3); | ||
m.cols.should.eql(2); | ||
}, | ||
'data': function() { | ||
var a = [ [1,2], [3,4], [5,6] ]; | ||
new this.Matrix(a).data().should.eql(a); | ||
new this.Matrix(a).data.should.eql(a); | ||
}, | ||
@@ -188,3 +191,3 @@ 'scale': function() { | ||
m2.should.not.eql(m); | ||
m2.data().should.eql([ [6,12], [18,24], [30,36] ]); | ||
m2.data.should.eql([ [6,12], [18,24], [30,36] ]); | ||
}, | ||
@@ -195,3 +198,3 @@ 'scale in-place': function() { | ||
m.scaleP(6).should.eql(m); | ||
m.data().should.eql([ [6,12], [18,24], [30,36] ]); | ||
m.data.should.eql([ [6,12], [18,24], [30,36] ]); | ||
}, | ||
@@ -205,3 +208,3 @@ 'transpose': function() { | ||
res.should.be.instanceOf(this.Matrix); | ||
res.data().should.eql([ [1, 3, 5], [2, 4, 6] ]); | ||
res.data.should.eql([ [1, 3, 5], [2, 4, 6] ]); | ||
}, | ||
@@ -250,3 +253,3 @@ 'dot product': { | ||
res.data().should.eql([ 2, 5, 8 ]); | ||
res.data.should.eql([ 2, 5, 8 ]); | ||
} | ||
@@ -271,3 +274,3 @@ }, | ||
res.data().should.eql([ [6, -1, 18.5, 6], [17, 5, 37.5, 20], [28, 11, 56.5, 34] ]); | ||
res.data.should.eql([ [6, -1, 18.5, 6], [17, 5, 37.5, 20], [28, 11, 56.5, 34] ]); | ||
} | ||
@@ -280,3 +283,3 @@ } | ||
m1.should.be.instanceOf(this.Matrix); | ||
m1.data().should.eql([ [1.2, 0, 0], [0, 1.2, 0], [0, 0, 1.2] ]); | ||
m1.data.should.eql([ [1.2, 0, 0], [0, 1.2, 0], [0, 0, 1.2] ]); | ||
}, | ||
@@ -287,3 +290,3 @@ 'identity': function() { | ||
m1.should.be.instanceOf(this.Matrix); | ||
m1.data().should.eql([ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]); | ||
m1.data.should.eql([ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]); | ||
} | ||
@@ -290,0 +293,0 @@ }; |
"use strict"; | ||
var _ = require('lodash'), | ||
sinon = require('sinon'), | ||
var sinon = require('sinon'), | ||
chai = require('chai'), | ||
@@ -6,0 +5,0 @@ expect = chai.expect, |
"use strict"; | ||
var sinon = require('sinon'); | ||
var chai = require('chai'), | ||
var sinon = require('sinon'), | ||
chai = require('chai'), | ||
expect = chai.expect, | ||
@@ -6,0 +6,0 @@ should = chai.should(); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26
13.04%222
11%0
-100%58638
-0.23%12
9.09%1612
-1.29%