|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 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 | +# idamax |
| 22 | + |
| 23 | +> Find the index of the first element having the maximum absolute value. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var idamax = require( '@stdlib/blas/base/idamax' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### idamax( N, x, strideX ) |
| 34 | + |
| 35 | +Finds the index of the first element having the maximum absolute value. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 39 | + |
| 40 | +var x = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); |
| 41 | + |
| 42 | +var idx = idamax( x.length, x, 1 ); |
| 43 | +// returns 3 |
| 44 | +``` |
| 45 | + |
| 46 | +The function has the following parameters: |
| 47 | + |
| 48 | +- **N**: number of indexed elements. |
| 49 | +- **x**: input [`Float64Array`][@stdlib/array/float64]. |
| 50 | +- **strideX**: index increment for `x`. |
| 51 | + |
| 52 | +The `N` and `strideX` parameters determine which elements in `x` are accessed at runtime. For example, to traverse every other value, |
| 53 | + |
| 54 | +```javascript |
| 55 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 56 | + |
| 57 | +var x = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); |
| 58 | + |
| 59 | +var idx = idamax( 4, x, 2 ); |
| 60 | +// returns 2 |
| 61 | +``` |
| 62 | + |
| 63 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 64 | + |
| 65 | +```javascript |
| 66 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 67 | + |
| 68 | +// Initial array: |
| 69 | +var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); |
| 70 | + |
| 71 | +// Create an offset view: |
| 72 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 73 | + |
| 74 | +// Find index of element having the maximum absolute value: |
| 75 | +var idx = idamax( 3, x1, 2 ); |
| 76 | +// returns 2 |
| 77 | +``` |
| 78 | + |
| 79 | +#### idamax.ndarray( N, x, strideX, offset ) |
| 80 | + |
| 81 | +Finds the index of the first element having the maximum absolute value using alternative indexing semantics. |
| 82 | + |
| 83 | +```javascript |
| 84 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 85 | + |
| 86 | +var x = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); |
| 87 | + |
| 88 | +var idx = idamax.ndarray( x.length, x, 1, 0 ); |
| 89 | +// returns 3 |
| 90 | +``` |
| 91 | + |
| 92 | +The function has the following additional parameters: |
| 93 | + |
| 94 | +- **offsetX**: starting index. |
| 95 | + |
| 96 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the `offset` parameter supports indexing semantics based on a starting index. For example, to start from the second index, |
| 97 | + |
| 98 | +```javascript |
| 99 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 100 | + |
| 101 | +var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); |
| 102 | + |
| 103 | +var idx = idamax.ndarray( 5, x, 1, 1 ); |
| 104 | +// returns 4 |
| 105 | +``` |
| 106 | + |
| 107 | +</section> |
| 108 | + |
| 109 | +<!-- /.usage --> |
| 110 | + |
| 111 | +<section class="notes"> |
| 112 | + |
| 113 | +## Notes |
| 114 | + |
| 115 | +- If `N < 1` or `strideX <= 0`, both functions return `0`. |
| 116 | +- `idamax()` corresponds to the [BLAS][blas] level 1 function [`idamax`][idamax]. |
| 117 | + |
| 118 | +</section> |
| 119 | + |
| 120 | +<!-- /.notes --> |
| 121 | + |
| 122 | +<section class="examples"> |
| 123 | + |
| 124 | +## Examples |
| 125 | + |
| 126 | +<!-- eslint no-undef: "error" --> |
| 127 | + |
| 128 | +```javascript |
| 129 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 130 | +var idamax = require( '@stdlib/blas/base/idamax' ); |
| 131 | + |
| 132 | +var opts = { |
| 133 | + 'dtype': 'float64' |
| 134 | +}; |
| 135 | +var x = discreteUniform( 10, -100, 100, opts ); |
| 136 | +console.log( x ); |
| 137 | + |
| 138 | +var idx = idamax( x.length, x, 1 ); |
| 139 | +console.log( idx ); |
| 140 | +``` |
| 141 | + |
| 142 | +</section> |
| 143 | + |
| 144 | +<!-- /.examples --> |
| 145 | + |
| 146 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 147 | + |
| 148 | +<section class="related"> |
| 149 | + |
| 150 | +</section> |
| 151 | + |
| 152 | +<!-- /.related --> |
| 153 | + |
| 154 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 155 | + |
| 156 | +<section class="links"> |
| 157 | + |
| 158 | +[blas]: http://www.netlib.org/blas |
| 159 | + |
| 160 | +[idamax]: http://www.netlib.org/lapack/explore-html/de/da4/group__double__blas__level1.html |
| 161 | + |
| 162 | +[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64 |
| 163 | + |
| 164 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 165 | + |
| 166 | +</section> |
| 167 | + |
| 168 | +<!-- /.links --> |
0 commit comments