From f2ba0789e2c904278c27de22491f0c184813fbe2 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 25 Apr 2026 20:24:51 +0500 Subject: [PATCH 1/3] feat: add ndarray/base/reinterpret-complex --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../base/reinterpret-complex/README.md | 118 +++++++ .../benchmark/benchmark.js | 198 ++++++++++++ .../base/reinterpret-complex/docs/repl.txt | 41 +++ .../reinterpret-complex/docs/types/index.d.ts | 76 +++++ .../reinterpret-complex/docs/types/test.ts | 62 ++++ .../reinterpret-complex/examples/index.js | 33 ++ .../base/reinterpret-complex/lib/index.js | 44 +++ .../base/reinterpret-complex/lib/main.js | 79 +++++ .../base/reinterpret-complex/package.json | 71 +++++ .../base/reinterpret-complex/test/test.js | 301 ++++++++++++++++++ 10 files changed, 1023 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/README.md b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/README.md new file mode 100644 index 000000000000..d8efbd8d466e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/README.md @@ -0,0 +1,118 @@ + + +# reinterpret + +> Reinterpret a complex-valued floating-point [ndarray][@stdlib/ndarray/base/ctor] as a real-valued floating-point [ndarray][@stdlib/ndarray/base/ctor] having the same precision. + +
+ +
+ + + +
+ +## Usage + +```javascript +var reinterpretComplex = require( '@stdlib/ndarray/base/reinterpret-complex' ); +``` + +#### reinterpretComplex( x ) + +Reinterprets a complex-valued floating-point [ndarray][@stdlib/ndarray/base/ctor] as a real-valued floating-point [ndarray][@stdlib/ndarray/base/ctor] having the same precision. + +```javascript +var ones = require( '@stdlib/ndarray/base/ones' ); + +var x = ones( 'complex128', [ 2, 2 ], 'row-major' ); +// returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] + +var out = reinterpretComplex( x ); +// returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +``` + +
+ + + +
+ +## Notes + +- If provided an [ndarray][@stdlib/ndarray/base/ctor] whose underlying data buffer is neither a `Complex128Array` nor a `Complex64Array`, the function returns the input [ndarray][@stdlib/ndarray/base/ctor] unchanged. +- The returned [ndarray][@stdlib/ndarray/base/ctor] is a view on the input [ndarray][@stdlib/ndarray/base/ctor] data buffer. +- The returned [ndarray][@stdlib/ndarray/base/ctor] has an additional trailing dimension of size two whose elements correspond to the real and imaginary components, respectively, of each complex-valued element in the input [ndarray][@stdlib/ndarray/base/ctor]. +- The returned [ndarray][@stdlib/ndarray/base/ctor] is a "base" [ndarray][@stdlib/ndarray/base/ctor], and, thus, the returned [ndarray][@stdlib/ndarray/base/ctor] does not perform bounds checking or afford any of the guarantees of the non-base [ndarray][@stdlib/ndarray/ctor] constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var reinterpretComplex = require( '@stdlib/ndarray/base/reinterpret-complex' ); + +// Create a double-precision complex floating-point ndarray: +var buf = new Complex128Array( discreteUniform( 8, -5, 5 ) ); +var x = ndarray( 'complex128', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +// Reinterpret as a double-precision floating-point ndarray: +var out = reinterpretComplex( x ); +console.log( ndarray2array( out ) ); +``` + +
+ + + +
+ +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/benchmark/benchmark.js new file mode 100644 index 000000000000..0edfd57fc848 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/benchmark/benchmark.js @@ -0,0 +1,198 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 Complex128Array = require( '@stdlib/array/complex128' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var ndarrayBase = require( '@stdlib/ndarray/base/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var reinterpretComplex = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::base_ndarray:dtype=complex128', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'complex128'; + buffer = new Complex128Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reinterpretComplex( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::ndarray:dtype=complex128', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'complex128'; + buffer = new Complex128Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reinterpretComplex( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base_ndarray:dtype=complex64', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'complex64'; + buffer = new Complex64Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reinterpretComplex( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::ndarray:dtype=complex64', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'complex64'; + buffer = new Complex64Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reinterpretComplex( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/repl.txt new file mode 100644 index 000000000000..b60bcb1ba70e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/repl.txt @@ -0,0 +1,41 @@ + +{{alias}}( x ) + Reinterprets a complex-valued floating-point ndarray as a real-valued + floating-point ndarray having the same precision. + + If provided an ndarray whose underlying data buffer is neither a + Complex128Array nor a Complex64Array, the function returns the input + ndarray unchanged. + + The returned ndarray is a view on the input ndarray data buffer. + + The returned ndarray has an additional trailing dimension of size two whose + elements correspond to the real and imaginary components, respectively, of + each complex-valued element in the input ndarray. + + The returned ndarray is a "base" ndarray, and, thus, the returned ndarray + does not perform bounds checking or afford any of the guarantees of the + non-base ndarray constructor. The primary intent of this function is to + reinterpret an ndarray-like object within internal implementations and to + do so with minimal overhead. + + Parameters + ---------- + x: ndarray + Input ndarray. + + Returns + ------- + out: ndarray + Real-valued floating-point ndarray view. + + Examples + -------- + > var dt = 'complex128'; + > var x = {{alias:@stdlib/ndarray/base/zeros}}( dt, [ 2, 2 ], 'row-major' ); + > var out = {{alias}}( x ) + [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/index.d.ts new file mode 100644 index 000000000000..b491eeaa599e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/index.d.ts @@ -0,0 +1,76 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 { complex128ndarray, complex64ndarray, float64ndarray, float32ndarray } from '@stdlib/types/ndarray'; + +/** +* Reinterprets a double-precision complex floating-point ndarray as a real-valued double-precision floating-point ndarray containing interleaved real and imaginary components. +* +* ## Notes +* +* - If provided an ndarray whose underlying data buffer is neither a `Complex128Array` nor a `Complex64Array`, the function returns the input ndarray unchanged. +* - The returned ndarray is a view on the input ndarray data buffer. +* - The returned ndarray has an additional trailing dimension of size two whose elements correspond to the real and imaginary components, respectively, of each complex-valued element in the input ndarray. +* - The returned ndarray is a "base" ndarray, and, thus, the returned ndarray does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. +* +* @param x - input ndarray +* @returns double-precision floating-point ndarray view +* +* @example +* var ones = require( '@stdlib/ndarray/base/ones' ); +* +* var x = ones( 'complex128', [ 2, 2 ], 'row-major' ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* +* var out = reinterpretComplex( x ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +*/ +declare function reinterpretComplex( x: complex128ndarray ): float64ndarray; + +/** +* Reinterprets a single-precision complex floating-point ndarray as a real-valued single-precision floating-point ndarray containing interleaved real and imaginary components. +* +* ## Notes +* +* - If provided an ndarray whose underlying data buffer is neither a `Complex128Array` nor a `Complex64Array`, the function returns the input ndarray unchanged. +* - The returned ndarray is a view on the input ndarray data buffer. +* - The returned ndarray has an additional trailing dimension of size two whose elements correspond to the real and imaginary components, respectively, of each complex-valued element in the input ndarray. +* - The returned ndarray is a "base" ndarray, and, thus, the returned ndarray does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. +* +* @param x - input ndarray +* @returns single-precision floating-point ndarray view +* +* @example +* var ones = require( '@stdlib/ndarray/base/ones' ); +* +* var x = ones( 'complex64', [ 2, 2 ], 'row-major' ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* +* var out = reinterpretComplex( x ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +*/ +declare function reinterpretComplex( x: complex64ndarray ): float32ndarray; + + +// EXPORTS // + +export = reinterpretComplex; diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/test.ts new file mode 100644 index 000000000000..79d9c02b1a94 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/test.ts @@ -0,0 +1,62 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/base/zeros' ); +import reinterpretComplex = require( './index' ); + + +// TESTS // + +// The function returns a float64ndarray when provided a complex128ndarray... +{ + const x = zeros( 'complex128', [ 2, 2 ], 'row-major' ); + + reinterpretComplex( x ); // $ExpectType float64ndarray +} + +// The function returns a float32ndarray when provided a complex64ndarray... +{ + const x = zeros( 'complex64', [ 2, 2 ], 'row-major' ); + + reinterpretComplex( x ); // $ExpectType float32ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is a complex-valued floating-point ndarray... +{ + reinterpretComplex( '5' ); // $ExpectError + reinterpretComplex( 5 ); // $ExpectError + reinterpretComplex( true ); // $ExpectError + reinterpretComplex( false ); // $ExpectError + reinterpretComplex( null ); // $ExpectError + reinterpretComplex( {} ); // $ExpectError + reinterpretComplex( [ '5' ] ); // $ExpectError + reinterpretComplex( ( x: number ): number => x ); // $ExpectError + reinterpretComplex( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + reinterpretComplex( zeros( 'float32', [ 2, 2 ], 'row-major' ) ); // $ExpectError + reinterpretComplex( zeros( 'int32', [ 2, 2 ], 'row-major' ) ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( 'complex128', [ 2, 2 ], 'row-major' ); + + reinterpretComplex(); // $ExpectError + reinterpretComplex( x, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/examples/index.js new file mode 100644 index 000000000000..f2f21236da3c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 Complex128Array = require( '@stdlib/array/complex128' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var reinterpretComplex = require( './../lib' ); + +// Create a double-precision complex floating-point ndarray: +var buf = new Complex128Array( discreteUniform( 8, -5, 5 ) ); +var x = ndarray( 'complex128', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +// Reinterpret as a double-precision floating-point ndarray: +var out = reinterpretComplex( x ); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/index.js new file mode 100644 index 000000000000..3f07c038858f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Reinterpret a complex-valued floating-point ndarray as a real-valued floating-point ndarray having the same precision. +* +* @module @stdlib/ndarray/base/reinterpret-complex +* +* @example +* var ones = require( '@stdlib/ndarray/base/ones' ); +* var reinterpretComplex = require( '@stdlib/ndarray/base/reinterpret-complex' ); +* +* var x = ones( 'complex128', [ 2, 2 ], 'row-major' ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* +* var out = reinterpretComplex( x ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js new file mode 100644 index 000000000000..1daea9ef2b58 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isComplex128Array = require( '@stdlib/array/base/assert/is-complex128array' ); +var isComplex64Array = require( '@stdlib/array/base/assert/is-complex64array' ); +var reinterpret128 = require( '@stdlib/ndarray/base/reinterpret-complex128' ); +var reinterpret64 = require( '@stdlib/ndarray/base/reinterpret-complex64' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); + + +// MAIN // + +/** +* Reinterprets a complex-valued floating-point ndarray as a real-valued floating-point ndarray having the same precision. +* +* ## Notes +* +* - If provided an ndarray whose underlying data buffer is neither a `Complex128Array` nor a `Complex64Array`, the function returns the input ndarray unchanged. +* - The returned ndarray is a view on the input ndarray data buffer. +* - The returned ndarray has an additional trailing dimension of size two whose elements correspond to the real and imaginary components, respectively, of each complex-valued element in the input ndarray. +* - The returned ndarray is a "base" ndarray, and, thus, the returned ndarray does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. +* +* @param {ndarray} x - input ndarray +* @returns {ndarray} real-valued floating-point ndarray view +* +* @example +* var ones = require( '@stdlib/ndarray/base/ones' ); +* +* var x = ones( 'complex128', [ 2, 2 ], 'row-major' ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* +* var out = reinterpretComplex( x ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* +* @example +* var ones = require( '@stdlib/ndarray/base/ones' ); +* +* var x = ones( 'complex64', [ 2, 2 ], 'row-major' ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* +* var out = reinterpretComplex( x ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +*/ +function reinterpretComplex( x ) { + var buf; + + buf = getData( x ); + if ( isComplex128Array( buf ) ) { + return reinterpret128( x ); + } + if ( isComplex64Array( buf ) ) { + return reinterpret64( x ); + } + return x; +} + + +// EXPORTS // + +module.exports = reinterpretComplex; diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/package.json b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/package.json new file mode 100644 index 000000000000..9a79a057549c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/ndarray/base/reinterpret-complex", + "version": "0.0.0", + "description": "Reinterpret a complex-valued floating-point ndarray as a real-valued floating-point ndarray having the same precision.", + "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", + "stdtypes", + "types", + "base", + "ndarray", + "reinterpret", + "cast", + "complex", + "complex128", + "complex64", + "cmplx", + "float64", + "float32", + "double", + "single", + "view", + "real" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/test/test.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/test/test.js new file mode 100644 index 000000000000..ae50da5d048a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/test/test.js @@ -0,0 +1,301 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 Complex64Array = require( '@stdlib/array/complex64' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Int32Array = require( '@stdlib/array/int32' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var getOffset = require( '@stdlib/ndarray/offset' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var reinterpretComplex = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reinterpretComplex, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a "base" ndarray instance', function test( t ) { + var buf; + var x; + var y; + + buf = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = ndarray( 'complex128', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + y = reinterpretComplex( x ); + + t.notEqual( y, x, 'returns new instance' ); + t.strictEqual( y instanceof ndarray, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reinterprets a complex128 ndarray as a float64 ndarray view (row-major)', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = ndarray( 'complex128', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + y = reinterpretComplex( x ); + + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.strictEqual( getData( y ) instanceof Float64Array, true, 'returns expected value' ); + t.strictEqual( getData( y ).buffer, buf.buffer, 'returns expected value' ); + t.strictEqual( String( getDType( y ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 4, 2, 1 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 0, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reinterprets a complex128 ndarray as a float64 ndarray view (column-major)', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = ndarray( 'complex128', buf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + y = reinterpretComplex( x ); + + expected = [ + [ + [ 1.0, 2.0 ], + [ 5.0, 6.0 ] + ], + [ + [ 3.0, 4.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.strictEqual( String( getDType( y ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 2, 4, 1 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 0, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reinterprets a complex64 ndarray as a float32 ndarray view (row-major)', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = ndarray( 'complex64', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + y = reinterpretComplex( x ); + + expected = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.strictEqual( getData( y ) instanceof Float32Array, true, 'returns expected value' ); + t.strictEqual( getData( y ).buffer, buf.buffer, 'returns expected value' ); + t.strictEqual( String( getDType( y ) ), 'float32', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 4, 2, 1 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 0, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reinterprets a complex64 ndarray as a float32 ndarray view (column-major)', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = ndarray( 'complex64', buf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + y = reinterpretComplex( x ); + + expected = [ + [ + [ 1.0, 2.0 ], + [ 5.0, 6.0 ] + ], + [ + [ 3.0, 4.0 ], + [ 7.0, 8.0 ] + ] + ]; + + t.strictEqual( String( getDType( y ) ), 'float32', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 2, 4, 1 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 0, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function doubles the input ndarray offset', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = ndarray( 'complex128', buf, [ 2 ], [ 1 ], 1, 'row-major' ); + + y = reinterpretComplex( x ); + + expected = [ + [ 3.0, 4.0 ], + [ 5.0, 6.0 ] + ]; + + t.strictEqual( getOffset( y ), 2, 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 2, 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports zero-dimensional input ndarrays (complex128)', function test( t ) { + var buf; + var x; + var y; + + buf = new Complex128Array( [ 1.0, 2.0 ] ); + x = ndarray( 'complex128', buf, [], [ 0 ], 0, 'row-major' ); + + y = reinterpretComplex( x ); + + t.strictEqual( String( getDType( y ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 0, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), [ 1.0, 2.0 ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports zero-dimensional input ndarrays (complex64)', function test( t ) { + var buf; + var x; + var y; + + buf = new Complex64Array( [ 1.0, 2.0 ] ); + x = ndarray( 'complex64', buf, [], [ 0 ], 0, 'row-major' ); + + y = reinterpretComplex( x ); + + t.strictEqual( String( getDType( y ) ), 'float32', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 0, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), [ 1.0, 2.0 ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = ndarray( 'complex128', buf, [ 2, 2 ], [ -2, -1 ], 3, 'row-major' ); + + y = reinterpretComplex( x ); + + expected = [ + [ + [ 7.0, 8.0 ], + [ 5.0, 6.0 ] + ], + [ + [ 3.0, 4.0 ], + [ 1.0, 2.0 ] + ] + ]; + + t.deepEqual( getStrides( y ), [ -4, -2, 1 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 6, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the input ndarray unchanged when the underlying data buffer is not a Complex128Array or Complex64Array', function test( t ) { + var values; + var i; + + values = [ + ndarray( 'float64', new Float64Array( 4 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ), + ndarray( 'float32', new Float32Array( 4 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ), + ndarray( 'int32', new Int32Array( 4 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( reinterpretComplex( values[ i ] ), values[ i ], 'returns expected value' ); + } + t.end(); +}); From a30c7c4bdb166cd2bc0ecec56aee7d4d76d91f7e Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 28 Apr 2026 16:40:27 +0500 Subject: [PATCH 2/3] fix: apply suggestions from code review --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../base/reinterpret-complex/README.md | 20 +++---- .../base/reinterpret-complex/docs/repl.txt | 6 +- .../reinterpret-complex/docs/types/index.d.ts | 22 ++++---- .../reinterpret-complex/examples/index.js | 9 ++- .../base/reinterpret-complex/lib/index.js | 10 ++-- .../base/reinterpret-complex/lib/main.js | 33 +++++++---- .../base/reinterpret-complex/test/test.js | 56 ++++++++++++++----- 7 files changed, 95 insertions(+), 61 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/README.md b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/README.md index d8efbd8d466e..ef786ed3e1ca 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/README.md @@ -41,13 +41,15 @@ var reinterpretComplex = require( '@stdlib/ndarray/base/reinterpret-complex' ); Reinterprets a complex-valued floating-point [ndarray][@stdlib/ndarray/base/ctor] as a real-valued floating-point [ndarray][@stdlib/ndarray/base/ctor] having the same precision. ```javascript -var ones = require( '@stdlib/ndarray/base/ones' ); +var zeroTo = require( '@stdlib/blas/ext/zero-to' ); -var x = ones( 'complex128', [ 2, 2 ], 'row-major' ); -// returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +var x = zeroTo( [ 2, 2 ], { + 'dtype': 'complex128' +}); +// returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] var out = reinterpretComplex( x ); -// returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +// returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] ``` @@ -58,7 +60,6 @@ var out = reinterpretComplex( x ); ## Notes -- If provided an [ndarray][@stdlib/ndarray/base/ctor] whose underlying data buffer is neither a `Complex128Array` nor a `Complex64Array`, the function returns the input [ndarray][@stdlib/ndarray/base/ctor] unchanged. - The returned [ndarray][@stdlib/ndarray/base/ctor] is a view on the input [ndarray][@stdlib/ndarray/base/ctor] data buffer. - The returned [ndarray][@stdlib/ndarray/base/ctor] has an additional trailing dimension of size two whose elements correspond to the real and imaginary components, respectively, of each complex-valued element in the input [ndarray][@stdlib/ndarray/base/ctor]. - The returned [ndarray][@stdlib/ndarray/base/ctor] is a "base" [ndarray][@stdlib/ndarray/base/ctor], and, thus, the returned [ndarray][@stdlib/ndarray/base/ctor] does not perform bounds checking or afford any of the guarantees of the non-base [ndarray][@stdlib/ndarray/ctor] constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. @@ -74,15 +75,14 @@ var out = reinterpretComplex( x ); ```javascript -var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var Complex128Array = require( '@stdlib/array/complex128' ); -var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var zeroTo = require( '@stdlib/blas/ext/zero-to' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var reinterpretComplex = require( '@stdlib/ndarray/base/reinterpret-complex' ); // Create a double-precision complex floating-point ndarray: -var buf = new Complex128Array( discreteUniform( 8, -5, 5 ) ); -var x = ndarray( 'complex128', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var x = zeroTo( [ 2, 2 ], { + 'dtype': 'complex128' +}); // Reinterpret as a double-precision floating-point ndarray: var out = reinterpretComplex( x ); diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/repl.txt index b60bcb1ba70e..129ee0f935e4 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/repl.txt @@ -3,10 +3,6 @@ Reinterprets a complex-valued floating-point ndarray as a real-valued floating-point ndarray having the same precision. - If provided an ndarray whose underlying data buffer is neither a - Complex128Array nor a Complex64Array, the function returns the input - ndarray unchanged. - The returned ndarray is a view on the input ndarray data buffer. The returned ndarray has an additional trailing dimension of size two whose @@ -32,7 +28,7 @@ Examples -------- > var dt = 'complex128'; - > var x = {{alias:@stdlib/ndarray/base/zeros}}( dt, [ 2, 2 ], 'row-major' ); + > var x = {{alias:@stdlib/ndarray/zeros}}( [ 2, 2 ], { 'dtype': dt } ); > var out = {{alias}}( x ) [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/index.d.ts index b491eeaa599e..859e05cd48f1 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/docs/types/index.d.ts @@ -27,7 +27,6 @@ import { complex128ndarray, complex64ndarray, float64ndarray, float32ndarray } f * * ## Notes * -* - If provided an ndarray whose underlying data buffer is neither a `Complex128Array` nor a `Complex64Array`, the function returns the input ndarray unchanged. * - The returned ndarray is a view on the input ndarray data buffer. * - The returned ndarray has an additional trailing dimension of size two whose elements correspond to the real and imaginary components, respectively, of each complex-valued element in the input ndarray. * - The returned ndarray is a "base" ndarray, and, thus, the returned ndarray does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. @@ -36,13 +35,15 @@ import { complex128ndarray, complex64ndarray, float64ndarray, float32ndarray } f * @returns double-precision floating-point ndarray view * * @example -* var ones = require( '@stdlib/ndarray/base/ones' ); +* var zeroTo = require( '@stdlib/blas/ext/zero-to' ); * -* var x = ones( 'complex128', [ 2, 2 ], 'row-major' ); -* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* var x = zeroTo( [ 2, 2 ], { +* 'dtype': 'complex128' +* }); +* // returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] * * var out = reinterpretComplex( x ); -* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* // returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] */ declare function reinterpretComplex( x: complex128ndarray ): float64ndarray; @@ -51,7 +52,6 @@ declare function reinterpretComplex( x: complex128ndarray ): float64ndarray; * * ## Notes * -* - If provided an ndarray whose underlying data buffer is neither a `Complex128Array` nor a `Complex64Array`, the function returns the input ndarray unchanged. * - The returned ndarray is a view on the input ndarray data buffer. * - The returned ndarray has an additional trailing dimension of size two whose elements correspond to the real and imaginary components, respectively, of each complex-valued element in the input ndarray. * - The returned ndarray is a "base" ndarray, and, thus, the returned ndarray does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. @@ -60,13 +60,15 @@ declare function reinterpretComplex( x: complex128ndarray ): float64ndarray; * @returns single-precision floating-point ndarray view * * @example -* var ones = require( '@stdlib/ndarray/base/ones' ); +* var zeroTo = require( '@stdlib/blas/ext/zero-to' ); * -* var x = ones( 'complex64', [ 2, 2 ], 'row-major' ); -* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* var x = zeroTo( [ 2, 2 ], { +* 'dtype': 'complex64' +* }); +* // returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] * * var out = reinterpretComplex( x ); -* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* // returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] */ declare function reinterpretComplex( x: complex64ndarray ): float32ndarray; diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/examples/index.js index f2f21236da3c..6d075ded0674 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/examples/index.js @@ -18,15 +18,14 @@ 'use strict'; -var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var Complex128Array = require( '@stdlib/array/complex128' ); -var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var zeroTo = require( '@stdlib/blas/ext/zero-to' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var reinterpretComplex = require( './../lib' ); // Create a double-precision complex floating-point ndarray: -var buf = new Complex128Array( discreteUniform( 8, -5, 5 ) ); -var x = ndarray( 'complex128', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var x = zeroTo( [ 2, 2 ], { + 'dtype': 'complex128' +}); // Reinterpret as a double-precision floating-point ndarray: var out = reinterpretComplex( x ); diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/index.js index 3f07c038858f..99428b811a9e 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/index.js @@ -24,14 +24,16 @@ * @module @stdlib/ndarray/base/reinterpret-complex * * @example -* var ones = require( '@stdlib/ndarray/base/ones' ); +* var zeroTo = require( '@stdlib/blas/ext/zero-to' ); * var reinterpretComplex = require( '@stdlib/ndarray/base/reinterpret-complex' ); * -* var x = ones( 'complex128', [ 2, 2 ], 'row-major' ); -* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* var x = zeroTo( [ 2, 2 ], { +* 'dtype': 'complex128' +* }); +* // returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] * * var out = reinterpretComplex( x ); -* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* // returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] */ // MODULES // diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js index 1daea9ef2b58..4f5a5cb491eb 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js @@ -24,7 +24,9 @@ var isComplex128Array = require( '@stdlib/array/base/assert/is-complex128array' var isComplex64Array = require( '@stdlib/array/base/assert/is-complex64array' ); var reinterpret128 = require( '@stdlib/ndarray/base/reinterpret-complex128' ); var reinterpret64 = require( '@stdlib/ndarray/base/reinterpret-complex64' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var format = require( '@stdlib/string/format' ); // MAIN // @@ -34,43 +36,50 @@ var getData = require( '@stdlib/ndarray/base/data-buffer' ); * * ## Notes * -* - If provided an ndarray whose underlying data buffer is neither a `Complex128Array` nor a `Complex64Array`, the function returns the input ndarray unchanged. * - The returned ndarray is a view on the input ndarray data buffer. * - The returned ndarray has an additional trailing dimension of size two whose elements correspond to the real and imaginary components, respectively, of each complex-valued element in the input ndarray. * - The returned ndarray is a "base" ndarray, and, thus, the returned ndarray does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. * * @param {ndarray} x - input ndarray +* @throws {TypeError} first argument must be a complex-valued floating-point ndarray * @returns {ndarray} real-valued floating-point ndarray view * * @example -* var ones = require( '@stdlib/ndarray/base/ones' ); +* var zeroTo = require( '@stdlib/blas/ext/zero-to' ); * -* var x = ones( 'complex128', [ 2, 2 ], 'row-major' ); -* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* var x = zeroTo( [ 2, 2 ], { +* 'dtype': 'complex128' +* }); +* // returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] * * var out = reinterpretComplex( x ); -* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* // returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] * * @example -* var ones = require( '@stdlib/ndarray/base/ones' ); +* var zeroTo = require( '@stdlib/blas/ext/zero-to' ); * -* var x = ones( 'complex64', [ 2, 2 ], 'row-major' ); -* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* var x = zeroTo( [ 2, 2 ], { +* 'dtype': 'complex64' +* }); +* // returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] * * var out = reinterpretComplex( x ); -* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* // returns [ [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 1.0, 0.0 ] ] ] */ function reinterpretComplex( x ) { + var dtype; var buf; + dtype = getDType( x ); buf = getData( x ); - if ( isComplex128Array( buf ) ) { + if ( dtype === 'complex128' && isComplex128Array( buf ) ) { return reinterpret128( x ); } - if ( isComplex64Array( buf ) ) { + if ( dtype === 'complex64' && isComplex64Array( buf ) ) { return reinterpret64( x ); } - return x; + // Note: intentionally throw here to catch the scenario in which we add, e.g., a Complex32Array and need to explicitly add support here... + throw new TypeError( format( 'invalid argument. First argument must be a complex-valued floating-point ndarray. Value: `%s`.', x ) ); } diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/test/test.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/test/test.js index ae50da5d048a..8c0819fd62aa 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/test/test.js @@ -45,6 +45,47 @@ tape( 'main export is a function', function test( t ) { t.end(); }); +tape( 'the function throws an error if provided an ndarray having a non-complex-valued data type', function test( t ) { + var values; + var i; + + values = [ + ndarray( 'float64', new Float64Array( 4 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ), + ndarray( 'float32', new Float32Array( 4 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ), + ndarray( 'int32', new Int32Array( 4 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + getDType( values[ i ] ) ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + reinterpretComplex( value ); + }; + } +}); + +tape( 'the function throws an error if provided an ndarray having a data type/buffer mismatch', function test( t ) { + var values; + var i; + + values = [ + ndarray( 'complex128', new Float64Array( 8 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ), + ndarray( 'complex64', new Float32Array( 8 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + getDType( values[ i ] ) + ' with a non-matching data buffer' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + reinterpretComplex( value ); + }; + } +}); + tape( 'the function returns a "base" ndarray instance', function test( t ) { var buf; var x; @@ -284,18 +325,3 @@ tape( 'the function supports negative strides', function test( t ) { t.end(); }); - -tape( 'the function returns the input ndarray unchanged when the underlying data buffer is not a Complex128Array or Complex64Array', function test( t ) { - var values; - var i; - - values = [ - ndarray( 'float64', new Float64Array( 4 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ), - ndarray( 'float32', new Float32Array( 4 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ), - ndarray( 'int32', new Int32Array( 4 ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ) - ]; - for ( i = 0; i < values.length; i++ ) { - t.strictEqual( reinterpretComplex( values[ i ] ), values[ i ], 'returns expected value' ); - } - t.end(); -}); From 39700f45ca8172e400ead8d01913b8707c57c203 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 28 Apr 2026 21:12:11 -0700 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/ndarray/base/reinterpret-complex/lib/main.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js index 4f5a5cb491eb..345c4682ccd7 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-complex/lib/main.js @@ -24,6 +24,7 @@ var isComplex128Array = require( '@stdlib/array/base/assert/is-complex128array' var isComplex64Array = require( '@stdlib/array/base/assert/is-complex64array' ); var reinterpret128 = require( '@stdlib/ndarray/base/reinterpret-complex128' ); var reinterpret64 = require( '@stdlib/ndarray/base/reinterpret-complex64' ); +var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' ); var getDType = require( '@stdlib/ndarray/base/dtype' ); var getData = require( '@stdlib/ndarray/base/data-buffer' ); var format = require( '@stdlib/string/format' ); @@ -41,7 +42,7 @@ var format = require( '@stdlib/string/format' ); * - The returned ndarray is a "base" ndarray, and, thus, the returned ndarray does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. * * @param {ndarray} x - input ndarray -* @throws {TypeError} first argument must be a complex-valued floating-point ndarray +* @throws {TypeError} must provide a complex-valued floating-point ndarray * @returns {ndarray} real-valued floating-point ndarray view * * @example @@ -70,7 +71,7 @@ function reinterpretComplex( x ) { var dtype; var buf; - dtype = getDType( x ); + dtype = resolveStr( getDType( x ) ); buf = getData( x ); if ( dtype === 'complex128' && isComplex128Array( buf ) ) { return reinterpret128( x ); @@ -79,7 +80,7 @@ function reinterpretComplex( x ) { return reinterpret64( x ); } // Note: intentionally throw here to catch the scenario in which we add, e.g., a Complex32Array and need to explicitly add support here... - throw new TypeError( format( 'invalid argument. First argument must be a complex-valued floating-point ndarray. Value: `%s`.', x ) ); + throw new TypeError( format( 'invalid argument. Must provide a complex-valued floating-point ndarray. Value: `%s`.', x ) ); }