Skip to content

bench: refactor random number generation math/base/special/cexp* #5888

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

Merged
merged 6 commits into from
Apr 3, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,22 @@ static double rand_double( void ) {
* @return elapsed time in seconds
*/
static double benchmark( void ) {
double re[ 100 ];
double im[ 100 ];
double elapsed;
double re;
double im;
double t;
int i;

double complex z;

for ( i = 0; i < 100; i++ ) {
re[ i ] = ( 100.0*rand_double() ) - 50.0;
im[ i ] = ( 100.0*rand_double() ) - 50.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
re = ( 100.0*rand_double() ) - 50.0;
im = ( 100.0*rand_double() ) - 50.0;

z = cexp( re + I * im );
z = cexp( re[ i%100 ] + I * im[ i%100 ] );
if ( z != z ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,23 @@ static double rand_double( void ) {
* @return elapsed time in seconds
*/
static double benchmark( void ) {
double v[ 100 ];
double elapsed;
double re;
double im;
double t;
double v;
int i;

stdlib_complex128_t x;
stdlib_complex128_t y;

for ( i = 0; i < 100; i++ ) {
v[ i ] = ( 1000.0*rand_double() ) - 500.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
v = ( 1000.0*rand_double() ) - 500.0;
x = stdlib_complex128( v, v );
x = stdlib_complex128( v[ i%100 ], v[ i%100 ] );
y = stdlib_base_cexp( x );
stdlib_complex128_reim( y, &re, &im );
if ( re != re ) {
Expand Down
32 changes: 16 additions & 16 deletions lib/node_modules/@stdlib/math/base/special/cexp/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,17 @@ tape( 'if imaginary component is `+Infinity`, the function returns a complex num
var v;

v = cexp( new Complex128( 0.0, PINF ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
t.end();
});

tape( 'if imaginary component is `-Infinity`, the function returns a complex number having `NaN` components', function test( t ) {
var v;

v = cexp( new Complex128( 0.0, NINF ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
t.end();
});

Expand All @@ -167,7 +167,7 @@ tape( 'if real component is `NaN` and imaginary component is `0.0`, the function
var v;

v = cexp( new Complex128( NaN, 0.0 ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( imag( v ), 0.0, 'returns 0.0' );
t.end();
});
Expand All @@ -176,16 +176,16 @@ tape( 'if imaginary component is `NaN`, the function returns a complex number wi
var v;

v = cexp( new Complex128( 5.0, NaN ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );

v = cexp( new Complex128( 0.0, NaN ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );

v = cexp( new Complex128( NaN, NaN ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );

t.end();
});
Expand All @@ -210,16 +210,16 @@ tape( 'if real component is `NaN` and imaginary component is nonzero, the functi
var v;

v = cexp( new Complex128( NaN, 3.0 ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
t.end();
});

tape( 'if real component is `NaN` and imaginary component is `+0.0`, the function returns a complex number with an imaginary component equal to `+0.0`', function test( t ) {
var v;

v = cexp( new Complex128( NaN, 0.0 ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isPositiveZero( imag( v ) ), true, 'returns +0.0' );
t.end();
});
Expand All @@ -228,7 +228,7 @@ tape( 'if real component is `NaN` and imaginary component is `-0.0`, the functio
var v;

v = cexp( new Complex128( NaN, -0.0 ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isNegativeZero( imag( v ) ), true, 'returns +0.0' );
t.end();
});
Expand All @@ -238,7 +238,7 @@ tape( 'if real component is `+Infinity` and imaginary component is `+Infinity`,

v = cexp( new Complex128( PINF, PINF ) );
t.strictEqual( real( v ), NINF, 'returns -Infinity' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
t.end();
});

Expand Down
32 changes: 16 additions & 16 deletions lib/node_modules/@stdlib/math/base/special/cexp/test/test.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,17 @@ tape( 'if imaginary component is `+Infinity`, the function returns a complex num
var v;

v = cexp( new Complex128( 0.0, PINF ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
t.end();
});

tape( 'if imaginary component is `-Infinity`, the function returns a complex number having `NaN` components', opts, function test( t ) {
var v;

v = cexp( new Complex128( 0.0, NINF ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
t.end();
});

Expand All @@ -176,7 +176,7 @@ tape( 'if real component is `NaN` and imaginary component is `0.0`, the function
var v;

v = cexp( new Complex128( NaN, 0.0 ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( imag( v ), 0.0, 'returns 0.0' );
t.end();
});
Expand All @@ -185,16 +185,16 @@ tape( 'if imaginary component is `NaN`, the function returns a complex number wi
var v;

v = cexp( new Complex128( 5.0, NaN ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );

v = cexp( new Complex128( 0.0, NaN ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );

v = cexp( new Complex128( NaN, NaN ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );

t.end();
});
Expand All @@ -219,16 +219,16 @@ tape( 'if real component is `NaN` and imaginary component is nonzero, the functi
var v;

v = cexp( new Complex128( NaN, 3.0 ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
t.end();
});

tape( 'if real component is `NaN` and imaginary component is `+0.0`, the function returns a complex number with an imaginary component equal to `+0.0`', opts, function test( t ) {
var v;

v = cexp( new Complex128( NaN, 0.0 ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isPositiveZero( imag( v ) ), true, 'returns +0.0' );
t.end();
});
Expand All @@ -237,7 +237,7 @@ tape( 'if real component is `NaN` and imaginary component is `-0.0`, the functio
var v;

v = cexp( new Complex128( NaN, -0.0 ) );
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
t.strictEqual( isNegativeZero( imag( v ) ), true, 'returns +0.0' );
t.end();
});
Expand All @@ -247,7 +247,7 @@ tape( 'if real component is `+Infinity` and imaginary component is `+Infinity`,

v = cexp( new Complex128( PINF, PINF ) );
t.strictEqual( real( v ), NINF, 'returns -Infinity' );
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
t.end();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,24 @@ static double rand_double( void ) {
* @return elapsed time in seconds
*/
static double benchmark( void ) {
double v[ 100 ];
double elapsed;
double re;
double im;
double t;
double v;
int i;

stdlib_complex128_t x;
stdlib_complex128_t y;

for ( i = 0; i < 100; i++ ) {
v[ i ] = ( 1000.0*rand_double() ) - 500.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
v = ( 1000.0*rand_double() ) - 500.0;
x = stdlib_complex128( v, v );
y = stdlib_base_cflipsign( x, -v );
x = stdlib_complex128( v[ i%100 ], v[ i%100 ] );
y = stdlib_base_cflipsign( x, -v[ i%100 ] );
stdlib_complex128_reim( y, &re, &im );
if ( re != re ) {
printf( "unexpected result\n" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,21 @@ static float rand_float( void ) {
* @return elapsed time in seconds
*/
static double benchmark( void ) {
float v[ 100 ];
float complex x;
float complex y;
double elapsed;
double t;
float v;
int i;

for ( i = 0; i < 100; i++ ) {
v[ i ] = ( 1000.0f*rand_float() ) - 500.0f;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
v = ( 1000.0f*rand_float() ) - 500.0f;
x = v + v*I;
y = stdlib_base_cflipsignf( x, -v );
x = v[ i%100 ] + v[ i%100 ]*I;
y = stdlib_base_cflipsignf( x, -v[ i%100 ] );
if ( crealf( y ) != crealf( y ) ) {
printf( "unexpected result\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var randu = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var real = require( '@stdlib/complex/float64/real' );
Expand Down Expand Up @@ -66,11 +66,12 @@ bench( pkg+'::manual', function benchmark( b ) {
var y;
var i;

re = randu( 10, -500.0, 500.0 );
im = randu( 10, -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 = [ floor( re ), floor( im ) ];
y = [ floor( re[ i%re.length ] ), floor( im[ i%im.length ] ) ];
if ( y.length === 0 ) {
b.fail( 'should not be empty' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,23 @@ static double rand_double( void ) {
* @return elapsed time in seconds
*/
static double benchmark( void ) {
double re[ 100 ];
double im[ 100 ];
double elapsed;
double re;
double im;
double t;
int i;

double complex z;
double complex y;

for ( i = 0; i < 100; i++ ) {
re[ i ] = ( 1000.0*rand_double() ) - 500.0;
im[ i ] = ( 1000.0*rand_double() ) - 500.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
re = ( 1000.0*rand_double() ) - 500.0;
im = ( 1000.0*rand_double() ) - 500.0;
z = re + im*I;
z = re[ i%100 ] + im[ i%100 ]*I;
y = floor( creal(z) ) + floor( cimag(z) )*I;
if ( y != y ) {
printf( "should not return NaN\n" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,23 @@ static double rand_double( void ) {
* @return elapsed time in seconds
*/
static double benchmark( void ) {
double v[ 100 ];
double elapsed;
double re;
double im;
double t;
double v;
int i;

stdlib_complex128_t x;
stdlib_complex128_t y;

for ( i = 0; i < 100; i++ ) {
v[ i ] = ( 1000.0*rand_double() ) - 500.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
v = ( 1000.0*rand_double() ) - 500.0;
x = stdlib_complex128( v, v );
x = stdlib_complex128( v[ i%100 ], v[ i%100 ] );
y = stdlib_base_cfloorn( x, -2 );
stdlib_complex128_reim( y, &re, &im );
if ( re != re ) {
Expand Down
Loading