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