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: add native addon to cinv API #981

Merged
merged 9 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
157 changes: 127 additions & 30 deletions lib/node_modules/@stdlib/math/base/special/cinv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ limitations under the License.

-->

# inv
# cinv

> Compute the inverse of a complex number.
> Compute the inverse of a double-precision complex floating-point number.

<section class="intro">

Expand All @@ -47,27 +47,23 @@ The inverse (or reciprocal) of a non-zero complex number `z = a + bi` is defined
var cinv = require( '@stdlib/math/base/special/cinv' );
```

#### cinv( \[out,] re1, im1 )
#### cinv( z )

Computes the inverse of a `complex` number comprised of a **real** component `re` and an **imaginary** component `im`.
Computes the inverse of a double-precision complex floating-point number.

```javascript
var v = cinv( 2.0, 4.0 );
// returns [ 0.1, -0.2 ]
```

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.

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

var out = new Float64Array( 2 );
var v = cinv( new Complex128( 2.0, 4.0 ) );
// returns <Complex128>

var v = cinv( out, 2.0, 4.0 );
// returns <Float64Array>[ 0.1, -0.2 ]
var re = real( v );
// returns 0.1

var bool = ( v === out );
// returns true
var im = imag( v );
// returns -0.2
```

</section>
Expand All @@ -82,26 +78,16 @@ var bool = ( v === out );

```javascript
var Complex128 = require( '@stdlib/complex/float64' );
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );
var uniform = require( '@stdlib/random/base/uniform' );
var cinv = require( '@stdlib/math/base/special/cinv' );

var re;
var im;
var z1;
var z2;
var o;
var i;

for ( i = 0; i < 100; i++ ) {
re = round( randu()*100.0 ) - 50.0;
im = round( randu()*100.0 ) - 50.0;
z1 = new Complex128( re, im );

o = cinv( real(z1), imag(z1) );
z2 = new Complex128( o[ 0 ], o[ 1 ] );
z1 = new Complex128( uniform( -50.0, 50.0 ), uniform( -50.0, 50.0 ) );
z2 = cinv( z1 );

console.log( '1.0 / (%s) = %s', z1.toString(), z2.toString() );
}
Expand All @@ -111,6 +97,117 @@ 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/cinv.h"
```

#### stdlib_base_cinv( z )

Computes the inverse 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( 2.0, 4.0 );

stdlib_complex128_t out = stdlib_base_cinv( z );

double re = stdlib_real( out );
// returns 0.1

double im = stdlib_imag( out );
// returns -0.2
```

The function accepts the following arguments:

- **z**: `[in] stdlib_complex128_t` input value.

```c
stdlib_complex128_t stdlib_base_cinv( const stdlib_complex128_t z );
```

</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/cinv.h"
#include "stdlib/complex/float64.h"
#include "stdlib/complex/reim.h"
#include <stdio.h>

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

stdlib_complex128_t v;
stdlib_complex128_t y;
double re1;
double im1;
double re2;
double im2;
int i;
for ( i = 0; i < 4; i++ ) {
v = x[ i ];
y = stdlib_base_cinv( v );
stdlib_reim( v, &re1, &im1 );
stdlib_reim( y, &re2, &im2 );
printf( "cinv(%lf + %lfi) = %lf + %lfi\n", re1, im1, re2, im2 );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

* * *

<section class="references">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,37 @@
// MODULES //

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


// MAIN //

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
re = ( randu()*1000.0 ) - 500.0;
im = ( randu()*1000.0 ) - 500.0;
y = cinv( re, im );
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();
});

bench( pkg+'::memory_reuse', function benchmark( b ) {
var out;
var re;
var im;
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 ) )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
re = ( randu()*1000.0 ) - 500.0;
im = ( randu()*1000.0 ) - 500.0;
y = cinv( out, re, im );
if ( y.length === 0 ) {
b.fail( 'should not be empty' );
y = cinv( values[ i%values.length ] );
if ( isnan( real( y ) ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( !isArray( y ) ) {
b.fail( 'should return an array' );
if ( isnan( imag( y ) ) ) {
b.fail( 'should not return not NaN' );
}
b.pass( 'benchmark finished' );
b.end();
Expand Down
Loading