|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +#include "stdlib/ndarray/base/every.h" |
| 20 | +#include "stdlib/ndarray/dtypes.h" |
| 21 | +#include "stdlib/ndarray/index_modes.h" |
| 22 | +#include "stdlib/ndarray/orders.h" |
| 23 | +#include "stdlib/ndarray/ctor.h" |
| 24 | +#include <stdbool.h> |
| 25 | +#include <stdint.h> |
| 26 | +#include <stdlib.h> |
| 27 | +#include <stdio.h> |
| 28 | +#include <inttypes.h> |
| 29 | + |
| 30 | +static void print_ndarray_contents( const struct ndarray *x ) { |
| 31 | + int64_t i; |
| 32 | + int8_t s; |
| 33 | + bool v; |
| 34 | + |
| 35 | + for ( i = 0; i < stdlib_ndarray_length( x ); i++ ) { |
| 36 | + s = stdlib_ndarray_iget_bool( x, i, &v ); |
| 37 | + if ( s != 0 ) { |
| 38 | + fprintf( stderr, "Unable to resolve data element.\n" ); |
| 39 | + exit( EXIT_FAILURE ); |
| 40 | + } |
| 41 | + fprintf( stdout, "data[%"PRId64"] = %s\n", i, ( v ) ? "true" : "false" ); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +int main( void ) { |
| 46 | + // Define the ndarray data types: |
| 47 | + enum STDLIB_NDARRAY_DTYPE xdtype = STDLIB_NDARRAY_UINT8; |
| 48 | + enum STDLIB_NDARRAY_DTYPE ydtype = STDLIB_NDARRAY_BOOL; |
| 49 | + |
| 50 | + // Create underlying byte arrays: |
| 51 | + uint8_t xbuf[] = { 1, 2, 3, 4, 0, 6, 7, 8 }; |
| 52 | + uint8_t ybuf[] = { 0 }; |
| 53 | + |
| 54 | + // Define the number of dimensions: |
| 55 | + int64_t ndims = 3; |
| 56 | + |
| 57 | + // Define the array shapes: |
| 58 | + int64_t shx[] = { 2, 2, 2 }; |
| 59 | + int64_t *shy = NULL; |
| 60 | + |
| 61 | + // Define the strides: |
| 62 | + int64_t sx[] = { 4, 2, 1 }; |
| 63 | + int64_t sy[] = { 0 }; |
| 64 | + |
| 65 | + // Define the offsets: |
| 66 | + int64_t ox = 0; |
| 67 | + int64_t oy = 0; |
| 68 | + |
| 69 | + // Define the array order: |
| 70 | + enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; |
| 71 | + |
| 72 | + // Specify the index mode: |
| 73 | + enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; |
| 74 | + |
| 75 | + // Specify the subscript index modes: |
| 76 | + int8_t submodes[] = { imode }; |
| 77 | + int64_t nsubmodes = 1; |
| 78 | + |
| 79 | + // Create an input ndarray: |
| 80 | + struct ndarray *x = stdlib_ndarray_allocate( xdtype, xbuf, ndims, shx, sx, ox, order, imode, nsubmodes, submodes ); |
| 81 | + if ( x == NULL ) { |
| 82 | + fprintf( stderr, "Error allocating memory.\n" ); |
| 83 | + exit( EXIT_FAILURE ); |
| 84 | + } |
| 85 | + |
| 86 | + // Create an output ndarray: |
| 87 | + struct ndarray *y = stdlib_ndarray_allocate( ydtype, ybuf, 0, shy, sy, oy, order, imode, nsubmodes, submodes ); |
| 88 | + if ( y == NULL ) { |
| 89 | + fprintf( stderr, "Error allocating memory.\n" ); |
| 90 | + exit( EXIT_FAILURE ); |
| 91 | + } |
| 92 | + |
| 93 | + // Define an array containing the ndarrays: |
| 94 | + struct ndarray *arrays[] = { x, y }; |
| 95 | + |
| 96 | + // Test elements: |
| 97 | + int8_t status = stdlib_ndarray_every_b_x( arrays, NULL ); |
| 98 | + if ( status != 0 ) { |
| 99 | + fprintf( stderr, "Error during computation.\n" ); |
| 100 | + exit( EXIT_FAILURE ); |
| 101 | + } |
| 102 | + |
| 103 | + // Print the results: |
| 104 | + print_ndarray_contents( y ); |
| 105 | + fprintf( stdout, "\n" ); |
| 106 | + |
| 107 | + // Free allocated memory: |
| 108 | + stdlib_ndarray_free( x ); |
| 109 | + stdlib_ndarray_free( y ); |
| 110 | +} |
0 commit comments