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
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
Overview
Add support for
complex64(two float32 components) to NumSharp. Currently NumSharp only supportscomplex128(two float64 components).Problem
NumSharp has
Complex(complex128) but notcomplex64:Use cases:
.npyfiles use complex64Proposal
Task List
Complex64struct inUtilities/Complex64.cs:NPTypeCode.Complex64 = 64toNPTypeCode.csnp.complex64type aliasNPTypeCodeExtensions.GetTypeCode()for Complex64InfoOf<T>for Complex64UnmanagedStoragetype switchesArraySliceallocationC# 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