dlite-arrays#
Simple API for accessing the data of multidimensional array properties.
The DLiteArray structure adds some basic functionality for accessing multidimensional array data. It it not a complete array library and do no memory management. It is neither optimised for speed, so don’t use it for writing optimised solvers.
Included features:
indexing
iteration
comparisons
reshaping
slicing
transpose
make_continuous
pretty printing
Typedefs
-
typedef struct _DLiteArray DLiteArray#
DLite n-dimensional arrays
-
typedef struct _DLiteArrayIter DLiteArrayIter#
Array iterator object.
Functions
-
DLiteArray *dlite_array_create(void *data, DLiteType type, size_t size, int ndims, const size_t *shape)#
Creates a new array object.
data
is a pointer to array data. No copy is done.type
is the type of each element.size
is the size of each element.ndims
is the number of dimensions.shape
is the size of each dimensions. Length:ndims
.Returns the new array or NULL on error.
-
DLiteArray *dlite_array_create_order(void *data, DLiteType type, size_t size, int ndims, const size_t *shape, int order)#
Like dlite_array_create(), but with argument
order
, which can have the values: ‘C’: row-major (C-style) order, no reordering. ‘F’: coloumn-major (Fortran-style) order, transposed order.
-
void dlite_array_free(DLiteArray *arr)#
Free an array object, but not the associated data.
-
size_t dlite_array_size(const DLiteArray *arr)#
Returns the memory size in bytes of array
arr
.
-
int dlite_array_is_continuous(const DLiteArray *arr)#
Returns non-zero if array
arr
describes a C-continuous memory layout.
-
void *dlite_array_index(const DLiteArray *arr, int *ind)#
Returns a pointer to data at index
ind
, whereind
is an array of lengtharr->ndims
.
-
int dlite_array_iter_init(DLiteArrayIter *iter, const DLiteArray *arr)#
Initialise array iterator object
iter
, for iteration over arrayarr
.Returns non-zero on error.
-
void dlite_array_iter_deinit(DLiteArrayIter *iter)#
Deinitialise array iterator object
iter
.
-
void *dlite_array_iter_next(DLiteArrayIter *iter)#
Returns the next element of from array iterator, or NULL if all elements has been visited.
-
int dlite_array_compare(const DLiteArray *a, const DLiteArray *b)#
Returns 1 is arrays
a
andb
are equal, zero otherwise.
-
DLiteArray *dlite_array_slice(const DLiteArray *arr, int *start, int *stop, int *step)#
Returns a new array object representing a slice of
arr
.start
,stop
andstep
has the same meaning as in Python and should be either NULL or arrays of lengtharr->ndims
.For
step[n] > 0
the range for dimensionn
is increasing (assumingstep[n]=1
):Forstart[n], start[n]+1, ... stop[n]-2, stop[n]-1
step[n] < 0
the range for dimensionn
is decreasing: (assumingstep[n]=1
):Like Python, negative values ofstart[n]-1, start[n]-2, ... stop[n]+1, stop[n]
start
orstop
counts from the back. Hence index-k
is equivalent toarr->shape[n]-|k|
.If
start
is NULL, it will default to zero for dimensionsn
with positivestep
andarr->shape[n]
for dimensions with negativestep
.If
stop
is NULL, it will default toarr->shape[n]
for dimensionsn
with positivestep
and zero for dimensions with negativestep
.If
step
is NULL, it defaults to one.Returns NULL on error.
Note
The above behavior is not fully consistent with Python for negative step sizes. While the range for negative steps in dlite is given above, Python returns the following range:
In Python, you can get the full reversed range by specifyingstart[n], start[n]-1, ... stop[n]+2, stop[n]+1
None
as the stop value. ButNone
is not a valid C integer. In dlite you can get the full reversed range by settingstop[n]
to zero.
-
DLiteArray *dlite_array_reshape(const DLiteArray *arr, int ndims, const size_t *shape)#
Returns a new array object representing
arr
with a new shape specified withndims
andshape
.shape
should be compatible with the old shape. The current implementation also requires thatarr
is C-continuous.Returns NULL on error.
-
DLiteArray *dlite_array_transpose(DLiteArray *arr)#
Returns a new array object corresponding to the transpose of
arr
(that is, an array with reversed order of dimensions).Returns NULL on error.
Note
This function does not change the underlying data. If you want to convert between C and Fortran array layout, you should call dlite_make_continuous() on the array object returned by this function.
-
void *dlite_array_make_continuous(DLiteArray *arr)#
Creates a continuous copy of the data for
arr
(using malloc()) and updatesarr
.Returns a the new copy of the data or NULL on error.
-
int dlite_array_printf(FILE *fp, const DLiteArray *arr, int width, int prec)#
Print array
arr
to streamfp
.The
width
andprec
arguments corresponds to the printf() minimum field width and precision/length modifier. If you set them to -1, a suitable value will selected according totype
. To ignore their effect, setwidth
to zero orprec
to -2.Returns non-zero on error.
-
struct _DLiteArray#
- #include <dlite-arrays.h>
DLite n-dimensional arrays
Public Members
-
void *data#
pointer to array data
-
size_t size#
size of each element in bytes
-
int ndims#
number of dimensions
-
size_t *shape#
dimension sizes [ndims]
-
int *strides#
strides, that is number of bytes between two following elements along each dimension [ndims] Note: strides can be negative, so we must use a signed type.
-
void *data#
-
struct _DLiteArrayIter#
- #include <dlite-arrays.h>
Array iterator object.
Public Members
-
const DLiteArray *arr#
pointer to the array we are iterating over
-
int *ind#
the current index
-
const DLiteArray *arr#