Skip to content
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
58 changes: 58 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/gammaincinv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,64 @@ logEachMap( 'p: %0.4f, \t a: %0.4f, \t P^(-1)(p, a): %0.4f', p, a, gammaincinv )

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

<section class="c">

## C API


<pre>
<code>
#include "stdlib/math/base/special/gammaincinv.h"
#include <stdbool.h>

double stdlib_base_special_gammaincinv( const double p, const double a, const bool upper )
</code>
</pre>

<p>
Inverts the lower incomplete gamma function.
</p>

<pre>
<code>
double y = stdlib_base_special_gammaincinv( 0.5, 1.0, false );
// returns ~0.693

y = stdlib_base_special_gammaincinv( 0.1, 1.0, true );
// returns ~2.303
</code>
</pre>

The function accepts the following arguments:

* <b>p</b>: `[in] double` probability value.
* <b>a</b>: `[in] double` scale parameter.
* <b>upper</b>: `[in] bool` boolean indicating if the function should invert the upper tail of the incomplete gamma function instead.

<pre>
<code>
double stdlib_base_special_gammaincinv( const double p, const double a, const bool upper );
</code>
</pre>

<p>

</p>


<pre>
<code>
$ <a href="https://github.com/stdlib-js/stdlib/blob/develop/docs/repl.md">stdlib repl</a>
> #include "stdlib/math/base/special/gammaincinv.h"
> #include <stdio.h>
>
> double y = stdlib_base_special_gammaincinv( 0.8, 2.0, false );
> printf( "y = %f\n", y );
> // => y = 1.213038...
</code>
</pre>


<section class="related">

* * *
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var pow = require( '@stdlib/math/base/special/pow' );
var isFunction = require( '@stdlib/assert/is-function' );
var pkg = require( './../package.json' ).name;
var gammaincinv = require( './../lib/native.js' );


// MAIN //

bench( pkg+'::native', function benchmark( b ) {
var x;
var a;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1.0 );
a = ( randu()*10.0 );
y = gammaincinv( x, a, false );
if ( y !== y ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( y !== y ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::native:upper', function benchmark( b ) {
var x;
var a;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1.0 );
a = ( randu()*10.0 );
y = gammaincinv( x, a, true );
if ( y !== y ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( y !== y ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#
# @license Apache-2.0
#
# Copyright (c) 2025 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Define the Node.js executable:
NODE_EXE ?= node

# Get the project root directory:
PROJECT_ROOT_DIR := $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/gammaincinv' ), '..', '..', '..', '..', '..' ) )" )

# Define the path to the stdlib make includes:
stdlib_make_path := $(PROJECT_ROOT_DIR)/tools/make/lib

include $(stdlib_make_path)/Makefile.c.benchmark

# Define the C source file:
SRC := $(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/gammaincinv/src/main.c

# Define the include directories:
INCLUDE_DIRS := \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/gammaincinv/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/assert/is-nan/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/abs/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/erfcinv/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/exp/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/gamma/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/gammainc/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/gammaln/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/ln/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/pow/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/sqrt/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/constants/float32/max/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/constants/float32/smallest-normal/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/constants/float64/ln-sqrt-two-pi/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/constants/float64/pinf/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/constants/float64/sqrt-two-pi/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/constants/float64/two-pi/include \
$(PROJECT_ROOT_DIR)/lib/node_modules/@stdlib/math/base/special/randu/include

# Add commands for including C source files from dependencies:
include $(stdlib_make_path)/c.dependencies

# Add a command to include the C source file for `is_nan`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/assert/is-nan' ), '..', '..', 'make', 'c.src.mk' ) )" )

# Add a command to include the C source file for `abs`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/abs' ), '..', '..', 'make', 'c.src.mk' ) )" )

# Add a command to include the C source file for `erfcinv`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/erfcinv' ), '..', '..', 'make', 'c.src.mk' ) )" )

# Add a command to include the C source file for `exp`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/exp' ), '..', '..', 'make', 'c.src.mk' ) )" )

# Add a command to include the C source file for `gamma`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/gamma' ), '..', '..', 'make', 'c.src.mk' ) )" )

# Add a command to include the C source file for `gammainc`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/gammainc' ), '..', '..', 'make', 'c.src.mk' ) )" )

# Add a command to include the C source file for `gammaln`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/gammaln' ), '..', '..', 'make', 'c.src.mk' ) )" )

# Add a command to include the C source file for `ln`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/ln' ), '..', '..', 'make', 'c.src.mk' ) )" )

# Add a command to include the C source file for `pow`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/pow' ), '..', '..', 'make', 'c.src.mk' ) )" )

# Add a command to include the C source file for `sqrt`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/sqrt' ), '..', '..', 'make', 'c.src.mk' ) )" )

# Add a command to include the C source file for `randu`:
include $(shell $(NODE_EXE) -e "console.log( require('path').join( require.resolve( '@stdlib/math/base/special/randu' ), '..', '..', 'make', 'c.src.mk' ) )" )
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "stdlib/math/base/special/gammaincinv.h"
#include "stdlib/math/base/special/randu.h"
#include "stdlib/math/base/special/pow.h"
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <sys/time.h>

// MACROS //

#define NAME "gammaincinv"
#define ITERATIONS 1000000
#define REPEATS 3

// FUNCTIONS //

/**
* Returns the current time in nanoseconds.
*/
static uint64_t tic( void ) {
struct timeval tv;
gettimeofday( &tv, NULL );
return (uint64_t)tv.tv_sec * 1000000000L + (uint64_t)tv.tv_usec * 1000L;
}

/**
* Prints the TAP version.
*/
static void print_version( void ) {
printf( "TAP version 13\n" );
}

/**
* Prints the TAP plan.
*/
static void print_plan( const int n ) {
printf( "1..%d\n", n );
}

/**
* Prints the benchmark results.
*/
static void print_results( const char *name, const uint64_t elapsed, const int iterations ) {
double rate = (double)iterations / ( (double)elapsed / 1000000000.0 );
printf( " ---\n" );
printf( " iterations: %d\n", iterations );
printf( " elapsed: %llu\n", elapsed );
printf( " rate: %0.2f\n", rate );
printf( " ...\n" );
}

// MAIN //

int main( void ) {
double x[ ITERATIONS ];
double a[ ITERATIONS ];
double y;
int i;
int j;
uint64_t elapsed;
uint64_t t;

// Use the same values as the JS benchmark:
for ( i = 0; i < ITERATIONS; i++ ) {
x[ i ] = stdlib_base_randu() * 1.0;
a[ i ] = stdlib_base_randu() * 10.0;
}

print_version();
print_plan( REPEATS );

for ( j = 0; j < REPEATS; j++ ) {
t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
y = stdlib_base_special_gammaincinv( x[ i ], a[ i ], false );
if ( y != y ) {
printf( "should not return NaN\n" );
}
}
elapsed = tic() - t;
printf( "ok %d benchmark( %s, %s, %s ) - lower\n", j+1, NAME, "number", "number" );
print_results( NAME, elapsed, ITERATIONS );

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
y = stdlib_base_special_gammaincinv( x[ i ], a[ i ], true );
if ( y != y ) {
printf( "should not return NaN\n" );
}
}
elapsed = tic() - t;
printf( "ok %d benchmark( %s, %s, %s ) - upper\n", j+2, NAME, "number", "number" );
print_results( NAME, elapsed, ITERATIONS );
}
}
Loading
Loading