| |
#define | array_SIZEOF_DTYPE(size, dtype) |
get size of datatype. | |
#define | array_DTYPESTRING(dtstring, dtype) |
convert datatype to a string representation. | |
#define | array_NUMEL(a) ( ((a)->nbytes)/((a)->dtype_size) ) |
get number of elements in array. | |
#define | array_DTYPEPRINT(out, dtype, mem) |
Print an array-datatype. | |
#define | array_MEMSET(mem, dtype, val) |
set the memory mem to value of type dtype. | |
#define | array_INDEX1(array, dtype, i1) |
fast 1D array indexing typecasting to C-type. | |
#define | array_INDEX2(array, dtype, i1, i2) |
fast 2D array indexing typecasting to C-type. | |
#define | array_INDEX3(array, dtype, i1, i2, i3) |
fast 3D array indexing typecasting to C-type. | |
#define | array_INDEXMEM1(array, i1) |
fast 1D array indexing returning a void pointer. | |
#define | array_INDEXMEM2(array, i1, i2) |
fast 2D array indexing returning a void pointer. | |
#define | array_INDEXMEM3(array, i1, i2, i3) |
fast 3D array indexing returning a void pointer. | |
enum | DType { CHAR = 0, UINT, INT, LONG, ULONG, FLOAT, DOUBLE } |
Data-types for Array struct. More... | |
Array * | array_new (DType dtype, uint ndim, const uint *dims) |
Initialize new array struct. | |
Array * | array_new2 (DType dtype, uint ndim,...) |
Initialize new array struct. | |
Array * | array_fromptr (DType dtype, uint ndim, void *data, const uint *size) |
Initialize new array struct. | |
Array * | array_fromptr2 (DType dtype, uint ndim, void *data,...) |
Initialize new array struct. | |
Array * | array_new_dummy (DType dtype, uint ndim,...) |
create array that is filled with values from 1 to n over all dimensions. | |
Array * | array_randunif (unsigned long seed, uint ndim,...) |
create array that is filled with random values from [0, ..., 1]. | |
bool | array_comparable (const Array *a, const Array *b) |
Compare two array's dimensions and datatype. | |
Array * | array_copy (const Array *in, bool allocdata) |
makes a copy of an array. | |
void | array_free (Array *a) |
free all memory associated with the array. | |
void | array_shuffle (Array *a, unsigned long seed) |
shuffle the entries of an array. | |
void | array_typecast (Array *a, DType target_type) |
Array-typecast. | |
void * | array_index (const Array *a, uint *idx) |
Index the array. | |
void * | array_index2 (const Array *a,...) |
Index the array. | |
Array * | array_slice (const Array *a, const char *slicedesc) |
Extract sub-arrays from array. | |
Array * | array_concatenate (const Array *a, const Array *b, int dim) |
concatenate two arrays. | |
int | array_scale (Array *a, double x) |
multiply all entries in array with x. | |
void | array_reverse (Array *a) |
Reverse order of elements in a (in-place). | |
int | array_dimred (Array *a) |
Delete unnecessary dimensions (of size 1). | |
void * | array_max (const Array *a) |
Get maximum element from array. | |
void * | array_min (const Array *a) |
Get minimum element from array. | |
Array * | array_convert_rowcolmajor (Array *a, bool alloc) |
convert a row-major array to col-major. | |
void | array_calc_colindex (ulong idx, const uint *size, uint nsize, uint *index) |
calculate the column-major index-tuple given the offset from element 0. | |
void | array_calc_rowindex (ulong idx, const uint *size, uint nsize, uint *index) |
calculate the row-major index-tuple given the offset from element 0. | |
void | array_print (Array *a, uint nel_per_dim, FILE *out) |
print an array. | |
void | array_dtype_to_double (double *out, void *mem, DType dt) |
cast memory of type dt in location mem to double. |
STATUS: work in progress Numerical Arrays.
This is an efficient implementation of a numerical array of
The library provides some convenient functionality:
#define array_DTYPEPRINT | ( | out, | ||
dtype, | ||||
mem | ||||
) |
switch( dtype ){ \ case CHAR: \ fprintf( out, "%c", *(char*)(mem) ); break; \ case UINT: \ fprintf( out, "%i", *(uint*)(mem) ); break; \ case INT: \ fprintf( out, "%i", *(int*)(mem) ); break; \ case LONG: \ fprintf( out, "%li", *(long*)(mem) ); break; \ case ULONG: \ fprintf( out, "%li", *(ulong*)(mem) ); break; \ case FLOAT: \ fprintf( out, "%.2f", *(float*)(mem) ); break; \ case DOUBLE: \ fprintf( out, "%.2f", *(double*)(mem) ); break; \ default: \ warnprintf("Do not know this datatype: %i\n", dtype ); \ }
Print an array-datatype.
out | FILE* output stream | |
dtype | one of DType | |
mem | void* memory containing the to-be-printed value |
#define array_DTYPESTRING | ( | dtstring, | ||
dtype | ||||
) |
switch( dtype ){ \ case CHAR: \ dtstring="char";break; \ case UINT: \ dtstring="uint";break; \ case INT: \ dtstring="int";break; \ case LONG: \ dtstring="long";break; \ case ULONG: \ dtstring="unsigned long"; break; \ case FLOAT: \ dtstring="float"; break; \ case DOUBLE: \ dtstring="double"; break; \ default: \ warnprintf("Do not know this datatype: %i\n", dtype ); \ }
convert datatype to a string representation.
dtstring | (output), char* to hold the string | |
dtype | (input) the datatype |
#define array_INDEX1 | ( | array, | ||
dtype, | ||||
i1 | ||||
) |
(*((dtype*)( ((array)->data) + \ ((i1)*((array)->dtype_size)) )))
fast 1D array indexing typecasting to C-type.
index the array and assume only one dimension (not checked). These indexing macros are not safe but as fast as you can get. For more convenience, you can use the dimension independant array_index() and array_index2() functions.
Example:
double a; a = array_INDEX1( a, double, 10 ); // accesses a[10] and converts to double
array | an Array object | |
dtype | a valid c-data type (int, float, double, ... ) | |
i1,...,iN | the index for each of the dimensions |
#define array_INDEX2 | ( | array, | ||
dtype, | ||||
i1, | ||||
i2 | ||||
) |
#define array_INDEX3 | ( | array, | ||
dtype, | ||||
i1, | ||||
i2, | ||||
i3 | ||||
) |
#define array_INDEXMEM1 | ( | array, | ||
i1 | ||||
) |
( ((array)->data) + \ ((i1)*((array)->dtype_size)) )
fast 1D array indexing returning a void pointer.
index the array and assume only one dimension (not checked). These indexing macros are not safe but as fast as you can get. For more convenience, you can use the dimension independant array_index() and array_index2() functions.
Example:
void *a; a = array_INDEXMEM1( a, 10 ); // accesses a[10] of any type
array | an Array object | |
i1,...,iN | the index for each of the dimensions |
#define array_INDEXMEM2 | ( | array, | ||
i1, | ||||
i2 | ||||
) |
#define array_INDEXMEM3 | ( | array, | ||
i1, | ||||
i2, | ||||
i3 | ||||
) |
#define array_MEMSET | ( | mem, | ||
dtype, | ||||
val | ||||
) |
switch( dtype ){ \ case CHAR: \ *((char*)(mem)) = (char)val;break; \ case UINT: \ *((uint*)(mem)) = (uint)val;break; \ case INT: \ *((int*)(mem)) = (int)val;break; \ case LONG: \ *((long*)(mem)) = (long)val;break; \ case ULONG: \ *((ulong*)(mem)) = (ulong)val;break; \ case FLOAT: \ *((float*)(mem)) = (float)val;break; \ case DOUBLE: \ *((double*)(mem)) = (double)val;break; \ default: \ warnprintf("Do not know this datatype: %i\n", dtype ); \ }
set the memory mem to value of type dtype.
(out) | void * mem | |
dtype | one of DType | |
val | int,float,double... |
#define array_NUMEL | ( | a | ) | ( ((a)->nbytes)/((a)->dtype_size) ) |
#define array_SIZEOF_DTYPE | ( | size, | ||
dtype | ||||
) |
switch( dtype ) { \ case CHAR: \ size=sizeof(char);break; \ case UINT: \ size=sizeof(uint);break; \ case INT: \ size=sizeof(int);break; \ case LONG: \ size=sizeof(long);break; \ case ULONG: \ size=sizeof(unsigned long); break; \ case FLOAT: \ size=sizeof(float); break; \ case DOUBLE: \ size=sizeof(double); break; \ default: \ warnprintf("Do not know this datatype: %i\n", dtype ); \ }
get size of datatype.
fill variable size with the size of the datatype dtype in bytes. E.g.
array_SIZEOF_DTYPE( array->dtype_size, dtype );
size | (output) uint to hold the size | |
dtype | (input) the datatype |
enum DType |
concatenate two arrays.
The two arrays must have the same number of elements in all dimensions except in the concatenated dimension.
Example:
[ 1 2 [ 7 8 3 4 9 10 5 6 ] 11 12] => [ 1 2 3 4 5 6 7 8 9 10 11 12 ]
a,b | the two arrays to concatenate (must be 1D/2D arrays of arbitrary type); if one of them is NULL, return a copy of the other one | |
dim | the dimension along which they are to be concatenated (0-rows, 1-columns) |
int array_dimred | ( | Array * | a | ) |
void array_dtype_to_double | ( | double * | out, | |
void * | mem, | |||
DType | dt | |||
) |
void array_free | ( | Array * | a | ) |
Initialize new array struct.
Memory for the data is not allocated but set to data.
dtype | the datatype of the array | |
ndim | number of dimensions | |
data | the data for the array | |
... | the number of elements in each of the dimensions |
Initialize new array struct.
Memory for the data is not allocated but set to data.
dtype | the datatype of the array | |
ndim | number of dimensions | |
data | the data for the array | |
... | the number of elements in each of the dimensions |
Index the array.
You need to provide as many arguments as there are dimensions in the array. A pointer to the corresponding memory in the array is returned. To use it you need to cast it:
uint idx[3]={1,2,3}; float a = *(float*)array_index( arr, idx );
a | the array | |
idx | array of size a->ndim giving the indices |
void* array_index2 | ( | const Array * | a, | |
... | ||||
) |
Index the array.
You need to provide as many arguments as there are dimensions in the array. A pointer to the corresponding memory in the array is returned. To use it you need to cast it:
float a = *(float*)array_index2( arr, 1, 2 );
a | the array | |
... | a->ndim integers |
void* array_max | ( | const Array * | a | ) |
Get maximum element from array.
You can easily find the index of this element by doing
Array *a = get_array_from_somewhere(); long idx = (array_max(a)-(a->data))/a->dtype_size; // if you need an index-tuple, you can do uint idxtuple[num_dimension]; array_calc_rowindex( idx, a->size, a->ndim, idxtuple );
a | the array |
void* array_min | ( | const Array * | a | ) |
Get minimum element from array.
You can easily find the index of this element by doing
Array *a = get_array_from_somewhere(); long idx = (array_min(a)-(a->data))/a->dtype_size; // if you need an index-tuple, you can do uint idxtuple[num_dimension]; array_calc_rowindex( idx, a->size, a->ndim, idxtuple );
a | the array |
create array that is filled with random values from [0, ..., 1].
Mainly used for debugging stuff.
seed | to use for the random numbers; if seed=0, the current time is used | |
ndim | number of dimensions | |
... | the number of elements in each of the dimensions |
void array_reverse | ( | Array * | a | ) |
int array_scale | ( | Array * | a, | |
double | x | |||
) |
void array_shuffle | ( | Array * | a, | |
unsigned long | seed | |||
) |
Extract sub-arrays from array.
This is a function for creating array slices from an existing array. I.e. you can extract sub-arrays similar to interpreted languages like python or MATLAB.
See Slicing Array Objects for the format of the slice string.
a | the array | |
slicedesc | the description of the slice as described in Slicing Array Objects |
Array-typecast.
Typecast the whole array.
Warning:
a | the array | |
target_type | the target type |