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
Functions
-
DLiteArray *dlite_array_create(void *data, DLiteType type, size_t size, int ndims, const size_t *shape)#
Creates a new array object.
datais a pointer to array data. No copy is done.typeis the type of each element.sizeis the size of each element.ndimsis the number of dimensions.shapeis 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
arrdescribes a C-continuous memory layout.
-
void *dlite_array_index(const DLiteArray *arr, int *ind)#
Returns a pointer to data at index
ind, whereindis 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
aandbare 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,stopandstephas the same meaning as in Python and should be either NULL or arrays of lengtharr->ndims.For
step[n] > 0the range for dimensionnis increasing (assumingstep[n]=1):Forstart[n], start[n]+1, ... stop[n]-2, stop[n]-1
step[n] < 0the range for dimensionnis decreasing: (assumingstep[n]=1):Like Python, negative values ofstart[n]-1, start[n]-2, ... stop[n]+1, stop[n]
startorstopcounts from the back. Hence index-kis equivalent toarr->shape[n]-|k|.If
startis NULL, it will default to zero for dimensionsnwith positivestepandarr->shape[n]for dimensions with negativestep.If
stopis NULL, it will default toarr->shape[n]for dimensionsnwith positivestepand zero for dimensions with negativestep.If
stepis 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
Noneas the stop value. ButNoneis 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
arrwith a new shape specified withndimsandshape.shapeshould be compatible with the old shape. The current implementation also requires thatarris 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
arrto streamfp.The
widthandprecarguments 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, setwidthto zero orprecto -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#