diff --git a/src/intro.js b/src/intro.js new file mode 100644 index 0000000..8068dca --- /dev/null +++ b/src/intro.js @@ -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(){}; diff --git a/src/numeric.js b/src/numeric.js index 537b68f..257e763 100644 --- a/src/numeric.js +++ b/src/numeric.js @@ -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"; @@ -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'+ @@ -353,10 +354,11 @@ 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'+ @@ -364,7 +366,7 @@ numeric.mapreduce2 = function mapreduce2(body,setup) { ' '+body+';\n'+ '}\n'+ 'return accum;' - ); + )); } @@ -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'+ @@ -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 = ""; } @@ -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'+ @@ -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; } @@ -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'+ @@ -704,11 +709,12 @@ 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'+ @@ -716,7 +722,7 @@ numeric.mapreducers = { ' 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 ../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