mathjs = {
var mathjs = await require("mathjs");
function integrate (f, start, end, step) {
let total = 0
step = step || 0.01
for (let x = start; x < end; x += step) {
total += f(x + step / 2) * step
}
return total
}
integrate.transform = function (args, math, scope) {
if (!args[1].isSymbolNode) {
throw new Error('Second argument must be a symbol')
}
const variable = args[1].name
const start = args[2].compile().evaluate(scope)
const end = args[3].compile().evaluate(scope)
const step = args[4] && args[4].compile().evaluate(scope)
const fnScope = Object.create(scope)
const fnCode = args[0].compile()
const f = function (x) {
fnScope[variable] = x
return fnCode.evaluate(fnScope)
}
return integrate(f, start, end, step)
}
integrate.transform.rawArgs = true
mathjs.import({
integrate: integrate
})
return mathjs;
}