Class DTypeMeta
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
The DType CLASS — NumPy's PyArray_DTypeMeta (type(np.dtype('f8')), i.e.
np.dtypes.Float64DType). One live object per dtype class; a DType is an INSTANCE of it
(the descriptor). This is NEP 41/42's two-level model: behaviour lives on the class as virtual "slots"
(CommonDType(DTypeMeta), CommonInstance(DType, DType), DefaultDescr(),
EnsureCanonical(DType), DiscoverDescrFromObject(object), IsKnownScalarType(Type), the
casting implementations), parameters live on the instance (a datetime unit, a byte order).
public abstract class DTypeMeta
- Inheritance
-
DTypeMeta
- Derived
- Inherited Members
Remarks
Compatibility contract. NPTypeCode stays the storage / kernel discriminator: every meta that has storage exposes it as TypeCode, and a future storage-backed dtype adds one enum member and registers it here — the enum is an optimisation the registry hands out, not the identity. A meta without storage (DatetimeDTypeMeta in Stage A) has TypeCode == Empty; allocating storage for its descriptors raises NotSupportedException naming the missing piece.
Type numbers follow NumPy's NPY_TYPES (LP64 convention: Int32 = NPY_INT (5),
Int64 = NPY_LONG (7), Half = 23, DateTime = 21, TimeDelta = 22); the two NumSharp-only
dtypes take the user-defined range (Decimal = 256, Char = 257), which is also what makes their
descriptors report isbuiltin == 2. The number ORDER is load-bearing: the builtin
common_dtype defers to the operand with the larger type number ("the more generic one handles it"),
exactly as NumPy's default_builtin_common_dtype does.
NotImplemented is spelled null: CommonDType(DTypeMeta) returns null when this class
does not know the other one, and CommonDType(DTypeMeta, DTypeMeta) then asks the other class
(NEP 42's two-sided __common_dtype__ protocol) before raising DTypePromotionError.
NEP 55 reservations. HasReferences, GetClearLoop(DType) and
GetFillZeroLoop(DType) are the slots a reference-holding dtype (StringDType) will override; the
builtins report false / null.
Constructors
DTypeMeta(string, string, int, Type, string, NPTypeCode, char, char, int, int, DTypeFlags, IReadOnlyList<string>)
Builds a DType class. Called by the registry / subclasses only — every class is a singleton reachable via
DTypeRegistry and np.dtypes.
protected DTypeMeta(string name, string modulePath, int typeNum, Type scalarType, string scalarName, NPTypeCode typeCode, char kind, char typeChar, int itemSize, int alignment, DTypeFlags flags, IReadOnlyList<string> aliases = null)
Parameters
namestringNumPy's class name (
"Int32DType","DateTime64DType","_PyLongDType").modulePathstringThe module the class lives in —
"numpy.dtypes"for the NumPy-mirrored classes,"numsharp.dtypes"for the NumSharp-only ones — so ToString() renders NumPy's<class 'numpy.dtypes.Int32DType'>.typeNumintNumPy's
type_num(see the class remarks); -1 for abstract classes.scalarTypeTypeThe C# scalar type of one element (
typeof(int)); null when there is none yet.scalarNamestringNumPy's
dtype.type.name("int32","datetime64","decimal").typeCodeNPTypeCodeThe storage discriminator, Empty when the class has no storage.
kindcharNumPy's
dtype.kindcharacter ('b' 'i' 'u' 'f' 'c' 'M' 'm' 'U').typeCharcharNumPy's
dtype.char('?' 'b' 'B' 'h' 'H' 'i' 'I' 'l' 'L' 'e' 'f' 'd' 'D' 'M' 'm').itemSizeintElement size in bytes (-1 for abstract classes).
alignmentintRequired alignment in bytes (NumPy's
dtype.alignment).flagsDTypeFlagsThe DTypeFlags (legacy / abstract / parametric / numeric).
aliasesIReadOnlyList<string>Alias class names NumPy also exports (
"IntDType"forInt32DType).
Properties
Aliases
Alias class names NumPy also exports for this class (e.g. ByteDType for Int8DType).
public IReadOnlyList<string> Aliases { get; }
Property Value
Alignment
Required alignment in bytes.
public int Alignment { get; }
Property Value
Flags
The class flags.
public DTypeFlags Flags { get; }
Property Value
FullName
The fully qualified class name: "numpy.dtypes.Int32DType".
public string FullName { get; }
Property Value
HasReferences
Whether items of this dtype hold references that must be cleared (NumPy's NPY_ITEM_REFCOUNT); false for every builtin.
public virtual bool HasReferences { get; }
Property Value
HasStorage
True when descriptors of this class can back an NDArray today (a storage NPTypeCode exists).
public bool HasStorage { get; }
Property Value
IsAbstract
An abstract dtype (no instances; promotion only).
public bool IsAbstract { get; }
Property Value
IsLegacy
A legacy (pre-NEP 41 layout) dtype — every builtin and the datetime pair.
public bool IsLegacy { get; }
Property Value
IsNumeric
A numeric dtype (bool, integer, float, complex).
public bool IsNumeric { get; }
Property Value
IsParametric
A parametric dtype (instances carry parameters — a unit, a length).
public bool IsParametric { get; }
Property Value
ItemSize
Element size in bytes (-1 for abstract classes).
public int ItemSize { get; }
Property Value
Kind
NumPy's dtype.kind character ('b' bool, 'i'/'u' integers, 'f', 'c', 'M', 'm', 'U').
public char Kind { get; }
Property Value
ModulePath
The module path used in ToString(): "numpy.dtypes" or "numsharp.dtypes".
public string ModulePath { get; }
Property Value
Name
NumPy's class name: "Int32DType", "DateTime64DType", "_PyLongDType".
public string Name { get; }
Property Value
ScalarName
NumPy's dtype.type.name: "int32", "bool", "datetime64", "decimal".
public string ScalarName { get; }
Property Value
ScalarType
The C# scalar type of one element (NumPy's DType.type); null when the class has no C# scalar yet.
public Type ScalarType { get; }
Property Value
Singleton
The canonical native descriptor of a non-parametric class — NumPy's DType.singleton, the object
np.dtype('i8') returns every time (np.dtype('i8') is np.dtype('i8')). Null for abstract
classes; for the parametric datetime pair it is the generic-unit descriptor and DefaultDescr()
hands out COPIES of it (a copy reports isbuiltin == 0, as in NumPy).
public DType Singleton { get; }
Property Value
TypeChar
NumPy's dtype.char — the unique one-character type code.
public char TypeChar { get; }
Property Value
TypeCode
The storage / kernel discriminator; Empty when the class has no storage.
public NPTypeCode TypeCode { get; }
Property Value
TypeNum
NumPy's type_num (NPY_TYPES, LP64 convention; 256+ for the NumSharp-only dtypes; -1 for abstract).
public int TypeNum { get; }
Property Value
WithinDTypeCastingImpl
The casting implementation between two instances of this class (within_dtype_castingimpl).
public CastingImpl WithinDTypeCastingImpl { get; }
Property Value
Methods
CommonDType(DTypeMeta)
common_dtype: the class that can hold values of both this class and other, or
null (NumPy's NotImplemented) when this class does not know the other — the caller
then asks other the mirrored question.
public abstract DTypeMeta CommonDType(DTypeMeta other)
Parameters
otherDTypeMeta
Returns
CommonInstance(DType, DType)
common_instance: for a PARAMETRIC class, the descriptor that can hold values of two descriptors
of this class (the unit GCD for datetimes; the longer length for strings). Non-parametric classes return
their default descriptor.
public virtual DType CommonInstance(DType descr1, DType descr2)
Parameters
Returns
DefaultDescr()
default_descr: the descriptor a bare class stands for — the singleton for the builtins, a fresh
generic-unit descriptor for datetime64 / timedelta64, the default (int64/float64/complex128)
for the abstract NEP 50 scalar classes.
public abstract DType DefaultDescr()
Returns
DiscoverDescrFromObject(object)
discover_descr_from_pyobject: the descriptor a scalar of this class should get (the default for
every non-parametric class).
public virtual DType DiscoverDescrFromObject(object obj)
Parameters
objobject
Returns
EnsureCanonical(DType)
ensure_canonical: the native-byte-order form of descr (the descriptor itself when
already native).
public virtual DType EnsureCanonical(DType descr)
Parameters
descrDType
Returns
GetCastingImpl(DTypeMeta)
The CastingImpl from this class to to (the within-dtype implementation when
to is this class), or null when no cast exists — NumPy's PyArray_GetCastingImpl.
public CastingImpl GetCastingImpl(DTypeMeta to)
Parameters
toDTypeMeta
Returns
GetClearLoop(DType)
get_clear_loop: a loop that releases the references an item holds; null when HasReferences is false.
public virtual NDInnerLoopFunc GetClearLoop(DType descr)
Parameters
descrDType
Returns
GetFillZeroLoop(DType)
get_fill_zero_loop: a loop that writes this dtype's zero; null when a zeroed buffer already is the zero.
public virtual NDInnerLoopFunc GetFillZeroLoop(DType descr)
Parameters
descrDType
Returns
Instantiate()
The NumPy class call Int8DType(): the no-argument instantiation returns the singleton for a
non-parametric legacy class, and raises NumPy's TypeError for a parametric one
(DateTime64DType() → "Preliminary-API: Flexible/Parametric legacy DType … can only be instantiated
using np.dtype(...)"), verbatim.
public virtual DType Instantiate()
Returns
IsKnownScalarType(Type)
is_known_scalar_type: whether a C# scalar of type belongs to this class.
public virtual bool IsKnownScalarType(Type type)
Parameters
typeType
Returns
ToString()
NumPy's rendering of the class object: <class 'numpy.dtypes.Int32DType'>.
public override string ToString()