|
| 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 | +# any |
| 22 | + |
| 23 | +> Test whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions 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 any = require( '@stdlib/ndarray/any' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### any( x\[, options] ) |
| 40 | + |
| 41 | +Tests whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions is truthy. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var array = require( '@stdlib/ndarray/array' ); |
| 45 | + |
| 46 | +// Create an input ndarray: |
| 47 | +var x = array( [ [ [ -1.0, 0.0 ] ], [ [ -3.0, -4.0 ] ], [ [ 5.0, -6.0 ] ] ] ); |
| 48 | +// returns <ndarray> |
| 49 | + |
| 50 | +// Perform reduction: |
| 51 | +var out = any( x ); |
| 52 | +// returns <ndarray> |
| 53 | + |
| 54 | +var v = out.get(); |
| 55 | +// returns true |
| 56 | +``` |
| 57 | + |
| 58 | +The function accepts the following arguments: |
| 59 | + |
| 60 | +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. |
| 61 | +- **options**: function options (_optional_). |
| 62 | + |
| 63 | +The function accepts the following `options`: |
| 64 | + |
| 65 | +- **dims**: list of dimensions over which to perform a reduction. |
| 66 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [`ndarray`][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. |
| 67 | + |
| 68 | +By default, the function performs a reduction over all elements in a provided [`ndarray`][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option. |
| 69 | + |
| 70 | +```javascript |
| 71 | +var array = require( '@stdlib/ndarray/array' ); |
| 72 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 73 | + |
| 74 | +// Create an input ndarray: |
| 75 | +var x = array( [ [ [ -1.0, 0.0 ] ], [ [ -3.0, 0.0 ] ], [ [ 5.0, 0.0 ] ] ] ); |
| 76 | +// returns <ndarray> |
| 77 | + |
| 78 | +// Perform reduction: |
| 79 | +var out = any( x, { |
| 80 | + 'dims': [ 1, 2 ] |
| 81 | +}); |
| 82 | +// returns <ndarray> |
| 83 | + |
| 84 | +var v = ndarray2array( out ); |
| 85 | +// returns [ true, true, true ] |
| 86 | +``` |
| 87 | + |
| 88 | +By default, the function returns an [`ndarray`][@stdlib/ndarray/ctor] having a shape matching only the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor] (i.e., the reduced dimensions are dropped). To include the reduced dimensions as singleton dimensions in the output [`ndarray`][@stdlib/ndarray/ctor], set the `keepdims` option to `true`. |
| 89 | + |
| 90 | +```javascript |
| 91 | +var array = require( '@stdlib/ndarray/array' ); |
| 92 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 93 | + |
| 94 | +// Create an input ndarray: |
| 95 | +var x = array( [ [ [ -1.0, 0.0 ] ], [ [ -3.0, 0.0 ] ], [ [ 5.0, 0.0 ] ] ] ); |
| 96 | +// returns <ndarray> |
| 97 | + |
| 98 | +// Perform reduction: |
| 99 | +var out = any( x, { |
| 100 | + 'dims': [ 1, 2 ], |
| 101 | + 'keepdims': true |
| 102 | +}); |
| 103 | +// returns <ndarray> |
| 104 | + |
| 105 | +var v = ndarray2array( out ); |
| 106 | +// returns [ [ [ true ] ], [ [ true ] ], [ [ true ] ] ] |
| 107 | +``` |
| 108 | + |
| 109 | +#### any.assign( x, out\[, options] ) |
| 110 | + |
| 111 | +Tests whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions is truthy and assigns results to a provided output [`ndarray`][@stdlib/ndarray/ctor]. |
| 112 | + |
| 113 | +```javascript |
| 114 | +var array = require( '@stdlib/ndarray/array' ); |
| 115 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 116 | + |
| 117 | +// Create an input ndarray: |
| 118 | +var x = array( [ [ [ -1.0, 0.0 ] ], [ [ -3.0, 0.0 ] ], [ [ 5.0, 0.0 ] ] ] ); |
| 119 | +// returns <ndarray> |
| 120 | + |
| 121 | +// Create an output ndarray: |
| 122 | +var y = empty( [], { |
| 123 | + 'dtype': 'bool' |
| 124 | +}); |
| 125 | + |
| 126 | +// Perform reduction: |
| 127 | +var out = any.assign( x, y ); |
| 128 | +// returns <ndarray> |
| 129 | + |
| 130 | +var bool = ( out === y ); |
| 131 | +// returns true |
| 132 | + |
| 133 | +var v = y.get(); |
| 134 | +// returns true |
| 135 | +``` |
| 136 | + |
| 137 | +The function accepts the following arguments: |
| 138 | + |
| 139 | +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. |
| 140 | +- **out**: output [`ndarray`][@stdlib/ndarray/ctor]. The output [`ndarray`][@stdlib/ndarray/ctor] must have a shape matching the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor]. |
| 141 | +- **options**: function options (_optional_). |
| 142 | + |
| 143 | +The function accepts the following `options`: |
| 144 | + |
| 145 | +- **dims**: list of dimensions over which to perform a reduction. |
| 146 | + |
| 147 | +By default, the function performs a reduction over all elements in a provided [`ndarray`][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option. |
| 148 | + |
| 149 | +```javascript |
| 150 | +var array = require( '@stdlib/ndarray/array' ); |
| 151 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 152 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 153 | + |
| 154 | +// Create an input ndarray: |
| 155 | +var x = array( [ [ [ -1.0, 0.0 ] ], [ [ -3.0, 0.0 ] ], [ [ 5.0, 0.0 ] ] ] ); |
| 156 | +// returns <ndarray> |
| 157 | + |
| 158 | +// Create an output ndarray: |
| 159 | +var y = empty( [ 3 ], { |
| 160 | + 'dtype': 'bool' |
| 161 | +}); |
| 162 | + |
| 163 | +// Perform reduction: |
| 164 | +var out = any.assign( x, y, { |
| 165 | + 'dims': [ 1, 2 ] |
| 166 | +}); |
| 167 | + |
| 168 | +var bool = ( out === y ); |
| 169 | +// returns true |
| 170 | + |
| 171 | +var v = ndarray2array( y ); |
| 172 | +// returns [ true, true, true ] |
| 173 | +``` |
| 174 | + |
| 175 | +</section> |
| 176 | + |
| 177 | +<!-- /.usage --> |
| 178 | + |
| 179 | +<section class="notes"> |
| 180 | + |
| 181 | +</section> |
| 182 | + |
| 183 | +<!-- /.notes --> |
| 184 | + |
| 185 | +<section class="examples"> |
| 186 | + |
| 187 | +## Examples |
| 188 | + |
| 189 | +<!-- eslint no-undef: "error" --> |
| 190 | + |
| 191 | +```javascript |
| 192 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ).factory; |
| 193 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 194 | +var fillBy = require( '@stdlib/ndarray/fill-by' ); |
| 195 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 196 | +var any = require( '@stdlib/ndarray/any' ); |
| 197 | + |
| 198 | +var x = zeros( [ 2, 4, 5 ], { |
| 199 | + 'dtype': 'float64' |
| 200 | +}); |
| 201 | +x = fillBy( x, bernoulli( 0.50 ) ); |
| 202 | +console.log( ndarray2array( x ) ); |
| 203 | + |
| 204 | +var y = any( x ); |
| 205 | +console.log( 'any(x[:,:,:]) =' ); |
| 206 | +console.log( y.get() ); |
| 207 | + |
| 208 | +y = any( x, { |
| 209 | + 'dims': [ 0 ], |
| 210 | + 'keepdims': true |
| 211 | +}); |
| 212 | +console.log( 'any(x[:,j,k]) =' ); |
| 213 | +console.log( ndarray2array( y ) ); |
| 214 | + |
| 215 | +y = any( x, { |
| 216 | + 'dims': [ 1 ], |
| 217 | + 'keepdims': true |
| 218 | +}); |
| 219 | +console.log( 'any(x[i,:,k]) =' ); |
| 220 | +console.log( ndarray2array( y ) ); |
| 221 | + |
| 222 | +y = any( x, { |
| 223 | + 'dims': [ 2 ], |
| 224 | + 'keepdims': true |
| 225 | +}); |
| 226 | +console.log( 'any(x[i,j,:]) =' ); |
| 227 | +console.log( ndarray2array( y ) ); |
| 228 | + |
| 229 | +y = any( x, { |
| 230 | + 'dims': [ 0, 1 ], |
| 231 | + 'keepdims': true |
| 232 | +}); |
| 233 | +console.log( 'any(x[:,:,k]) =' ); |
| 234 | +console.log( ndarray2array( y ) ); |
| 235 | + |
| 236 | +y = any( x, { |
| 237 | + 'dims': [ 0, 2 ], |
| 238 | + 'keepdims': true |
| 239 | +}); |
| 240 | +console.log( 'any(x[:,j,:]) =' ); |
| 241 | +console.log( ndarray2array( y ) ); |
| 242 | + |
| 243 | +y = any( x, { |
| 244 | + 'dims': [ 1, 2 ], |
| 245 | + 'keepdims': true |
| 246 | +}); |
| 247 | +console.log( 'any(x[i,:,:]) =' ); |
| 248 | +console.log( ndarray2array( y ) ); |
| 249 | + |
| 250 | +y = any( x, { |
| 251 | + 'dims': [ 0, 1, 2 ], |
| 252 | + 'keepdims': true |
| 253 | +}); |
| 254 | +console.log( 'any(x[:,:,:]) =' ); |
| 255 | +console.log( ndarray2array( y ) ); |
| 256 | +``` |
| 257 | + |
| 258 | +</section> |
| 259 | + |
| 260 | +<!-- /.examples --> |
| 261 | + |
| 262 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 263 | + |
| 264 | +<section class="related"> |
| 265 | + |
| 266 | +</section> |
| 267 | + |
| 268 | +<!-- /.related --> |
| 269 | + |
| 270 | +<section class="links"> |
| 271 | + |
| 272 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 273 | + |
| 274 | +<!-- <related-links> --> |
| 275 | + |
| 276 | +<!-- </related-links> --> |
| 277 | + |
| 278 | +</section> |
| 279 | + |
| 280 | +<!-- /.links --> |
0 commit comments