Skip to content

Commit e7e1a3a

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add blas/base/igamax
PR-URL: #7287 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent b479fcf commit e7e1a3a

File tree

13 files changed

+1449
-0
lines changed

13 files changed

+1449
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
# igamax
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 igamax = require( '@stdlib/blas/base/igamax' );
31+
```
32+
33+
#### igamax( N, x, strideX )
34+
35+
Finds the index of the first element having the maximum absolute value.
36+
37+
```javascript
38+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
39+
40+
var idx = igamax( x.length, x, 1 );
41+
// returns 3
42+
```
43+
44+
The function has the following parameters:
45+
46+
- **N**: number of indexed elements.
47+
- **x**: input array.
48+
- **strideX**: stride length for `x`.
49+
50+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to traverse every other value,
51+
52+
```javascript
53+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
54+
55+
var idx = igamax( 4, x, 2 );
56+
// returns 2
57+
```
58+
59+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
60+
61+
```javascript
62+
var Float64Array = require( '@stdlib/array/float64' );
63+
64+
// Initial array:
65+
var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
66+
67+
// Create an offset view:
68+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
69+
70+
// Find index of element having the maximum absolute value:
71+
var idx = igamax( 3, x1, 2 );
72+
// returns 2
73+
```
74+
75+
#### igamax.ndarray( N, x, strideX, offset )
76+
77+
Finds the index of the first element having the maximum absolute value using alternative indexing semantics.
78+
79+
```javascript
80+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
81+
82+
var idx = igamax.ndarray( x.length, x, 1, 0 );
83+
// returns 3
84+
```
85+
86+
The function has the following additional parameters:
87+
88+
- **offsetX**: starting index.
89+
90+
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,
91+
92+
```javascript
93+
var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
94+
95+
var idx = igamax.ndarray( 5, x, 1, 1 );
96+
// returns 4
97+
```
98+
99+
</section>
100+
101+
<!-- /.usage -->
102+
103+
<section class="notes">
104+
105+
## Notes
106+
107+
- If `N < 1`, both functions return `-1`.
108+
- `igamax()` corresponds to the [BLAS][blas] level 1 function [`idamax`][idamax] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`idamax`][@stdlib/blas/base/idamax], [`isamax`][@stdlib/blas/base/isamax], etc.) are likely to be significantly more performant.
109+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
110+
111+
</section>
112+
113+
<!-- /.notes -->
114+
115+
<section class="examples">
116+
117+
## Examples
118+
119+
<!-- eslint no-undef: "error" -->
120+
121+
```javascript
122+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
123+
var igamax = require( '@stdlib/blas/base/igamax' );
124+
125+
var opts = {
126+
'dtype': 'generic'
127+
};
128+
var x = discreteUniform( 10, -100, 100, opts );
129+
console.log( x );
130+
131+
var idx = igamax( x.length, x, 1 );
132+
console.log( idx );
133+
```
134+
135+
</section>
136+
137+
<!-- /.examples -->
138+
139+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
140+
141+
<section class="related">
142+
143+
</section>
144+
145+
<!-- /.related -->
146+
147+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="links">
150+
151+
[blas]: http://www.netlib.org/blas
152+
153+
[idamax]: https://netlib.org/lapack/explore-html-3.6.1/dd/de0/idamax_8f_a285793254ff0adaf58c605682efb880c.html
154+
155+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
156+
157+
[@stdlib/blas/base/idamax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/idamax
158+
159+
[@stdlib/blas/base/isamax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/isamax
160+
161+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
162+
163+
</section>
164+
165+
<!-- /.links -->
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var igamax = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Create a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
var x = uniform( len, -100.0, 100.0, options );
49+
return benchmark;
50+
51+
function benchmark( b ) {
52+
var idx;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
idx = igamax( x.length, x, 1 );
58+
if ( isnan( idx ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( idx ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
function main() {
75+
var len;
76+
var min;
77+
var max;
78+
var f;
79+
var i;
80+
81+
min = 1; // 10^min
82+
max = 6; // 10^max
83+
84+
for ( i = min; i <= max; i++ ) {
85+
len = pow( 10, i );
86+
f = createBenchmark( len );
87+
bench( pkg+':len='+len, f );
88+
}
89+
}
90+
91+
main();
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var igamax = require( './../lib' ).ndarray;
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Create a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
var x = uniform( len, -100.0, 100.0, options );
49+
return benchmark;
50+
51+
function benchmark( b ) {
52+
var idx;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
idx = igamax( x.length, x, 1, 0 );
58+
if ( isnan( idx ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( idx ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
function main() {
75+
var len;
76+
var min;
77+
var max;
78+
var f;
79+
var i;
80+
81+
min = 1; // 10^min
82+
max = 6; // 10^max
83+
84+
for ( i = min; i <= max; i++ ) {
85+
len = pow( 10, i );
86+
f = createBenchmark( len );
87+
bench( pkg+':ndarray:len='+len, f );
88+
}
89+
}
90+
91+
main();

0 commit comments

Comments
 (0)