-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add ndarray/base/reinterpret-complex128
#11629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
lib/node_modules/@stdlib/ndarray/base/reinterpret-complex128/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # reinterpretComplex128 | ||
|
|
||
| > Reinterpret a double-precision complex floating-point [ndarray][@stdlib/ndarray/base/ctor] as a real-valued double-precision floating-point [ndarray][@stdlib/ndarray/base/ctor] containing interleaved real and imaginary components. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var reinterpretComplex128 = require( '@stdlib/ndarray/base/reinterpret-complex128' ); | ||
| ``` | ||
|
|
||
| #### reinterpretComplex128( x ) | ||
|
|
||
| Reinterprets a double-precision complex floating-point [ndarray][@stdlib/ndarray/base/ctor] as a real-valued double-precision floating-point [ndarray][@stdlib/ndarray/base/ctor] containing interleaved real and imaginary components. | ||
|
|
||
| ```javascript | ||
| var ones = require( '@stdlib/ndarray/base/ones' ); | ||
|
|
||
| var x = ones( 'complex128', [ 2, 2 ], 'row-major' ); | ||
| // returns <ndarray>[ [ <Complex128>[ 1.0, 0.0 ], <Complex128>[ 1.0, 0.0 ] ], [ <Complex128>[ 1.0, 0.0 ], <Complex128>[ 1.0, 0.0 ] ] ] | ||
|
|
||
| var out = reinterpretComplex128( x ); | ||
| // returns <ndarray>[ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - 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. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```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 reinterpretComplex128 = require( '@stdlib/ndarray/base/reinterpret-complex128' ); | ||
|
|
||
| // 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 = reinterpretComplex128( x ); | ||
| console.log( ndarray2array( out ) ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor | ||
|
|
||
| [@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
115 changes: 115 additions & 0 deletions
115
lib/node_modules/@stdlib/ndarray/base/reinterpret-complex128/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * @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 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 reinterpretComplex128 = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( format( '%s::base_ndarray,2d', 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 = reinterpretComplex128( 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,2d', 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 = reinterpretComplex128( 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(); | ||
| }); |
38 changes: 38 additions & 0 deletions
38
lib/node_modules/@stdlib/ndarray/base/reinterpret-complex128/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
|
|
||
| {{alias}}( x ) | ||
| Reinterprets a double-precision complex floating-point ndarray as a real- | ||
| valued double-precision floating-point ndarray containing interleaved real | ||
| and imaginary components. | ||
|
|
||
| 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 | ||
| Double-precision floating-point ndarray view. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var dt = 'complex128'; | ||
| > var x = {{alias:@stdlib/ndarray/base/zeros}}( dt, [ 2, 2 ], 'row-major' ); | ||
| > var out = {{alias}}( x ) | ||
| <ndarray>[ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
51 changes: 51 additions & 0 deletions
51
lib/node_modules/@stdlib/ndarray/base/reinterpret-complex128/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * @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 | ||
|
|
||
| /// <reference types="@stdlib/types"/> | ||
|
|
||
| import { complex128ndarray, float64ndarray } 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 | ||
| * | ||
| * - 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 <ndarray>[ [ <Complex128>[ 1.0, 0.0 ], <Complex128>[ 1.0, 0.0 ] ], [ <Complex128>[ 1.0, 0.0 ], <Complex128>[ 1.0, 0.0 ] ] ] | ||
| * | ||
| * var out = reinterpretComplex128( x ); | ||
| * // returns <ndarray>[ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] | ||
| */ | ||
| declare function reinterpretComplex128( x: complex128ndarray ): float64ndarray; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = reinterpretComplex128; |
54 changes: 54 additions & 0 deletions
54
lib/node_modules/@stdlib/ndarray/base/reinterpret-complex128/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * @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. | ||
| */ | ||
|
|
||
| import zeros = require( '@stdlib/ndarray/base/zeros' ); | ||
| import reinterpretComplex128 = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns a float64ndarray... | ||
| { | ||
| const x = zeros( 'complex128', [ 2, 2 ], 'row-major' ); | ||
|
|
||
| reinterpretComplex128( x ); // $ExpectType float64ndarray | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is not provided a first argument which is a complex128ndarray... | ||
| { | ||
| reinterpretComplex128( '5' ); // $ExpectError | ||
| reinterpretComplex128( 5 ); // $ExpectError | ||
| reinterpretComplex128( true ); // $ExpectError | ||
| reinterpretComplex128( false ); // $ExpectError | ||
| reinterpretComplex128( null ); // $ExpectError | ||
| reinterpretComplex128( {} ); // $ExpectError | ||
| reinterpretComplex128( [ '5' ] ); // $ExpectError | ||
| reinterpretComplex128( ( x: number ): number => x ); // $ExpectError | ||
| reinterpretComplex128( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError | ||
| reinterpretComplex128( zeros( 'float32', [ 2, 2 ], 'row-major' ) ); // $ExpectError | ||
| reinterpretComplex128( zeros( 'int32', [ 2, 2 ], 'row-major' ) ); // $ExpectError | ||
| reinterpretComplex128( zeros( 'complex64', [ 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' ); | ||
|
|
||
| reinterpretComplex128(); // $ExpectError | ||
| reinterpretComplex128( x, {} ); // $ExpectError | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.