|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# every |
| 22 | + |
| 23 | +> Test whether every element in an ndarray is truthy. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var every = require( '@stdlib/ndarray/base/every' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### every( arrays ) |
| 40 | + |
| 41 | +Tests whether every element in an ndarray is truthy. |
| 42 | + |
| 43 | +<!-- eslint-disable max-len --> |
| 44 | + |
| 45 | +```javascript |
| 46 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 47 | + |
| 48 | +// Create a data buffer: |
| 49 | +var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 50 | + |
| 51 | +// Define the shape of the input array: |
| 52 | +var shape = [ 3, 1, 2 ]; |
| 53 | + |
| 54 | +// Define the array strides: |
| 55 | +var sx = [ 4, 4, 1 ]; |
| 56 | + |
| 57 | +// Define the index offset: |
| 58 | +var ox = 0; |
| 59 | + |
| 60 | +// Create the input ndarray-like object: |
| 61 | +var x = { |
| 62 | + 'dtype': 'float64', |
| 63 | + 'data': xbuf, |
| 64 | + 'shape': shape, |
| 65 | + 'strides': sx, |
| 66 | + 'offset': ox, |
| 67 | + 'order': 'row-major' |
| 68 | +}; |
| 69 | + |
| 70 | +// Test elements: |
| 71 | +var out = every( [ x ] ); |
| 72 | +// returns true |
| 73 | +``` |
| 74 | + |
| 75 | +The function accepts the following arguments: |
| 76 | + |
| 77 | +- **arrays**: array-like object containing an input ndarray. |
| 78 | + |
| 79 | +The provided ndarray should be an `object` with the following properties: |
| 80 | + |
| 81 | +- **dtype**: data type. |
| 82 | +- **data**: data buffer. |
| 83 | +- **shape**: dimensions. |
| 84 | +- **strides**: stride lengths. |
| 85 | +- **offset**: index offset. |
| 86 | +- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style). |
| 87 | + |
| 88 | +</section> |
| 89 | + |
| 90 | +<!-- /.usage --> |
| 91 | + |
| 92 | +<section class="notes"> |
| 93 | + |
| 94 | +## Notes |
| 95 | + |
| 96 | +- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing the operation in order to achieve better performance. |
| 97 | + |
| 98 | +</section> |
| 99 | + |
| 100 | +<!-- /.notes --> |
| 101 | + |
| 102 | +<section class="examples"> |
| 103 | + |
| 104 | +## Examples |
| 105 | + |
| 106 | +<!-- eslint no-undef: "error" --> |
| 107 | + |
| 108 | +```javascript |
| 109 | +var bernoulli = require( '@stdlib/random/array/bernoulli' ); |
| 110 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 111 | +var every = require( '@stdlib/ndarray/base/every' ); |
| 112 | + |
| 113 | +var x = { |
| 114 | + 'dtype': 'generic', |
| 115 | + 'data': bernoulli( 10, 0.9, { |
| 116 | + 'dtype': 'generic' |
| 117 | + }), |
| 118 | + 'shape': [ 5, 2 ], |
| 119 | + 'strides': [ 2, 1 ], |
| 120 | + 'offset': 0, |
| 121 | + 'order': 'row-major' |
| 122 | +}; |
| 123 | +console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); |
| 124 | + |
| 125 | +var out = every( [ x ] ); |
| 126 | +console.log( out ); |
| 127 | +``` |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.examples --> |
| 132 | + |
| 133 | +<!-- C interface documentation. --> |
| 134 | + |
| 135 | +* * * |
| 136 | + |
| 137 | +<section class="c"> |
| 138 | + |
| 139 | +## C APIs |
| 140 | + |
| 141 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 142 | + |
| 143 | +<section class="intro"> |
| 144 | + |
| 145 | +Character codes for data types: |
| 146 | + |
| 147 | +<!-- The following is auto-generated. Do not manually edit. See scripts/loops.js. --> |
| 148 | + |
| 149 | +<!-- charcodes --> |
| 150 | + |
| 151 | +- **x**: `bool` (boolean). |
| 152 | +- **z**: `complex128` (double-precision floating-point complex number). |
| 153 | +- **c**: `complex64` (single-precision floating-point complex number). |
| 154 | +- **f**: `float32` (single-precision floating-point number). |
| 155 | +- **d**: `float64` (double-precision floating-point number). |
| 156 | +- **k**: `int16` (signed 16-bit integer). |
| 157 | +- **i**: `int32` (signed 32-bit integer). |
| 158 | +- **s**: `int8` (signed 8-bit integer). |
| 159 | +- **t**: `uint16` (unsigned 16-bit integer). |
| 160 | +- **u**: `uint32` (unsigned 32-bit integer). |
| 161 | +- **b**: `uint8` (unsigned 8-bit integer). |
| 162 | + |
| 163 | +<!-- ./charcodes --> |
| 164 | + |
| 165 | +Function name suffix naming convention: |
| 166 | + |
| 167 | +```text |
| 168 | +stdlib_ndarray_<input_data_type>_<output_data_type>[_as_<callback_arg_data_type>_<callback_return_data_type>] |
| 169 | +``` |
| 170 | + |
| 171 | +For example, |
| 172 | + |
| 173 | +<!-- run-disable --> |
| 174 | + |
| 175 | +```c |
| 176 | +void stdlib_ndarray_every_d_x(...) {...} |
| 177 | +``` |
| 178 | +
|
| 179 | +is a function which accepts one double-precision floating-point input ndarray and one boolean output ndarray. In other words, the suffix encodes the function type signature. |
| 180 | +
|
| 181 | +To support callbacks whose input arguments are of a different data type than the input ndarray data type, the naming convention supports appending an `as` suffix. For example, |
| 182 | +
|
| 183 | +<!-- run-disable --> |
| 184 | +
|
| 185 | +```c |
| 186 | +void stdlib_ndarray_every_f_x_as_d_x(...) {...} |
| 187 | +``` |
| 188 | + |
| 189 | +is a function which accepts one single-precision floating-point input ndarray and one boolean output ndarray. However, the callback accepts double-precision floating-point numbers. Accordingly, the input values need to be cast using the following conversion sequence |
| 190 | + |
| 191 | +```c |
| 192 | +#include <stdbool.h> |
| 193 | + |
| 194 | +// Convert each input array element to double-precision: |
| 195 | +double in1 = (double)x[ i ]; |
| 196 | + |
| 197 | +// Evaluate the callback: |
| 198 | +bool out = f( in1 ); |
| 199 | +``` |
| 200 | + |
| 201 | +</section> |
| 202 | + |
| 203 | +<!-- /.intro --> |
| 204 | + |
| 205 | +<!-- C usage documentation. --> |
| 206 | + |
| 207 | +<section class="usage"> |
| 208 | + |
| 209 | +### Usage |
| 210 | + |
| 211 | +```c |
| 212 | +#include "stdlib/ndarray/base/every.h" |
| 213 | +``` |
| 214 | + |
| 215 | +<!-- The following is auto-generated. Do not manually edit. See scripts/loops.js. --> |
| 216 | + |
| 217 | +<!-- loops --> |
| 218 | + |
| 219 | +<!-- ./loops --> |
| 220 | + |
| 221 | +<!-- macros --> |
| 222 | + |
| 223 | +<!-- TODO: consider documenting macros --> |
| 224 | + |
| 225 | +<!-- ./macros --> |
| 226 | + |
| 227 | +</section> |
| 228 | + |
| 229 | +<!-- /.usage --> |
| 230 | + |
| 231 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 232 | + |
| 233 | +<section class="notes"> |
| 234 | + |
| 235 | +</section> |
| 236 | + |
| 237 | +<!-- /.notes --> |
| 238 | + |
| 239 | +<!-- C API usage examples. --> |
| 240 | + |
| 241 | +* * * |
| 242 | + |
| 243 | +<section class="examples"> |
| 244 | + |
| 245 | +### Examples |
| 246 | + |
| 247 | +```c |
| 248 | +#include "stdlib/ndarray/base/every.h" |
| 249 | +#include "stdlib/ndarray/dtypes.h" |
| 250 | +#include "stdlib/ndarray/index_modes.h" |
| 251 | +#include "stdlib/ndarray/orders.h" |
| 252 | +#include "stdlib/ndarray/ctor.h" |
| 253 | +#include <stdbool.h> |
| 254 | +#include <stdint.h> |
| 255 | +#include <stdlib.h> |
| 256 | +#include <stdio.h> |
| 257 | +#include <inttypes.h> |
| 258 | + |
| 259 | +static void print_ndarray_contents( const struct ndarray *x ) { |
| 260 | + int64_t i; |
| 261 | + int8_t s; |
| 262 | + bool v; |
| 263 | + |
| 264 | + for ( i = 0; i < stdlib_ndarray_length( x ); i++ ) { |
| 265 | + s = stdlib_ndarray_iget_bool( x, i, &v ); |
| 266 | + if ( s != 0 ) { |
| 267 | + fprintf( stderr, "Unable to resolve data element.\n" ); |
| 268 | + exit( EXIT_FAILURE ); |
| 269 | + } |
| 270 | + fprintf( stdout, "data[%"PRId64"] = %s\n", i, ( v ) ? "true" : "false" ); |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +static bool fcn( const uint8_t x ) { |
| 275 | + return ( x != 0 ); |
| 276 | +} |
| 277 | + |
| 278 | +int main( void ) { |
| 279 | + // Define the ndarray data type: |
| 280 | + enum STDLIB_NDARRAY_DTYPE dtype = STDLIB_NDARRAY_UINT8; |
| 281 | + |
| 282 | + // Create underlying byte arrays: |
| 283 | + uint8_t xbuf[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 284 | + bool ybuf[] = { false }; |
| 285 | + |
| 286 | + // Define the number of input array dimensions: |
| 287 | + int64_t ndims = 3; |
| 288 | + |
| 289 | + // Define the array shapes: |
| 290 | + int64_t shx[] = { 2, 2, 2 }; |
| 291 | + int64_t shy[] = {}; |
| 292 | + |
| 293 | + // Define the strides: |
| 294 | + int64_t sx[] = { 4, 2, 1 }; |
| 295 | + int64_t sy[] = { 0 }; |
| 296 | + |
| 297 | + // Define the offsets: |
| 298 | + int64_t ox = 0; |
| 299 | + int64_t oy = 0; |
| 300 | + |
| 301 | + // Define the array order: |
| 302 | + enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; |
| 303 | + |
| 304 | + // Specify the index mode: |
| 305 | + enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; |
| 306 | + |
| 307 | + // Specify the subscript index modes: |
| 308 | + int8_t submodes[] = { imode }; |
| 309 | + int64_t nsubmodes = 1; |
| 310 | + |
| 311 | + // Create an input ndarray: |
| 312 | + struct ndarray *x = stdlib_ndarray_allocate( dtype, xbuf, ndims, shx, sx, ox, order, imode, nsubmodes, submodes ); |
| 313 | + if ( x == NULL ) { |
| 314 | + fprintf( stderr, "Error allocating memory.\n" ); |
| 315 | + exit( EXIT_FAILURE ); |
| 316 | + } |
| 317 | + |
| 318 | + // Create an output ndarray: |
| 319 | + struct ndarray *y = stdlib_ndarray_allocate( dtype, ybuf, 0, shy, sy, oy, order, imode, nsubmodes, submodes ); |
| 320 | + if ( y == NULL ) { |
| 321 | + fprintf( stderr, "Error allocating memory.\n" ); |
| 322 | + exit( EXIT_FAILURE ); |
| 323 | + } |
| 324 | + |
| 325 | + // Define an array containing the ndarrays: |
| 326 | + struct ndarray *arrays[] = { x, y }; |
| 327 | + |
| 328 | + // Test elements: |
| 329 | + int8_t status = stdlib_ndarray_every_b_x( arrays, (void *)fcn ); |
| 330 | + if ( status != 0 ) { |
| 331 | + fprintf( stderr, "Error during computation.\n" ); |
| 332 | + exit( EXIT_FAILURE ); |
| 333 | + } |
| 334 | + |
| 335 | + // Print the results: |
| 336 | + print_ndarray_contents( y ); |
| 337 | + fprintf( stdout, "\n" ); |
| 338 | + |
| 339 | + // Free allocated memory: |
| 340 | + stdlib_ndarray_free( x ); |
| 341 | + stdlib_ndarray_free( y ); |
| 342 | +} |
| 343 | +``` |
| 344 | +
|
| 345 | +</section> |
| 346 | +
|
| 347 | +<!-- /.examples --> |
| 348 | +
|
| 349 | +</section> |
| 350 | +
|
| 351 | +<!-- /.c --> |
| 352 | +
|
| 353 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 354 | +
|
| 355 | +<section class="related"> |
| 356 | +
|
| 357 | +</section> |
| 358 | +
|
| 359 | +<!-- /.related --> |
| 360 | +
|
| 361 | +<section class="links"> |
| 362 | +
|
| 363 | +<!-- <related-links> --> |
| 364 | +
|
| 365 | +<!-- </related-links> --> |
| 366 | +
|
| 367 | +</section> |
| 368 | +
|
| 369 | +<!-- /.links --> |
0 commit comments