-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Complex number support #752
Comments
Currently there is nothing to prevent adding a complex number type as an add-on package, including complex versions of common math functions. To make it efficient, however, it would probably be best to be built in, which means consider the following issues:
Without a special syntax, it would be awkward to work with complex numbers, as you need to use constructor functions all the time... |
For scientific applications, it might be suitable to have a special type for a larger complex matrices or ndarrays, and then have a custom type for a complex scalar. I need to look more closely at the documentation of custom types and how That's a good point about the syntax. I don't understand the parser, but it seems like the parser might follow operator precedence in the example you gave. It seems like
Would parse as Then (float(2)) + (complex(0, 3.5)) Then (complex(2, 3.5)) That requires handling arithmetic types between a complex type and int/float. Is filling out those arithmetic functions between complex and float (for example) easily achievable if the complex scalar is in an add-on package? |
It would be difficult to alter the built-in precedence values for different math operators... and definitely difficult to have different precedence values for different cases, for example: 1 + 2 * 3 // should be 1 + (2 * 3)
1 + 2i * 3 // should be (1 + 2i) * 3 ? Or should we force all complex literals to use parentheses? (1 + 2i) * 3 // always use parens |
Just thinking about this some more, an easy-to-use complex number system should also have overloads for let x = 42 + 99i;
let y = x*; // conjugate But how to do this without conflicting with the |
In the docs, the numeric types are INT, FLOAT, or Decimal. I'm wondering if it's aligned with the goals of the rhai project to add support for a
Complex
type that defaults to storing real and imaginary components as (2) f64 fields.I'm working on a REPL that automatically loads
rhai-sci
, and wondering how complex matrix operations might work, and thinking it might be nice if aComplex
number type was eventually built-in to rhai. There's already complex number support in nalgebra, which I believerha-sci
depends on:Numpy and MATLAB both use "1i" or "1j" to represent an imaginary component, and this seems like good syntax - i.e. "let a = 2.0 + 1j * 3.5" would create a complex number. There would have to be some associated functions for arithmetic, sine, cosine, exponentials, etc.
Does that sound like a reasonable idea? I'm interested to prototype this and submit a PR. Any pointers on where to start or things to look for are appreciated.
The text was updated successfully, but these errors were encountered: