Skip to content

Commit 95653a3

Browse files
aman-095kgryte
andauthored
feat: add blas/base/idamax
PR-URL: #2152 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Pranav Goswami <[email protected]>
1 parent 65c3b27 commit 95653a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4246
-0
lines changed

Diff for: lib/node_modules/@stdlib/blas/base/idamax/README.md

+168
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 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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 idamax = require( './../lib/idamax.js' );
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 = idamax( 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();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var idamax = tryRequire( resolve( __dirname, './../lib/idamax.native.js' ) );
35+
var opts = {
36+
'skip': ( idamax instanceof Error )
37+
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
41+
42+
43+
// FUNCTIONS //
44+
45+
/**
46+
* Create a benchmark function.
47+
*
48+
* @private
49+
* @param {PositiveInteger} len - array length
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark( len ) {
53+
var x = uniform( len, -100.0, 100.0, options );
54+
return benchmark;
55+
56+
function benchmark( b ) {
57+
var idx;
58+
var i;
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
idx = idamax( x.length, x, 1 );
63+
if ( isnan( idx ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
}
67+
b.toc();
68+
if ( isnan( idx ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
}
74+
}
75+
76+
77+
// MAIN //
78+
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
f = createBenchmark( len );
92+
bench( pkg+'::native:len='+len, opts, f );
93+
}
94+
}
95+
96+
main();

0 commit comments

Comments
 (0)