Skip to content

feat: add C implementation for math/base/special/gammainc #4790

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

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
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
92 changes: 92 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/gammainc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,98 @@ 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/gammainc.h"
```

#### stdlib_base_gammainc( x, s, regularized, upper )

Computes the regularized lower [incomplete gamma function][incomplete-gamma-function] for inputs `x` and `s`. .

```c
double out = stdlib_base_gammainc( 1.0, 2.0, true, true );
// returns ~0.7358
```

The function accepts the following arguments:

- **x**: `[in] double` input value.
- **s**: `[in] double` input value.
- **regularized**: `[in] bool` indicating if the function should evaluate the regularized or non-regularized incomplete gamma functions.
- **upper**: `[in] bool` indicating if the function should return the upper tail of the incomplete gamma function

```c
double stdlib_base_gammainc( double x, double a, bool regularized, bool upper );
```

</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/gammainc.h"
#include <stdio.h>
#include <stdbool.h>

int main( void ) {
const double x[] = { 6.0, 1.0, 7.0, 7.0, 0.0/0.0, 6.0 };
const double a[] = { 2.0, 2.0, 5.0, 5.0, 2.0, 0.0/0.0 };
const bool regularized[] = { true, true, true, false, true, true };
const bool upper[] = { false, true, false, false, false, false };

double y;
int i;
for ( i = 0; i <6; i++ ) {
y = stdlib_base_gammainc( x[ i ], a[ i ], regularized[ i ], upper[ i ] );
printf( "gammainc( %lf, %lf, %s, %s ) = %lf\n", x[ i ], a[ i ], regularized[ i ] ? "true" : "false", upper[ i ] ? "true" : "false", y );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

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

<section class="related">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;
var gammainc = require( './../lib' );


// VARIABLES //

var options = {
'dtype': 'float64'
};


// MAIN //

bench( pkg, function benchmark( b ) {
Expand All @@ -36,11 +43,12 @@ bench( pkg, function benchmark( b ) {
var z;
var i;

x = uniform( b.iterations, 10, 1000, options );
y = uniform( b.iterations, EPS, 1000, options );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 0.0;
y = ( randu()*1000.0 ) + EPS;
z = gammainc( x, y );
z = gammainc( x[ i ], y[ i ] );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -59,11 +67,12 @@ bench( pkg+':regularized=true', function benchmark( b ) {
var z;
var i;

x = uniform( b.iterations, 0, 1000, options );
y = uniform( b.iterations, EPS, 1000, options );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 0.0;
y = ( randu()*1000.0 ) + EPS;
z = gammainc( x, y, true );
z = gammainc( x[ i ], y[ i ], true );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -82,11 +91,12 @@ bench( pkg+':regularized=false', function benchmark( b ) {
var z;
var i;

x = uniform( b.iterations, 0, 1000, options );
y = uniform( b.iterations, EPS, 1000, options );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 0.0;
y = ( randu()*1000.0 ) + EPS;
z = gammainc( x, y, false );
z = gammainc( x[ i ], y[ i ], false );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -105,11 +115,12 @@ bench( pkg+':regularized=true,upper=true', function benchmark( b ) {
var z;
var i;

x = uniform( b.iterations, 0, 1000, options );
y = uniform( b.iterations, EPS, 1000, options );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 0.0;
y = ( randu()*1000.0 ) + EPS;
z = gammainc( x, y, true, true );
z = gammainc( x[ i ], y[ i ], true, true );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -128,11 +139,12 @@ bench( pkg+':regularized=true,upper=false', function benchmark( b ) {
var z;
var i;

x = uniform( b.iterations, 0, 1000, options );
y = uniform( b.iterations, EPS, 1000, options );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 0.0;
y = ( randu()*1000.0 ) + EPS;
z = gammainc( x, y, true, false );
z = gammainc( x[ i ], y[ i ], true, false );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -151,11 +163,12 @@ bench( pkg+':regularized=false,upper=true', function benchmark( b ) {
var z;
var i;

x = uniform( b.iterations, 0, 1000, options );
y = uniform( b.iterations, EPS, 1000, options );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 0.0;
y = ( randu()*1000.0 ) + EPS;
z = gammainc( x, y, false, true );
z = gammainc( x[ i ], y[ i ], false, true );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -174,11 +187,12 @@ bench( pkg+':regularized=false,upper=false', function benchmark( b ) {
var z;
var i;

x = uniform( b.iterations, 0, 1000, options );
y = uniform( b.iterations, EPS, 1000, options );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 0.0;
y = ( randu()*1000.0 ) + EPS;
z = gammainc( x, y, false, false );
z = gammainc( x[ i ], y[ i ], false, false );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Loading