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
References
Reactions are currently unavailable
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:
Use cases:
Proposal
Task List
C# Type Implementation
Implementation Effort
MEDIUM — Requires custom struct with arithmetic and math operations.
Estimated: ~200-300 lines for the struct + type switch updates.
Related
References