Preferring real outputs #2790
-
I'm encountering behavior where
Question: Is there a way to prefer real outputs when evaluating, but allow complex outputs? I.e., what I'm after would be soemthing like math.evaluate('(-8)^(1/3)') // -2
math.evaluate('(-4)^(1/2)') // 2i This is what I think a reasonably well-informed high-school student would expect: they know that complex numbers exist, but would be surprised that My motivation is for largely for math parsing & evaluation at https://www.math3d.org/, which is focused on real-valued functions and inputs, though I know that some users have taken advantage of its support for complex numbers, so I am hesitant to set |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Well, yes, in general pow(z, a) is defined as exp(a ln(z)), as usual in mathematics, which helps keep everything consistent, but that does does yield one of the complex cube roots of -8. I at the moment am not having any clever ideas of a consistent alternative in which you would generally get real values for multivalued expressions that potentially have a real value, so I guess I would have to recommend that for your application you write a "realpow" function that looks for the special cases where you want to display something different and behaves accordingly. Depending on your application, you might then face the problem of getting the expression parser to execute realpow instead of pow for the ^ operator. Sadly, the function correspondences for operators are hardcoded into src/expression/parse.js (and quite possibly elsewhere, like simplify()), so they're not easily modifiable from client code. I guess for that my only suggestion is to do a transform on the parse tree between parsing and evaluation. Or a hacky approach would be to evaluate expressions twice, with predictable off and on, and return the result you think your users will like better. (Or try it with predictable off, and if you get a complex result, try with predictable on and see if you now get a real result rather than say NaN or null or undefined...) Sorry I don't myself have anything better to suggest. It's just hard for me to come up with another configuration option like |
Beta Was this translation helpful? Give feedback.
-
Hm yeah that is interesting. The function See also: #851 |
Beta Was this translation helpful? Give feedback.
Well, yes, in general pow(z, a) is defined as exp(a ln(z)), as usual in mathematics, which helps keep everything consistent, but that does does yield one of the complex cube roots of -8. I at the moment am not having any clever ideas of a consistent alternative in which you would generally get real values for multivalued expressions that potentially have a real value, so I guess I would have to recommend that for your application you write a "realpow" function that looks for the special cases where you want to display something different and behaves accordingly. Depending on your application, you might then face the problem of getting the expression parser to execute realpow instead of po…