Skip to content

Conversation

youdie323323
Copy link

@youdie323323 youdie323323 commented Sep 2, 2025

New imaginary unit

Previously, users had to manually define the imaginary unit when working with complex numbers, like this: const i: Complex(T) = .init(0, 1);.

For instance, what used to look like:

const i: Complex(f32) = .init(0, 1);

...

const i_diff_2 = i.mul(diff_2);

Can now be written more cleanly as:

const i_diff_2 = diff_2.mul(.i);

or:

const i_diff_2: Complex(f32) = .mul(.i, diff_2);

(don't forget that you can actually use mulByI).

This PR includes addition of a built-in imaginary unit constant to Complex(T), which simplifies things a bit.

Others

Changes

  • Complex(T).mulByMinusI is added -- this will multiplies -i to a complex number.

Breaking Changes

These renames are for consistency with other naming conventions:

  • Complex(T).conjugate has been renamed to Complex(T).conj.
  • Complex(T).mulbyi has been renamed to Complex(T).mulByI.
  • Complex(T).reciprocal has been renamed to Complex(T).recip.

Other breaking changes:

  • std.math.complex.conj has been removed. Use Complex(T).conj instead.

@youdie323323
Copy link
Author

Since there's already conjugate method implemented in Complex, should we just remove math/complex/conj.zig? (and maybe rename conjugate to conj, as math/complex/conj.zig did it?)

@youdie323323 youdie323323 changed the title std.math.Complex: add imaginary unit std.math.complex: add imaginary unit, other breaking changes, etc. Sep 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant