|
| 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 "stdlib/complex/float32/ctor.h" |
| 25 | +#include <stdbool.h> |
| 26 | +#include <stdint.h> |
| 27 | +#include <stdlib.h> |
| 28 | +#include <stdio.h> |
| 29 | +#include <inttypes.h> |
| 30 | + |
| 31 | +static void print_ndarray_contents( const struct ndarray *x ) { |
| 32 | + int64_t i; |
| 33 | + int8_t s; |
| 34 | + bool v; |
| 35 | + |
| 36 | + for ( i = 0; i < stdlib_ndarray_length( x ); i++ ) { |
| 37 | + s = stdlib_ndarray_iget_bool( x, i, &v ); |
| 38 | + if ( s != 0 ) { |
| 39 | + fprintf( stderr, "Unable to resolve data element.\n" ); |
| 40 | + exit( EXIT_FAILURE ); |
| 41 | + } |
| 42 | + fprintf( stdout, "data[%"PRId64"] = %s\n", i, ( v ) ? "true" : "false" ); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +int main( void ) { |
| 47 | + // Define the ndarray data types: |
| 48 | + enum STDLIB_NDARRAY_DTYPE xdtype = STDLIB_NDARRAY_COMPLEX64; |
| 49 | + enum STDLIB_NDARRAY_DTYPE ydtype = STDLIB_NDARRAY_BOOL; |
| 50 | + |
| 51 | + // Create underlying byte arrays: |
| 52 | + stdlib_complex64_t xvalues[] = { |
| 53 | + stdlib_complex64( 1.0f, 2.0f ), |
| 54 | + stdlib_complex64( 2.0f, 0.0f ), |
| 55 | + stdlib_complex64( 3.0f, 0.0f ), |
| 56 | + stdlib_complex64( 4.0f, 0.0f ), |
| 57 | + stdlib_complex64( 0.0f, 0.0f ), |
| 58 | + stdlib_complex64( 6.0f, 0.0f ), |
| 59 | + stdlib_complex64( 7.0f, 0.0f ), |
| 60 | + stdlib_complex64( 8.0f, 0.0f ) |
| 61 | + }; |
| 62 | + |
| 63 | + // cppcheck-suppress invalidPointerCast |
| 64 | + uint8_t *xbuf = (uint8_t *)xvalues; |
| 65 | + uint8_t ybuf[] = { 0 }; |
| 66 | + |
| 67 | + // Define the number of dimensions: |
| 68 | + int64_t ndims = 3; |
| 69 | + |
| 70 | + // Define the array shapes: |
| 71 | + int64_t shx[] = { 2, 2, 2 }; |
| 72 | + int64_t *shy = NULL; |
| 73 | + |
| 74 | + // Define the strides: |
| 75 | + int64_t sx[] = { 32, 16, 8 }; |
| 76 | + int64_t sy[] = { 0 }; |
| 77 | + |
| 78 | + // Define the offsets: |
| 79 | + int64_t ox = 0; |
| 80 | + int64_t oy = 0; |
| 81 | + |
| 82 | + // Define the array order: |
| 83 | + enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; |
| 84 | + |
| 85 | + // Specify the index mode: |
| 86 | + enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; |
| 87 | + |
| 88 | + // Specify the subscript index modes: |
| 89 | + int8_t submodes[] = { imode }; |
| 90 | + int64_t nsubmodes = 1; |
| 91 | + |
| 92 | + // Create an input ndarray: |
| 93 | + struct ndarray *x = stdlib_ndarray_allocate( xdtype, xbuf, ndims, shx, sx, ox, order, imode, nsubmodes, submodes ); |
| 94 | + if ( x == NULL ) { |
| 95 | + fprintf( stderr, "Error allocating memory.\n" ); |
| 96 | + exit( EXIT_FAILURE ); |
| 97 | + } |
| 98 | + |
| 99 | + // Create an output ndarray: |
| 100 | + struct ndarray *y = stdlib_ndarray_allocate( ydtype, ybuf, 0, shy, sy, oy, order, imode, nsubmodes, submodes ); |
| 101 | + if ( y == NULL ) { |
| 102 | + fprintf( stderr, "Error allocating memory.\n" ); |
| 103 | + exit( EXIT_FAILURE ); |
| 104 | + } |
| 105 | + |
| 106 | + // Define an array containing the ndarrays: |
| 107 | + struct ndarray *arrays[] = { x, y }; |
| 108 | + |
| 109 | + // Test elements: |
| 110 | + int8_t status = stdlib_ndarray_every_c_x( arrays, NULL ); |
| 111 | + if ( status != 0 ) { |
| 112 | + fprintf( stderr, "Error during computation.\n" ); |
| 113 | + exit( EXIT_FAILURE ); |
| 114 | + } |
| 115 | + |
| 116 | + // Print the results: |
| 117 | + print_ndarray_contents( y ); |
| 118 | + fprintf( stdout, "\n" ); |
| 119 | + |
| 120 | + // Free allocated memory: |
| 121 | + stdlib_ndarray_free( x ); |
| 122 | + stdlib_ndarray_free( y ); |
| 123 | +} |
0 commit comments