Skip to content

feat: add blas/base/icamax #5068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 34 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
79a0bcf
feat: add blas/base/icamax
ShabiShett07 Feb 5, 2025
5cbf955
docs: files completed
ShabiShett07 Feb 11, 2025
f1afdc1
fix: resolve lint errors
stdlib-bot Feb 11, 2025
0b7117d
chore: update copyright years
stdlib-bot Feb 19, 2025
aefb83a
Update package.json
aman-095 Feb 19, 2025
ea43236
Update README.md
aman-095 Feb 19, 2025
ec7efeb
fix: resolved errors
ShabiShett07 Feb 20, 2025
61c28cf
fix: resolved errors
ShabiShett07 Feb 20, 2025
229f39c
Merge remote-tracking branch 'upstream/develop' into feature/icamax
stdlib-bot Apr 22, 2025
a6b105b
chore: update implementation
ShabiShett07 Apr 23, 2025
eb6d0b0
chore: update variable name
ShabiShett07 Apr 23, 2025
117f26e
chore: update implementation
ShabiShett07 Jun 12, 2025
a0e1f2e
chore: update implementation
ShabiShett07 Jun 12, 2025
ff25470
chore: add ndarray example
ShabiShett07 Jun 12, 2025
2486f5f
chore: minor clean-up
ShabiShett07 Jun 12, 2025
aad7629
Merge remote-tracking branch 'upstream/develop' into feature/icamax
stdlib-bot Jun 13, 2025
c218d94
chore: minor clean-up
ShabiShett07 Jun 13, 2025
cfa22e8
chore: minor clean-up
ShabiShett07 Jun 13, 2025
4e71757
chore: minor clean-up
ShabiShett07 Jun 13, 2025
66ef6c5
chore: clean-up
ShabiShett07 Jun 27, 2025
939fff6
chore: clean-up
ShabiShett07 Jun 27, 2025
aed923a
chore: clean-up
ShabiShett07 Jun 27, 2025
9ec9c5c
chore: minor clean-up
ShabiShett07 Jun 27, 2025
54132e0
chore: minor clean-up
ShabiShett07 Jun 27, 2025
9d0ba9e
chore: change y to idx
ShabiShett07 Jul 18, 2025
2e8d355
chore: add appropriate statements
ShabiShett07 Jul 18, 2025
c2fa56c
test: add negative stride
ShabiShett07 Jul 18, 2025
d793db6
docs: update jsdoc
ShabiShett07 Jul 18, 2025
e4f78af
chore: update comment
ShabiShett07 Jul 18, 2025
ce66993
chore: reducing variable
ShabiShett07 Jul 18, 2025
d7fb24e
chore: add appropriate keyword
ShabiShett07 Jul 18, 2025
99d56c1
test: remove duplicate
ShabiShett07 Jul 18, 2025
9f4ecb6
refactor: reduce variable
ShabiShett07 Jul 18, 2025
dcd48eb
chore: add appropriate parameter value
ShabiShett07 Jul 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions lib/node_modules/@stdlib/blas/base/icamax/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# icamax

> Find the index of the first element having maximum |Re(.)| + |Im(.)|.

<section class="usage">

## Usage

```javascript
var icamax = require( '@stdlib/blas/base/icamax' );
```

#### icamax( N, x, strideX )

Finds the index of the first element having maximum |Re(.)| + |Im(.)|.

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );

var x = new Complex64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );

var idx = icamax( x.length, x, 1 );
// returns 1
```

The function has the following parameters:

- **N**: number of indexed elements.
- **x**: input [`Complex64Array`][@stdlib/array/complex64].
- **strideX**: index increment for `x`.

The `N` and `strideX` parameters determine which elements in `x` are accessed at runtime. For example, to traverse every other value,

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );

var x = new Complex64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );

var idx = icamax( 2, x, 2 );
// returns 1
```

Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );

// Initial array:
var x0 = new Complex64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );

// Create an offset view:
var x1 = new Complex64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

// Find index of element having the maximum |Re(.)| + |Im(.)|:
var idx = icamax( 2, x1, 1 );
// returns 1
```

#### icamax.ndarray( N, x, strideX, offsetX )

Finds the index of the first element having maximum |Re(.)| + |Im(.)| using alternative indexing semantics.

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );

var x = new Complex64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );

var idx = icamax.ndarray( x.length, x, 1, 0 );
// returns 1
```

The function has the following additional parameters:

- **offsetX**: starting index.

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,

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );

var x = new Complex64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 ] );

var idx = icamax.ndarray( 3, x, 1, 1 );
// returns 2
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- If `N < 1`, both functions return `-1`.
- `icamax()` corresponds to the [BLAS][blas] level 1 function [`icamax`][icamax].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var complex64 = require( '@stdlib/complex/float32/ctor' );
var icamax = require( '@stdlib/blas/base/icamax' );

function rand() {
return new complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
}

// Generate random input arrays:
var x = filledarrayBy( 10, 'complex64', rand );
console.log( x.toString() );

var idx = icamax( x.length, x, 1 );
console.log( idx );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[blas]: http://www.netlib.org/blas

[icamax]: https://www.netlib.org/lapack/explore-html/dd/d52/group__iamax_gafdf273dcc3f020e2aa5c716c1b3d7265.html

[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

</section>

<!-- /.links -->
104 changes: 104 additions & 0 deletions lib/node_modules/@stdlib/blas/base/icamax/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pow = require( '@stdlib/math/base/special/pow' );
var Complex64Array = require( '@stdlib/array/complex64' );
var pkg = require( './../package.json' ).name;
var icamax = require( './../lib/icamax.js' );


// VARIABLES //

var options = {
'dtype': 'float32'
};


// FUNCTIONS //

/**
* Create a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = new Complex64Array( uniform( len*2, -100.0, 100.0, options ) );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var idx;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx = icamax( x.length, x, 1 );
if ( isnanf( idx ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( idx ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':len='+len, f );
}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pow = require( '@stdlib/math/base/special/pow' );
var Complex64Array = require( '@stdlib/array/complex64' );
var pkg = require( './../package.json' ).name;
var icamax = require( './../lib/ndarray.js' );


// VARIABLES //

var options = {
'dtype': 'float32'
};


// FUNCTIONS //

/**
* Create a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = new Complex64Array( uniform( len*2, -100.0, 100.0, options ) );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var idx;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx = icamax( x.length, x, 1, 0 );
if ( isnanf( idx ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( idx ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':ndarray:len='+len, f );
}
}

main();
Loading