Skip to content
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

feat: remove out keyword, add C implementation and addon to cpolar API #1067

Merged
merged 17 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 118 additions & 13 deletions lib/node_modules/@stdlib/math/base/special/cpolar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ limitations under the License.

-->

# polar
# cpolar

> Compute the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a complex number.
> Compute the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a double-precision complex floating-point number.

<section class="intro">

Expand All @@ -36,26 +36,31 @@ limitations under the License.
var cpolar = require( '@stdlib/math/base/special/cpolar' );
```

#### cpolar( \[out,] re, im )
#### cpolar( z )

Computes the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a complex number comprised of a **real** component `re` and an **imaginary** component `im`.
Computes the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a double-precision complex floating-point number.

```javascript
var o = cpolar( 5.0, 3.0 );
var Complex128 = require( '@stdlib/complex/float64' );

var o = cpolar( new Complex128( 5.0, 3.0 ) );
// returns [ ~5.83, ~0.5404 ]
```
steff456 marked this conversation as resolved.
Show resolved Hide resolved

By default, the function returns real and imaginary components as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object.
#### cpolar.assign( z, out, stride, offset )

Computes the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a double-precision complex floating-point number and assigns results to a provided output array.

```javascript
var Complex128 = require( '@stdlib/complex/float64' );
var Float64Array = require( '@stdlib/array/float64' );

var out = new Float64Array( 2 );

var o = cpolar( out, 5.0, 3.0 );
var v = cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, 0 );
// returns <Float64Array>[ ~5.83, ~0.5404 ]

var bool = ( o === out );
var bool = ( v === out );
// returns true
```

Expand Down Expand Up @@ -87,7 +92,7 @@ for ( i = 0; i < 100; i++ ) {
re = round( randu()*100.0 ) - 50.0;
im = round( randu()*100.0 ) - 50.0;
z = new Complex128( re, im );
o = cpolar( real(z), imag(z) );
o = cpolar( z );
z = z.toString();
console.log( 'abs(%s) = %d. arg(%s) = %d', z, o[0], z, o[1] );
}
Expand All @@ -97,6 +102,110 @@ for ( i = 0; i < 100; i++ ) {

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

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

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/math/base/special/cpolar.h"
```

#### stdlib_base_cpolar( z, cabs, cphase )

Computes the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a double-precision complex floating-point number.

```c
#include "stdlib/complex/float64.h"
#include "stdlib/complex/real.h"
#include "stdlib/complex/imag.h"

stdlib_complex128_t z = stdlib_complex128( 5.0, 3.0 );
double cabs;
double cphase;
stdlib_base_cpolar( z, &cabs, &cphase );
```

The function accepts the following arguments:

- **z**: `[in] stdlib_complex128_t` input value.
- **cabs**: `[out] double*` destination for the absolute value.
- **cphase**: `[out] double*` destination for the phase value in radians.

```c
double stdlib_base_cpolar( const stdlib_complex128_t z, double *cabs, double *cphase );
```

</section>

<!-- /.usage -->

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

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/math/base/special/cpolar.h"
#include "stdlib/complex/float64.h"
#include "stdlib/complex/reim.h"
#include <stdio.h>

int main( void ) {
const stdlib_complex128_t x[] = {
stdlib_complex128( 3.14, 1.0 ),
stdlib_complex128( -3.14, -1.0 ),
stdlib_complex128( 0.0, 0.0 ),
stdlib_complex128( 0.0/0.0, 0.0/0.0 )
};

double cphase;
double cabs;
double re;
double im;
int i;
for ( i = 0; i < 12; i++ ) {
stdlib_base_cpolar( x[i], &cabs, &cphase );
stdlib_reim( x[i], &re, &im );
printf( "cpolar(%lf + %lfi) => cabs: %lf, cphase: %lf\n", re, im, cabs, cphase );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">
Expand All @@ -122,10 +231,6 @@ for ( i = 0; i < 100; i++ ) {

<!-- <related-links> -->

[@stdlib/math/base/special/cabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cabs

[@stdlib/math/base/special/cphase]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cphase

<!-- </related-links> -->

</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,28 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isArray = require( '@stdlib/assert/is-array' );
var Complex128 = require( '@stdlib/complex/float64' );
var pkg = require( './../package.json' ).name;
var cpolar = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var re;
var im;
var values;
var y;
var i;

values = [
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
re = ( randu()*1000.0 ) - 500.0;
im = ( randu()*1000.0 ) - 500.0;
y = cpolar( re, im );
y = cpolar( values[ i%values.length ] );
if ( y.length === 0 ) {
b.fail( 'should not be empty' );
}
Expand All @@ -52,21 +55,22 @@ bench( pkg, function benchmark( b ) {
b.end();
});

bench( pkg+'::memory_reuse', function benchmark( b ) {
var out;
var re;
var im;
bench( pkg+':assign', function benchmark( b ) {
var values;
var y;
var i;

out = new Array( 2 );
values = [
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
];

y = [ 0.0, 0.0 ];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
re = ( randu()*1000.0 ) - 500.0;
im = ( randu()*1000.0 ) - 500.0;
y = cpolar( out, re, im );
if ( y.length === 0 ) {
cpolar.assign( values[ i%values.length ], y, 1, 0 );
if ( y[ 0 ] !== y[ 0 ] || y[ 1 ] !== y[ 1 ] ) {
b.fail( 'should not be empty' );
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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 resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/base/uniform' );
var isArray = require( '@stdlib/assert/is-array' );
var Complex128 = require( '@stdlib/complex/float64' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var cpolar = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( cpolar instanceof Error )
};


// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var values;
var y;
var i;

values = [
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = cpolar( values[ i%values.length ] );
if ( y.length === 0 ) {
b.fail( 'should not be empty' );
}
}
b.toc();
if ( !isArray( y ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading