Commit dd5ac8c
committed
feat(dtypes): NumPy 2.x type-alias alignment + np.dtype parser rewrite
Completes the NumPy 2.4.2 parity pass started with Rounds 1-15 by aligning
np.* class-level type aliases, rewriting np.dtype(string) with a full
FrozenDictionary lookup, extending finfo/iinfo to the new dtypes, adding
TypeError / IndexError throwing at NumPy-canonical rejection sites, and
plumbing Complex through matmul + UnmanagedMemoryBlock fills. ~1,912 new
test LoC across 9 new files + updates to 6 existing test files.
np.* type aliases (src/NumSharp.Core/APIs/np.cs)
------------------------------------------------
Breaking changes to match NumPy 2.4.2:
np.byte byte (uint8) -> sbyte (int8) NumPy C-char convention
np.complex64 complex128 -> throws NSE no silent widening
np.csingle complex128 -> throws NSE no silent widening
np.uint uint64 -> uintp (ptr) NumPy 2.x
np.intp nint -> long on 64-bit (nint has NPTypeCode.Empty
which breaks dispatch)
np.uintp nuint -> ulong on 64-bit
np.int_ long -> intp NumPy 2.x (int_ == intp)
Added aliases:
np.short, np.ushort, np.intc, np.uintc, np.longlong, np.ulonglong,
np.single, np.cdouble, np.clongdouble
Platform-detected (C-long convention: 32-bit MSVC / 64-bit *nix LP64):
np.@long, np.@Ulong
np.dtype(string) parser (src/NumSharp.Core/Creation/np.dtype.cs)
-----------------------------------------------------------------
Regex parser replaced with a FrozenDictionary<string, Type> built once
at static init. Platform-detection helpers (_cLongType, _cULongType,
_intpType, _uintpType) declared BEFORE the dictionary since static
initializers run top-down and BuildDtypeStringMap reads them.
Covers:
- Single-char NumPy codes: ? b B h H i I l L q Q p P e f d g D G
- Sized forms: b1 i1 u1 i2 u2 i4 u4 i8 u8 f2 f4 f8 c16
- Lowercase names: bool int8..int64 uint8..uint64 float16..float64
complex complex128 half single double byte ubyte
short ushort intc uintc int_ intp uintp bool_ int
uint long ulong longlong ulonglong longdouble
clongdouble
- NumSharp-friendly: SByte Byte UByte Int16..UInt64 Half Single Float
Double Complex Bool Boolean boolean Char char
decimal
Unsupported codes throw NotSupportedException:
- Bytestring (S / a), Unicode (U), datetime (M), timedelta (m),
object (O), void (V) - NumSharp has no equivalents
- complex64 / 'F' / 'c8' - NumSharp only has complex128
np.finfo + np.iinfo (src/NumSharp.Core/APIs/np.{finfo,iinfo}.cs)
----------------------------------------------------------------
np.finfo gains:
- Half (IEEE binary16: bits=16, eps=2^-10, smallest_subnormal=2^-24,
maxexp=16, minexp=-14)
- Complex (reports underlying float64 values with dtype=float64 per
NumPy parity: finfo(complex128).dtype == float64)
np.iinfo gains SByte (int8) with signed min/max and 'i' kind.
IsSupportedType extended to accept Half, Complex, SByte.
find_common_type table (src/NumSharp.Core/Logic/np.find_common_type.cs)
-----------------------------------------------------------------------
~30 table entries swapped from np.complex64 -> np.complex128 to reflect
NumPy 2.4.2 rules and avoid relying on the now-throwing alias. No
behavioral change for callers: the previous complex64 alias pointed at
Complex anyway.
NDArray implicit/explicit casts
-------------------------------
src/NumSharp.Core/Casting/Implicit/NdArray.Implicit.ValueTypes.cs
Added implicit scalar -> NDArray for `sbyte` and `Half`.
Added explicit NDArray -> `sbyte` scalar.
Common validation factored into EnsureCastableToScalar(nd, targetType,
targetIsComplex):
- ndim != 0 -> IncorrectShapeException
- non-complex target + complex source -> TypeError
Python's `int(complex(1, 2))` raises TypeError; NumSharp matches.
NumPy's ComplexWarning (silent imaginary drop) treated as a hard error
since NumSharp has no warning mechanism.
NumPy-parity error types at rejection sites
--------------------------------------------
- Default.Shift.ValidateIntegerType: NotSupportedException -> TypeError
("ufunc 'left_shift' not supported for the input types, ... safe casting")
- NDArray.Indexing.Selection.{Getter,Setter}: ArgumentException -> IndexError
("only integers, slices (':'), ellipsis ('...'), numpy.newaxis ('None')
and integer or boolean arrays are valid indices")
- np.repeat: permissive Half/Complex truncation -> TypeError
("Cannot cast array data from dtype('float16') to dtype('int64')
according to the rule 'safe'")
New exception (src/NumSharp.Core/Exceptions/IndexError.cs):
public class IndexError : NumSharpException
Mirrors Python's IndexError. Raised for out-of-range subscripts and
invalid index types (e.g. float/complex index on an ndarray).
UnmanagedMemoryBlock.Allocate cross-type fill
---------------------------------------------
src/NumSharp.Core/Backends/Unmanaged/UnmanagedMemoryBlock.cs
Replaced direct boxing casts `(Half)fill` / `(Complex)fill` / etc with
Utilities.Converts.ToXxx(fill) dispatchers. Previously `fill = 1` passed
to a Half array threw InvalidCastException because a boxed int cannot
unbox to Half. Now follows the same NumPy-parity wrapping path as the
rest of the casting subsystem (int -> Half, double -> Complex, etc).
Complex matmul
--------------
src/NumSharp.Core/Backends/Default/Math/BLAS/Default.MatMul.2D2D.cs
MatMulMixedType<TResult> now short-circuits to MatMulComplexAccumulator
when TResult is Complex - the double-precision accumulator was dropping
imaginary parts for Complex outputs. The dedicated path accumulates in
Complex across K and writes Complex-precision results.
np.asanyarray
-------------
src/NumSharp.Core/Creation/np.asanyarray.cs
Half and System.Numerics.Complex added to the scalar-detection branch.
Previously fell through to "Unable to resolve asanyarray for type
Half/Complex" because neither matched IsPrimitive.
Test coverage (~1,912 new LoC)
------------------------------
NpTypeAliasParityTests 174 LoC - every np.* alias vs NumPy 2.4.2
np.finfo.NewDtypesTests 262 LoC - Half / Complex finfo
np.iinfo.NewDtypesTests 95 LoC - SByte iinfo
UnmanagedMemoryBlockAllocateTests 226 LoC - cross-type fills
ComplexToRealTypeErrorTests 170 LoC - Complex -> int/float scalar cast
NDArrayScalarCastTests 384 LoC - 0-d cast matrix (implicit + explicit)
Complex64RefusalTests 116 LoC - complex64 / csingle throw
DTypePlatformDivergenceTests 166 LoC - 'l'/'L'/'int' platform behavior
DTypeStringParityTests 319 LoC - every dtype string vs NumPy
Updates to existing tests:
- ConvertsBattleTests.cs: [Misaligned] tags removed from Half/Complex
repeat/shift/index cases; assertions aligned to NumPy-parity TypeError
- ShiftOpTests.cs: NotSupportedException -> TypeError
- np.finfo.BattleTest / np.iinfo.BattleTest: "float" now -> 64 bits
(alias for float64); "int" now -> intp (64 on 64-bit)
- np.dtype.Test: split into Case1_ValidForms / renamed classes
- np.find_common_type.Test: complex64 -> complex128; added
Case4b_c8_ThrowsNotSupported guard
Docs
----
docs/website-src/docs/NDArray.md 663 LoC - user-facing NDArray guide
docs/website-src/docs/dtypes.md 610 LoC - dtype reference
docs/website-src/docs/toc.yml NDArray + Dtypes added to TOC
docs/plans/REVIEW_FINDINGS.md 306 LoC - review notes
docs/releases/RELEASE_0.51.0-prerelease.md - release notes for the branch1 parent bacf59d commit dd5ac8c
34 files changed
Lines changed: 4356 additions & 427 deletions
File tree
- docs
- plans
- releases
- website-src/docs
- src/NumSharp.Core
- APIs
- Backends
- Default/Math
- BLAS
- Unmanaged
- Casting/Implicit
- Creation
- Exceptions
- Logic
- Manipulation
- Selection
- test/NumSharp.UnitTest
- APIs
- Backends
- Kernels
- Unmanaged
- Casting
- Creation
- Logic
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
5 | 9 | | |
6 | 10 | | |
7 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
19 | 32 | | |
20 | 33 | | |
21 | 34 | | |
22 | 35 | | |
23 | 36 | | |
24 | 37 | | |
25 | 38 | | |
26 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
27 | 45 | | |
28 | 46 | | |
29 | 47 | | |
30 | | - | |
31 | | - | |
32 | | - | |
| 48 | + | |
33 | 49 | | |
34 | 50 | | |
| 51 | + | |
35 | 52 | | |
36 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
37 | 57 | | |
38 | | - | |
39 | 58 | | |
40 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
41 | 72 | | |
42 | 73 | | |
43 | | - | |
44 | | - | |
| 74 | + | |
| 75 | + | |
45 | 76 | | |
46 | 77 | | |
47 | 78 | | |
48 | 79 | | |
49 | | - | |
| 80 | + | |
50 | 81 | | |
51 | 82 | | |
52 | 83 | | |
53 | 84 | | |
54 | 85 | | |
| 86 | + | |
55 | 87 | | |
56 | 88 | | |
57 | 89 | | |
58 | 90 | | |
59 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
60 | 95 | | |
61 | 96 | | |
62 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
63 | 119 | | |
64 | 120 | | |
65 | 121 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
91 | 95 | | |
92 | 96 | | |
93 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
94 | 114 | | |
95 | 115 | | |
96 | 116 | | |
| |||
110 | 130 | | |
111 | 131 | | |
112 | 132 | | |
| 133 | + | |
113 | 134 | | |
114 | 135 | | |
115 | 136 | | |
| |||
165 | 186 | | |
166 | 187 | | |
167 | 188 | | |
| 189 | + | |
168 | 190 | | |
169 | 191 | | |
| 192 | + | |
170 | 193 | | |
171 | 194 | | |
172 | 195 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
| 85 | + | |
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
| |||
99 | 100 | | |
100 | 101 | | |
101 | 102 | | |
| 103 | + | |
102 | 104 | | |
103 | 105 | | |
104 | 106 | | |
| |||
Lines changed: 42 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
296 | 296 | | |
297 | 297 | | |
298 | 298 | | |
| 299 | + | |
299 | 300 | | |
300 | 301 | | |
301 | 302 | | |
302 | 303 | | |
303 | 304 | | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
304 | 312 | | |
305 | 313 | | |
306 | 314 | | |
| |||
341 | 349 | | |
342 | 350 | | |
343 | 351 | | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
344 | 386 | | |
345 | 387 | | |
346 | 388 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
47 | | - | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| |||
0 commit comments