From 251547efc83f5e3b1c5e390d8ef2e0094ef5e0d3 Mon Sep 17 00:00:00 2001 From: aman-095 Date: Mon, 15 Apr 2024 22:04:34 +0530 Subject: [PATCH 1/2] feat: add BLAS Level 1 routine for igamax --- .../@stdlib/blas/base/igamax/README.md | 168 +++++++++++++++++ .../blas/base/igamax/benchmark/benchmark.js | 102 +++++++++++ .../igamax/benchmark/benchmark.ndarray.js | 102 +++++++++++ .../@stdlib/blas/base/igamax/docs/repl.txt | 88 +++++++++ .../blas/base/igamax/docs/types/index.d.ts | 88 +++++++++ .../blas/base/igamax/docs/types/test.ts | 157 ++++++++++++++++ .../blas/base/igamax/examples/index.js | 31 ++++ .../@stdlib/blas/base/igamax/lib/index.js | 57 ++++++ .../@stdlib/blas/base/igamax/lib/main.js | 83 +++++++++ .../@stdlib/blas/base/igamax/lib/ndarray.js | 72 ++++++++ .../@stdlib/blas/base/igamax/package.json | 70 ++++++++ .../@stdlib/blas/base/igamax/test/test.js | 38 ++++ .../blas/base/igamax/test/test.main.js | 153 ++++++++++++++++ .../blas/base/igamax/test/test.ndarray.js | 169 ++++++++++++++++++ 14 files changed, 1378 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/README.md create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/benchmark/benchmark.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/lib/ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/package.json create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/test/test.js create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/test/test.main.js create mode 100644 lib/node_modules/@stdlib/blas/base/igamax/test/test.ndarray.js diff --git a/lib/node_modules/@stdlib/blas/base/igamax/README.md b/lib/node_modules/@stdlib/blas/base/igamax/README.md new file mode 100644 index 000000000000..9f22e24189f7 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/README.md @@ -0,0 +1,168 @@ + + +# idamax + +> Find the index of the first element having maximum absolute value. + +
+ +## Usage + +```javascript +var idamax = require( '@stdlib/blas/base/idamax' ); +``` + +#### idamax( N, x, strideX ) + +Finds the index of the first element having maximum absolute value. + +```javascript +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; + +var idx = igamax( x.length, x, 1 ); +// returns 3.0 +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **x**: first input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideX**: index increment for `x`. + +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, + +```javascript +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; + +var idx = igamax( 4, x, 2 ); +// returns 2 +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +// Initial array: +var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); + +// Create offset view: +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + +// Find index of element having maximum absolute value for every other element... +var idx = igamax( 3, x1, 2 ); +// returns 2 +``` + +If either `N` is less than `1` or `strideX` is less than or equal to `0`, the function returns `0`. + +#### igamax.ndarray( N, x, strideX, offsetX ) + +Finds the index of the first element having maximum absolute value using alternative indexing semantics. + +```javascript +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; + +var idx = igamax.ndarray( x.length, x, 1, 0 ); +// returns 3 +``` + +The function has the following additional parameter: + +- **offsetX**: starting index for `x`. + +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, + +```javascript +var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + +var idx = igamax.ndarray( 5, x, 1, 1 ); +// returns 4 +``` + +
+ + + +
+ +## Notes + +- If `N < 1` or `strideX <= 0`, both functions return `0`. +- `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. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var igamax = require( '@stdlib/blas/base/igamax' ); + +var opts = { + 'dtype': 'float64' +}; +var x = discreteUniform( 10, 0, 500, opts ); +console.log( x ); + +var idx = igamax.ndarray( x.length, x, 1, 0 ); +console.log( idx ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/base/igamax/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/base/igamax/benchmark/benchmark.js new file mode 100644 index 000000000000..962578995516 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/benchmark/benchmark.js @@ -0,0 +1,102 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var igamax = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -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 = igamax( len, x, 1 ); + if ( isnan( idx ) ) { + b.fail( 'something went wrong' ); + } + } + b.toc(); + if ( isnan( idx ) ) { + b.fail( 'something went wrong' ); + } + 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(); diff --git a/lib/node_modules/@stdlib/blas/base/igamax/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/blas/base/igamax/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..2cfb002f01bb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/benchmark/benchmark.ndarray.js @@ -0,0 +1,102 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var igamax = require( './../lib' ).ndarray; + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -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 = igamax( len, x, 1, 0 ); + if ( isnan( idx ) ) { + b.fail( 'something went wrong' ); + } + } + b.toc(); + if ( isnan( idx ) ) { + b.fail( 'something went wrong' ); + } + 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(); diff --git a/lib/node_modules/@stdlib/blas/base/igamax/docs/repl.txt b/lib/node_modules/@stdlib/blas/base/igamax/docs/repl.txt new file mode 100644 index 000000000000..28d4e37aa35d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/docs/repl.txt @@ -0,0 +1,88 @@ + +{{alias}}( N, x, strideX ) + Finds the index of the first element having maximum absolute value. + + The `N` and `strideX` parameters determine which elements in `x` are + accessed at runtime. + + Indexing is relative to the first index. To introduce an offset, use typed + array views. + + If `N < 1` or `strideX <= 0`, the function returns `0`. + + Parameters + ---------- + N: integer + Number of indexed elements. + + x: Array|TypedArray + First input array. + + strideX: integer + Index increment for `x`. + + Returns + ------- + idx: integer + Index value. + + Examples + -------- + // Standard usage: + > var x = [ 4.0, 2.0, -3.0, 5.0, -1.0 ]; + > var idx = {{alias}}( x.length, x, 1 ) + 3 + + // Strides: + > x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; + > idx = {{alias}}( 4, x, 2 ) + 2 + + // Using view offsets: + > x = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); + > var x1 = new {{alias:@stdlib/array/float64}}( x.buffer, x.BYTES_PER_ELEMENT*1 ); + > idx = {{alias}}( 3, x1, 2 ) + 2 + + +{{alias}}.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) + Finds the index of the first element having maximum absolute value using + alternative indexing semantics. + + While typed array views mandate a view offset based on the underlying + buffer, the `offsetX` parameter supports indexing semantics based on a + starting index. + + Parameters + ---------- + N: integer + Number of indexed elements. + + x: Array|TypedArray + First input array. + + strideX: integer + Index increment for `x`. + + offsetX: integer + Starting index for `x`. + + Returns + ------- + idx: integer + Index value. + + Examples + -------- + // Standard usage: + > var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ]; + > var idx = {{alias}}.ndarray( x.length, x, 1, 0 ) + 3 + + // Using the index offset: + > x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + > idx = {{alias}}.ndarray( 3, x, 2, 1 ) + 2 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/blas/base/igamax/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/base/igamax/docs/types/index.d.ts new file mode 100644 index 000000000000..8936017c0d63 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/docs/types/index.d.ts @@ -0,0 +1,88 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { NumericArray } from '@stdlib/types/array'; + +/** +* Interface describing `igamax`. +*/ +interface Routine { + /** + * Finds the index of the first element having maximum absolute value. + * + * @param N - number of indexed elements + * @param x - first input array + * @param strideX - `x` stride length + * @returns index value + * + * @example + * var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; + * + * var idx = igamax( x.length, x, 1 ); + * // returns 3 + */ + ( N: number, x: NumericArray, strideX: number ): number; + + /** + * Finds the index of the first element having maximum absolute value using alternative indexing semantics. + * + * @param N - number of indexed elements + * @param x - first input array + * @param strideX - `x` stride length + * @param offsetX - starting index for `x` + * @returns index value + * + * @example + * var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; + * + * var idx = igamax.ndarray( x.length, x, 1, 0 ); + * // returns 3 + */ + ndarray( N: number, x: NumericArray, strideX: number, offsetX: number ): number; +} + +/** +* Finds the index of the first element having maximum absolute value. +* +* @param N - number of indexed elements +* @param x - first input array +* @param strideX - `x` stride length +* @returns index value +* +* @example +* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; +* +* var idx = igamax( 4, x, 2 ); +* // returns 2 +* +* @example +* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; +* +* var idx = igamax.ndarray( x.length, x, 1, 1 ); +* // returns 2 +*/ +declare var igamax: Routine; + + +// EXPORTS // + +export = igamax; diff --git a/lib/node_modules/@stdlib/blas/base/igamax/docs/types/test.ts b/lib/node_modules/@stdlib/blas/base/igamax/docs/types/test.ts new file mode 100644 index 000000000000..ed10fb40acea --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/docs/types/test.ts @@ -0,0 +1,157 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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. +*/ + +import igamax = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = new Float64Array( 10 ); + + igamax( x.length, x, 1 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + igamax( '10', x, 1 ); // $ExpectError + igamax( true, x, 1 ); // $ExpectError + igamax( false, x, 1 ); // $ExpectError + igamax( null, x, 1 ); // $ExpectError + igamax( undefined, x, 1 ); // $ExpectError + igamax( [], x, 1 ); // $ExpectError + igamax( {}, x, 1 ); // $ExpectError + igamax( ( x: number ): number => x, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + igamax( x.length, 10, 1 ); // $ExpectError + igamax( x.length, '10', 1 ); // $ExpectError + igamax( x.length, true, 1 ); // $ExpectError + igamax( x.length, false, 1 ); // $ExpectError + igamax( x.length, null, 1 ); // $ExpectError + igamax( x.length, undefined, 1 ); // $ExpectError + igamax( x.length, [ '1' ], 1 ); // $ExpectError + igamax( x.length, {}, 1 ); // $ExpectError + igamax( x.length, ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + + igamax( x.length, x, '10' ); // $ExpectError + igamax( x.length, x, true ); // $ExpectError + igamax( x.length, x, false ); // $ExpectError + igamax( x.length, x, null ); // $ExpectError + igamax( x.length, x, undefined ); // $ExpectError + igamax( x.length, x, [] ); // $ExpectError + igamax( x.length, x, {} ); // $ExpectError + igamax( x.length, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + igamax(); // $ExpectError + igamax( x.length ); // $ExpectError + igamax( x.length, x ); // $ExpectError + igamax( x.length, x, 1, 10 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a number... +{ + const x = new Float64Array( 10 ); + + igamax.ndarray( x.length, x, 1, 0 ); // $ExpectType number +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + igamax.ndarray( '10', x, 1, 0 ); // $ExpectError + igamax.ndarray( true, x, 1, 0 ); // $ExpectError + igamax.ndarray( false, x, 1, 0 ); // $ExpectError + igamax.ndarray( null, x, 1, 0 ); // $ExpectError + igamax.ndarray( undefined, x, 1, 0 ); // $ExpectError + igamax.ndarray( [], x, 1, 0 ); // $ExpectError + igamax.ndarray( {}, x, 1, 0 ); // $ExpectError + igamax.ndarray( ( x: number ): number => x, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + igamax.ndarray( x.length, 10, 1, 0 ); // $ExpectError + igamax.ndarray( x.length, '10', 1, 0 ); // $ExpectError + igamax.ndarray( x.length, true, 1, 0 ); // $ExpectError + igamax.ndarray( x.length, false, 1, 0 ); // $ExpectError + igamax.ndarray( x.length, null, 1, 0 ); // $ExpectError + igamax.ndarray( x.length, undefined, 1, 0 ); // $ExpectError + igamax.ndarray( x.length, [ '1' ], 1, 0 ); // $ExpectError + igamax.ndarray( x.length, {}, 1, 0 ); // $ExpectError + igamax.ndarray( x.length, ( x: number ): number => x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + + igamax.ndarray( x.length, x, '10', 0 ); // $ExpectError + igamax.ndarray( x.length, x, true, 0 ); // $ExpectError + igamax.ndarray( x.length, x, false, 0 ); // $ExpectError + igamax.ndarray( x.length, x, null, 0 ); // $ExpectError + igamax.ndarray( x.length, x, undefined, 0 ); // $ExpectError + igamax.ndarray( x.length, x, [], 0 ); // $ExpectError + igamax.ndarray( x.length, x, {}, 0 ); // $ExpectError + igamax.ndarray( x.length, x, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + igamax.ndarray( x.length, x, 1, '10' ); // $ExpectError + igamax.ndarray( x.length, x, 1, true ); // $ExpectError + igamax.ndarray( x.length, x, 1, false ); // $ExpectError + igamax.ndarray( x.length, x, 1, null ); // $ExpectError + igamax.ndarray( x.length, x, 1, undefined ); // $ExpectError + igamax.ndarray( x.length, x, 1, [] ); // $ExpectError + igamax.ndarray( x.length, x, 1, {} ); // $ExpectError + igamax.ndarray( x.length, x, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + igamax.ndarray(); // $ExpectError + igamax.ndarray( x.length ); // $ExpectError + igamax.ndarray( x.length, x ); // $ExpectError + igamax.ndarray( x.length, x, 1 ); // $ExpectError + igamax.ndarray( x.length, x, 1, 0, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/base/igamax/examples/index.js b/lib/node_modules/@stdlib/blas/base/igamax/examples/index.js new file mode 100644 index 000000000000..54ef3ac12aac --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/examples/index.js @@ -0,0 +1,31 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var igamax = require( './../lib' ).ndarray; + +var opts = { + 'dtype': 'float64' +}; +var x = discreteUniform( 10, 0, 500, opts ); +console.log( x ); + +var idx = igamax( x.length, x, 1, 0 ); +console.log( idx ); diff --git a/lib/node_modules/@stdlib/blas/base/igamax/lib/index.js b/lib/node_modules/@stdlib/blas/base/igamax/lib/index.js new file mode 100644 index 000000000000..d3a232811c3c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* BLAS level 1 routine to find the index of the first element having maximum absolute value. +* +* @module @stdlib/blas/base/igamax +* +* @example +* var igamax = require( '@stdlib/blas/base/igamax' ); +* +* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; +* +* var idx = igamax( x.length, x, 1 ); +* // returns 3 +* +* @example +* var igamax = require( '@stdlib/blas/base/igamax' ); +* +* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; +* +* var z = igamax.ndarray( x.length, x, 1, 0 ); +* // returns 3 +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/base/igamax/lib/main.js b/lib/node_modules/@stdlib/blas/base/igamax/lib/main.js new file mode 100644 index 000000000000..07567fa3f7c1 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/lib/main.js @@ -0,0 +1,83 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 abs = require( '@stdlib/math/base/special/abs' ); + + +// MAIN // + +/** +* Finds the index of the first element having maximum absolute value. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {NumericArray} x - first input array +* @param {integer} strideX - `x` stride length +* @returns {integer} index value +* +* @example +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + +* var idx = igamax( x.length, x, 1 ); +* // returns 4 +*/ +function igamax( N, x, strideX ) { + var dmax; + var idx; + var ix; + var i; + + idx = 0; + if ( N < 1 || strideX <= 0 ) { + return -1; + } + if ( N === 1 ) { + return idx; + } + if (strideX === 1 ) { + // Code for stride equal to `1`... + dmax = abs( x[ 0 ] ); + for ( i = 1; i < N; i++ ) { + if ( abs( x[ i ] ) > dmax ) { + idx = i; + dmax = abs( x[ i ] ); + } + } + return idx; + } + // Code for stride not equal to `1`... + ix = 0; + dmax = abs( x[ 0 ] ); + ix += strideX; + for ( i = 1; i < N; i++ ) { + if ( abs( x[ ix ] ) > dmax ) { + idx = i; + dmax = abs( x[ ix ] ); + } + ix += strideX; + } + return idx; +} + + +// EXPORTS // + +module.exports = igamax; diff --git a/lib/node_modules/@stdlib/blas/base/igamax/lib/ndarray.js b/lib/node_modules/@stdlib/blas/base/igamax/lib/ndarray.js new file mode 100644 index 000000000000..d1061b413898 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/lib/ndarray.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 abs = require( '@stdlib/math/base/special/abs' ); + + +// MAIN // + +/** +* Finds the index of the first element having maximum absolute value. +* +* @param {integer} N - number of indexed elements +* @param {NumericArray} x - first input array +* @param {integer} strideX - `x` stride length +* @param {NonNegativeInteger} offsetX - starting index for `x` +* @returns {integer} index value +* +* @example +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + +* var idx = igamax( x.length, x, 1, 0 ); +* // returns 4 +*/ +function igamax( N, x, strideX, offsetX ) { + var dmax; + var idx; + var ix; + var i; + + idx = 0; + if ( N < 1 || strideX <= 0 ) { + return -1; + } + if ( N === 1 ) { + return idx; + } + ix = offsetX; + dmax = abs( x[ 0 ] ); + ix += strideX; + for ( i = 1; i < N; i++ ) { + if ( abs( x[ ix ] ) > dmax ) { + idx = i; + dmax = abs( x[ ix ] ); + } + ix += strideX; + } + return idx; +} + + +// EXPORTS // + +module.exports = igamax; diff --git a/lib/node_modules/@stdlib/blas/base/igamax/package.json b/lib/node_modules/@stdlib/blas/base/igamax/package.json new file mode 100644 index 000000000000..69cd44800c6c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/blas/base/igamax", + "version": "0.0.0", + "description": "Find the index of the first element having maximum absolute value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "level 1", + "linear", + "algebra", + "subroutines", + "idamax", + "isamax", + "maximum", + "index", + "absolute", + "vector", + "array", + "ndarray" + ] +} diff --git a/lib/node_modules/@stdlib/blas/base/igamax/test/test.js b/lib/node_modules/@stdlib/blas/base/igamax/test/test.js new file mode 100644 index 000000000000..86fcd97912ff --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/test/test.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 tape = require( 'tape' ); +var igamax = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof igamax, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method providing an ndarray interface', function test( t ) { + t.strictEqual( typeof igamax.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/base/igamax/test/test.main.js b/lib/node_modules/@stdlib/blas/base/igamax/test/test.main.js new file mode 100644 index 000000000000..3ec1fadd8ef6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/test/test.main.js @@ -0,0 +1,153 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var igamax = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof igamax, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 3', function test( t ) { + t.strictEqual( igamax.length, 3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function finds the index of the element with the maximum absolute value', function test( t ) { + var expected; + var idx; + var x; + + x = [ + 0.1, // 1 + -0.3, // 2 + 0.5, // 3 + -0.1, // 4 + 6.0, + 6.0, + 6.0 + ]; + expected = 2; + + idx = igamax( 4, x, 1 ); + t.deepEqual( idx, expected, 'returns expected value' ); + + x = [ + 0.2, // 1 + -0.6, // 2 + 0.3, // 3 + 5.0, + 5.0 + ]; + expected = 1; + + idx = igamax( 3, x, 1 ); + t.deepEqual( idx, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than `1`, the function returns `-1`', function test( t ) { + var expected; + var idx; + var x; + + x = [ + 1.0, + 2.0, + 3.0 + ]; + expected = -1; + + idx = igamax( 0, x, 1 ); + t.deepEqual( idx, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `strideX` parameter less than or equal to `0`, the function returns `-1`', function test( t ) { + var expected; + var idx; + var x; + + x = [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ]; + expected = -1; + + idx = igamax( x.length, x, 0 ); + t.deepEqual( idx, expected, 'returns expected value' ); + + idx = igamax( x.length, x, -1 ); + t.deepEqual( idx, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var idx; + var x; + + x = [ + 0.1, // 1 + 4.0, + -0.3, // 2 + 6.0, + -0.5, // 3 + 7.0, + -0.1, // 4 + 3.0 + ]; + expected = 2; + + idx = igamax( 4, x, 2 ); + t.deepEqual( idx, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports view offsets', function test( t ) { + var expected; + var idx; + var x0; + var x1; + + x0 = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + expected = 2; + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + + idx = igamax( 3, x1, 2 ); + t.deepEqual( idx, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/base/igamax/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/base/igamax/test/test.ndarray.js new file mode 100644 index 000000000000..c7b653c187b9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/igamax/test/test.ndarray.js @@ -0,0 +1,169 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 tape = require( 'tape' ); +var igamax = require( './../lib/ndarray.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof igamax, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 4', function test( t ) { + t.strictEqual( igamax.length, 4, 'returns expected value' ); + t.end(); +}); + +tape( 'the function finds the index of the element with the maximum absolute value', function test( t ) { + var expected; + var idx; + var x; + + x = [ + 0.1, // 1 + -0.3, // 2 + 0.5, // 3 + -0.1, // 4 + 6.0, + 6.0, + 6.0 + ]; + expected = 2; + + idx = igamax( 4, x, 1, 0 ); + t.deepEqual( idx, expected, 'returns expected value' ); + + x = [ + 0.2, // 1 + -0.6, // 2 + 0.3, // 3 + 5.0, + 5.0 + ]; + expected = 1; + + idx = igamax( 3, x, 1, 0 ); + t.deepEqual( idx, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than `1`, the function returns `0`', function test( t ) { + var expected; + var idx; + var x; + + x = [ + 1.0, + 2.0, + 3.0 + ]; + expected = -1; + + idx = igamax( 0, x, 1, 0 ); + t.deepEqual( idx, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `strideX` parameter less than or equal to `0`, the function returns `0`', function test( t ) { + var expected; + var idx; + var x; + + x = [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ]; + expected = -1; + + idx = igamax( x.length, x, 0, 0 ); + t.deepEqual( idx, expected, 'returns expected value' ); + + idx = igamax( x.length, x, -1, 0 ); + t.deepEqual( idx, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var idx; + var x; + + x = [ + 0.1, // 1 + 4.0, + -0.3, // 2 + 6.0, + -0.5, // 3 + 7.0, + -0.1, // 4 + 3.0 + ]; + expected = 2; + + idx = igamax( 4, x, 2, 0 ); + t.deepEqual( idx, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying an `x` offset', function test( t ) { + var expected; + var idx; + var x; + + x = [ + 1.0, + 2.0, // 0 + 3.0, // 1 + 4.0, // 2 + 5.0, // 3 + 6.0 // 4 + ]; + expected = 4; + + idx = igamax( 5, x, 1, 1 ); + t.deepEqual( idx, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports complex access patterns', function test( t ) { + var expected; + var idx; + var x; + + x = [ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]; + expected = 2; + + idx = igamax( 3, x, 2, 1 ); + t.deepEqual( idx, expected, 'returns expected value' ); + t.end(); +}); From 6ab28b2efa0d391243ddc5b6719458504e057c1d Mon Sep 17 00:00:00 2001 From: aman-095 Date: Mon, 15 Apr 2024 22:21:43 +0530 Subject: [PATCH 2/2] chore: resolve readme errors --- lib/node_modules/@stdlib/blas/base/igamax/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/base/igamax/README.md b/lib/node_modules/@stdlib/blas/base/igamax/README.md index 9f22e24189f7..d1c6973276ea 100644 --- a/lib/node_modules/@stdlib/blas/base/igamax/README.md +++ b/lib/node_modules/@stdlib/blas/base/igamax/README.md @@ -18,7 +18,7 @@ limitations under the License. --> -# idamax +# igamax > Find the index of the first element having maximum absolute value. @@ -27,10 +27,10 @@ limitations under the License. ## Usage ```javascript -var idamax = require( '@stdlib/blas/base/idamax' ); +var igamax = require( '@stdlib/blas/base/igamax' ); ``` -#### idamax( N, x, strideX ) +#### igamax( N, x, strideX ) Finds the index of the first element having maximum absolute value.