Skip to content

Commit

Permalink
feat: add C implementation to math/base/special/csignum
Browse files Browse the repository at this point in the history
PR-URL: #1010
Reviewed-by: Athan Reines <[email protected]>
Private-ref: stdlib-js/todo#1454
  • Loading branch information
steff456 authored Jul 14, 2023
1 parent 87620ba commit 35ef1b1
Show file tree
Hide file tree
Showing 25 changed files with 1,537 additions and 415 deletions.
176 changes: 143 additions & 33 deletions lib/node_modules/@stdlib/math/base/special/csignum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ limitations under the License.
-->

# Signum
# csignum

> Evaluate the [signum][signum] function of a complex number.
> Evaluate the [signum][signum] function of a double-precision complex floating-point number.
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

Expand All @@ -40,33 +40,41 @@ limitations under the License.
var csignum = require( '@stdlib/math/base/special/csignum' );
```

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

Evaluates the [signum][signum] function of a `complex` number comprised of a **real** component `re` and an **imaginary** component `im`.
Evaluates the [signum][signum] function of a double-precision complex floating-point number.

```javascript
var v = csignum( -4.2, 5.5 );
// returns [ -0.6069136033622302, 0.79476781392673 ]
var Complex128 = require( '@stdlib/complex/float64' );
var real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );

v = csignum( 0.0, 0.0 );
// returns [ 0.0, 0.0 ]
var v = csignum( new Complex128( -4.2, 5.5 ) );
// returns <Complex128>

v = csignum( NaN, NaN );
// returns [ NaN, NaN ]
```
var re = real( v );
// returns -0.6069136033622302

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.
var im = imag( v );
// returns 0.79476781392673

```javascript
var Float64Array = require( '@stdlib/array/float64' );
v = csignum( new Complex128( 0.0, 0.0 ) );
// returns <Complex128>

var out = new Float64Array( 2 );
re = real( v );
// returns 0.0

var v = csignum( out, -4.2, 5.5 );
// returns <Float64Array>[ -0.6069136033622302, 0.79476781392673 ]
im = imag( v );
// returns 0.0

var bool = ( v === out );
// returns true
v = csignum( new Complex128( NaN, NaN ) );
// returns <Complex128>

re = real( v );
// returns NaN

im = imag( v );
// returns NaN
```

</section>
Expand All @@ -90,33 +98,135 @@ var bool = ( v === out );
<!-- eslint no-undef: "error" -->

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

var re;
var im;
var rand = uniform( -50.0, 50.0 );

var z;
var o;
var w;
var i;

for ( i = 0; i < 100; i++ ) {
re = ( randu()*100.0 ) - 50.0;
im = ( randu()*100.0 ) - 50.0;
z = new Complex128( re, im );
o = csignum( real(z), imag(z) );
w = new Complex128( o[ 0 ], o[ 1 ] );
console.log( 'signum(%s) = %s', z.toString(), w.toString() );
z = new Complex128( rand(), rand() );
console.log( 'csignum(%s) = %s', z, csignum( z ) );
}
```

</section>

<!-- /.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/csignum.h"
```

#### stdlib_base_csignum( z )

Evaluates the [signum][signum] function 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( -4.2, 5.5 );

stdlib_complex128_t out = stdlib_base_csignum( z );

double re = stdlib_real( out );
// returns -0.6069136033622302

double im = stdlib_imag( out );
// returns 0.79476781392673
```

The function accepts the following arguments:

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

```c
stdlib_complex128_t stdlib_base_csignum( 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/csignum.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.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_csignum( v );
stdlib_reim( v, &re1, &im1 );
stdlib_reim( y, &re2, &im2 );
printf( "csignum(%lf + %lfi) = %lf + %lfi\n", re1, im1, re2, im2 );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- 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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,113 +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 cabs = require( '@stdlib/math/base/special/cabs' );
var real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );
var pkg = require( './../package.json' ).name;
var csignum = 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 = csignum( 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 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
re = ( randu()*1000.0 ) - 500.0;
im = ( randu()*1000.0 ) - 500.0;
y = csignum( out, 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+'::manual', function benchmark( b ) {
var az;
var re;
var im;
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;
az = cabs( new Complex128( re, im ) );
y = [ re/az, im/az ];
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+'::manual,memory_reuse', function benchmark( b ) {
var az;
var re;
var im;
var y;
var i;

y = 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;
az = cabs( new Complex128( re, im ) );
y[ 0 ] = re / az;
y[ 1 ] = im / az;
if ( y.length === 0 ) {
b.fail( 'should not be empty' );
y = csignum( 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

0 comments on commit 35ef1b1

Please sign in to comment.