jiggzson / nerdamer

a symbolic math expression evaluator for javascript
http://www.nerdamer.com
MIT License
517 stars 82 forks source link

Integral of "1/x" should be "log(abs(x))" #571

Closed ElectrifyPro closed 3 years ago

ElectrifyPro commented 4 years ago
nerdamer('integrate(1/x)');
// log(x)

I saw this same problem in one of Algebrite's issues, and so I'm not too sure if it'll be a complication for you, but it'd be convenient if you can fix it.

Thank you so much for all the work you've put into this amazing library!

jiggzson commented 3 years ago

@ElectrifyPro, I can't remember why this particular result off the top of my head. Changing the output isn't really the problem but since integrate relies on itself, this may break other integrals. I'll take a look at it.

jiggzson commented 3 years ago

@ElectrifyPro,

I looked into this a little more and compared it to Maxima and Wolfram Alpha, which both give the current output. Small unexpected changes can lead to hours of debugging down the line so since I don't know the implications, I'll leave this as it is for now. I'm always open to discuss this in the future.

You can always override the function responsible for this with this little snippet after the core and add-ons.

var core = nerdamer.getCore();
var _ = core.PARSER;
core.Calculus.integration.poly_integrate = function(x) {
    var p = x.power.toString(),
        m = x.multiplier.toDecimal(),
        s = x.toUnitMultiplier().toLinear();
    if(Number(p) === -1) {
        return _.multiply(new core.Symbol(m), _.symfunction('log', [_.symfunction('abs', [s])]));
    }
    return _.parse(core.Utils.format('({0})*({1})^(({2})+1)/(({2})+1)', m, s, p));  
};

console.log(nerdamer('integrate(1/x, x)').toString()); // log(abs(x))

And thank you.