blas-idamax
Version:
Finds the first element equal to the maximum absolute value of x and returns the element index.
56 lines (47 loc) • 1.17 kB
JavaScript
;
// MODULES //
var abs = require( 'math-abs' );
// IDAMAX //
/**
* FUNCTION: idamax( N, x, stride, offset )
* Finds the first element equal to the maximum absolute-value of `x` and returns the element index.
*
* @param {Number} N - number of accessed elements
* @param {Array|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} x - input array
* @param {Number} stride - specifies how to index into `x`
* @param {Number} offset - specifies where to begin indexing into `x`
* @returns {Number|Void} index or undefined
*/
function idamax( N, x, stride, offset ) {
var max; // max value
var im; // max index
var ix; // stride index
var v; // absolute value
var i;
if (
N <= 0 ||
stride <= 0 ||
offset < 0
) {
// note that the reference implementation returns an invalid index
return;
}
ix = offset;
if ( N === 1 ) {
return 0;
}
max = abs( x[ ix ] );
ix += stride;
im = 0;
for ( i = 1; i < N; i++ ) {
v = abs( x[ ix ] );
if ( v > max ) {
max = v;
im = i;
}
ix += stride;
}
return im;
} // end FUNCTION idamax()
// EXPORTS //
module.exports = idamax;