Table of Contents

Class NPTypeCodeExtensions

Namespace
NumSharp
Assembly
NumSharp.dll
public static class NPTypeCodeExtensions
Inheritance
NPTypeCodeExtensions
Inherited Members

Methods

AsNumpyDtypeName(NPTypeCode)

Returns the equivalent numpy's name, e.g. Int32 is np.int32, therefore the return is "int32". Error texts quote it as dtype('…'), so it is the NumPy name a NumPy user would see for that data.

public static string AsNumpyDtypeName(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

The storage code.

Returns

string

NumPy's dtype name; "str" for String (NumPy's np.dtype('U').name; NumPy has no dtype named "string"); "" for Empty. NumSharp's own dtypes answer with the NumPy dtype they stand in for: Char is "uint16" (it promotes and computes as uint16) and Decimal is "float64", so a decimal array reads as float64 in a NumPy-worded error.

Exceptions

ArgumentOutOfRangeException

typeCode is not a defined storage code.

AsType(NPTypeCode)

Convert NPTypeCode into its Type

public static Type AsType(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

Type

AsType(NPTypeCode?)

Convert a nullable NPTypeCode into its Type, or null when the code is null — the "infer / preserve dtype" sentinel the dtype-carrying APIs use. Lets a call site pass either an NPTypeCode or an NPTypeCode? to a Type-typed dtype parameter uniformly via x.AsType().

public static Type AsType(this NPTypeCode? typeCode)

Parameters

typeCode NPTypeCode?

Returns

Type

CompareTo(NPTypeCode, NPTypeCode)

public static int CompareTo(this NPTypeCode left, NPTypeCode right)

Parameters

left NPTypeCode
right NPTypeCode

Returns

int

GetAccumulatingType(NPTypeCode)

Gets the dtype that is used as accumulation in case when statistics are computed like sum(NDArray, NDAxis?, DType, NDArray, bool, object, NDArray)

public static NPTypeCode GetAccumulatingType(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

NPTypeCode

dtype in case when statistics are computed like sum(NDArray, NDAxis?, DType, NDArray, bool, object, NDArray)

GetComputingType(NPTypeCode)

Gets the dtype that is used as return type in case when statistics are computed with high decimal precision like sin(NDArray, NDArray, NDArray, DType)

public static NPTypeCode GetComputingType(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

NPTypeCode

dtype in case when statistics are computed like mean(NDArray, NDAxis?, DType, NDArray, bool, NDArray)

GetDefaultValue(NPTypeCode)

Gets the default value of typeCode.

public static object GetDefaultValue(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

object

GetOneValue(NPTypeCode)

Gets the multiplicative identity (1) for a type. Used for reduction identity in Product operations.

public static object GetOneValue(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

object

GetTypeCode(Type)

Extracts NPTypeCode from given Type.

public static NPTypeCode GetTypeCode(this Type type)

Parameters

type Type

Returns

NPTypeCode

Remarks

In case there was no successful cast to NPTypeCode, return will be Empty

GetTypeCode<T>()

Extracts NPTypeCode from given T.

public static NPTypeCode GetTypeCode<T>()

Returns

NPTypeCode

Type Parameters

T

Remarks

In case there was no successful cast to NPTypeCode, return will be Empty

IsFloatingPoint(NPTypeCode)

Is typeCode a floating point type (float, double, or decimal)?

public static bool IsFloatingPoint(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

bool

IsInteger(NPTypeCode)

Is typeCode an integer type (signed or unsigned)?

public static bool IsInteger(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

bool

Remarks

Excludes Boolean and Char.

IsNumerical(NPTypeCode)

Returns true if typecode is a number (incl. bool, char, Half and Complex).

public static bool IsNumerical(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

bool

IsRealNumber(NPTypeCode)

Is typeCode a float, double, complex or decimal?

public static bool IsRealNumber(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

bool

IsSigned(NPTypeCode)

Is typeCode a float, double, complex or decimal?

public static bool IsSigned(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

bool

IsSimdCapable(NPTypeCode)

Is typeCode capable of SIMD operations?

public static bool IsSimdCapable(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

bool

Remarks

Returns true if the type can use SIMD when hardware support is available. Does not check if SIMD hardware is actually available. Boolean, Char, and Decimal cannot use SIMD.

IsUnsigned(NPTypeCode)

Is typeCode a uint, byte, ulong and so on.

public static bool IsUnsigned(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

bool

IsValidNPType(Type)

Checks if given Type has a match in NPTypeCode.

public static bool IsValidNPType(this Type type)

Parameters

type Type

Returns

bool

SizeOf(NPTypeCode)

Gets the size of given typeCode

public static int SizeOf(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

Returns

int

Remarks

The size is computed by SizeOf<T>()

ToTYPECHAR(NPTypeCode)

NumPy's type character for the dtype stored in typeCode: that lane's descriptor character, exactly the np.dtype(...).char NumPy reports on this host.

public static NPY_TYPECHAR ToTYPECHAR(this NPTypeCode typeCode)

Parameters

typeCode NPTypeCode

The storage code.

Returns

NPY_TYPECHAR

The character as an NPY_TYPECHAR. Platform-dependent for the 32- and 64-bit integers: int32 is NPY_LONGLTR on Windows and NPY_INTLTR elsewhere, int64 NPY_LONGLONGLTR on Windows and NPY_LONGLTR elsewhere. Complex is NPY_CDOUBLELTR (not the kind letter NPY_COMPLEXLTR). For every dtype np.dtype can parse outside the string lane, ToTypeCode(NPY_TYPECHAR) of the result is typeCode again.

Exceptions

ArgumentException

typeCode is Empty: no dtype, so no character.

NotSupportedException

typeCode is one of NumSharp's own dtypes (Decimal, Char): NumPy has no such dtype, so no NumPy character names it. Their descriptors' own char is a letter of NumSharp's (decimal 'A', char 'u', as NumPy's user dtypes have their own), which no NPY_TYPECHAR member holds. Or typeCode is String: the lane of the string classes (S, U and StringDType) names no one dtype, so ask the array's own descriptor (a.dtype.@char) instead.

ToTypeCode(NPY_TYPECHAR)

The storage code of the dtype a NumPy type character names: NumPy's PyArray_DescrFromType(c), then that descriptor's lane. It IS np.dtype(c.ToString()).GetTypeCode(), so the character table cannot drift away from the parser (a hand-written switch here once read 'D' as float64 and Windows' 'l' as int64).

public static NPTypeCode ToTypeCode(this NPY_TYPECHAR typeCode)

Parameters

typeCode NPY_TYPECHAR

A NumPy type character: an NPY_TYPECHAR member, or a valid character that has none, cast to the enum ((NPY_TYPECHAR)'n', (NPY_TYPECHAR)'p').

Returns

NPTypeCode

The NPTypeCode NumSharp stores the dtype in, resolved for THIS host like C itself: NPY_LONGLTR is Int32 on Windows and Int64 on Linux and macOS. 'g' and 'G' come back as Double and Complex, the lanes np.longdouble and np.clongdouble use. The string characters (bytes 'S'/'a'/'c', str 'U', StringDType 'T') all come back as String, the one lane of the three string classes: the class and the width are gone, so keep the descriptor (np.dtype(...)) when either matters.

Remarks

A KIND letter is not a type character. NPY_UNSIGNEDLTR ('u') is refused, as np.dtype('u') is in NumPy, and NPY_COMPLEXLTR has the value of NPY_CHARLTR, so it is read as NumPy's one-byte string S1 (the String lane), never as complex. Use NPY_CDOUBLELTR for complex128.

Being the parser's answer, it reads every value the way np.dtype reads that one character, including values that are no type character: a control character below '\x18' is a TYPE NUMBER to NumPy (np.dtype('\x0c') is float64), so ((NPY_TYPECHAR)0).ToTypeCode() is Boolean, not a refusal. Validate the value first when it comes from outside.

Exceptions

ArgumentOutOfRangeException

typeCode is outside 0..0xFFFF, so it is no UTF-16 character.

TypeError

NumPy does not know the character either: NumPy's data type 'u' not understood (a digit too: NumPy reads '3' as a comma string whose one field has no dtype, data type '' not understood).

SyntaxError

The comma ',': a comma string to NumPy, whose field ',' is no Python literal (invalid syntax (<unknown>, line 1), NumPy's own error).

NotSupportedException

NumSharp has no such dtype (complex64 'F', void 'V', object 'O'), or the dtype has no storage lane yet (datetime64 'M', timedelta64 'm').