Skip to content

Commit

Permalink
Make nblas a peer dependency and add explicit 'withblas' and 'without…
Browse files Browse the repository at this point in the history
…blas' entry points
  • Loading branch information
Bart van Andel committed Jan 20, 2017
1 parent 09a4182 commit 228cd30
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 77 deletions.
68 changes: 68 additions & 0 deletions applyBlasOptimizations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module.exports = function(Vector, Matrix, nblas) {
'use strict';

// BLAS optimizations
Vector.prototype.add =
Matrix.prototype.add = function (data) {
var l1 = this instanceof Vector ? this.length : this.shape[0] * this.shape[1],
l2 = data instanceof Vector ? data.length : data.shape[0] * data.shape[1];
if (l1 !== l2)
throw new Error('sizes do not match!');
if (!l1 && !l2)
return this;

nblas.axpy(data.data, this.data);
return this;
};

Vector.prototype.subtract =
Matrix.prototype.subtract = function (data) {
var l1 = this instanceof Vector ? this.length : this.shape[0] * this.shape[1],
l2 = data instanceof Vector ? data.length : data.shape[0] * data.shape[1];
if (l1 !== l2)
throw new Error('sizes do not match!');
if (!l1 && !l2)
return this;

nblas.axpy(data.data, this.data, -1);
return this;
};

Vector.prototype.scale =
Matrix.prototype.scale = function (scalar) {
nblas.scal(this.data, scalar);
return this;
};

Vector.prototype.dot = function (vector) {
if (this.length !== vector.length)
throw new Error('sizes do not match!');

return nblas.dot(this.data, vector.data);
};

Vector.prototype.magnitude = function () {
if (!this.length)
return 0;

return nblas.nrm2(this.data);
};

Vector.prototype.max = function() {
return this.data[nblas.idamax(this.length, this.data, 1)];
};

Matrix.prototype.multiply = function(matrix) {
var r1 = this.shape[0],
c1 = this.shape[1],
r2 = matrix.shape[0],
c2 = matrix.shape[1],
data = new this.type(r1 * c2);

if (c1 !== r2)
throw new Error('sizes do not match');

nblas.gemm(this.data, matrix.data, data, r1, c2, c1);
return Matrix.fromTypedArray(data, [r1, c2]);
};
};
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A high performance linear algebra library.",
"main": "vectorious.js",
"scripts": {
"test": "./node_modules/mocha/bin/mocha",
"test": "mocha",
"benchmark": "node ./benchmarks/vector.js && node ./benchmarks/matrix.js",
"coverage": "istanbul cover _mocha -- -R spec"
},
Expand All @@ -19,6 +19,9 @@
"algebra"
],
"author": "Mateo Gianolio",
"contributors": [
"Bart van Andel <bavanandel@gmail.com>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/mateogianolio/vectorious/issues"
Expand All @@ -28,7 +31,7 @@
"benchmark": "^2.1.0",
"mocha": "^2.4.5"
},
"dependencies": {
"peerDependencies": {
"nblas": "^1.2.0"
}
}
83 changes: 8 additions & 75 deletions vectorious.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,15 @@
(function () {
'use strict';

var Vector = require('./vector'),
Matrix = require('./matrix');
var Vector = module.exports.Vector = require('./vector'),
Matrix = module.exports.Matrix = require('./matrix');

try {
var nblas = require('nblas');
var nblas = module.exports.BLAS = require('nblas'),
applyBlasOptimizations = require('./applyBlasOptimizations');

applyBlasOptimizations(Vector, Matrix, nblas);
} catch (error) {
module.exports.Vector = Vector;
module.exports.Matrix = Matrix;
return;
}

// BLAS optimizations
Vector.prototype.add =
Matrix.prototype.add = function (data) {
var l1 = this instanceof Vector ? this.length : this.shape[0] * this.shape[1],
l2 = data instanceof Vector ? data.length : data.shape[0] * data.shape[1];
if (l1 !== l2)
throw new Error('sizes do not match!');
if (!l1 && !l2)
return this;

nblas.axpy(data.data, this.data);
return this;
};

Vector.prototype.subtract =
Matrix.prototype.subtract = function (data) {
var l1 = this instanceof Vector ? this.length : this.shape[0] * this.shape[1],
l2 = data instanceof Vector ? data.length : data.shape[0] * data.shape[1];
if (l1 !== l2)
throw new Error('sizes do not match!');
if (!l1 && !l2)
return this;

nblas.axpy(data.data, this.data, -1);
return this;
};

Vector.prototype.scale =
Matrix.prototype.scale = function (scalar) {
nblas.scal(this.data, scalar);
return this;
};

Vector.prototype.dot = function (vector) {
if (this.length !== vector.length)
throw new Error('sizes do not match!');

return nblas.dot(this.data, vector.data);
};

Vector.prototype.magnitude = function () {
if (!this.length)
return 0;

return nblas.nrm2(this.data);
};

Vector.prototype.max = function() {
return this.data[nblas.idamax(this.length, this.data, 1)];
};

Matrix.prototype.multiply = function(matrix) {
var r1 = this.shape[0],
c1 = this.shape[1],
r2 = matrix.shape[0],
c2 = matrix.shape[1],
data = new this.type(r1 * c2);

if (c1 !== r2)
throw new Error('sizes do not match');

nblas.gemm(this.data, matrix.data, data, r1, c2, c1);
return Matrix.fromTypedArray(data, [r1, c2]);
};

module.exports.Vector = Vector;
module.exports.Matrix = Matrix;
module.exports.BLAS = nblas;
}
}());
11 changes: 11 additions & 0 deletions withblas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function () {
'use strict';

var Vector = module.exports.Vector = require('./vector'),
Matrix = module.exports.Matrix = require('./matrix'),
nblas = module.exports.BLAS = require('nblas'),

applyBlasOptimizations = require('./applyBlasOptimizations');

applyBlasOptimizations(Vector, Matrix, nblas);
}());
6 changes: 6 additions & 0 deletions withoutblas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function () {
'use strict';

var Vector = module.exports.Vector = require('./vector'),
Matrix = module.exports.Matrix = require('./matrix');
}());

0 comments on commit 228cd30

Please sign in to comment.