Class PyScalarDTypeMeta
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
NumPy's abstract DType classes for Python scalars — numpy.dtypes._PyLongDType,
_PyFloatDType, _PyComplexDType (abstractdtypes.c). They are how NEP 50 "weak" promotion
works: a literal contributes only its abstract CLASS to result_type (no descriptor, so no value or
width), and the class's common_dtype(DTypeMeta) lets the other operand's dtype win within its kind
(int8 + 300 → int8, float32 + 1e300 → float32) while still lifting across kinds
(int8 + 1.5 → float64). A literal that survives alone falls back to the class default
(int64 / float64 / complex128).
public sealed class PyScalarDTypeMeta : DTypeMeta
- Inheritance
-
PyScalarDTypeMeta
- Inherited Members
Remarks
C# has no Python-literal/NumPy-scalar split, so the mapping is by TYPE (the house NEP 50 rule): the integer
primitives, float/double and Complex are weak; bool, char, Half,
decimal and every NDArray (0-d included) are strong.
Properties
PyKind
Which literal category this class stands for.
public PyScalarKind PyKind { get; }
Property Value
Methods
common_dtype(DTypeMeta)
Ports of int_common_dtype / float_common_dtype / complex_common_dtype. The builtin
classes handle most pairs themselves (their common_dtype is asked FIRST and answers for a literal of
their own or a lower kind); these implement the remaining directions: a weak int with bool is the
default integer, a weak float or complex with an integer or bool class is float64 /
complex128, and two abstract classes resolve to the higher category. The NumSharp-only storage
classes (Decimal, Char) are treated as the numeric builtins they masquerade as — Char is an unsigned
integer here, so result_type(char_array, 1.5) is float64 exactly like uint16 — rather
than through NumPy's opaque-user-dtype fallback; a genuinely foreign legacy class still takes that
fallback (promote with the smallest concrete type of the literal's category and let it decide).
public override DTypeMeta common_dtype(DTypeMeta other)
Parameters
otherDTypeMetaThe other operand's class.
Returns
- DTypeMeta
The common class, or null (NotImplemented) when the other class must answer.
Exceptions
- ArgumentNullException
otheris null.
default_descr()
The default descriptor a lone literal resolves to: int64 (NumPy's intp), float64, complex128.
public override DType default_descr()
Returns
- DType
The singleton of the default class.
discover_descr_from_pyobject(object)
The slot discover_descr_from_pyobject (NumPy's discover_descriptor_from_py{long,float,complex},
abstractdtypes.c): the descriptor array coercion gives a Python scalar of this category, which is also what
np.result_type(literal) answers (a lone operand skips promotion and returns the dtype of
np.asarray(literal)). For a Python int NumPy's discover_descriptor_from_pylong reads the VALUE:
int64 when it fits (NumPy's intp), uint64 when only that holds it (264 - 1;
np.result_type(263) is uint64), and NumPy's object descriptor past 64 bits, which NumSharp refuses. A
Python float is float64 and a Python complex complex128 whatever their value
(discover_descriptor_from_pyfloat / _pycomplex). As an operand among others the literal stays weak
and its value never matters (np.result_type(np.uint8, 2**63) is uint8).
public override DType discover_descr_from_pyobject(object obj)
Parameters
objobjectA value of this category in the C# boundary map: an integer primitive or BigInteger for
_PyLongDType.
Returns
- DType
The default descriptor, or uint64 for a Python int past int64 that uint64 holds.
Exceptions
- NotSupportedException
A Python int past 64 bits (NumPy builds an object array, a dtype NumSharp does not have).
is_known_scalar_type(Type)
Weak literals map onto the C# primitives of their category.
public override bool is_known_scalar_type(Type type)
Parameters
typeTypeThe C# type of the literal.
Returns
- bool
True when
typeis a C# primitive of this class's category.