Skip to content

Commit 167fc92

Browse files
bench: refactor C benchmark and update main.c formating
1 parent e5fdc8d commit 167fc92

4 files changed

Lines changed: 43 additions & 24 deletions

File tree

lib/node_modules/@stdlib/math/base/special/kernel-betainc/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ Evaluates the incomplete beta function and its first derivative.
167167

168168
```c
169169
double out;
170-
double deriv;
170+
double derivative;
171171

172-
stdlib_base_kernel_betainc( 0.5, 2.0, 5.0, true, false, &out, &deriv );
172+
stdlib_base_kernel_betainc( 0.2, 1.0, 2.0, true, false, &out, &derivative );
173173
```
174174
175175
The function accepts the following arguments:
@@ -212,14 +212,13 @@ void stdlib_base_kernel_betainc( double x, double a, double b, const bool regula
212212
#include <stdbool.h>
213213

214214
int main( void ) {
215-
216-
double out;
215+
struct BasePRNGObject *obj = stdlib_base_random_randu_allocate( 0 );
217216
double deriv;
217+
double out;
218+
int32_t i;
218219
double x;
219220
double a;
220221
double b;
221-
int32_t i;
222-
struct BasePRNGObject *obj = stdlib_base_random_randu_allocate( 0 );
223222

224223
for ( i = 0; i < 100; i++ ) {
225224
x = stdlib_base_random_randu( obj );

lib/node_modules/@stdlib/math/base/special/kernel-betainc/benchmark/c/native/benchmark.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/kernel_betainc.h"
20+
#include "stdlib/constants/float64/eps.h"
2021
#include <stdlib.h>
2122
#include <stdio.h>
2223
#include <stdbool.h>
@@ -77,13 +78,15 @@ static double tic( void ) {
7778
}
7879

7980
/**
80-
* Generates a random number on the interval [0,1).
81+
* Generates a random number on the interval [min,max).
8182
*
82-
* @return random number
83+
* @param min minimum value (inclusive)
84+
* @param max maximum value (exclusive)
85+
* @return random number
8386
*/
84-
static double rand_double( void ) {
85-
int r = rand();
86-
return (double)r / ( (double)RAND_MAX + 1.0 );
87+
static double random_uniform( const double min, const double max ) {
88+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
89+
return min + ( v*(max-min) );
8790
}
8891

8992
/**
@@ -93,19 +96,23 @@ static double rand_double( void ) {
9396
*/
9497
static double benchmark( void ) {
9598
double x[ 100 ];
99+
double a[ 100 ];
100+
double b[ 100 ];
96101
double elapsed;
97-
double out;
98102
double deriv;
103+
double out;
99104
double t;
100105
int i;
101106

102107
for ( i = 0; i < 100; i++ ) {
103-
x[ i ] = rand_double();
108+
x[ i ] = random_uniform( 0.0, 1.0 );
109+
a[ i ] = random_uniform( STDLIB_FLOAT64_EPS, 100.0 );
110+
b[ i ] = random_uniform( STDLIB_FLOAT64_EPS, 100.0 );
104111
}
105112

106113
t = tic();
107114
for ( i = 0; i < ITERATIONS; i++ ) {
108-
stdlib_base_kernel_betainc( x[ i % 100 ], 2.0, 5.0, true, false, &out, &deriv );
115+
stdlib_base_kernel_betainc( x[ i % 100 ], a[ i % 100 ], b[ i % 100 ], true, false, &out, &deriv );
109116
if ( out != out || deriv != deriv ) {
110117
printf( "unexpected results\n" );
111118
break;

lib/node_modules/@stdlib/math/base/special/kernel-betainc/examples/c/example.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
#include <stdbool.h>
2424

2525
int main( void ) {
26-
27-
double out;
26+
struct BasePRNGObject *obj = stdlib_base_random_randu_allocate( 0 );
2827
double deriv;
28+
double out;
2929
int32_t i;
3030
double x;
3131
double a;
3232
double b;
33-
struct BasePRNGObject *obj = stdlib_base_random_randu_allocate( 0 );
33+
3434
for ( i = 0; i < 100; i++ ) {
3535
x = stdlib_base_random_randu( obj );
3636
a = stdlib_base_random_randu( obj ) * 10.0;

lib/node_modules/@stdlib/math/base/special/kernel-betainc/src/main.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#include <stdint.h>
5555
#include <stddef.h>
5656

57-
// VARIABLES //
5857
static const double ONE_OVER_PI = 1.0 / STDLIB_CONSTANT_FLOAT64_PI;
5958

6059
/**
@@ -67,6 +66,7 @@ static const double ONE_OVER_PI = 1.0 / STDLIB_CONSTANT_FLOAT64_PI;
6766
static double fullIGammaPrefix( const double a, const double z ) {
6867
double prefix;
6968
double alz;
69+
7070
alz = a * stdlib_base_ln( z );
7171
if ( z >= 1.0 ) {
7272
if ( ( alz < STDLIB_CONSTANT_FLOAT64_MAX_LN ) && ( -z > STDLIB_CONSTANT_FLOAT64_MIN_LN ) ) {
@@ -101,6 +101,7 @@ static double regularizedGammaPrefix( const double a, const double z ) {
101101
double amz;
102102
double sq;
103103
double d;
104+
104105
agh = a + STDLIB_CONSTANT_FLOAT64_GAMMA_LANCZOS_G - 0.5;
105106
d = ( ( z - a ) - STDLIB_CONSTANT_FLOAT64_GAMMA_LANCZOS_G + 0.5 ) / agh;
106107
if ( a < 1.0 ) {
@@ -158,6 +159,7 @@ static void ibetaFraction2t( const double a, const double b, const double x, dou
158159
double denom;
159160
double aN;
160161
double bN;
162+
161163
aN = ( a + ( *m ) - 1 ) * ( a + b + ( *m ) - 1 ) * ( *m ) * ( b - ( *m ) ) * x * x;
162164
denom = a + ( 2.0 * ( *m ) ) - 1.0;
163165
aN /= denom * denom;
@@ -177,6 +179,7 @@ static double ibetaFraction2CF( const double a, const double b, const double x,
177179
double D;
178180
double f;
179181
double m;
182+
180183
m = 0.0;
181184
ibetaFraction2t( a, b, x, &m, y, outTerms );
182185
f = outTerms[ 1 ];
@@ -262,6 +265,7 @@ static double ibetaPowerTerms( const double a, const double b, const double x, c
262265
double b2;
263266
double c;
264267
double l;
268+
265269
if ( !normalized ) {
266270
// Can we do better here?
267271
return stdlib_base_pow( x, a ) * stdlib_base_pow( y, b );
@@ -386,6 +390,7 @@ static double ibetaPowerTerms( const double a, const double b, const double x, c
386390
static double ibetaFraction2( const double a, const double b, const double x, const double y, const bool normalized, double *out ) {
387391
double result;
388392
double fract;
393+
389394
result = ibetaPowerTerms( a, b, x, y, normalized );
390395
if ( out ) {
391396
*out = result;
@@ -410,8 +415,8 @@ static double ibetaFraction2( const double a, const double b, const double x, co
410415
* @return function value
411416
*/
412417
static double betaSmallBLargeASeries( const double a, const double b, const double x, const double y, const double s0, const double mult, const bool normalized ) {
413-
double prefix;
414418
double p[ 30 ];
419+
double prefix;
415420
double tmp1;
416421
double tnp1;
417422
double sum;
@@ -422,13 +427,14 @@ static double betaSmallBLargeASeries( const double a, const double b, const doub
422427
double mbn;
423428
double lx;
424429
double t4;
425-
double h;
426-
double j;
427430
int32_t m;
428431
int32_t n;
432+
double h;
433+
double j;
429434
double r;
430435
double t;
431436
double u;
437+
432438
// Some values we'll need later, these are Eq 9.1:
433439
bm1 = b - 1.0;
434440
t = a + ( bm1 / 2.0 );
@@ -511,6 +517,7 @@ static double ibetaAStep( const double a, const double b, const double x, const
511517
double term;
512518
double sum;
513519
double i;
520+
514521
prefix = ibetaPowerTerms( a, b, x, y, normalized );
515522
if ( out ) {
516523
*out = prefix;
@@ -551,6 +558,7 @@ static double ibetaAStep( const double a, const double b, const double x, const
551558
static double risingFactorialRatio( const double a, const double b, const double k ) {
552559
double result;
553560
double i;
561+
554562
if ( k == 0 ) {
555563
return 1.0;
556564
}
@@ -572,6 +580,7 @@ static double risingFactorialRatio( const double a, const double b, const double
572580
*/
573581
static double ibetaSeriesT( double *a, const double b, const double x, double *poch, int32_t *n, double *result ) {
574582
double r;
583+
575584
r = ( *result ) / ( *a );
576585
*a += 1.0;
577586
*result *= ( *poch ) * x / (double)( *n );
@@ -595,6 +604,7 @@ static double binomialCCDF( const double n, const double k, const double x, cons
595604
double start;
596605
double term;
597606
double i;
607+
598608
result = stdlib_base_pow( x, n );
599609
if ( result > STDLIB_CONSTANT_FLOAT64_SMALLEST_NORMAL ) {
600610
term = result;
@@ -646,19 +656,20 @@ static double binomialCCDF( const double n, const double k, const double x, cons
646656
* @return function value
647657
*/
648658
static double ibetaSeries( const double a, const double b, const double x, const double s0, const bool normalized, double *out, const double y ) {
659+
int32_t counter;
649660
double result;
661+
double poch;
650662
double agh;
651663
double bgh;
652664
double cgh;
665+
double sum;
653666
double ac;
654-
double poch;
655667
int32_t n;
656-
int32_t counter;
657668
double l1;
658669
double l2;
659670
double c;
660671
double r;
661-
double sum;
672+
662673
if ( normalized ) {
663674
c = a + b;
664675
// Incomplete beta power term, combined with the Lanczos approximation:
@@ -721,6 +732,7 @@ static double ibetaSeries( const double a, const double b, const double x, const
721732
* @example
722733
* double out;
723734
* double derivative;
735+
*
724736
* stdlib_base_kernel_betainc( 0.2, 1.0, 2.0, true, false, &out, &derivative );
725737
*/
726738
void stdlib_base_kernel_betainc( double x, double a, double b, const bool regularized, bool upper, double *out, double *derivative ) {
@@ -734,6 +746,7 @@ void stdlib_base_kernel_betainc( double x, double a, double b, const bool regula
734746
double n;
735747
double p;
736748
double y;
749+
737750
y = 1.0 - x;
738751
// Derivative not set...
739752
*derivative = -1;

0 commit comments

Comments
 (0)