mathjs
mathjs copied to clipboard
Rationalize giving error on 1/(x^-2)
Running
math.rationalize('1/(x^-2)')
gives the error
Cannot read property '1' of undefined.
However, running
math.rationalize('1/(x^-1)')
gives the result x as you'd expect.
I'm using mathjs 6.5.0. Haven't tested other versions.
Hm, that looks like a bug. The stacktrace I get is:
> math.rationalize('1/(x^(-2))').toString()
TypeError: Cannot read property '1' of undefined
at recPoly (/home/jos/Dropbox/Jos/projects/js/mathjs/lib/function/algebra/rationalize.js:291:24)
at recPoly (/home/jos/Dropbox/Jos/projects/js/mathjs/lib/function/algebra/rationalize.js:302:13)
at polynomial (/home/jos/Dropbox/Jos/projects/js/mathjs/lib/function/algebra/rationalize.js:257:5)
at NodeObjectBoolean (/home/jos/Dropbox/Jos/projects/js/mathjs/lib/function/algebra/rationalize.js:143:21)
at generic (/home/jos/Dropbox/Jos/projects/js/mathjs/node_modules/typed-function/typed-function.js:1073:27)
at rationalize (/home/jos/Dropbox/Jos/projects/js/mathjs/node_modules/typed-function/typed-function.js:1092:24)
at string (/home/jos/Dropbox/Jos/projects/js/mathjs/lib/function/algebra/rationalize.js:120:14)
at Object.rationalize (/home/jos/Dropbox/Jos/projects/js/mathjs/node_modules/typed-function/typed-function.js:1085:85)
The cause of this issue is here, in the part if (node.args[1].fn === 'unaryMinus') {...}:
https://github.com/josdejong/mathjs/blob/2a71fe8438b41a8495179953657cdf061b6070c2/src/function/algebra/rationalize.js#L277-L285
When I remove these three lines 278 to 280, no unit tests break and the expression '1/(x^(-2))' gives a normal error Error: There is a non-integer exponent.
@paulobuchsbaum do you still remember what these three lines where supposed to do? If I'm not mistaken you have done some changes there in commit 443d42a7fcea1fb09828e5cf703aa28e2b043005.
Dear Jos de Jong
These 3 lines don't seem to make any sense for me.
Best regards, Paulo Buchsbaum
Em 01/02/2020 13:05:50, "Jos de Jong" [email protected] escreveu:
The cause of this issue is here, in the part if (node.args[1].fn === 'unaryMinus') {...}:
https://github.com/josdejong/mathjs/blob/2a71fe8438b41a8495179953657cdf061b6070c2/src/function/algebra/rationalize.js#L277-L285
When I remove these three lines 278 to 280, no unit tests break and the expression '1/(x^(-2))' gives a normal error Error: There is a non-integer exponent.
@paulobuchsbaum https://github.com/paulobuchsbaum do you still remember what these three lines where supposed to do? If I'm not mistaken you have done some changes there in commit 443d42a https://github.com/josdejong/mathjs/commit/443d42a7fcea1fb09828e5cf703aa28e2b043005.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/josdejong/mathjs/issues/1724?email_source=notifications&email_token=AHI5QJTVVWRNGYTTGBYG32LRAWMV5A5CNFSM4KMIB4IKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKRANRA#issuecomment-581043908, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHI5QJVONV62RMBOPTMFJMLRAWMV5ANCNFSM4KMIB4IA.
Thanks Paulo. I think the intention of these lines was to maybe turn a unary minus operator with a constant into a constant with a negative value.
I will remove these three lines for now, that will at least give a clear error message.
It would be great if anyone can help out actually implementing support for negative exponents!
Just ran into this in 9.1.0, specifically 'There is a non-integer exponent' on math.rationalize('x ^ -2').
I'm looking at trying to make a fix. I have recPoly identifying the unaryMinus negative exponent and throwing Error('There is a negative exponent').
What do we want to happen when this occurs? Should rationalize just return the simplified input expression? It seems to do this on math.rationalize('x ^ -1'), which comes out as 1 / x
Or should rationalize itself propogate the 'There is a negative exponent' error, like happens right now for 'There is a non-integer exponent'? It's notable that this doesn't happen for ('x ^ -1') which simplifies, whereas bigger negative exponents do not.
Thanks @joshhansen for looking into this.
If possible I think the outcome 1 / x would be best: this is a proper fraction notation which is what rationalize tries to generate. If not possible, a more accurate error message would be nice. I'm not sure at this point whether there is a bug into play here, or that we need to extend the code to be able to handle cases like these.
I agree that x^-2 should rationalize to 1/x^2 and 1/(x^-2) to x^2 and yet, almost four years on, still neither works. Hopefully this issue can get some love eventually. ;-)