Skip to content

Instantly share code, notes, and snippets.

@jiggzson
Created May 21, 2017 03:49
Show Gist options
  • Save jiggzson/ccc4ec0c93cbacfea40c66e2456c5d95 to your computer and use it in GitHub Desktop.
Save jiggzson/ccc4ec0c93cbacfea40c66e2456c5d95 to your computer and use it in GitHub Desktop.
var defint = function(symbol, dx, from, to) {
var vars = core.Utils.variables(symbol),
integral = __.integrate(symbol, dx),
retval;
if(!integral.hasIntegral()) {
var upper = {},
lower = {};
upper[dx] = to;
lower[dx] = from;
retval = _.subtract(_.parse(integral, upper), _.parse(integral, lower));
}
else if(vars.length === 1) {
retval = 0;
var step = 0.005,
f = core.Utils.build(symbol),
n = Math.floor((to - from)/step),
c = from, e;
//we'll stop around 1e6;
if(n > 1e6)
retval = _.symfunction('defint', [symbol, dx, from , to]);
else {
while(true) {
e = c + step;
if(e > to)
e = to;
try {
retval += (e-c)*(f(c)+f(e))/2;
}
catch(e){
return _.symfunction('defint', [symbol, dx, from , to]); //exit. Not continuous
}
c += step;
if(c >= to)
break;
}
}
}
else
retval = _.symfunction('defint', [symbol, dx, from , to]);
return retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment