From bb65398024f06c64579a478158c8b5559bff0dba Mon Sep 17 00:00:00 2001 From: aman-095 Date: Mon, 27 May 2024 16:33:17 +0530 Subject: [PATCH 01/16] feat: add BLAS Level 1 routine for dznmr2 --- .../@stdlib/blas/base/dznrm2/README.md | 172 +++++++++++++++ .../blas/base/dznrm2/benchmark/benchmark.js | 93 ++++++++ .../dznrm2/benchmark/benchmark.ndarray.js | 93 ++++++++ .../@stdlib/blas/base/dznrm2/docs/repl.txt | 90 ++++++++ .../blas/base/dznrm2/docs/types/index.d.ts | 96 +++++++++ .../blas/base/dznrm2/docs/types/test.ts | 158 ++++++++++++++ .../blas/base/dznrm2/examples/index.js | 35 +++ .../@stdlib/blas/base/dznrm2/lib/dznrm2.js | 86 ++++++++ .../@stdlib/blas/base/dznrm2/lib/index.js | 68 ++++++ .../@stdlib/blas/base/dznrm2/lib/main.js | 35 +++ .../@stdlib/blas/base/dznrm2/lib/ndarray.js | 87 ++++++++ .../@stdlib/blas/base/dznrm2/package.json | 75 +++++++ .../blas/base/dznrm2/test/test.dznrm2.js | 199 ++++++++++++++++++ .../@stdlib/blas/base/dznrm2/test/test.js | 82 ++++++++ .../blas/base/dznrm2/test/test.ndarray.js | 196 +++++++++++++++++ 15 files changed, 1565 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/README.md create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/benchmark/benchmark.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/lib/dznrm2.js create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/lib/ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/package.json create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/test/test.dznrm2.js create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/test/test.js create mode 100644 lib/node_modules/@stdlib/blas/base/dznrm2/test/test.ndarray.js diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/README.md b/lib/node_modules/@stdlib/blas/base/dznrm2/README.md new file mode 100644 index 000000000000..d2555a075aaa --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/README.md @@ -0,0 +1,172 @@ + + +# dznrm2 + +> Find the euclidean norm of a complex double-precision floating-point vector. + +
+ +## Usage + +```javascript +var dznrm2 = require( '@stdlib/blas/base/dznrm2' ); +``` + +#### dznrm2( N, zx, strideX ) + +Finds the euclidean norm of a complex double-precision floating-point vector. + +```javascript +var Complex128Array = require( '@stdlib/array/complex128' ); + +var zx = new Complex128Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2 ] ); + +var norm = dznrm2( 4, zx, 1 ); +// returns ~0.8 +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **zx**: input [`Complex128Array`][@stdlib/array/complex128]. +- **strideX**: index increment for `zx`. + +The `N` and `strideX` parameters determine which elements in `zx` are accessed at runtime. For example, to traverse every other value, + +```javascript +var Complex128Array = require( '@stdlib/array/complex128' ); + +var zx = new Complex128Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); + +var norm = dznrm2( 2, zx, 2 ); +// returns ~4.6 +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + +```javascript +var Complex128Array = require( '@stdlib/array/complex128' ); + +// Initial array: +var zx0 = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); + +// Create an offset view: +var zx1 = new Complex128Array( zx0.buffer, zx0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + +// Find euclidean norm: +var norm = dznrm2( 2, zx1, 1 ); +// returns ~9.3 +``` + +#### dznrm2.ndarray( N, zx, strideX, offset ) + +Finds the euclidean norm of a complex double-precision floating-point vector using alternative indexing semantics. + +```javascript +var Complex128Array = require( '@stdlib/array/complex128' ); + +var zx = new Complex128Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2 ] ); + +var norm = dznrm2.ndarray( 4, zx, 1, 0 ); +// returns ~0.8 +``` + +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 Complex128Array = require( '@stdlib/array/complex128' ); + +var zx = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); + +var norm = dznrm2.ndarray( 3, zx, 1, 1 ); +// returns ~9.3 +``` + +
+ + + +
+ +## Notes + +- If `N < 1` or `strideX < 0`, both functions return `0.0`. +- `dznrm2()` corresponds to the [BLAS][blas] level 1 function [`dznrm2`][dznrm2]. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var Complex128 = require( '@stdlib/complex/float32' ); +var dznrm2 = require( '@stdlib/blas/base/dznrm2' ); + +function rand() { + return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) ); +} + +var zx = filledarrayBy( 10, 'complex128', rand ); +console.log( zx.toString() ); + +// Finds the euclidean norm: +var norm = dznrm2( zx.length, zx, 1 ); +console.log( norm ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/base/dznrm2/benchmark/benchmark.js new file mode 100644 index 000000000000..b6cbe29e6f14 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/benchmark/benchmark.js @@ -0,0 +1,93 @@ +/** +* @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 Complex128Array = require( '@stdlib/array/complex128' ); +var pkg = require( './../package.json' ).name; +var dznrm2 = require( './../lib/dznrm2.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var cx = uniform( len, -10.0, 10.0, options ); + cx = new Complex128Array( cx.buffer ); + return benchmark; + + function benchmark( b ) { + var norm; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + norm = dznrm2( cx.length, cx, 1 ); + if ( isnan( norm ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( norm ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +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/dznrm2/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/blas/base/dznrm2/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..3d97028d378e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/benchmark/benchmark.ndarray.js @@ -0,0 +1,93 @@ +/** +* @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 Complex128Array = require( '@stdlib/array/complex128' ); +var pkg = require( './../package.json' ).name; +var dznrm2 = require( './../lib/ndarray.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var zx = uniform( len, -10.0, 10.0, options ); + zx = new Complex128Array( zx.buffer ); + return benchmark; + + function benchmark( b ) { + var norm; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + norm = dznrm2( zx.length, zx, 1, 0 ); + if ( isnan( norm ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( norm ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +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/dznrm2/docs/repl.txt b/lib/node_modules/@stdlib/blas/base/dznrm2/docs/repl.txt new file mode 100644 index 000000000000..1f1cc455d59c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/docs/repl.txt @@ -0,0 +1,90 @@ + +{{alias}}( N, zx, strideX ) + Finds the the euclidean norm of a complex double-precision floating-point + vector. + + The `N` and `strideX` parameters determine which elements in `zx` 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.0`. + + Parameters + ---------- + N: integer + Number of indexed elements. + + zx: Complex64Array + Input array. + + strideX: integer + Index increment for `zx`. + + Returns + ------- + norm: number + Euclidean norm. + + Examples + -------- + // Standard Usage: + > var zx = new {{alias:@stdlib/array/complex128}}( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5 ] ); + > var norm = {{alias}}( 3, zx, 1 ) + ~0.77 + + // Using `N` and stride parameters: + > zx = new {{alias:@stdlib/array/complex128}}( [ -2.0, 1.0, 3.0, -5.0 ] ); + > norm = {{alias}}( 1, zx, 2 ) + ~2.2 + + // Using view offsets: + > var zx0 = new {{alias:@stdlib/array/complex128}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); + > var zx1 = new {{alias:@stdlib/array/complex128}}( zx0.buffer, zx0.BYTES_PER_ELEMENT*1 ); + > norm = {{alias}}( 2, zx1, 1 ) + ~9.3 + + +{{alias}}.ndarray( N, zx, strideX, offsetX ) + Finds the euclidean norm of a complex double-precision floating-point + vector 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. + + zx: Complex64Array + Input array. + + strideX: integer + Index increment for `zx`. + + offsetX: integer + Starting index of `zx`. + + Returns + ------- + norm: number + Euclidean norm. + + Examples + -------- + // Standard Usage: + > var zx = new {{alias:@stdlib/array/complex128}}( [ -2.0, 1.0, 3.0, -5.0 ] ); + > var norm = {{alias}}.ndarray( 1, zx, 2, 0 ) + ~2.2 + + // Using an index offset: + > zx = new {{alias:@stdlib/array/complex128}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); + > norm = {{alias}}.ndarray( 3, zx, 1, 1 ) + ~9.3 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/base/dznrm2/docs/types/index.d.ts new file mode 100644 index 000000000000..a9b756ea1c47 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/docs/types/index.d.ts @@ -0,0 +1,96 @@ +/* +* @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 { Complex128Array } from '@stdlib/types/array'; + +/** +* Interface describing `dznrm2`. +*/ +interface Routine { + /** + * Finds the euclidean norm of a complex double-precision floating-point vector. + * + * @param N - number of indexed elements + * @param zx - input array + * @param strideX - stride length for `zx` + * @returns euclidean norm + * + * @example + * var Complex128Array = require( '@stdlib/array/complex128' ); + * + * var zx = new Complex128Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2, 2.0, 3.0 ] ); + * + * var norm = dznrm2( 4, zx, 1 ); + * // returns ~0.8 + */ + ( N: number, x: Complex128Array, strideX: number ): number; + + /** + * Finds the euclidean norm of a complex double-precision floating-point vector using alternative indexing semantics. + * + * @param N - number of indexed elements + * @param zx - input array + * @param strideX - stride length for `zx` + * @param offsetX - starting index for `zx` + * @returns index value + * + * @example + * var Complex128Array = require( '@stdlib/array/complex128' ); + * + * var zx = new Complex128Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2, 2.0, 3.0 ] ); + * + * var norm = dznrm2.ndarray( 4, zx, 1, 0 ); + * // returns ~0.8 + */ + ndarray( N: number, x: Complex128Array, strideX: number, offsetX: number ): number; +} + +/** +* Finds the euclidean norm of a complex double-precision floating-point vector. +* +* @param N - number of indexed elements +* @param zx - input array +* @param strideX - stride length for `zx` +* @returns index value +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* +* var x = new Complex128Array( [ -2.0, 1.0, 3.0, -5.0 ] ); +* +* var norm = dznrm2( 1, zx, 2 ); +* // returns ~2.2 +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* +* var x = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); +* +* var norm = dznrm2.ndarray( 2, zx, 1, 1 ); +* // returns ~9.3 +*/ +declare var dznrm2: Routine; + + +// EXPORTS // + +export = dznrm2; diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/docs/types/test.ts b/lib/node_modules/@stdlib/blas/base/dznrm2/docs/types/test.ts new file mode 100644 index 000000000000..4327f535c2f3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/docs/types/test.ts @@ -0,0 +1,158 @@ +/* +* @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 Complex128Array = require( '@stdlib/array/complex128' ); +import dznrm2 = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const zx = new Complex128Array( 10 ); + + dznrm2( zx.length, zx, 1 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const zx = new Complex128Array( 10 ); + + dznrm2( '10', zx, 1 ); // $ExpectError + dznrm2( true, zx, 1 ); // $ExpectError + dznrm2( false, zx, 1 ); // $ExpectError + dznrm2( null, zx, 1 ); // $ExpectError + dznrm2( undefined, zx, 1 ); // $ExpectError + dznrm2( [], zx, 1 ); // $ExpectError + dznrm2( {}, zx, 1 ); // $ExpectError + dznrm2( ( zx: number ): number => zx, zx, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a Complex128Array... +{ + const zx = new Complex128Array( 10 ); + + dznrm2( zx.length, 10, 1 ); // $ExpectError + dznrm2( zx.length, '10', 1 ); // $ExpectError + dznrm2( zx.length, true, 1 ); // $ExpectError + dznrm2( zx.length, false, 1 ); // $ExpectError + dznrm2( zx.length, null, 1 ); // $ExpectError + dznrm2( zx.length, undefined, 1 ); // $ExpectError + dznrm2( zx.length, [], 1 ); // $ExpectError + dznrm2( zx.length, {}, 1 ); // $ExpectError + dznrm2( zx.length, ( zx: number ): number => zx, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const zx = new Complex128Array( 10 ); + + dznrm2( zx.length, zx, '10' ); // $ExpectError + dznrm2( zx.length, zx, true ); // $ExpectError + dznrm2( zx.length, zx, false ); // $ExpectError + dznrm2( zx.length, zx, null ); // $ExpectError + dznrm2( zx.length, zx, undefined ); // $ExpectError + dznrm2( zx.length, zx, [] ); // $ExpectError + dznrm2( zx.length, zx, {} ); // $ExpectError + dznrm2( zx.length, zx, ( zx: number ): number => zx ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const zx = new Complex128Array( 10 ); + + dznrm2(); // $ExpectError + dznrm2( zx.length ); // $ExpectError + dznrm2( zx.length, zx ); // $ExpectError + dznrm2( zx.length, zx, 1, 10 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a number... +{ + const zx = new Complex128Array( 10 ); + + dznrm2.ndarray( zx.length, zx, 1, 0 ); // $ExpectType number +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const zx = new Complex128Array( 10 ); + + dznrm2.ndarray( '10', zx, 1, 0 ); // $ExpectError + dznrm2.ndarray( true, zx, 1, 0 ); // $ExpectError + dznrm2.ndarray( false, zx, 1, 0 ); // $ExpectError + dznrm2.ndarray( null, zx, 1, 0 ); // $ExpectError + dznrm2.ndarray( undefined, zx, 1, 0 ); // $ExpectError + dznrm2.ndarray( [], zx, 1, 0 ); // $ExpectError + dznrm2.ndarray( {}, zx, 1, 0 ); // $ExpectError + dznrm2.ndarray( ( zx: number ): number => zx, zx, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a Complex128Array... +{ + const zx = new Complex128Array( 10 ); + + dznrm2.ndarray( zx.length, 10, 1, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, '10', 1, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, true, 1, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, false, 1, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, null, 1, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, undefined, 1, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, [], 1, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, {}, 1, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, ( zx: number ): number => zx, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a number... +{ + const zx = new Complex128Array( 10 ); + + dznrm2.ndarray( zx.length, zx, '10', 0 ); // $ExpectError + dznrm2.ndarray( zx.length, zx, true, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, zx, false, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, zx, null, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, zx, undefined, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, zx, [], 0 ); // $ExpectError + dznrm2.ndarray( zx.length, zx, {}, 0 ); // $ExpectError + dznrm2.ndarray( zx.length, zx, ( zx: number ): number => zx, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... +{ + const zx = new Complex128Array( 10 ); + + dznrm2.ndarray( zx.length, zx, 1, '10' ); // $ExpectError + dznrm2.ndarray( zx.length, zx, 1, true ); // $ExpectError + dznrm2.ndarray( zx.length, zx, 1, false ); // $ExpectError + dznrm2.ndarray( zx.length, zx, 1, null ); // $ExpectError + dznrm2.ndarray( zx.length, zx, 1, undefined ); // $ExpectError + dznrm2.ndarray( zx.length, zx, 1, [] ); // $ExpectError + dznrm2.ndarray( zx.length, zx, 1, {} ); // $ExpectError + dznrm2.ndarray( zx.length, zx, 1, ( zx: number ): number => zx ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const zx = new Complex128Array( 10 ); + + dznrm2.ndarray(); // $ExpectError + dznrm2.ndarray( zx.length ); // $ExpectError + dznrm2.ndarray( zx.length, zx ); // $ExpectError + dznrm2.ndarray( zx.length, zx, 1 ); // $ExpectError + dznrm2.ndarray( zx.length, zx, 1, 0, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/examples/index.js b/lib/node_modules/@stdlib/blas/base/dznrm2/examples/index.js new file mode 100644 index 000000000000..211248525ffb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/examples/index.js @@ -0,0 +1,35 @@ +/** +* @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/base/discrete-uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var dznrm2 = require( '@stdlib/blas/base/dznrm2' ); + +function rand() { + return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) ); +} + +var zx = filledarrayBy( 10, 'complex64', rand ); +console.log( zx.toString() ); + +// Finds the euclidean norm: +var norm = dznrm2( zx.length, zx, 1 ); +console.log( norm ); diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/lib/dznrm2.js b/lib/node_modules/@stdlib/blas/base/dznrm2/lib/dznrm2.js new file mode 100644 index 000000000000..ff63ec71b59a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/lib/dznrm2.js @@ -0,0 +1,86 @@ +/** +* @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' ); +var imag = require( '@stdlib/complex/imag' ); +var real = require( '@stdlib/complex/real' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); + + +// MAIN // + +/** +* Finds the euclidean norm of a complex double-precision floating-point vector. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {Complex128Array} zx - input array +* @param {NonNegativeInteger} strideX - `zx` stride length +* @returns {number} index value +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* +* var zx = new Complex128Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2, 2.0, 3.0 ] ); +* +* var norm = dznrm2( 4, zx, 1 ); +* // returns ~0.8 +*/ +function dznrm2( N, zx, strideX ) { + var scale; + var norm; + var temp; + var ssq; + var ix; + + if ( N < 1 || strideX < 0 ) { + return 0.0; + } + scale = 0.0; + ssq = 1.0; + for ( ix = 0; ix <= ( N - 1 ) * strideX; ix += strideX ) { + if ( real( zx.get( ix ) ) !== 0.0 ) { + temp = abs( real( zx.get( ix ) ) ); + if ( scale < temp ) { + ssq = 1.0 + ( ssq * ( ( scale / temp ) * ( scale / temp ) ) ); + scale = temp; + } else { + ssq += ( ( temp / scale ) * ( temp / scale ) ); + } + } + if ( imag( zx.get( ix ) ) !== 0.0 ) { + temp = abs( imag( zx.get( ix ) ) ); + if ( scale < temp ) { + ssq = 1.0 + ( ssq * ( ( scale / temp ) * ( scale / temp ) ) ); + scale = temp; + } else { + ssq += ( ( temp / scale ) * ( temp / scale ) ); + } + } + norm = scale * sqrt( ssq ); + } + return norm; +} + + +// EXPORTS // + +module.exports = dznrm2; diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/lib/index.js b/lib/node_modules/@stdlib/blas/base/dznrm2/lib/index.js new file mode 100644 index 000000000000..8dd07a5fa14e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/lib/index.js @@ -0,0 +1,68 @@ +/** +* @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 euclidean norm of a complex double-precision floating-point vector. +* +* @module @stdlib/blas/base/dznrm2 +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var dznrm2 = require( '@stdlib/blas/base/dznrm2' ); +* +* var zx = new Complex128Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2, 2.0, 3.0 ] ); +* +* var norm = dznrm2( 4, zx, 1 ); +* // returns ~0.8 +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var dznrm2 = require( '@stdlib/blas/base/dznrm2' ); +* +* var zx = new Complex128Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2, 2.0, 3.0 ] ); +* +* var norm = dznrm2.ndarray( 4, zx, 1, 0 ); +* // returns ~0.8 +*/ + +// MODULES // + +var join = require( 'path' ).join; +var tryRequire = require( '@stdlib/utils/try-require' ); +var isError = require( '@stdlib/assert/is-error' ); +var main = require( './main.js' ); + + +// MAIN // + +var dznrm2; +var tmp = tryRequire( join( __dirname, './native.js' ) ); +if ( isError( tmp ) ) { + dznrm2 = main; +} else { + dznrm2 = tmp; +} + + +// EXPORTS // + +module.exports = dznrm2; + +// exports: { "ndarray": "dznrm2.ndarray" } diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/lib/main.js b/lib/node_modules/@stdlib/blas/base/dznrm2/lib/main.js new file mode 100644 index 000000000000..bbe7ec871d2e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/lib/main.js @@ -0,0 +1,35 @@ +/** +* @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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var dznrm2 = require( './dznrm2.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( dznrm2, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = dznrm2; diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/lib/ndarray.js b/lib/node_modules/@stdlib/blas/base/dznrm2/lib/ndarray.js new file mode 100644 index 000000000000..fa4e1d05d8bd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/lib/ndarray.js @@ -0,0 +1,87 @@ +/** +* @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' ); +var imag = require( '@stdlib/complex/imag' ); +var real = require( '@stdlib/complex/real' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); + + +// MAIN // + +/** +* Finds the euclidean norm of a complex double-precision floating-point vector. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {Complex128Array} zx - input array +* @param {NonNegativeInteger} strideX - `zx` stride length +* @param {NonNegativeInteger} offsetX - starting index for `zx` +* @returns {number} index value +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* +* var zx = new Complex128Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2, 2.0, 3.0 ] ); +* +* var norm = dznrm2( 4, zx, 1, 0 ); +* // returns ~0.8 +*/ +function dznrm2( N, zx, strideX, offsetX ) { + var scale; + var norm; + var temp; + var ssq; + var ix; + + if ( N < 1 || strideX < 0 ) { + return 0.0; + } + scale = 0.0; + ssq = 1.0; + for ( ix = offsetX; ix <= ( N - 1 ) * strideX; ix += strideX ) { + if ( real( zx.get( ix ) ) !== 0.0 ) { + temp = abs( real( zx.get( ix ) ) ); + if ( scale < temp ) { + ssq = 1.0 + ( ssq * ( scale / temp ) * ( scale / temp ) ); + scale = temp; + } else { + ssq += ( ( temp / scale ) * ( temp / scale ) ); + } + } + if ( imag( zx.get( ix ) ) !== 0.0 ) { + temp = abs( imag( zx.get( ix ) ) ); + if ( scale < temp ) { + ssq = 1.0 + ( ssq * ( scale / temp ) * ( scale / temp ) ); + scale = temp; + } else { + ssq += ( ( temp / scale ) * ( temp / scale ) ); + } + } + norm = scale * sqrt( ssq ); + } + return norm; +} + + +// EXPORTS // + +module.exports = dznrm2; diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/package.json b/lib/node_modules/@stdlib/blas/base/dznrm2/package.json new file mode 100644 index 000000000000..0e70208f73f0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/blas/base/dznrm2", + "version": "0.0.0", + "description": "Find the euclidean norm of a complex double-precision floating-point vector.", + "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", + "dznrm2", + "euclidean", + "norm", + "euclidean-norm", + "linear", + "algebra", + "subroutines", + "vector", + "array", + "ndarray", + "complex", + "complex128", + "float64", + "float", + "single", + "float64array" + ] +} diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/test/test.dznrm2.js b/lib/node_modules/@stdlib/blas/base/dznrm2/test/test.dznrm2.js new file mode 100644 index 000000000000..f548f53a1ce4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/test/test.dznrm2.js @@ -0,0 +1,199 @@ +/** +* @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 Complex128Array = require( '@stdlib/array/complex128' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var dznrm2 = require( './../lib/dznrm2.js' ); + + +// FUNCTIONS // + +/** +* Tests for element-wise approximate equality. +* +* @private +* @param {Object} t - test object +* @param {Collection} actual - actual values +* @param {Collection} expected - expected values +* @param {number} rtol - relative tolerance +*/ +function isApprox( t, actual, expected, rtol ) { + var delta; + var tol; + var i; + + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + for ( i = 0; i < expected.length; i++ ) { + if ( actual[ i ] === expected[ i ] ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } else { + delta = abs( actual[ i ] - expected[ i ] ); + tol = rtol * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' ); + } + } +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dznrm2, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 3', function test( t ) { + t.strictEqual( dznrm2.length, 3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function finds the euclidean norm', function test( t ) { + var expected; + var norm; + var zx; + + zx = new Complex128Array([ + 0.3, // 1 + 0.1, // 1 + 0.5, // 2 + 0.0, // 2 + 0.0, // 3 + 0.5, // 3 + 0.0, // 4 + 0.2, // 4 + 2.0, + 3.0 + ]); + expected = 0.8; + + norm = dznrm2( 4, zx, 1 ); + isApprox( t, norm, expected, 2 ); + + zx = new Complex128Array( [ + 0.1, // 1 + 0.1, // 1 + -0.6, // 2 + 0.1, // 2 + 0.1, // 3 + -0.3, // 3 + 7.0, + 8.0 + ] ); + expected = 0.7; + + norm = dznrm2( 3, zx, 1 ); + isApprox( t, norm, expected, 2 ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than `1`, the function returns `0.0`', function test( t ) { + var expected; + var norm; + var zx; + + zx = new Complex128Array( [ + 1.0, + 2.0, + 3.0, + 4.0 + ] ); + expected = 0.0; + + norm = dznrm2( 0, zx, 1 ); + t.strictEqual( norm, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `strideX` parameter less than `0`, the function returns `0.0`', function test( t ) { + var expected; + var norm; + var zx; + + zx = new Complex128Array( [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ] ); + expected = 0.0; + + norm = dznrm2( zx.length, zx, -1 ); + t.strictEqual( norm, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var norm; + var zx; + + zx = new Complex128Array([ + 0.3, // 1 + 0.1, // 1 + 5.0, + 8.0, + 0.5, // 2 + 0.0, // 2 + 6.0, + 9.0, + 0.0, // 3 + 0.5, // 3 + 8.0, + 3.0, + 0.0, // 4 + 0.2, // 4 + 9.0, + 4.0 + ]); + expected = 0.8; + + norm = dznrm2( 4, zx, 2 ); + isApprox( t, norm, expected, 2 ); + t.end(); +}); + +tape( 'the function supports view offsets', function test( t ) { + var expected; + var norm; + var zx0; + var zx1; + + zx0 = new Complex128Array([ + 1.0, + 2.0, + 3.0, // 1 + 4.0, // 1 + 5.0, // 2 + 6.0, // 2 + 7.0, // 3 + 8.0, // 3 + 9.0, + 10.0 + ]); + expected = 14.1; + + zx1 = new Complex128Array( zx0.buffer, zx0.BYTES_PER_ELEMENT*1 ); + + norm = dznrm2( 3, zx1, 1 ); + isApprox( t, norm, expected, 2 ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/test/test.js b/lib/node_modules/@stdlib/blas/base/dznrm2/test/test.js new file mode 100644 index 000000000000..4b5a50893221 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/test/test.js @@ -0,0 +1,82 @@ +/** +* @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 proxyquire = require( 'proxyquire' ); +var isBrowser = require( '@stdlib/assert/is-browser' ); +var dznrm2 = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': isBrowser +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dznrm2, '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 dznrm2.ndarray, 'function', 'method is a function' ); + t.end(); +}); + +tape( 'if a native implementation is available, the main export is the native implementation', opts, function test( t ) { + var dznrm2 = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + + t.strictEqual( dznrm2, mock, 'returns native implementation' ); + t.end(); + + function tryRequire() { + return mock; + } + + function mock() { + // Mock... + } +}); + +tape( 'if a native implementation is not available, the main export is a JavaScript implementation', opts, function test( t ) { + var dznrm2; + var main; + + main = require( './../lib/dznrm2.js' ); + + dznrm2 = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + + t.strictEqual( dznrm2, main, 'returns JavaScript implementation' ); + t.end(); + + function tryRequire() { + return new Error( 'Cannot find module' ); + } +}); diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/base/dznrm2/test/test.ndarray.js new file mode 100644 index 000000000000..a8f0a660da88 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/test/test.ndarray.js @@ -0,0 +1,196 @@ +/** +* @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 Complex128Array = require( '@stdlib/array/complex128' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var dznrm2 = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Tests for element-wise approximate equality. +* +* @private +* @param {Object} t - test object +* @param {Collection} actual - actual values +* @param {Collection} expected - expected values +* @param {number} rtol - relative tolerance +*/ +function isApprox( t, actual, expected, rtol ) { + var delta; + var tol; + var i; + + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + for ( i = 0; i < expected.length; i++ ) { + if ( actual[ i ] === expected[ i ] ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } else { + delta = abs( actual[ i ] - expected[ i ] ); + tol = rtol * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' ); + } + } +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dznrm2, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 4', function test( t ) { + t.strictEqual( dznrm2.length, 4, 'returns expected value' ); + t.end(); +}); + +tape( 'the function finds the euclidean norm', function test( t ) { + var expected; + var norm; + var zx; + + zx = new Complex128Array([ + 0.3, // 1 + 0.1, // 1 + 0.5, // 2 + 0.0, // 2 + 0.0, // 3 + 0.5, // 3 + 0.0, // 4 + 0.2, // 4 + 2.0, + 3.0 + ]); + expected = 0.8; + + norm = dznrm2( 4, zx, 1, 0 ); + isApprox( t, norm, expected, 2 ); + + zx = new Complex128Array( [ + 0.1, // 1 + 0.1, // 1 + -0.6, // 2 + 0.1, // 2 + 0.1, // 3 + -0.3, // 3 + 7.0, + 8.0 + ] ); + expected = 0.7; + + norm = dznrm2( 3, zx, 1, 0 ); + isApprox( t, norm, expected, 2 ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than `1`, the function returns `0.0`', function test( t ) { + var expected; + var norm; + var zx; + + zx = new Complex128Array( [ + 1.0, + 2.0, + 3.0, + 4.0 + ] ); + expected = 0.0; + + norm = dznrm2( 0, zx, 1, 0 ); + t.strictEqual( norm, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `strideX` parameter less than `0`, the function returns `0.0`', function test( t ) { + var expected; + var norm; + var zx; + + zx = new Complex128Array( [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ] ); + expected = 0.0; + + norm = dznrm2( zx.length, zx, -1, 0 ); + t.strictEqual( norm, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var norm; + var zx; + + zx = new Complex128Array([ + 0.3, // 1 + 0.1, // 1 + 5.0, + 8.0, + 0.5, // 2 + 0.0, // 2 + 6.0, + 9.0, + 0.0, // 3 + 0.5, // 3 + 8.0, + 3.0, + 0.0, // 4 + 0.2, // 4 + 9.0, + 4.0 + ]); + expected = 0.8; + + norm = dznrm2( 4, zx, 2, 0 ); + isApprox( t, norm, expected, 2 ); + t.end(); +}); + +tape( 'the function supports specifying an offset', function test( t ) { + var expected; + var norm; + var zx; + + zx = new Complex128Array([ + 1.0, + 2.0, + 3.0, // 1 + 4.0, // 1 + 5.0, // 2 + 6.0, // 2 + 7.0, // 3 + 8.0, // 3 + 9.0, + 10.0 + ]); + expected = 14.1; + + norm = dznrm2( 3, zx, 1, 1 ); + isApprox( t, norm, expected, 2 ); + t.end(); +}); From 150dce776a58a0703a8c5da4fe26d1ce0cbefcda Mon Sep 17 00:00:00 2001 From: aman-095 Date: Tue, 25 Jun 2024 12:49:28 +0530 Subject: [PATCH 02/16] refactor: update implementation and descriptions --- .../@stdlib/blas/base/dznrm2/README.md | 115 +++++++++++++++-- .../blas/base/dznrm2/benchmark/benchmark.js | 5 +- .../dznrm2/benchmark/benchmark.ndarray.js | 3 +- .../@stdlib/blas/base/dznrm2/docs/repl.txt | 32 ++--- .../blas/base/dznrm2/docs/types/index.d.ts | 18 +-- .../blas/base/dznrm2/examples/index.js | 6 +- .../@stdlib/blas/base/dznrm2/lib/dznrm2.js | 48 ++----- .../@stdlib/blas/base/dznrm2/lib/ndarray.js | 122 ++++++++++++++---- 8 files changed, 241 insertions(+), 108 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/base/dznrm2/README.md b/lib/node_modules/@stdlib/blas/base/dznrm2/README.md index d2555a075aaa..c7ffa367638f 100644 --- a/lib/node_modules/@stdlib/blas/base/dznrm2/README.md +++ b/lib/node_modules/@stdlib/blas/base/dznrm2/README.md @@ -20,7 +20,7 @@ limitations under the License. # dznrm2 -> Find the euclidean norm of a complex double-precision floating-point vector. +> Compute the L2-norm of a complex double-precision floating-point vector.
@@ -32,7 +32,7 @@ var dznrm2 = require( '@stdlib/blas/base/dznrm2' ); #### dznrm2( N, zx, strideX ) -Finds the euclidean norm of a complex double-precision floating-point vector. +Computes the L2-norm of a complex double-precision floating-point vector. ```javascript var Complex128Array = require( '@stdlib/array/complex128' ); @@ -49,7 +49,7 @@ The function has the following parameters: - **zx**: input [`Complex128Array`][@stdlib/array/complex128]. - **strideX**: index increment for `zx`. -The `N` and `strideX` parameters determine which elements in `zx` are accessed at runtime. For example, to traverse every other value, +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to traverse every other value, ```javascript var Complex128Array = require( '@stdlib/array/complex128' ); @@ -71,14 +71,14 @@ var zx0 = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); // Create an offset view: var zx1 = new Complex128Array( zx0.buffer, zx0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -// Find euclidean norm: +// Compute the L2-norm: var norm = dznrm2( 2, zx1, 1 ); // returns ~9.3 ``` #### dznrm2.ndarray( N, zx, strideX, offset ) -Finds the euclidean norm of a complex double-precision floating-point vector using alternative indexing semantics. +Computes the L2-norm of a complex double-precision floating-point vector using alternative indexing semantics. ```javascript var Complex128Array = require( '@stdlib/array/complex128' ); @@ -100,7 +100,7 @@ var Complex128Array = require( '@stdlib/array/complex128' ); var zx = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); -var norm = dznrm2.ndarray( 3, zx, 1, 1 ); +var norm = dznrm2.ndarray( 2, zx, 1, 1 ); // returns ~9.3 ``` @@ -112,7 +112,7 @@ var norm = dznrm2.ndarray( 3, zx, 1, 1 ); ## Notes -- If `N < 1` or `strideX < 0`, both functions return `0.0`. +- If `N <= 0`, both functions return `0.0`. - `dznrm2()` corresponds to the [BLAS][blas] level 1 function [`dznrm2`][dznrm2].
@@ -128,7 +128,7 @@ var norm = dznrm2.ndarray( 3, zx, 1, 1 ); ```javascript var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); var filledarrayBy = require( '@stdlib/array/filled-by' ); -var Complex128 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); var dznrm2 = require( '@stdlib/blas/base/dznrm2' ); function rand() { @@ -138,7 +138,7 @@ function rand() { var zx = filledarrayBy( 10, 'complex128', rand ); console.log( zx.toString() ); -// Finds the euclidean norm: +// Computes the L2-norm: var norm = dznrm2( zx.length, zx, 1 ); console.log( norm ); ``` @@ -147,6 +147,101 @@ console.log( norm ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/base/dznrm2.h" +``` + +#### c_dznrm2( N, \*ZX, strideX ) + +Computes the L2-norm of a complex double-precision floating-point vector. + +```c +const double zx[] = { 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2 }; + +double norm = c_dznrm2( 4, (void *)zx, 1 ); +// returns 0.8 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **ZX**: `[in] void*` input array. +- **strideX**: `[in] CBLAS_INT` index increment for `ZX`. + +```c +double c_dznrm2( const CBLAS_INT N, const void *ZX, const CBLAS_INT strideX ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/base/dznrm2.h" +#include + +int main( void ) { + // Create a strided array of interleaved real and imaginary components: + const double zx[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }; + + // Specify the number of elements: + const int N = 4; + + // Specify stride length: + const int strideX = 1; + + // Compute the L2-norm: + c_dznrm2( N, (void *)zx, strideX ); + + // Print the result: + printf( "L2-norm: %lf\n", norm ); +} +``` + +
+ + + +
+ + +