|
2 | 2 |
|
3 | 3 | @license Apache-2.0 |
4 | 4 |
|
5 | | -Copyright (c) 2023 The Stdlib Authors. |
| 5 | +Copyright (c) 2024 The Stdlib Authors. |
6 | 6 |
|
7 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | 8 | you may not use this file except in compliance with the License. |
@@ -153,6 +153,7 @@ Character codes for data types: |
153 | 153 |
|
154 | 154 | <!-- charcodes --> |
155 | 155 |
|
| 156 | +- **x**: `bool` (boolean). |
156 | 157 | - **z**: `complex128` (double-precision floating-point complex number). |
157 | 158 | - **c**: `complex64` (single-precision floating-point complex number). |
158 | 159 | - **f**: `float32` (single-precision floating-point number). |
@@ -3028,6 +3029,85 @@ The function accepts the following arguments: |
3028 | 3029 | int8_t stdlib_ndarray_u_as_t( struct ndarray *arrays[], void *fcn ); |
3029 | 3030 | ``` |
3030 | 3031 |
|
| 3032 | +#### stdlib_ndarray_x( \*arrays\[], \*fcn ) |
| 3033 | + |
| 3034 | +Applies a nullary callback and assigns results to elements in an output ndarray. |
| 3035 | + |
| 3036 | +```c |
| 3037 | +#include "stdlib/ndarray/dtypes.h" |
| 3038 | +#include "stdlib/ndarray/index_modes.h" |
| 3039 | +#include "stdlib/ndarray/orders.h" |
| 3040 | +#include "stdlib/ndarray/ctor.h" |
| 3041 | +#include <stdbool.h> |
| 3042 | +#include <stdint.h> |
| 3043 | +#include <stdlib.h> |
| 3044 | +#include <stdio.h> |
| 3045 | + |
| 3046 | +// Define the ndarray data type: |
| 3047 | +enum STDLIB_NDARRAY_DTYPE xdtype = STDLIB_NDARRAY_BOOL; |
| 3048 | + |
| 3049 | +// Create an underlying byte array: |
| 3050 | +uint8_t xbuf[] = { 0, 0, 0, 0 }; |
| 3051 | + |
| 3052 | +// Define the number of dimensions: |
| 3053 | +int64_t ndims = 2; |
| 3054 | + |
| 3055 | +// Define the array shape: |
| 3056 | +int64_t shape[] = { 2, 2 }; |
| 3057 | + |
| 3058 | +// Define the strides: |
| 3059 | +int64_t sx[] = { 2, 1 }; |
| 3060 | + |
| 3061 | +// Define the index offset: |
| 3062 | +int64_t ox = 0; |
| 3063 | + |
| 3064 | +// Define the array order: |
| 3065 | +enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; |
| 3066 | + |
| 3067 | +// Specify the index mode: |
| 3068 | +enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; |
| 3069 | + |
| 3070 | +// Specify the subscript index modes: |
| 3071 | +int8_t submodes[] = { imode }; |
| 3072 | +int64_t nsubmodes = 1; |
| 3073 | + |
| 3074 | +// Create an output ndarray: |
| 3075 | +struct ndarray *x = stdlib_ndarray_allocate( xdtype, xbuf, ndims, shape, sx, ox, order, imode, nsubmodes, submodes ); |
| 3076 | +if ( x == NULL ) { |
| 3077 | + fprintf( stderr, "Error allocating memory.\n" ); |
| 3078 | + exit( EXIT_FAILURE ); |
| 3079 | +} |
| 3080 | + |
| 3081 | +// Create an array containing a pointer to the ndarray: |
| 3082 | +struct ndarray *arrays[] = { x }; |
| 3083 | + |
| 3084 | +// Define a callback: |
| 3085 | +static bool fcn( void ) { |
| 3086 | + return true; |
| 3087 | +} |
| 3088 | + |
| 3089 | +// Apply the callback: |
| 3090 | +int8_t status = stdlib_ndarray_x( arrays, (void *)fcn ); |
| 3091 | +if ( status != 0 ) { |
| 3092 | + fprintf( stderr, "Error during computation.\n" ); |
| 3093 | + exit( EXIT_FAILURE ); |
| 3094 | +} |
| 3095 | + |
| 3096 | +// ... |
| 3097 | + |
| 3098 | +// Free allocated memory: |
| 3099 | +stdlib_ndarray_free( x ); |
| 3100 | +``` |
| 3101 | +
|
| 3102 | +The function accepts the following arguments: |
| 3103 | +
|
| 3104 | +- **arrays**: `[inout] struct ndarray**` array whose only element is a pointer to an output ndarray. |
| 3105 | +- **fcn**: `[in] void*` a `bool (*f)()` function to apply provided as a `void` pointer. |
| 3106 | +
|
| 3107 | +```c |
| 3108 | +int8_t stdlib_ndarray_x( struct ndarray *arrays[], void *fcn ); |
| 3109 | +``` |
| 3110 | + |
3031 | 3111 | #### stdlib_ndarray_z( \*arrays\[], \*fcn ) |
3032 | 3112 |
|
3033 | 3113 | Applies a nullary callback and assigns results to elements in an output ndarray. |
|
0 commit comments