Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommonJS/RequireJS/AMD support #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/intro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function (root, factory) {
if (typeof exports === 'object') {
// Node.
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
} else {
// Browser globals (root is window)
root.numeric = factory();
}
}(this, function() {

'use strict';
var numeric = function numeric(){};
69 changes: 41 additions & 28 deletions src/numeric.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var numeric = (typeof exports === "undefined")?(function numeric() {}):(exports);
if(typeof global !== "undefined") { global.numeric = numeric; }
function _fn( fn ){
fn.numeric = numeric;
return fn;
}

numeric.version = "1.2.6";

Expand Down Expand Up @@ -329,7 +329,8 @@ numeric.dim = function dim(x) {
}

numeric.mapreduce = function mapreduce(body,init) {
return Function('x','accum','_s','_k',
return _fn(Function('x','accum','_s','_k',
'var numeric = arguments.callee.numeric;\n'+
'if(typeof accum === "undefined") accum = '+init+';\n'+
'if(typeof x === "number") { var xi = x; '+body+'; return accum; }\n'+
'if(typeof _s === "undefined") _s = numeric.dim(x);\n'+
Expand All @@ -353,18 +354,19 @@ numeric.mapreduce = function mapreduce(body,init) {
' '+body+'\n'+
'}\n'+
'return accum;'
);
));
}
numeric.mapreduce2 = function mapreduce2(body,setup) {
return Function('x',
return _fn(Function('x',
'var numeric = arguments.callee.numeric;\n'+
'var n = x.length;\n'+
'var i,xi;\n'+setup+';\n'+
'for(i=n-1;i!==-1;--i) { \n'+
' xi = x[i];\n'+
' '+body+';\n'+
'}\n'+
'return accum;'
);
));
}


Expand Down Expand Up @@ -543,6 +545,7 @@ numeric.pointwise = function pointwise(params,body,setup) {
fun[params.length] = '_s';
fun[params.length+1] = '_k';
fun[params.length+2] = (
'var numeric = arguments.callee.numeric;\n'+
'if(typeof _s === "undefined") _s = numeric.dim('+thevec+');\n'+
'if(typeof _k === "undefined") _k = 0;\n'+
'var _n = _s[_k];\n'+
Expand All @@ -557,7 +560,7 @@ numeric.pointwise = function pointwise(params,body,setup) {
'}\n'+
'return ret;'
);
return Function.apply(null,fun);
return _fn(Function.apply(null,fun));
}
numeric.pointwise2 = function pointwise2(params,body,setup) {
if(typeof setup === "undefined") { setup = ""; }
Expand All @@ -574,6 +577,7 @@ numeric.pointwise2 = function pointwise2(params,body,setup) {
fun.push(p);
}
fun[params.length] = (
'var numeric = arguments.callee.numeric;\n'+
'var _n = '+thevec+'.length;\n'+
'var i'+(haveret?'':', ret = Array(_n)')+';\n'+
setup+'\n'+
Expand All @@ -582,7 +586,7 @@ numeric.pointwise2 = function pointwise2(params,body,setup) {
'}\n'+
'return ret;'
);
return Function.apply(null,fun);
return _fn(Function.apply(null,fun));
}
numeric._biforeach = (function _biforeach(x,y,s,k,f) {
if(k === s.length-1) { f(x,y); return; }
Expand Down Expand Up @@ -693,7 +697,8 @@ numeric.mapreducers = {
numeric[i+'VV'] = numeric.pointwise2(['x[i]','y[i]'],code('ret[i]','x[i]','y[i]'),setup);
numeric[i+'SV'] = numeric.pointwise2(['x','y[i]'],code('ret[i]','x','y[i]'),setup);
numeric[i+'VS'] = numeric.pointwise2(['x[i]','y'],code('ret[i]','x[i]','y'),setup);
numeric[i] = Function(
numeric[i] = _fn(Function(
'var numeric = arguments.callee.numeric;\n'+
'var n = arguments.length, i, x = arguments[0], y;\n'+
'var VV = numeric.'+i+'VV, VS = numeric.'+i+'VS, SV = numeric.'+i+'SV;\n'+
'var dim = numeric.dim;\n'+
Expand All @@ -704,19 +709,20 @@ numeric.mapreducers = {
' else x = numeric._biforeach2(x,y,dim(x),0,VS);\n'+
' } else if(typeof y === "object") x = numeric._biforeach2(x,y,dim(y),0,SV);\n'+
' else '+codeeq('x','y')+'\n'+
'}\nreturn x;\n');
'}\nreturn x;\n'));
numeric[o] = numeric[i];
numeric[i+'eqV'] = numeric.pointwise2(['ret[i]','x[i]'], codeeq('ret[i]','x[i]'),setup);
numeric[i+'eqS'] = numeric.pointwise2(['ret[i]','x'], codeeq('ret[i]','x'),setup);
numeric[i+'eq'] = Function(
numeric[i+'eq'] = _fn(Function(
'var numeric = arguments.callee.numeric;\n'+
'var n = arguments.length, i, x = arguments[0], y;\n'+
'var V = numeric.'+i+'eqV, S = numeric.'+i+'eqS\n'+
'var s = numeric.dim(x);\n'+
'for(i=1;i!==n;++i) { \n'+
' y = arguments[i];\n'+
' if(typeof y === "object") numeric._biforeach(x,y,s,0,V);\n'+
' else numeric._biforeach(x,y,s,0,S);\n'+
'}\nreturn x;\n');
'}\nreturn x;\n'));
}
}
for(i=0;i<numeric.mathfuns2.length;++i) {
Expand All @@ -735,20 +741,22 @@ numeric.mapreducers = {
if(Math.hasOwnProperty(o)) setup = 'var '+o+' = Math.'+o+';\n';
}
numeric[i+'eqV'] = numeric.pointwise2(['ret[i]'],'ret[i] = '+o+'(ret[i]);',setup);
numeric[i+'eq'] = Function('x',
numeric[i+'eq'] = _fn(Function('x',
'var numeric = arguments.callee.numeric;\n'+
'if(typeof x !== "object") return '+o+'x\n'+
'var i;\n'+
'var V = numeric.'+i+'eqV;\n'+
'var s = numeric.dim(x);\n'+
'numeric._foreach(x,s,0,V);\n'+
'return x;\n');
'return x;\n'));
numeric[i+'V'] = numeric.pointwise2(['x[i]'],'ret[i] = '+o+'(x[i]);',setup);
numeric[i] = Function('x',
numeric[i] = _fn(Function('x',
'var numeric = arguments.callee.numeric;\n'+
'if(typeof x !== "object") return '+o+'(x)\n'+
'var i;\n'+
'var V = numeric.'+i+'V;\n'+
'var s = numeric.dim(x);\n'+
'return numeric._foreach2(x,s,0,V);\n');
'return numeric._foreach2(x,s,0,V);\n'));
}
}
for(i=0;i<numeric.mathfuns.length;++i) {
Expand All @@ -759,7 +767,8 @@ numeric.mapreducers = {
if(numeric.mapreducers.hasOwnProperty(i)) {
o = numeric.mapreducers[i];
numeric[i+'V'] = numeric.mapreduce2(o[0],o[1]);
numeric[i] = Function('x','s','k',
numeric[i] = _fn(Function('x','s','k',
'var numeric = arguments.callee.numeric;\n'+
o[1]+
'if(typeof x !== "object") {'+
' xi = x;\n'+
Expand All @@ -775,7 +784,7 @@ numeric.mapreducers = {
' xi = arguments.callee(x[i]);\n'+
o[0]+';\n'+
'}\n'+
'return accum;\n');
'return accum;\n'));
}
}
}());
Expand Down Expand Up @@ -1032,7 +1041,8 @@ numeric.Tbinop = function Tbinop(rr,rc,cr,cc,setup) {
}
}
}
return Function(['y'],
return _fn(Function(['y'],
'var numeric = arguments.callee.numeric;\n'+
'var x = this;\n'+
'if(!(y instanceof numeric.T)) { y = new numeric.T(y); }\n'+
setup+'\n'+
Expand All @@ -1046,7 +1056,7 @@ numeric.Tbinop = function Tbinop(rr,rc,cr,cc,setup) {
' return new numeric.T('+rc+');\n'+
'}\n'+
'return new numeric.T('+rr+');\n'
);
));
}

numeric.T.prototype.add = numeric.Tbinop(
Expand Down Expand Up @@ -1098,14 +1108,15 @@ numeric.T.prototype.transjugate = function transjugate() {
}
numeric.Tunop = function Tunop(r,c,s) {
if(typeof s !== "string") { s = ''; }
return Function(
return _fn(Function(
'var numeric = arguments.callee.numeric;\n'+
'var x = this;\n'+
s+'\n'+
'if(x.y) {'+
' '+c+';\n'+
'}\n'+
r+';\n'
);
));
}

numeric.T.prototype.exp = numeric.Tunop(
Expand Down Expand Up @@ -1964,7 +1975,8 @@ numeric.ccsLUPSolve = function ccsLUPSolve(LUP,B) {

numeric.ccsbinop = function ccsbinop(body,setup) {
if(typeof setup === "undefined") setup='';
return Function('X','Y',
return _fn(Function('X','Y',
'var numeric = arguments.callee.numeric;\n'+
'var Xi = X[0], Xj = X[1], Xv = X[2];\n'+
'var Yi = Y[0], Yj = Y[1], Yv = Y[2];\n'+
'var n = Xi.length-1,m = Math.max(numeric.sup(Xj),numeric.sup(Yj))+1;\n'+
Expand Down Expand Up @@ -2007,7 +2019,7 @@ numeric.ccsbinop = function ccsbinop(body,setup) {
' for(j=j0;j!==j1;++j) y[Yj[j]] = 0;\n'+
'}\n'+
'return [Zi,Zj,Zv];'
);
));
};

(function() {
Expand All @@ -2020,11 +2032,12 @@ numeric.ccsbinop = function ccsbinop(body,setup) {
if(isFinite(eval('1'+numeric.ops2[k]+'0')) && isFinite(eval('0'+numeric.ops2[k]+'1'))) C = 'numeric.ccs'+k+'MM(X,Y)';
else C = 'NaN';
numeric['ccs'+k+'MM'] = numeric.ccsbinop('zk = xk '+numeric.ops2[k]+'yk;');
numeric['ccs'+k] = Function('X','Y',
numeric['ccs'+k] = _fn(Function('X','Y',
'var numeric = arguments.callee.numeric;\n'+
'if(typeof X === "number") return '+A+';\n'+
'if(typeof Y === "number") return '+B+';\n'+
'return '+C+';\n'
);
));
}
}());

Expand Down
3 changes: 3 additions & 0 deletions src/outro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

return numeric;
}));
2 changes: 1 addition & 1 deletion tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if [ -f nodepid.log ]; then
fi
ver=`grep 'numeric.version.*=.*"' ../src/numeric.js | sed 's/numeric.version[ =]*"\([0-9.]*\)".*/\1/'`
echo "Version is $ver"
cat ../src/numeric.js ../src/seedrandom.js ../src/quadprog.js ../src/svd.js > ../lib/numeric-$ver.js
cat ../src/intro.js ../src/numeric.js ../src/seedrandom.js ../src/quadprog.js ../src/svd.js ../src/outro.js > ../lib/numeric-$ver.js
uglifyjs ../lib/numeric-$ver.js > ../lib/numeric-$ver.min.js
cat jquery-1.7.1.min.js jquery.flot.min.js 'Crypto-JS v2.4.0/crypto/crypto-min.js' 'Crypto-JS v2.4.0/crypto-sha256/crypto-sha256.js' json2.js > megalib.js
echo "" | cat closurelib.js sylvester.js - ../lib/numeric-$ver.min.js jquery-1.7.1.min.js jquery.flot.min.js > benchlib.js
Expand Down