Data Types in fastmat

To achieve high performance, fastmat is designed to support common data types only, namely

  • Floating point with single and double precision (float32 and float64)

  • Complex floating point with single and double precision (complex64 and complex128)

  • Signed integer of selected fixed sizes (int8, int16, int32, int64)

Some implementation of fastmat matrices use numpy under the hood. Although those could technically be able to deal with other data types offered by numpy as well, using other types than those listed above is disencouraged. This is important to ensure consistency throughout the intestines of fastmat, which is important for being able to reliably test the package.

The following sections detail the organization and handling of types in fastmat and explain the mechanisms how fastmat handles type promotion. The final section references the internal type API of fastmat.

Type handling

ftype

To distinguish between the supported data types fastmat uses the ftype internally as type identifier. All data type checks within the package as well as the type promotion logic is internally handled via these type numbers, that correspond directly to the associated numpy.dtype given in this table:

Data Type

numpy.dtype

fastmat ftype

Short

Signed Integer 8 bit Signed Integer 16 bit Signed Integer 32 bit Signed Integer 64 bit Single-precision Float Double-Precision Float Single-Precision Complex Double-Precision Complex

int8_t int16_t int32_t int64_t float32_t float64_t complex64_t complex128_t

0 1 2 3 4 5 6 7

i8 i16 i32 i64 f32 f64 c64 c128

Type promotion

Type promotion matrix of binary operators of kind f(A, B) as used throughout fastmat:

Type promotion

B

int

float

complex

i8

i16

i32

i64

f32

f64

c64

c128

A

int 8

i8

i16

i32

i64

f32

f64

c64

c128

int 16

i16

i16

i32

i64

f32

f64

c64

c128

int 32

i32

i32

i32

i64

f64

f64

c128

c128

int 64

i64

i64

i64

i64

f64

f64

c128

c128

float 32

f32

f32

f64

f64

f64

f64

c128

c128

float 64

f64

f64

f64

f64

f64

f64

c128

c128

complex 64

c64

c64

c128

c128

c64

c128

c64

c128

complex 128

c128

c128

c128

c128

c128

c128

c128

c128

Example:

The forward operator of a fastmat.Matrix of type float 32 will, if provided with an int 32 input vector, yield an output vector of type float 64.

Note

The output data type will be expanded to fit the mantissa of any of the operands best. As int 32 has a wider mantissa than float 32 offers, the example type promotion will yield float 64 to maintain accuracy.

Note

Data types will not be expanded automatically to the next larger data type for the sake of preventing overflows. You’ll need to specifically expand the data type – where necessary – by specifying minType=? during the generation of your fastmat.Matrix instance.

fastmat.core.types

fastmat.core.types.getFusedType()

Return fastmat type number for a given data type (or array).

Parameters
objobject

The object type or numpy.ndarray to query for.

Returns
ftype

The fastmat type number for that type.

Raises
TypeError

When the type is not supported by fastmat.

fastmat.core.types.getNumpyType()

Return numpy type number for a given data type (or array).

Parameters
objobject

The object type or numpy.ndarray to query for.

Returns
ntype

The numpy type number for that type.

Raises
TypeError

When the type is not supported by fastmat.

fastmat.core.types.getTypeEps()

Return eps for a given data type (or array).

Parameters
objobject

The object type or numpy.ndarray to query for.

Returns
np.float64_t

The epsilon value for that type.

Raises
TypeError

When the type is not supported by fastmat.

fastmat.core.types.getTypeMax()

Return the maximum representable value for a given data type (or array).

Parameters
objobject

The object type or numpy.ndarray to query for.

Returns
np.float64_t

The maximum representable value for that type.

Raises
TypeError

When the type is not supported by fastmat.

fastmat.core.types.getTypeMin()

Return the minimum representable value for a given data type (or array).

Parameters
objobject

The object type or numpy.ndarray to query for.

Returns
np.float64_t

The minimum representable value for that type.

Raises
TypeError

When the type is not supported by fastmat.

fastmat.core.types.isComplex()

Return whether a given data type or an array’s data type is complex.

Parameters
objobject

The object type to query for.

Returns
bool

True if the data type is of complex kind.

Raises
TypeError

When the type is not supported by fastmat.

fastmat.core.types.isFloat()

Return whether a given data type or an array’s data type is floating point.

Parameters
objobject

The object type to query for.

Returns
type

True if the data type is of floating point kind.

Raises
TypeError

When the type is not supported by fastmat.

fastmat.core.types.isInteger()

Return whether a given data type or an array’s data type is integer.

Parameters
objobject

The object type to query for.

Returns
type

True if the data type is of integer kind.

Raises
TypeError

When the type is not supported by fastmat.

fastmat.core.types.safeTypeExpansion()

Return a floating type expanding the given type with full accuracy.

Parameters
dtypeobject

A type object to be expanded to float without numerical accuracy loss.

Returns
object

The safely expanded datatype