SciSharp / SciSharp/NumSharp

[Types] Add complex64 support

Open
#569 0 comments 0 reactions 1 assignee View on GitHub

@Nucs is already working on this.

Since Feb 18, 2026.

core documentation-needed enhancement missing feature/s
Dominant language
C#
Stars
1.5k
Forks
205
Avg merge
7d 7h
Merged PRs (30d)
2

Description

Overview

Add support for complex64 (two float32 components) to NumSharp. Currently NumSharp only supports complex128 (two float64 components).

Problem

NumSharp has Complex (complex128) but not complex64:

>>> import numpy as np
>>> np.array([1+2j, 3+4j], dtype=np.complex64)
array([1.+2.j, 3.+4.j], dtype=complex64)
>>> np.dtype(np.complex64).itemsize
8  # 2 x 4 bytes (float32)

>>> np.dtype(np.complex128).itemsize
16  # 2 x 8 bytes (float64)

Use cases:

  • FFT/Signal processing — Many FFT implementations use complex64
  • Memory efficiency — 50% savings vs complex128
  • Scientific computing — Matches hardware complex types
  • NumPy file interop — Some .npy files use complex64

Proposal

Task List
  • Create Complex64 struct in Utilities/Complex64.cs:
    [StructLayout(LayoutKind.Sequential)]
    public readonly struct Complex64 : IEquatable<Complex64>
    {
        public readonly float Real;
        public readonly float Imaginary;
        // Arithmetic operators, math functions
    }
    
  • Add NPTypeCode.Complex64 = 64 to NPTypeCode.cs
  • Add np.complex64 type alias
  • Update NPTypeCodeExtensions.GetTypeCode() for Complex64
  • Update InfoOf<T> for Complex64
  • Update UnmanagedStorage type switches
  • Update ArraySlice allocation
  • Update type promotion tables
  • Implement arithmetic operations (+, -, *, /)
  • Implement math functions (abs, sqrt, exp, etc.)
  • Add tests
C# Type Implementation
[StructLayout(LayoutKind.Sequential)]
public readonly struct Complex64
{
    public readonly float Real;
    public readonly float Imaginary;
    
    public Complex64(float real, float imaginary) => (Real, Imaginary) = (real, imaginary);
    
    public static Complex64 operator +(Complex64 a, Complex64 b) 
        => new(a.Real + b.Real, a.Imaginary + b.Imaginary);
    
    public float Magnitude => MathF.Sqrt(Real * Real + Imaginary * Imaginary);
    // ... etc
}
NumPy C# Size Components
complex64 Complex64 (custom) 8 bytes 2 x float32
complex128 System.Numerics.Complex 16 bytes 2 x float64

Implementation Effort

MEDIUM — Requires custom struct with arithmetic and math operations.

Estimated: ~200-300 lines for the struct + type switch updates.

Related

  • Part of NumPy 2.x alignment: #529
  • Will benefit from DynamicMethod IL emission: #544
  • Will benefit from SIMD optimization: #545

References

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.