davedelong / DDMathParser

String → Number
MIT License
855 stars 153 forks source link

Complex number support #45

Open davedelong opened 11 years ago

davedelong commented 11 years ago

I got an email asking for complex number support. It turns out <complex.h> is for complex numbers, and appears to have enough mathematical primitives to make implementing the functions relatively straight-forward.

However, parsing would be a royal pain.

davedelong commented 10 years ago

Actually, parsing would be easy. It'd just need a new left associative unary operator i with an extremely high precedence.

The problem (for now) would be subclassing NSNumber.

davedelong commented 8 years ago

Maybe using this? https://github.com/dankogai/swift-pons

rmehta33 commented 5 years ago

Did this ever get implemented? Would be very very helpful

davedelong commented 5 years ago

@rmehta33 I have not implemented this. How are you thinking you'd use it?

rmehta33 commented 5 years ago

Not sure what you're asking exactly, like in what scenario or in the code (ex. "(5+5i)*2")? Sorry I haven't been able to respond quicker

davedelong commented 5 years ago

Sorry for being unclear.

What is the scenario you’re wanting to use this in? Why do you want this feature?

(While adding support for the syntax would be pretty straight-forward, making the math actually work would be quite involved)

davedelong commented 5 years ago

I realize that statement is also a reversal of the original issue description 😅

I believe that parsing would be “easy” by making i be a postfix unary operator to “complexify” its argument.

The work would be in that all the implementations of the evaluation functions currently are expecting real numbers, and I think I’d need to come up with either a new numeric type (instead of Double) and make all the functions use that, or somehow add overloads for every function to handle imaginary parameters.

rmehta33 commented 5 years ago

I'm trying to build a calculator for an ios app (like I'm assuming most are using this library). I would use another library, although I can't find any math parser for ios that handles imaginary numbers. So, this feature would avoid me from having to build one from scratch.

Since you mentioned this library works off of Doubles, I did find an "extension" of sorts for imaginary numbers for Doubles in swift here -> https://github.com/gongzhang/swift-complex-number. Hopefully this would avoid having to reformat the code to accept a different type.

Anyhow, thanks for the responses!

EDIT: I looked further into the library, and I misread it as an extension of sorts. The best thing I could come up with is https://github.com/dankogai/swift-complex, a closer idea to what I was originally thinking the library above did. I tested it out and it should handle the basic operations (+, -, /, *).

swainwri commented 5 years ago

Continuing on with the complex number support. My App to plot 3d surfaces requires multiple solutions, as an example y = 5.0sqrt(1-xx/9-zz/16) describes an ellipsoid with axis dimensions 3,4,5. Is it possible for me to make a Customised Function returning the negative and positive solutions or would this have to supported from within your code? Another example the torus: y = sqrt(25- (10 - sqrt(xx+z*z))^2) would produce multiple solutions. Cube roots would return real and complex solution, such that my app could generate a Riemann surface.

rmehta33 commented 5 years ago

@davedelong I thought a bit more about this. By creating an extension for a float imaginary number for type Double, and then, like you suggested overriding the Double operations in an extension, there should be minimal change in the code if any at all.