Skip to content

Commit fc11bfe

Browse files
committed
Add utility to reinterpret a Complex128Array as a Float64Array
1 parent 1fe20e5 commit fc11bfe

File tree

10 files changed

+656
-0
lines changed

10 files changed

+656
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# reinterpret
22+
23+
> Reinterpret a [`Complex128Array`][@stdlib/array/complex128] as a [`Float64Array`][@stdlib/array/float64].
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex128' );
41+
```
42+
43+
#### reinterpret( x, offset )
44+
45+
Returns a [`Float64Array`][@stdlib/array/float64] view of a [`Complex128Array`][@stdlib/array/complex128].
46+
47+
```javascript
48+
var Complex128Array = require( '@stdlib/array/complex128' );
49+
50+
var x = new Complex128Array( 10 );
51+
52+
var view = reinterpret( x, 0 );
53+
// returns <Float64Array>
54+
55+
var bool = ( view.buffer === x.buffer );
56+
// returns true
57+
58+
var len = view.length;
59+
// returns 20
60+
```
61+
62+
The `offset` argument specifies the starting index of the returned [`Float64Array`][@stdlib/array/float64] view relative to the [`Complex128Array`][@stdlib/array/complex128].
63+
64+
```javascript
65+
var Complex128Array = require( '@stdlib/array/complex128' );
66+
67+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
68+
69+
var view = reinterpret( x, 2 );
70+
// returns <Float64Array>
71+
72+
var len = view.length;
73+
// returns 4
74+
75+
var re = view[ 0 ];
76+
// returns 5.0
77+
78+
var im = view[ 1 ];
79+
// returns 6.0
80+
````
81+
82+
</section>
83+
84+
<!-- /.usage -->
85+
86+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
87+
88+
<section class="notes">
89+
90+
</section>
91+
92+
<!-- /.notes -->
93+
94+
<!-- Package usage examples. -->
95+
96+
<section class="examples">
97+
98+
## Examples
99+
100+
<!-- eslint no-undef: "error" -->
101+
102+
```javascript
103+
var Complex128Array = require( '@stdlib/array/complex128' );
104+
var real = require( '@stdlib/complex/real' );
105+
var imag = require( '@stdlib/complex/imag' );
106+
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex128' );
107+
108+
// Define a complex number array:
109+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
110+
// returns <Complex128Array>
111+
112+
// Reinterpret as a `float64` array:
113+
var view = reinterpret( x );
114+
// returns <Float64Array>
115+
116+
// Set view elements:
117+
view[ 0 ] = 9.0;
118+
view[ 1 ] = 10.0;
119+
120+
// Get the first element of the complex number array:
121+
var z = x.get( 0 );
122+
// returns <Complex128>
123+
124+
var re = real( z );
125+
// returns 9.0
126+
127+
var im = imag( z );
128+
// returns 10.0
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="references">
138+
139+
</section>
140+
141+
<!-- /.references -->
142+
143+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib
156+
157+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib
158+
159+
</section>
160+
161+
<!-- /.links -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 isFloat64Array = require( '@stdlib/assert/is-float64array' );
26+
var pkg = require( './../package.json' ).name;
27+
var reinterpret = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var values;
34+
var out;
35+
var i;
36+
37+
values = [
38+
new Complex128Array( 10 ),
39+
new Complex128Array( 5 ),
40+
new Complex128Array( 20 )
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = reinterpret( values[ i%values.length ], 1 );
46+
if ( typeof out !== 'object' ) {
47+
b.fail( 'should return an object' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isFloat64Array( out ) ) {
52+
b.fail( 'should return a Float64Array' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( x, offset )
3+
Returns a Float64Array view of a Complex128Array.
4+
5+
Parameters
6+
----------
7+
x: Complex128Array
8+
Input array.
9+
10+
offset: integer
11+
Starting index of the view relative to the Complex128Array.
12+
13+
Returns
14+
-------
15+
out: Float64Array
16+
Float64Array view.
17+
18+
Examples
19+
--------
20+
> var x = new {{alias:@stdlib/array/complex128}}( 10 );
21+
> var out = {{alias}}( x, 0 )
22+
<Float64Array>
23+
> var bool = ( out.buffer === x.buffer )
24+
true
25+
26+
See Also
27+
--------
28+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Complex128Array } from '@stdlib/types/array';
24+
25+
/**
26+
* Reinterprets a `Complex128Array` as a `Float64Array`.
27+
*
28+
* @param x - input array
29+
* @param offset - starting index
30+
* @returns `Float64Array` view
31+
*
32+
* @example
33+
* var Complex128Array = require( `@stdlib/array/complex128` );
34+
*
35+
* var x = new Complex128Array( 10 );
36+
*
37+
* var out = reinterpret( x, 0 );
38+
* // returns <Float64Array>
39+
*
40+
* var bool = ( out.buffer === x.buffer );
41+
*/
42+
declare function reinterpret( x: Complex128Array, offset: number ): Float64Array; // tslint:disable-line:max-line-length
43+
44+
45+
// EXPORTS //
46+
47+
export = reinterpret;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 Complex128Array = require( '@stdlib/array/complex128' );
20+
import reinterpret = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a Float64Array...
26+
{
27+
reinterpret( new Complex128Array( 10 ), 0 ); // $ExpectType Float64Array
28+
}
29+
30+
// The compiler throws an error if not provided a first argument which is a Complex128Array...
31+
{
32+
reinterpret( '10', 0 ); // $ExpectError
33+
reinterpret( 10, 0 ); // $ExpectError
34+
reinterpret( true, 0 ); // $ExpectError
35+
reinterpret( false, 0 ); // $ExpectError
36+
reinterpret( null, 0 ); // $ExpectError
37+
reinterpret( undefined, 0 ); // $ExpectError
38+
reinterpret( [], 0 ); // $ExpectError
39+
reinterpret( {}, 0 ); // $ExpectError
40+
reinterpret( ( x: number ): number => x, 0 ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if not provided a second argument which is a number...
44+
{
45+
const x = new Complex128Array( 10 );
46+
47+
reinterpret( x, '10' ); // $ExpectError
48+
reinterpret( x, true ); // $ExpectError
49+
reinterpret( x, false ); // $ExpectError
50+
reinterpret( x, null ); // $ExpectError
51+
reinterpret( x, undefined ); // $ExpectError
52+
reinterpret( x, [] ); // $ExpectError
53+
reinterpret( x, {} ); // $ExpectError
54+
reinterpret( x, ( x: number ): number => x ); // $ExpectError
55+
}

0 commit comments

Comments
 (0)