MathCore has first-class support for complex numbers throughout its expression system. Complex values are represented by theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt
Use this file to discover all available pages before exploring further.
Expr::Complex variant, backed by num_complex::Complex64 — a pair of 64-bit floats for the real and imaginary parts. You can write complex literals directly in expression strings, perform arithmetic on them through evaluate, and receive complex results automatically when solving equations whose roots are not real, all without any special configuration.
The Expr::Complex variant
The core expression type in MathCore is the Expr enum defined in mathcore::types. Its Complex variant wraps a num_complex::Complex64 value:
Complex64 is an alias for Complex<f64>, so it carries the full num-complex trait implementations including Add, Sub, Mul, Div, Norm, and Conj.
Parsing complex literals
The MathCore parser recognises complex literals in the forma+bi or a-bi directly inside expression strings. No wrapper or special prefix is required.
| Literal | Real part | Imaginary part |
|---|---|---|
3+4i | 3.0 | 4.0 |
2-1i | 2.0 | -1.0 |
0+1i | 0.0 | 1.0 |
5i | 0.0 | 5.0 |
Evaluating complex expressions
UseMathCore::evaluate (which returns Result<Expr, MathError>) rather than MathCore::calculate when working with complex numbers. evaluate can return any Expr variant, including Expr::Complex, whereas calculate returns f64 and will error if the result is not a real number.
MathCore::calculate returns Result<f64, MathError> and will return Err(MathError::InvalidOperation) if the evaluated result is an Expr::Complex. Always use evaluate when your expression may produce a complex value.Complex roots from solve
MathCore::solve automatically returns Expr::Complex roots when the discriminant of a quadratic is negative. You do not need to opt in — complex roots are part of the standard Vec<Expr> return value.
Δ = b² - 4ac are computed as:
Full example
How complex numbers are displayed
Expr::Complex implements Display. The sign between the real and imaginary parts follows the sign of the imaginary component directly from the source:
| Value | Display |
|---|---|
Complex64::new(3.0, 4.0) | 3+4i |
Complex64::new(3.0, -4.0) | 3-4i |
Complex64::new(0.0, 1.0) | 0+1i |
Complex64::new(0.0, -1.0) | 0-1i |
Using Complex64 directly alongside MathCore
Since MathCore re-exports through num_complex, you can use Complex64 directly in your own code and pass constructed values back into Expr:
Pattern-matching on complex results
When you need to branch on whether a result is real or complex, match on theExpr variants returned by evaluate or solve:
