Packages / rupm

rs-math-complex 0.1.0

Complex numbers over f64: arithmetic, polar form, exp, ln, powers, principal square root, parsing and Display, std only

Install

cargo add rs-math-complex --registry rupm

In Ruststudio: Packages, search for rs-math-complex, Install.

Owner: Rust Studio. Licence: MIT OR Apache-2.0. Total downloads: 0. Published 2026-11-23.

Versions

README

# rs-math-complex

Complex numbers over f64: arithmetic, polar form, exp, ln, powers, principal square root, parsing and Display, std only

Add it with `rupm add rs-math-complex` (or `cargo add rs-math-complex --registry rupm`).

`Complex` is a small `Copy` type with the usual operators. Functions follow the standard principal-value conventions (the branch cut of `ln`, `sqrt` and `pow` lies along the negative real axis) and are checked against well-known identities such as Euler's formula and `i^i = e^(-pi/2)`. Nothing panics: invalid text gives a `ParseError`, and undefined math such as `ln(0)` gives infinities or NaN as IEEE floats do.

## Install

```
rupm add rs-math-complex
```

## Usage

```rust
use rs_math_complex::Complex;

let a = Complex::new(1.0, 2.0);
let b: Complex = "3-4i".parse().unwrap();
assert_eq!(a * b, Complex::new(11.0, 2.0));
assert_eq!(b.abs(), 5.0);
assert_eq!(Complex::new(3.0, 4.0).sqrt().to_string(), "2+1i");

let z = Complex::from_polar(2.0, std::f64::consts::FRAC_PI_2);
assert!((z.im - 2.0).abs() < 1e-12);
```

## API overview

- `Complex::new(re, im)`, constants `ZERO`, `ONE`, `I`, `from_polar(r, theta)`, `cis(theta)`, `to_polar()`.
- Operators `+ - * /` between complex numbers and with `f64`, unary `-`, and the `*Assign` forms.
- `abs`, `arg`, `norm_sqr`, `conj`, `inv`, `is_nan`, `is_finite`.
- `exp`, `ln`, `sqrt`, `powi(i32)`, `powf(f64)`, `powc(Complex)`.
- Display as `a+bi` (honours precision such as `{:.3}`), and `FromStr` accepting `3+4i`, `-2.5i`, `i`, `7`, `1e3-2e-1j`.