|
| 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 | +# findLast |
| 22 | + |
| 23 | +> Return a new [ndarray][@stdlib/ndarray/ctor] containing the first elements which pass a test implemented by a predicate function along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var findLast = require( '@stdlib/ndarray/find-last' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### findLast( x\[, options], predicate\[, thisArg] ) |
| 40 | + |
| 41 | +Returns a new [ndarray][@stdlib/ndarray/ctor] containing the last elements which pass a test implemented by a predicate function along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 42 | + |
| 43 | +<!-- eslint-disable no-invalid-this, max-len --> |
| 44 | + |
| 45 | +```javascript |
| 46 | +var array = require( '@stdlib/ndarray/array' ); |
| 47 | + |
| 48 | +function isEven( value ) { |
| 49 | + return value % 2.0 === 0.0; |
| 50 | +} |
| 51 | + |
| 52 | +// Create an input ndarray: |
| 53 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 54 | +// returns <ndarray> |
| 55 | + |
| 56 | +// Perform reduction: |
| 57 | +var out = findLast( x, isEven ); |
| 58 | +// returns <ndarray> |
| 59 | + |
| 60 | +var v = out.get(); |
| 61 | +// returns 8.0 |
| 62 | +``` |
| 63 | + |
| 64 | +The function accepts the following arguments: |
| 65 | + |
| 66 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 67 | +- **options**: function options _(optional)_. |
| 68 | +- **predicate**: predicate function. |
| 69 | +- **thisArg**: predicate function execution context _(optional)_. |
| 70 | + |
| 71 | +The function accepts the following options: |
| 72 | + |
| 73 | +- **dims**: list of dimensions over which to perform a reduction. |
| 74 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. |
| 75 | +- **sentinelValue**: value to return when no element passes the test. May be either a scalar value or a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 76 | + |
| 77 | +By default, the function performs reduction over all all elements in a provided [ndarray][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option. |
| 78 | + |
| 79 | +```javascript |
| 80 | +var array = require( '@stdlib/ndarray/array' ); |
| 81 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 82 | + |
| 83 | +function isEven( value ) { |
| 84 | + return value % 2.0 === 0.0; |
| 85 | +} |
| 86 | + |
| 87 | +// Create an input ndarray: |
| 88 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 89 | +// returns <ndarray> |
| 90 | + |
| 91 | +var opts = { |
| 92 | + 'dims': [ 0 ] |
| 93 | +}; |
| 94 | + |
| 95 | +// Perform reduction: |
| 96 | +var out = findLast( x, opts, isEven ); |
| 97 | +// returns <ndarray> |
| 98 | + |
| 99 | +var v = ndarray2array( out ); |
| 100 | +// returns [ [ 8.0, NaN ], [ 6.0, NaN ] ] |
| 101 | +``` |
| 102 | + |
| 103 | +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`. |
| 104 | + |
| 105 | +```javascript |
| 106 | +var array = require( '@stdlib/ndarray/array' ); |
| 107 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 108 | + |
| 109 | +function isEven( value ) { |
| 110 | + return value % 2.0 === 0.0; |
| 111 | +} |
| 112 | + |
| 113 | +// Create an input ndarray: |
| 114 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 115 | +// returns <ndarray> |
| 116 | + |
| 117 | +var opts = { |
| 118 | + 'dims': [ 0 ], |
| 119 | + 'keepdims': true |
| 120 | +}; |
| 121 | + |
| 122 | +// Perform reduction: |
| 123 | +var out = findLast( x, opts, isEven ); |
| 124 | +// returns <ndarray> |
| 125 | + |
| 126 | +var v = ndarray2array( out ); |
| 127 | +// returns [ [ [ 8.0, NaN ], [ 6.0, NaN ] ] ] |
| 128 | +``` |
| 129 | + |
| 130 | +To specify a custom sentinel value to return when no element passes the test, set the `sentinelValue` option. |
| 131 | + |
| 132 | +```javascript |
| 133 | +var array = require( '@stdlib/ndarray/array' ); |
| 134 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 135 | + |
| 136 | +function isEven( value ) { |
| 137 | + return value % 2.0 === 0.0; |
| 138 | +} |
| 139 | + |
| 140 | +// Create an input ndarray: |
| 141 | +var x = array( [ [ [ 1.0, 3.0 ], [ 5.0, 7.0 ] ], [ [ 9.0, 11.0 ], [ 13.0, 15.0 ] ] ] ); |
| 142 | +// returns <ndarray> |
| 143 | + |
| 144 | +var opts = { |
| 145 | + 'sentinelValue': -999 |
| 146 | +}; |
| 147 | + |
| 148 | +// Perform reduction: |
| 149 | +var out = findLast( x, opts, isEven ); |
| 150 | +// returns <ndarray> |
| 151 | + |
| 152 | +var v = out.get(); |
| 153 | +// returns -999 |
| 154 | +``` |
| 155 | + |
| 156 | +To set the `predicate` function execution context, provide a `thisArg`. |
| 157 | + |
| 158 | +<!-- eslint-disable no-invalid-this --> |
| 159 | + |
| 160 | +```javascript |
| 161 | +var array = require( '@stdlib/ndarray/array' ); |
| 162 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 163 | + |
| 164 | +function isEven( value ) { |
| 165 | + this.count += 1; |
| 166 | + return value % 2.0 === 0.0; |
| 167 | +} |
| 168 | + |
| 169 | +// Create an input ndarray: |
| 170 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 171 | +// returns <ndarray> |
| 172 | + |
| 173 | +var ctx = { |
| 174 | + 'count': 0 |
| 175 | +}; |
| 176 | + |
| 177 | +// Perform reduction: |
| 178 | +var out = findLast( x, isEven, ctx ); |
| 179 | +// returns <ndarray> |
| 180 | + |
| 181 | +var v = out.get(); |
| 182 | +// returns 8.0 |
| 183 | + |
| 184 | +var count = ctx.count; |
| 185 | +// returns 1 |
| 186 | +``` |
| 187 | + |
| 188 | +#### findLast.assign( x, out\[, options], predicate\[, thisArg] ) |
| 189 | + |
| 190 | +Finds the last elements which pass a test implemented by a predicate function along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. |
| 191 | + |
| 192 | +```javascript |
| 193 | +var array = require( '@stdlib/ndarray/array' ); |
| 194 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 195 | + |
| 196 | +function isEven( value ) { |
| 197 | + return value % 2.0 === 0.0; |
| 198 | +} |
| 199 | + |
| 200 | +// Create an input ndarray: |
| 201 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 202 | +// returns <ndarray> |
| 203 | + |
| 204 | +// Create an output ndarray: |
| 205 | +var y = empty( [], { |
| 206 | + 'dtype': x.dtype |
| 207 | +}); |
| 208 | + |
| 209 | +// Perform reduction: |
| 210 | +var out = findLast.assign( x, y, isEven ); |
| 211 | +// returns <ndarray> |
| 212 | + |
| 213 | +var bool = ( out === y ); |
| 214 | +// returns true |
| 215 | + |
| 216 | +var v = y.get(); |
| 217 | +// returns 8.0 |
| 218 | +``` |
| 219 | + |
| 220 | +The function accepts the following arguments: |
| 221 | + |
| 222 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 223 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. |
| 224 | +- **options**: function options _(optional)_. |
| 225 | +- **predicate**: predicate function. |
| 226 | +- **thisArg**: predicate function execution context _(optional)_. |
| 227 | + |
| 228 | +The function accepts the following options: |
| 229 | + |
| 230 | +- **dims**: list of dimensions over which to perform a reduction. |
| 231 | +- **sentinelValue**: value to return when no element passes the test. May be either a scalar value or a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 232 | + |
| 233 | +```javascript |
| 234 | +var array = require( '@stdlib/ndarray/array' ); |
| 235 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 236 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 237 | + |
| 238 | +function isEven( value ) { |
| 239 | + return value % 2.0 === 0.0; |
| 240 | +} |
| 241 | + |
| 242 | +// Create an input ndarray: |
| 243 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 244 | +// returns <ndarray> |
| 245 | + |
| 246 | +// Create an output ndarray: |
| 247 | +var y = empty( [ 2, 2 ], { |
| 248 | + 'dtype': x.dtype |
| 249 | +}); |
| 250 | + |
| 251 | +var opts = { |
| 252 | + 'dims': [ 0 ] |
| 253 | +}; |
| 254 | + |
| 255 | +// Perform reduction: |
| 256 | +var out = findLast.assign( x, y, opts, isEven ); |
| 257 | + |
| 258 | +var bool = ( out === y ); |
| 259 | +// returns true |
| 260 | + |
| 261 | +var v = ndarray2array( y ); |
| 262 | +// returns [ [ 8.0, NaN ], [ 6.0, NaN ] ] |
| 263 | +``` |
| 264 | + |
| 265 | +</section> |
| 266 | + |
| 267 | +<!-- /.usage --> |
| 268 | + |
| 269 | +<section class="notes"> |
| 270 | + |
| 271 | +## Notes |
| 272 | + |
| 273 | +- By default, when no `sentinelValue` is provided, the function returns a default sentinel value based on the input [ndarray][@stdlib/ndarray/ctor] [data-type][@stdlib/ndarray/dtypes]: |
| 274 | + |
| 275 | + - real-valued floating-point data types: `NaN`. |
| 276 | + - complex-valued floating-point data types: `NaN + NaNj`. |
| 277 | + - integer data types: maximum value. |
| 278 | + - boolean data types: `false`. |
| 279 | + |
| 280 | +- The `predicate` function is provided the following arguments: |
| 281 | + |
| 282 | + - **value**: current array element. |
| 283 | + - **indices**: current array element indices. |
| 284 | + - **arr**: the input [ndarray][@stdlib/ndarray/ctor]. |
| 285 | + |
| 286 | +</section> |
| 287 | + |
| 288 | +<!-- /.notes --> |
| 289 | + |
| 290 | +<section class="examples"> |
| 291 | + |
| 292 | +## Examples |
| 293 | + |
| 294 | +<!-- eslint no-undef: "error" --> |
| 295 | + |
| 296 | +```javascript |
| 297 | +var uniform = require( '@stdlib/random/uniform' ); |
| 298 | +var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive; |
| 299 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 300 | +var findLast = require( '@stdlib/ndarray/find-last' ); |
| 301 | + |
| 302 | +var x = uniform( [ 2, 4, 5 ], -10.0, 10.0, { |
| 303 | + 'dtype': 'float64' |
| 304 | +}); |
| 305 | +console.log( ndarray2array( x ) ); |
| 306 | + |
| 307 | +var y = findLast( x, isPositive ); |
| 308 | +console.log( y.get() ); |
| 309 | +``` |
| 310 | + |
| 311 | +</section> |
| 312 | + |
| 313 | +<!-- /.examples --> |
| 314 | + |
| 315 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 316 | + |
| 317 | +<section class="related"> |
| 318 | + |
| 319 | +</section> |
| 320 | + |
| 321 | +<!-- /.related --> |
| 322 | + |
| 323 | +<section class="links"> |
| 324 | + |
| 325 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 326 | + |
| 327 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 328 | + |
| 329 | +<!-- <related-links> --> |
| 330 | + |
| 331 | +<!-- </related-links> --> |
| 332 | + |
| 333 | +</section> |
| 334 | + |
| 335 | +<!-- /.links --> |
0 commit comments