Skip to content

Commit 251547e

Browse files
committed
feat: add BLAS Level 1 routine for igamax
1 parent d9359b6 commit 251547e

File tree

14 files changed

+1378
-0
lines changed

14 files changed

+1378
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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 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 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.0
42+
```
43+
44+
The function has the following parameters:
45+
46+
- **N**: number of indexed elements.
47+
- **x**: first input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
48+
- **strideX**: index increment for `x`.
49+
50+
The `N` and `strideX` parameters determine which elements in `x` are accessed at runtime. For example, to find index of element having maximum absolute value traversing 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+
<!-- eslint-disable stdlib/capitalized-comments -->
62+
63+
```javascript
64+
var Float64Array = require( '@stdlib/array/float64' );
65+
66+
// Initial array:
67+
var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
68+
69+
// Create offset view:
70+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
71+
72+
// Find index of element having maximum absolute value for every other element...
73+
var idx = igamax( 3, x1, 2 );
74+
// returns 2
75+
```
76+
77+
If either `N` is less than `1` or `strideX` is less than or equal to `0`, the function returns `0`.
78+
79+
#### igamax.ndarray( N, x, strideX, offsetX )
80+
81+
Finds the index of the first element having maximum absolute value using alternative indexing semantics.
82+
83+
```javascript
84+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
85+
86+
var idx = igamax.ndarray( x.length, x, 1, 0 );
87+
// returns 3
88+
```
89+
90+
The function has the following additional parameter:
91+
92+
- **offsetX**: starting index for `x`.
93+
94+
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 find the index of the element having the maximum absolute value starting from second index,
95+
96+
```javascript
97+
var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
98+
99+
var idx = igamax.ndarray( 5, x, 1, 1 );
100+
// returns 4
101+
```
102+
103+
</section>
104+
105+
<!-- /.usage -->
106+
107+
<section class="notes">
108+
109+
## Notes
110+
111+
- If `N < 1` or `strideX <= 0`, both functions return `0`.
112+
- `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.
113+
114+
</section>
115+
116+
<!-- /.notes -->
117+
118+
<section class="examples">
119+
120+
## Examples
121+
122+
<!-- eslint no-undef: "error" -->
123+
124+
```javascript
125+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
126+
var igamax = require( '@stdlib/blas/base/igamax' );
127+
128+
var opts = {
129+
'dtype': 'float64'
130+
};
131+
var x = discreteUniform( 10, 0, 500, opts );
132+
console.log( x );
133+
134+
var idx = igamax.ndarray( x.length, x, 1, 0 );
135+
console.log( idx );
136+
```
137+
138+
</section>
139+
140+
<!-- /.examples -->
141+
142+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
143+
144+
<section class="related">
145+
146+
</section>
147+
148+
<!-- /.related -->
149+
150+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
151+
152+
<section class="links">
153+
154+
[blas]: http://www.netlib.org/blas
155+
156+
[idamax]: http://www.netlib.org/lapack/explore-html/de/da4/group__double__blas__level1.html
157+
158+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
159+
160+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
161+
162+
[@stdlib/blas/base/idamax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/idamax
163+
164+
[@stdlib/blas/base/isamax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/isamax
165+
166+
</section>
167+
168+
<!-- /.links -->
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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': 'generic'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates 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+
/**
52+
* Benchmark function.
53+
*
54+
* @private
55+
* @param {Benchmark} b - benchmark instance
56+
*/
57+
function benchmark( b ) {
58+
var idx;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
idx = igamax( len, x, 1 );
64+
if ( isnan( idx ) ) {
65+
b.fail( 'something went wrong' );
66+
}
67+
}
68+
b.toc();
69+
if ( isnan( idx ) ) {
70+
b.fail( 'something went wrong' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':len='+len, f );
99+
}
100+
}
101+
102+
main();
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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': 'generic'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates 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+
/**
52+
* Benchmark function.
53+
*
54+
* @private
55+
* @param {Benchmark} b - benchmark instance
56+
*/
57+
function benchmark( b ) {
58+
var idx;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
idx = igamax( len, x, 1, 0 );
64+
if ( isnan( idx ) ) {
65+
b.fail( 'something went wrong' );
66+
}
67+
}
68+
b.toc();
69+
if ( isnan( idx ) ) {
70+
b.fail( 'something went wrong' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':ndarray:len='+len, f );
99+
}
100+
}
101+
102+
main();

0 commit comments

Comments
 (0)