Skip to content

Commit

Permalink
feat: add strided/base/reinterpret-complex
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Feb 12, 2024
1 parent 5d74ee7 commit 8f40030
Show file tree
Hide file tree
Showing 10 changed files with 847 additions and 0 deletions.
157 changes: 157 additions & 0 deletions lib/node_modules/@stdlib/strided/base/reinterpret-complex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<!--
@license Apache-2.0
Copyright (c) 2024 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# reinterpret

> Reinterpret a complex-valued floating-point array as a real-valued floating-point array having the same precision.
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
```

#### reinterpret( x, offset )

Returns a real-valued floating-point array view of a complex-valued floating-point array.

```javascript
var Complex128Array = require( '@stdlib/array/complex128' );

var x = new Complex128Array( 10 );

var view = reinterpret( x, 0 );
// returns <Float64Array>

var bool = ( view.buffer === x.buffer );
// returns true

var len = view.length;
// returns 20
```

The `offset` argument specifies the starting index of the returned real-valued floating-point array view relative to the input array.

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );

var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );

var view = reinterpret( x, 2 );
// returns <Float32Array>

var len = view.length;
// returns 4

var re = view[ 0 ];
// returns 5.0

var im = view[ 1 ];
// returns 6.0
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var Complex128Array = require( '@stdlib/array/complex128' );
var real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );

// Define a complex number array:
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
// returns <Complex128Array>

// Reinterpret as a `float64` array:
var view = reinterpret( x, 0 );
// returns <Float64Array>

// Set view elements:
view[ 0 ] = 9.0;
view[ 1 ] = 10.0;

// Get the first element of the complex number array:
var z = x.get( 0 );
// returns <Complex128>

var re = real( z );
// returns 9.0

var im = imag( z );
// returns 10.0
```

</section>

<!-- /.examples -->

<!-- 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. -->

<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 for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var Complex128Array = require( '@stdlib/array/complex128' );
var isFloat64Array = require( '@stdlib/assert/is-float64array' );
var Complex64Array = require( '@stdlib/array/complex64' );
var isFloat32Array = require( '@stdlib/assert/is-float32array' );
var pkg = require( './../package.json' ).name;
var reinterpret = require( './../lib' );


// MAIN //

bench( pkg+'::complex128', function benchmark( b ) {
var values;
var out;
var i;

values = [
new Complex128Array( 10 ),
new Complex128Array( 5 ),
new Complex128Array( 20 )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = reinterpret( values[ i%values.length ], 1 );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isFloat64Array( out ) ) {
b.fail( 'should return a Float64Array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::complex64', function benchmark( b ) {
var values;
var out;
var i;

values = [
new Complex64Array( 10 ),
new Complex64Array( 5 ),
new Complex64Array( 20 )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = reinterpret( values[ i%values.length ], 1 );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isFloat32Array( out ) ) {
b.fail( 'should return a Float32Array' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

{{alias}}( x, offset )
Returns a real-valued floating-point array view of a complex-valued
floating-point array having the same precision.

Parameters
----------
x: Complex128Array|Complex64Array
Input array.

offset: integer
Starting index of the view relative to the input array.

Returns
-------
out: Float64Array|Float32Array
Real-valued floating-point array view.

Examples
--------
> var x = new {{alias:@stdlib/array/complex128}}( 10 );
> var out = {{alias}}( x, 0 )
<Float64Array>
> var bool = ( out.buffer === x.buffer )
true
> x = new {{alias:@stdlib/array/complex64}}( 10 );
> out = {{alias}}( x, 0 )
<Float32Array>
> bool = ( out.buffer === x.buffer )
true

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Complex128Array, Complex64Array } from '@stdlib/types/array';

/**
* Reinterprets a `Complex64Array` as a `Float32Array`.
*
* @param x - input array
* @param offset - starting index
* @returns `Float32Array` view
*
* @example
* var Complex64Array = require( '@stdlib/array/complex64' );
*
* var x = new Complex64Array( 10 );
*
* var out = reinterpret( x, 0 );
* // returns <Float32Array>
*
* var bool = ( out.buffer === x.buffer );
*/
declare function reinterpret( x: Complex64Array, offset: number ): Float32Array;

/**
* Reinterprets a `Complex128Array` as a `Float64Array`.
*
* @param x - input array
* @param offset - starting index
* @returns `Float64Array` view
*
* @example
* var Complex128Array = require( '@stdlib/array/complex128' );
*
* var x = new Complex128Array( 10 );
*
* var out = reinterpret( x, 0 );
* // returns <Float64Array>
*
* var bool = ( out.buffer === x.buffer );
*/
declare function reinterpret( x: Complex128Array, offset: number ): Float64Array;


// EXPORTS //

export = reinterpret;
Loading

0 comments on commit 8f40030

Please sign in to comment.