ndarray.h - justinmgarrigus/VGG-Simulation GitHub Wiki

Implementation of an n-dimensional array, mainly for the purpose of offsetting and storing arrays with a variable-length list of indices. Mirrors Kera's usage of numpy arrays for storing networks.

Macros

  • ND_TYPE: abstract data type defining the type of data to store. Can only be defined once; define this in the class that uses ndarrays.
  • ND_DISPLAY: string used in printf statements to display the ND_TYPE.

Properties

  • int dim: How many dimensions are in the array.
  • int *shape: The shape of each dimensions; equal in length to dim.
  • int *cumulative: Private member for storage. Should not be interacted with normally. Stores the multiplicative-cumulative shape.
    • For example, the shape {3, 5, 2} would have a cumulative of {3, 15, 30}.
  • void *arr: The actual data stored in the ndarray. The amount of indirection is equal to the array's dimension.

Functions

  • ndarray* ndarray_create(int count, int* shape): Creates a new n-dimensional array given the number of dimensions it has and the shape of each dimension.
  • void ndarray_free(ndarray* nd): Frees the data allocated in the ndarray, as well as the ndarray pointer itself.
  • ND_TYPE ndarray_val_list(ndarray* nd, int* pos): Returns the value stored in the ndarray at the position specified in the array offset.
    • Usage: if nd is a 3x5x2 integer array, the value at position nd[0][2][1] is retrieved via:
      int pos[3] = {0, 2, 1};
      int value = ndarray_val_list(nd, pos); 
      
  • ND_TYPE ndarray_val_param(ndarray* nd, ...): Returns the value stored in the ndarray at the position specified by the parameter list.
    • Usage: if nd is a 3x5x2 integer array, the value at position nd[0][2][1] is retrieved via:
      int value = ndarray_val_param(nd, 0, 2, 1); 
      
  • void ndarray_deep_display(ndarray* nd): Utility function that displays each element inside of nd along with their index.
  • int ndarray_decimal_count(int length, int* counter, int* shape): Increments counter so it remains less than shape. For instance, if length is 3, shape is {2, 2, 2}, and counter is originally {0, 0, 0}, counter will become {0, 0, 1} the first time run, then {0, 1, 0}, then {0, 1, 1}, and {1, 0, 0}. This example resembles a binary counter, but any number of counters can be formed. Returns 1 if the counter is less than the max (shape) value, and 0 if counter is greater than or equal to it.