|
| 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 | +# some |
| 22 | + |
| 23 | +> Test whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions are 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 some = require( '@stdlib/ndarray/some' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### some( x, n\[, options] ) |
| 40 | + |
| 41 | +Tests whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions are 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 ] ], [ [ 0.0, 6.0 ] ] ] ); |
| 48 | +// returns <ndarray> |
| 49 | + |
| 50 | +// Perform reduction: |
| 51 | +var out = some( x, 3 ); |
| 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 | +- **n**: number of elements which must be truthy. May be either a scalar or an [`ndarray`][@stdlib/ndarray/ctor]. Must be [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of input [`ndarray`][@stdlib/ndarray/ctor]. Must have an integer [data type][@stdlib/ndarray/dtypes]. |
| 62 | +- **options**: function options (_optional_). |
| 63 | + |
| 64 | +The function accepts the following options: |
| 65 | + |
| 66 | +- **dims**: list of dimensions over which to perform a reduction. |
| 67 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [`ndarray`][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. |
| 68 | + |
| 69 | +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. |
| 70 | + |
| 71 | +```javascript |
| 72 | +var array = require( '@stdlib/ndarray/array' ); |
| 73 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 74 | + |
| 75 | +// Create an input ndarray: |
| 76 | +var x = array( [ [ [ 1.0, 0.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] ); |
| 77 | +// returns <ndarray> |
| 78 | + |
| 79 | +var opts = { |
| 80 | + 'dims': [ 0, 1 ] |
| 81 | +}; |
| 82 | + |
| 83 | +// Perform reduction: |
| 84 | +var out = some( x, 2, opts ); |
| 85 | +// returns <ndarray> |
| 86 | + |
| 87 | +var v = ndarray2array( out ); |
| 88 | +// returns [ true, true ] |
| 89 | +``` |
| 90 | + |
| 91 | +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`. |
| 92 | + |
| 93 | +```javascript |
| 94 | +var array = require( '@stdlib/ndarray/array' ); |
| 95 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 96 | + |
| 97 | +// Create an input ndarray: |
| 98 | +var x = array( [ [ [ 1.0, 0.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] ); |
| 99 | +// returns <ndarray> |
| 100 | + |
| 101 | +var opts = { |
| 102 | + 'dims': [ 0, 1 ], |
| 103 | + 'keepdims': true |
| 104 | +}; |
| 105 | + |
| 106 | +// Perform reduction: |
| 107 | +var out = some( x, 2, opts ); |
| 108 | +// returns <ndarray> |
| 109 | + |
| 110 | +var v = ndarray2array( out ); |
| 111 | +// returns [ [ [ true, true ] ] ] |
| 112 | +``` |
| 113 | + |
| 114 | +#### some.assign( x, n, out\[, options] ) |
| 115 | + |
| 116 | +Tests whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions are truthy and assigns the results to an output [`ndarray`][@stdlib/ndarray/ctor]. |
| 117 | + |
| 118 | +```javascript |
| 119 | +var array = require( '@stdlib/ndarray/array' ); |
| 120 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 121 | + |
| 122 | +// Create an input ndarray: |
| 123 | +var x = array( [ [ [ 1.0, 0.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] ); |
| 124 | +// returns <ndarray> |
| 125 | + |
| 126 | +// Create an output ndarray: |
| 127 | +var y = empty( [], { |
| 128 | + 'dtype': 'bool' |
| 129 | +}); |
| 130 | + |
| 131 | +// Perform reduction: |
| 132 | +var out = some.assign( x, 3, y ); |
| 133 | +// returns <ndarray> |
| 134 | + |
| 135 | +var bool = ( out === y ); |
| 136 | +// returns true |
| 137 | + |
| 138 | +var v = y.get(); |
| 139 | +// returns true |
| 140 | +``` |
| 141 | + |
| 142 | +The function accepts the following arguments: |
| 143 | + |
| 144 | +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. |
| 145 | +- **n**: number of elements which must be truthy. May be either a scalar or an [`ndarray`][@stdlib/ndarray/ctor]. Must be [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of input [`ndarray`][@stdlib/ndarray/ctor]. Must have an integer [data type][@stdlib/ndarray/dtypes]. |
| 146 | +- **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]. |
| 147 | +- **options**: function options (_optional_). |
| 148 | + |
| 149 | +The function accepts the following `options`: |
| 150 | + |
| 151 | +- **dims**: list of dimensions over which to perform a reduction. |
| 152 | + |
| 153 | +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. |
| 154 | + |
| 155 | +```javascript |
| 156 | +var array = require( '@stdlib/ndarray/array' ); |
| 157 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 158 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 159 | + |
| 160 | +// Create an input ndarray: |
| 161 | +var x = array( [ [ [ 1.0, 0.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] ); |
| 162 | +// returns <ndarray> |
| 163 | + |
| 164 | +// Create an output ndarray: |
| 165 | +var y = empty( [ 2 ], { |
| 166 | + 'dtype': 'bool' |
| 167 | +}); |
| 168 | + |
| 169 | +var opts = { |
| 170 | + 'dims': [ 0, 1 ] |
| 171 | +}; |
| 172 | + |
| 173 | +// Perform reduction: |
| 174 | +var out = some.assign( x, 2, y, opts ); |
| 175 | +// returns <ndarray> |
| 176 | + |
| 177 | +var bool = ( out === y ); |
| 178 | +// returns true |
| 179 | + |
| 180 | +var v = ndarray2array( out ); |
| 181 | +// returns [ true, true ] |
| 182 | +``` |
| 183 | + |
| 184 | +</section> |
| 185 | + |
| 186 | +<!-- /.usage --> |
| 187 | + |
| 188 | +<section class="notes"> |
| 189 | + |
| 190 | +</section> |
| 191 | + |
| 192 | +<!-- /.notes --> |
| 193 | + |
| 194 | +<section class="examples"> |
| 195 | + |
| 196 | +## Examples |
| 197 | + |
| 198 | +<!-- eslint no-undef: "error" --> |
| 199 | + |
| 200 | +```javascript |
| 201 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; |
| 202 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 203 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 204 | +var fillBy = require( '@stdlib/ndarray/fill-by' ); |
| 205 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 206 | +var some = require( '@stdlib/ndarray/some' ); |
| 207 | + |
| 208 | +var x = zeros( [ 2, 4, 5 ], { |
| 209 | + 'dtype': 'float64' |
| 210 | +}); |
| 211 | +x = fillBy( x, discreteUniform( 0, 10 ) ); |
| 212 | +console.log( ndarray2array( x ) ); |
| 213 | + |
| 214 | +var n = scalar2ndarray( 4, { |
| 215 | + 'dtype': 'int8' |
| 216 | +}); |
| 217 | +var y = some( x, n ); |
| 218 | +console.log( y.get() ); |
| 219 | +``` |
| 220 | + |
| 221 | +</section> |
| 222 | + |
| 223 | +<!-- /.examples --> |
| 224 | + |
| 225 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 226 | + |
| 227 | +<section class="related"> |
| 228 | + |
| 229 | +</section> |
| 230 | + |
| 231 | +<!-- /.related --> |
| 232 | + |
| 233 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 234 | + |
| 235 | +<section class="links"> |
| 236 | + |
| 237 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 238 | + |
| 239 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 240 | + |
| 241 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 242 | + |
| 243 | +</section> |
| 244 | + |
| 245 | +<!-- /.links --> |
0 commit comments