|  | 
|  | 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 | +# lastIndexOf | 
|  | 22 | + | 
|  | 23 | +> Return the last index of a specified search element along an [ndarray][@stdlib/ndarray/ctor] dimension. | 
|  | 24 | +
 | 
|  | 25 | +<section class="usage"> | 
|  | 26 | + | 
|  | 27 | +## Usage | 
|  | 28 | + | 
|  | 29 | +```javascript | 
|  | 30 | +var lastIndexOf = require( '@stdlib/blas/ext/last-index-of' ); | 
|  | 31 | +``` | 
|  | 32 | + | 
|  | 33 | +#### lastIndexOf( x, searchElement\[, fromIndex]\[, options] ) | 
|  | 34 | + | 
|  | 35 | +Returns the last index of a specified search element along an [ndarray][@stdlib/ndarray/ctor] dimension. | 
|  | 36 | + | 
|  | 37 | +```javascript | 
|  | 38 | +var array = require( '@stdlib/ndarray/array' ); | 
|  | 39 | + | 
|  | 40 | +// Create an input ndarray: | 
|  | 41 | +var x = array( [ 1.0, 2.0, 3.0, 2.0, 5.0, 6.0 ] ); | 
|  | 42 | +// returns <ndarray> | 
|  | 43 | + | 
|  | 44 | +// Perform operation: | 
|  | 45 | +var out = lastIndexOf( x, 2.0 ); | 
|  | 46 | +// returns <ndarray> | 
|  | 47 | + | 
|  | 48 | +var idx = out.get(); | 
|  | 49 | +// returns 3 | 
|  | 50 | +``` | 
|  | 51 | + | 
|  | 52 | +The function has the following parameters: | 
|  | 53 | + | 
|  | 54 | +-   **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have at least one dimension. | 
|  | 55 | +-   **searchElement**: search element. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor]. If provided a scalar value, the value is cast to the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, the search element [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. | 
|  | 56 | +-   **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, a provided [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. If provided a negative integer, the index at which to begin searching along a dimension is determined by counting backward from the last element (where `-1` refers to the last element). Default: `-1`. | 
|  | 57 | +-   **options**: function options (_optional_). | 
|  | 58 | + | 
|  | 59 | +The function accepts the following options: | 
|  | 60 | + | 
|  | 61 | +-   **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be an integer index or generic [data type][@stdlib/ndarray/dtypes]. | 
|  | 62 | +-   **dim**: dimension over which to perform operation. If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). Default: `-1`. | 
|  | 63 | +-   **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. | 
|  | 64 | + | 
|  | 65 | +If the function is unable to find a search element along an [ndarray][@stdlib/ndarray/ctor], the corresponding element in the returned [ndarray][@stdlib/ndarray/ctor] is `-1`. | 
|  | 66 | + | 
|  | 67 | +```javascript | 
|  | 68 | +var array = require( '@stdlib/ndarray/array' ); | 
|  | 69 | + | 
|  | 70 | +// Create an input ndarray: | 
|  | 71 | +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); | 
|  | 72 | +// returns <ndarray> | 
|  | 73 | + | 
|  | 74 | +// Perform operation: | 
|  | 75 | +var out = lastIndexOf( x, 10.0 ); | 
|  | 76 | +// returns <ndarray> | 
|  | 77 | + | 
|  | 78 | +var idx = out.get(); | 
|  | 79 | +// returns -1 | 
|  | 80 | +``` | 
|  | 81 | + | 
|  | 82 | +By default, the function begins searching from the last element along the reduction dimension. To begin searching from a different index, provide a `fromIndex` argument. | 
|  | 83 | + | 
|  | 84 | +```javascript | 
|  | 85 | +var array = require( '@stdlib/ndarray/array' ); | 
|  | 86 | + | 
|  | 87 | +// Create an input ndarray: | 
|  | 88 | +var x = array( [ 1.0, 2.0, 3.0, 4.0, 2.0, 6.0 ] ); | 
|  | 89 | +// returns <ndarray> | 
|  | 90 | + | 
|  | 91 | +// Perform operation: | 
|  | 92 | +var out = lastIndexOf( x, 2.0, 3 ); | 
|  | 93 | +// returns <ndarray> | 
|  | 94 | + | 
|  | 95 | +var idx = out.get(); | 
|  | 96 | +// returns 1 | 
|  | 97 | +``` | 
|  | 98 | + | 
|  | 99 | +By default, the function performs the operation over elements in the last dimension. To perform the operation over a different dimension, provide a `dim` option. | 
|  | 100 | + | 
|  | 101 | +```javascript | 
|  | 102 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); | 
|  | 103 | +var array = require( '@stdlib/ndarray/array' ); | 
|  | 104 | + | 
|  | 105 | +var x = array( [ [ -3.0, 2.0 ], [ -3.0, 4.0 ] ] ); | 
|  | 106 | + | 
|  | 107 | +var out = lastIndexOf( x, -3.0, { | 
|  | 108 | +    'dim': 0 | 
|  | 109 | +}); | 
|  | 110 | +// returns <ndarray> | 
|  | 111 | + | 
|  | 112 | +var idx = ndarray2array( out ); | 
|  | 113 | +// returns [ 1, -1 ] | 
|  | 114 | +``` | 
|  | 115 | + | 
|  | 116 | +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. | 
|  | 117 | + | 
|  | 118 | +```javascript | 
|  | 119 | +var array = require( '@stdlib/ndarray/array' ); | 
|  | 120 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); | 
|  | 121 | + | 
|  | 122 | +var x = array( [ [ -3.0, 2.0 ], [ -3.0, 4.0 ] ] ); | 
|  | 123 | + | 
|  | 124 | +var opts = { | 
|  | 125 | +    'dim': 0, | 
|  | 126 | +    'keepdims': true | 
|  | 127 | +}; | 
|  | 128 | + | 
|  | 129 | +var out = lastIndexOf( x, -3.0, opts ); | 
|  | 130 | +// returns <ndarray> | 
|  | 131 | + | 
|  | 132 | +var idx = ndarray2array( out ); | 
|  | 133 | +// returns [ [ 1, -1 ] ] | 
|  | 134 | +``` | 
|  | 135 | + | 
|  | 136 | +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. | 
|  | 137 | + | 
|  | 138 | +```javascript | 
|  | 139 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); | 
|  | 140 | +var dtype = require( '@stdlib/ndarray/dtype' ); | 
|  | 141 | +var array = require( '@stdlib/ndarray/array' ); | 
|  | 142 | + | 
|  | 143 | +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); | 
|  | 144 | + | 
|  | 145 | +var idx = lastIndexOf( x, 2.0, { | 
|  | 146 | +    'dtype': 'generic' | 
|  | 147 | +}); | 
|  | 148 | +// returns <ndarray> | 
|  | 149 | + | 
|  | 150 | +var dt = dtype( idx ); | 
|  | 151 | +// returns 'generic' | 
|  | 152 | +``` | 
|  | 153 | + | 
|  | 154 | +#### lastIndexOf.assign( x, searchElement\[, fromIndex], out\[, options] ) | 
|  | 155 | + | 
|  | 156 | +Returns the last index of a specified search element along an [ndarray][@stdlib/ndarray/ctor] dimension and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. | 
|  | 157 | + | 
|  | 158 | +```javascript | 
|  | 159 | +var array = require( '@stdlib/ndarray/array' ); | 
|  | 160 | +var zeros = require( '@stdlib/ndarray/zeros' ); | 
|  | 161 | + | 
|  | 162 | +var x = array( [ 1.0, 2.0, 3.0, 2.0, 5.0, 6.0 ] ); | 
|  | 163 | +var y = zeros( [], { | 
|  | 164 | +    'dtype': 'int32' | 
|  | 165 | +}); | 
|  | 166 | + | 
|  | 167 | +var out = lastIndexOf.assign( x, 2.0, y ); | 
|  | 168 | +// returns <ndarray> | 
|  | 169 | + | 
|  | 170 | +var idx = out.get(); | 
|  | 171 | +// returns 3 | 
|  | 172 | + | 
|  | 173 | +var bool = ( out === y ); | 
|  | 174 | +// returns true | 
|  | 175 | +``` | 
|  | 176 | + | 
|  | 177 | +The method has the following parameters: | 
|  | 178 | + | 
|  | 179 | +-   **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have at least one dimension. | 
|  | 180 | +-   **searchElement**: search element. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor]. If provided a scalar value, the value is cast to the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, the search element [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. | 
|  | 181 | +-   **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, a provided [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. If provided a negative integer, the index at which to begin searching along a dimension is determined by counting backward from the last element (where `-1` refers to the last element). Default: `-1`. | 
|  | 182 | +-   **out**: output [ndarray][@stdlib/ndarray/ctor]. | 
|  | 183 | +-   **options**: function options (_optional_). | 
|  | 184 | + | 
|  | 185 | +The method accepts the following options: | 
|  | 186 | + | 
|  | 187 | +-   **dim**: dimension over which to perform operation. If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). Default: `-1`. | 
|  | 188 | + | 
|  | 189 | +</section> | 
|  | 190 | + | 
|  | 191 | +<!-- /.usage --> | 
|  | 192 | + | 
|  | 193 | +<section class="notes"> | 
|  | 194 | + | 
|  | 195 | +## Notes | 
|  | 196 | + | 
|  | 197 | +-   Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. | 
|  | 198 | +-   The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. | 
|  | 199 | + | 
|  | 200 | +</section> | 
|  | 201 | + | 
|  | 202 | +<!-- /.notes --> | 
|  | 203 | + | 
|  | 204 | +<section class="examples"> | 
|  | 205 | + | 
|  | 206 | +## Examples | 
|  | 207 | + | 
|  | 208 | +<!-- eslint no-undef: "error" --> | 
|  | 209 | + | 
|  | 210 | +```javascript | 
|  | 211 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | 
|  | 212 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); | 
|  | 213 | +var ndarray = require( '@stdlib/ndarray/ctor' ); | 
|  | 214 | +var lastIndexOf = require( '@stdlib/blas/ext/last-index-of' ); | 
|  | 215 | + | 
|  | 216 | +// Generate an array of random numbers: | 
|  | 217 | +var xbuf = discreteUniform( 10, 0, 20, { | 
|  | 218 | +    'dtype': 'float64' | 
|  | 219 | +}); | 
|  | 220 | + | 
|  | 221 | +// Wrap in an ndarray: | 
|  | 222 | +var x = new ndarray( 'float64', xbuf, [ 5, 2 ], [ 2, 1 ], 0, 'row-major' ); | 
|  | 223 | +console.log( ndarray2array( x ) ); | 
|  | 224 | + | 
|  | 225 | +// Perform operation: | 
|  | 226 | +var idx = lastIndexOf( x, 10.0, { | 
|  | 227 | +    'dim': 0 | 
|  | 228 | +}); | 
|  | 229 | + | 
|  | 230 | +// Print the results: | 
|  | 231 | +console.log( ndarray2array( idx ) ); | 
|  | 232 | +``` | 
|  | 233 | + | 
|  | 234 | +</section> | 
|  | 235 | + | 
|  | 236 | +<!-- /.examples --> | 
|  | 237 | + | 
|  | 238 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | 
|  | 239 | + | 
|  | 240 | +<section class="related"> | 
|  | 241 | + | 
|  | 242 | +</section> | 
|  | 243 | + | 
|  | 244 | +<!-- /.related --> | 
|  | 245 | + | 
|  | 246 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | 
|  | 247 | + | 
|  | 248 | +<section class="links"> | 
|  | 249 | + | 
|  | 250 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor | 
|  | 251 | + | 
|  | 252 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes | 
|  | 253 | + | 
|  | 254 | +[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies | 
|  | 255 | + | 
|  | 256 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes | 
|  | 257 | + | 
|  | 258 | +</section> | 
|  | 259 | + | 
|  | 260 | +<!-- /.links --> | 
0 commit comments