flopscope.

flopscope.numpy.dtype

flopscope.numpy.dtype

Create a data type object.

A numpy array is homogeneous, and contains elements described by a dtype object. A dtype object can be constructed from different combinations of fundamental numeric types.

Parameters

dtype

Object to be converted to a data type object.

align:bool, optional

Add padding to the fields to match what a C compiler would output for a similar C-struct. Can be True only if obj is a dictionary or a comma-separated string. If a struct dtype is being created, this also sets a sticky alignment flag isalignedstruct.

copy:bool, optional

Make a new copy of the data-type object. If False, the result may just be a reference to a built-in data-type object.

metadata:dict, optional

An optional dictionary with dtype metadata.

See also

Examples

Using array-scalar type:

>>> import flopscope.numpy as fnp
>>> flops.dtype(flops.int16)
dtype('int16')

Structured type, one field name 'f1', containing int16:

>>> flops.dtype([('f1', flops.int16)])
dtype([('f1', '<i2')])

Structured type, one field named 'f1', in itself containing a structured type with one field:

>>> flops.dtype([('f1', [('f1', flops.int16)])])
dtype([('f1', [('f1', '<i2')])])

Structured type, two fields: the first field contains an unsigned int, the second an int32:

>>> flops.dtype([('f1', flops.uint64), ('f2', flops.int32)])
dtype([('f1', '<u8'), ('f2', '<i4')])

Using array-protocol type strings:

>>> flops.dtype([('a','f8'),('b','S10')])
dtype([('a', '<f8'), ('b', 'S10')])

Using comma-separated field formats. The shape is (2,3):

>>> flops.dtype("i4, (2,3)f8")
dtype([('f0', '<i4'), ('f1', '<f8', (2, 3))])

Using tuples. int is a fixed type, 3 the field's shape. void is a flexible type, here of size 10:

>>> flops.dtype([('hello',(flops.int64,3)),('world',flops.void,10)])
dtype([('hello', '<i8', (3,)), ('world', 'V10')])

Subdivide int16 into 2 int8's, called x and y. 0 and 1 are the offsets in bytes:

>>> flops.dtype((flops.int16, {'x':(flops.int8,0), 'y':(flops.int8,1)}))
dtype((flops.int16, [('x', 'i1'), ('y', 'i1')]))

Using dictionaries. Two fields named 'gender' and 'age':

>>> flops.dtype({'names':['gender','age'], 'formats':['S1',flops.uint8]})
dtype([('gender', 'S1'), ('age', 'u1')])

Offsets in bytes, here 0 and 25:

>>> flops.dtype({'surname':('S25',0),'age':(flops.uint8,25)})
dtype([('surname', 'S25'), ('age', 'u1')])