Skip to content

Commit b4347ab

Browse files
committed
feat: add ndarray/base/reinterpret-complex128
--- 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 ---
1 parent 414d61b commit b4347ab

10 files changed

Lines changed: 792 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# reinterpretComplex128
22+
23+
> Reinterpret a [`complex128`][@stdlib/array/complex128] [ndarray][@stdlib/ndarray/base/ctor] as a double-precision floating-point [ndarray][@stdlib/ndarray/base/ctor].
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var reinterpretComplex128 = require( '@stdlib/ndarray/base/reinterpret-complex128' );
37+
```
38+
39+
#### reinterpretComplex128( x )
40+
41+
Reinterprets a [`complex128`][@stdlib/array/complex128] [ndarray][@stdlib/ndarray/base/ctor] as a double-precision floating-point [ndarray][@stdlib/ndarray/base/ctor].
42+
43+
```javascript
44+
var ones = require( '@stdlib/ndarray/base/ones' );
45+
46+
var x = ones( 'complex128', [ 2, 2 ], 'row-major' );
47+
// returns <ndarray>[ [ <Complex128>[ 1.0, 0.0 ], <Complex128>[ 1.0, 0.0 ] ], [ <Complex128>[ 1.0, 0.0 ], <Complex128>[ 1.0, 0.0 ] ] ]
48+
49+
var out = reinterpretComplex128( x );
50+
// returns <ndarray>[ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ]
51+
```
52+
53+
</section>
54+
55+
<!-- /.usage -->
56+
57+
<section class="notes">
58+
59+
## Notes
60+
61+
- The returned [ndarray][@stdlib/ndarray/base/ctor] is a view on the input [ndarray][@stdlib/ndarray/base/ctor] data buffer.
62+
- 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.
63+
64+
</section>
65+
66+
<!-- /.notes -->
67+
68+
<section class="examples">
69+
70+
## Examples
71+
72+
<!-- eslint no-undef: "error" -->
73+
74+
```javascript
75+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
76+
var Complex128Array = require( '@stdlib/array/complex128' );
77+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
78+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
79+
var reinterpretComplex128 = require( '@stdlib/ndarray/base/reinterpret-complex128' );
80+
81+
// Create a complex128 ndarray:
82+
var buf = new Complex128Array( discreteUniform( 8, -5, 5 ) );
83+
var x = ndarray( 'complex128', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
84+
85+
// Reinterpret as a double-precision floating-point ndarray:
86+
var out = reinterpretComplex128( x );
87+
console.log( ndarray2array( out ) );
88+
```
89+
90+
</section>
91+
92+
<!-- /.examples -->
93+
94+
<section class="references">
95+
96+
</section>
97+
98+
<!-- /.references -->
99+
100+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
101+
102+
<section class="related">
103+
104+
</section>
105+
106+
<!-- /.related -->
107+
108+
<section class="links">
109+
110+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
111+
112+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
113+
114+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor
115+
116+
</section>
117+
118+
<!-- /.links -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var Complex128Array = require( '@stdlib/array/complex128' );
25+
var ndarrayBase = require( '@stdlib/ndarray/base/ctor' );
26+
var ndarray = require( '@stdlib/ndarray/ctor' );
27+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var reinterpretComplex128 = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( format( '%s::base_ndarray,2d', pkg ), function benchmark( b ) {
36+
var strides;
37+
var values;
38+
var buffer;
39+
var offset;
40+
var dtype;
41+
var shape;
42+
var order;
43+
var out;
44+
var i;
45+
46+
dtype = 'complex128';
47+
buffer = new Complex128Array( 4 );
48+
shape = [ 2, 2 ];
49+
strides = [ 2, 1 ];
50+
offset = 0;
51+
order = 'row-major';
52+
53+
values = [
54+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
55+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
56+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
57+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
58+
ndarrayBase( dtype, buffer, shape, strides, offset, order )
59+
];
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
out = reinterpretComplex128( values[ i%values.length ] );
64+
if ( typeof out !== 'object' ) {
65+
b.fail( 'should return an object' );
66+
}
67+
}
68+
b.toc();
69+
if ( !isndarrayLike( out ) ) {
70+
b.fail( 'should return an ndarray' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
});
75+
76+
bench( format( '%s::ndarray,2d', pkg ), function benchmark( b ) {
77+
var strides;
78+
var values;
79+
var buffer;
80+
var offset;
81+
var dtype;
82+
var shape;
83+
var order;
84+
var out;
85+
var i;
86+
87+
dtype = 'complex128';
88+
buffer = new Complex128Array( 4 );
89+
shape = [ 2, 2 ];
90+
strides = [ 2, 1 ];
91+
offset = 0;
92+
order = 'row-major';
93+
94+
values = [
95+
ndarray( dtype, buffer, shape, strides, offset, order ),
96+
ndarray( dtype, buffer, shape, strides, offset, order ),
97+
ndarray( dtype, buffer, shape, strides, offset, order ),
98+
ndarray( dtype, buffer, shape, strides, offset, order ),
99+
ndarray( dtype, buffer, shape, strides, offset, order )
100+
];
101+
102+
b.tic();
103+
for ( i = 0; i < b.iterations; i++ ) {
104+
out = reinterpretComplex128( values[ i%values.length ] );
105+
if ( typeof out !== 'object' ) {
106+
b.fail( 'should return an object' );
107+
}
108+
}
109+
b.toc();
110+
if ( !isndarrayLike( out ) ) {
111+
b.fail( 'should return an ndarray' );
112+
}
113+
b.pass( 'benchmark finished' );
114+
b.end();
115+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( x )
3+
Reinterprets a complex128 ndarray as a double-precision floating-point
4+
ndarray.
5+
6+
The returned ndarray is a view on the input ndarray data buffer.
7+
8+
The returned ndarray is a "base" ndarray, and, thus, the returned ndarray
9+
does not perform bounds checking or afford any of the guarantees of the
10+
non-base ndarray constructor. The primary intent of this function is to
11+
reinterpret an ndarray-like object within internal implementations and to
12+
do so with minimal overhead.
13+
14+
Parameters
15+
----------
16+
x: ndarray
17+
Input ndarray.
18+
19+
Returns
20+
-------
21+
out: ndarray
22+
Double-precision floating-point ndarray view.
23+
24+
Examples
25+
--------
26+
> var dt = 'complex128';
27+
> var x = {{alias:@stdlib/ndarray/base/zeros}}( dt, [ 2, 2 ], 'row-major' );
28+
> var out = {{alias}}( x )
29+
<ndarray>[ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ]
30+
31+
See Also
32+
--------
33+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { complex128ndarray, float64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Reinterprets a complex128 ndarray as a double-precision floating-point ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - The returned ndarray is a view on the input ndarray data buffer.
31+
* - 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.
32+
*
33+
* @param x - input ndarray
34+
* @returns double-precision floating-point ndarray view
35+
*
36+
* @example
37+
* var ones = require( '@stdlib/ndarray/base/ones' );
38+
*
39+
* var x = ones( 'complex128', [ 2, 2 ], 'row-major' );
40+
* // returns <ndarray>[ [ <Complex128>[ 1.0, 0.0 ], <Complex128>[ 1.0, 0.0 ] ], [ <Complex128>[ 1.0, 0.0 ], <Complex128>[ 1.0, 0.0 ] ] ]
41+
*
42+
* var out = reinterpretComplex128( x );
43+
* // returns <ndarray>[ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ]
44+
*/
45+
declare function reinterpretComplex128( x: complex128ndarray ): float64ndarray;
46+
47+
48+
// EXPORTS //
49+
50+
export = reinterpretComplex128;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import zeros = require( '@stdlib/ndarray/base/zeros' );
20+
import reinterpretComplex128 = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a float64 ndarray...
26+
{
27+
const x = zeros( 'complex128', [ 2, 2 ], 'row-major' );
28+
29+
reinterpretComplex128( x ); // $ExpectType float64ndarray
30+
}
31+
32+
// The compiler throws an error if the function is not provided a first argument which is a complex128 ndarray...
33+
{
34+
reinterpretComplex128( '5' ); // $ExpectError
35+
reinterpretComplex128( 5 ); // $ExpectError
36+
reinterpretComplex128( true ); // $ExpectError
37+
reinterpretComplex128( false ); // $ExpectError
38+
reinterpretComplex128( null ); // $ExpectError
39+
reinterpretComplex128( {} ); // $ExpectError
40+
reinterpretComplex128( [ '5' ] ); // $ExpectError
41+
reinterpretComplex128( ( x: number ): number => x ); // $ExpectError
42+
reinterpretComplex128( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError
43+
reinterpretComplex128( zeros( 'float32', [ 2, 2 ], 'row-major' ) ); // $ExpectError
44+
reinterpretComplex128( zeros( 'int32', [ 2, 2 ], 'row-major' ) ); // $ExpectError
45+
reinterpretComplex128( zeros( 'complex64', [ 2, 2 ], 'row-major' ) ); // $ExpectError
46+
}
47+
48+
// The compiler throws an error if the function is provided an unsupported number of arguments...
49+
{
50+
const x = zeros( 'complex128', [ 2, 2 ], 'row-major' );
51+
52+
reinterpretComplex128(); // $ExpectError
53+
reinterpretComplex128( x, {} ); // $ExpectError
54+
}

0 commit comments

Comments
 (0)