Skip to content

Conversation

@shivasankarka
Copy link
Collaborator

@shivasankarka shivasankarka commented Dec 4, 2025

This PR expands complex-datatype support, improves ComplexSIMD and ComplexNDArray usability, introduces new convenience APIs, and enhances documentation across the complex-number ecosystem.

Added

ComplexDType & DType System

  • Support for additional complex dtypes (ComplexDType.int16, ComplexDType.int32).

ComplexSIMD

  • Added component_bitwidth() to distinguish SIMD bitwidth from the bitwidth of its real and imag components.

  • Added arithmetic operator overloads for more complete ComplexSIMD math support.

  • Added elem_pow for per-component exponentiation (distinct from __pow__).

  • Added all_close() for comparing real and imaginary components across two ComplexSIMD instances.

  • Added broadcasting support for scalar complex values, simplifying scalar construction:

    var a = CScalar(1.0)              # 1.0 + 1.0j
    var b = ComplexSIMD[f32, 2](1.0)  # [1.0, 1.0] + [1.0, 1.0]j
  • Added convenience constructors:

    • ComplexSIMD[cf64].zero()
    • ComplexSIMD[cf64].one()
    • ComplexSIMD[cf64].I()
    • ComplexSIMD[cf64].from_polar(2.0, 0.5)

ComplexNDArray

  • Expanded and clarified documentation for all ComplexNDArray methods.

Python-like Complex Literal

  • Introduce a ImaginaryUnit type and the alias 1j to enable natural, Pythonic complex number creation in NuMojo. Previously, creating complex numbers required explicit constructor calls which were pretty long, but now users can write mathematical expressions using familiar notation. The 1j constant automatically adapts to match operand types and SIMD widths. It also has full support for all operations between 1j and scalars, SIMD vectors, and other complex numbers. It is by default of type ComplexDType.float64.
from numojo import `1j`
# Scalar complex numbers
var c1 = 3 + 4 * `1j`                    # ComplexScalar[cint]: (3 + 4j)
var c2 = 2.0 * `1j`                      # ComplexScalar[cf64]: (0 + 2j)
var c3 = 5 - `1j`                        # ComplexScalar[cint]: (5 - 1j)

# SIMD complex vectors  
var c4 = SIMD[f32, 4](1.0) + `1j` * SIMD[f32, 4](2.0)  # ComplexSIMD[cf32, 4]
var c5 = SIMD[f64, 2](3.0, 4.0) + `1j`                 # ComplexSIMD[cf64, 2]
 var d = SIMD[f32, 2](1) + SIMD[f32, 2](2) * `1j` # creates [( 1 + 2 j) (1 + 2 j)]

# Mathematical properties
var c6 = `1j` * `1j`                     # -1 (Scalar[f64])
var c7 = `1j` ** 3                       # (0 - 1j) (ComplexScalar[cf64])
var c8 = (1 + `1j`) / `1j`               # (1 - 1j) (ComplexScalar[cf64])
  • Added documentation for ImaginaryUnit methods.

NDArray Improvements

  • Added normalize for handling negative indices.
  • Improved constructor documentation.

Changed

  • Enhanced documentation for ComplexSIMD, including clearer conceptual explanations and usage examples.
  • Change the behaviour of __getitem__, __setitem__, item, itemset methods of ComplexSIMD. The earlier method were confusing to use and were not flexible enough to access all parts of the complex SIMD vectors. The current methods allow accessing the ComplexSIMD vectors in the following manner,
var complex_simd = ComplexSIMD[cf32, 4](1.0, 2.0)  # All lanes set to 1+2i

# Lane-wise access
var lane2 = complex_simd[2]  # Get ComplexScalar at lane 2
complex_simd[1] = ComplexScalar[cf32](3.0, 4.0)  # Set lane 1 to 3+4i

# Component access
var real_part = complex_simd.item["re"](2)  # Get real part of lane 2
complex_simd.itemset["im"](1, 5.0)  # Set imaginary part of lane 1 to 5.0

# Bulk access
var all_reals = complex_simd.re  # Get all real parts as SIMD vector
var all_imags = complex_simd.im  # Get all imaginary parts as SIMD vector

4. Removed

  • N/A

@shivasankarka shivasankarka changed the title [core][Complex] Improve Complex SIMD, Array [core][Complex] Improve Complex-number ecosystem Dec 4, 2025
Copy link
Collaborator

@forfudan forfudan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thanks!

@shivasankarka shivasankarka merged commit 0fd9b3f into Mojo-Numerics-and-Algorithms-group:pre-0.8 Dec 6, 2025
2 checks passed
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.

2 participants