|
37 | 37 | *
|
38 | 38 | * - The user is responsible for freeing the allocated memory.
|
39 | 39 | *
|
| 40 | +* - To create a zero-dimensional array, |
| 41 | +* |
| 42 | +* - provide an `ndims` argument equal to `0`. |
| 43 | +* - a `shape` argument equal to `NULL`. |
| 44 | +* - a `strides` argument containing a single element equal to `0`. |
| 45 | +* |
| 46 | +* The `order` argument can be either row-major or column-major and has no effect on data storage or access. |
| 47 | +* |
| 48 | +* ```javascript |
| 49 | +* #include "stdlib/ndarray/ctor.h" |
| 50 | +* #include "stdlib/ndarray/dtypes.h" |
| 51 | +* #include "stdlib/ndarray/index_modes.h" |
| 52 | +* #include "stdlib/ndarray/orders.h" |
| 53 | +* #include <stdint.h> |
| 54 | +* |
| 55 | +* uint8_t buffer[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 56 | +* int64_t shape[] = NULL; |
| 57 | +* int64_t strides = { 0 }; |
| 58 | +* int64_t offset = 0; |
| 59 | +* |
| 60 | +* struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, buffer, 0, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, { STDLIB_NDARRAY_INDEX_ERROR } ); |
| 61 | +* ``` |
| 62 | +* |
40 | 63 | * @param dtype data type
|
41 | 64 | * @param data pointer to the underlying byte array
|
42 | 65 | * @param ndims number of dimensions
|
@@ -115,7 +138,11 @@ struct ndarray * stdlib_ndarray_allocate( int16_t dtype, uint8_t *data, int64_t
|
115 | 138 | arr->strides = strides;
|
116 | 139 | arr->submodes = submodes;
|
117 | 140 |
|
118 |
| - len = stdlib_ndarray_numel( ndims, shape ); |
| 141 | + if ( shape == NULL ) { |
| 142 | + len = 1; |
| 143 | + } else { |
| 144 | + len = stdlib_ndarray_numel( ndims, shape ); |
| 145 | + } |
119 | 146 | arr->length = len;
|
120 | 147 |
|
121 | 148 | arr->BYTES_PER_ELEMENT = stdlib_ndarray_bytes_per_element( dtype );
|
@@ -146,17 +173,21 @@ uint8_t * stdlib_ndarray_data( const struct ndarray *arr ) {
|
146 | 173 | }
|
147 | 174 |
|
148 | 175 | /**
|
149 |
| -* Returns an ndarray dimension. |
| 176 | +* Returns an ndarray dimension size. |
150 | 177 | *
|
151 | 178 | * ## Notes
|
152 | 179 | *
|
153 | 180 | * - The function does not perform any sanity checks.
|
| 181 | +* - If an input ndarray is zero-dimensional, the function always returns `-1`. |
154 | 182 | *
|
155 | 183 | * @param arr input ndarray
|
156 | 184 | * @param i dimension index
|
157 |
| -* @return dimension |
| 185 | +* @return dimension size |
158 | 186 | */
|
159 | 187 | int64_t stdlib_ndarray_dimension( const struct ndarray *arr, const int64_t i ) {
|
| 188 | + if ( arr->shape == NULL ) { |
| 189 | + return -1; |
| 190 | + } |
160 | 191 | return arr->shape[ i ];
|
161 | 192 | }
|
162 | 193 |
|
@@ -229,8 +260,11 @@ int64_t stdlib_ndarray_flags( const struct ndarray *arr ) {
|
229 | 260 |
|
230 | 261 | // Determine if the array can be stored contiguously...
|
231 | 262 | if ( len == 0 || stdlib_ndarray_iteration_order( ndims, strides ) == 0 ) {
|
232 |
| - // If an array does not contain any elements, then no data to store, and, if the array is unordered, adjacent array elements are not guaranteed to be stored next to each other. |
| 263 | + // If an array does not contain any elements, then no data to store, and, if the array is unordered, adjacent array elements are not guaranteed to be stored next to each other: |
233 | 264 | contiguous = 0;
|
| 265 | + } else if ( ndims == 0 ) { |
| 266 | + // If an array is zero-dimensional, data consists of a single element which means it is trivially compatible with a single memory segment: |
| 267 | + contiguous = 1; |
234 | 268 | } else {
|
235 | 269 | // Ensure that the array is compatible with a single memory segment:
|
236 | 270 | stdlib_ndarray_minmax_view_buffer_index( ndims, arr->shape, strides, arr->offset, tmp );
|
@@ -343,6 +377,10 @@ int8_t stdlib_ndarray_order( const struct ndarray *arr ) {
|
343 | 377 | /**
|
344 | 378 | * Returns a pointer to an array containing an ndarray shape (dimensions).
|
345 | 379 | *
|
| 380 | +* ## Notes |
| 381 | +* |
| 382 | +* - If an input ndarray is zero-dimensional, the function returns a null pointer. |
| 383 | +* |
346 | 384 | * @param arr input ndarray
|
347 | 385 | * @return array shape (dimensions)
|
348 | 386 | */
|
|
0 commit comments