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
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,106 @@ for ( i = 0; i < 10; 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/stats/base/dists/studentized_range/quantile.h"
```

#### stdlib_base_dists_studentized_range_quantile( p, r, v, nranges )

Evaluates the quantile function for a studentized range distribution.

```c
double out = stdlib_base_dists_studentized_range_quantile( 0.5, 3.0, 2.0, 1 );
// returns ~1.908
```

The function accepts the following arguments:

- **p**: `[in] double` input probability.
- **r**: `[in] double` sample size for range (must be >= 2).
- **v**: `[in] double` degrees of freedom (must be >= 2).
- **nranges**: `[in] int32_t` number of groups whose maximum range is considered.

```c
double stdlib_base_dists_studentized_range_quantile( const double p, const double r, const double v, const int32_t nranges );
```

</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/stats/base/dists/studentized_range/quantile.h"
#include <stdlib.h>
#include <stdio.h>

static double random_double( void ) {
int r = rand();
return (double)r / ( (double)RAND_MAX + 1.0 );
}

int main( void ) {
double p;
double r;
double v;
double y;
int i;

r = 3.0;
v = 5.0;

for ( i = 0; i < 25; i++ ) {
p = random_double();
y = stdlib_base_dists_studentized_range_quantile( p, r, v, 1 );
printf( "p: %lf, r: %lf, v: %lf, Q(p;r,v): %lf\n", p, r, v, 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
@@ -0,0 +1,65 @@
/**
* @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 resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var quantile = tryRequire( resolve( __dirname, '..', 'lib', 'native.js' ) );
var opts = {
'skip': ( quantile instanceof Error )
};


// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var r;
var v;
var p;
var y;
var i;

r = 3.0;
v = 5.0;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
p = randu();
y = quantile( p, r, v );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( 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,44 @@
#/
# @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.
#/

# TARGETS #

# Run benchmark.
#
# This target runs a benchmark.

benchmark: benchmark.out
-$< | $( BENCHMARK_FILTER )

.PHONY: benchmark

# Generate a benchmark executable.
#
# This target generates a benchmark executable.

benchmark.out: benchmark.c
$(QUIET) $(CC) $(CFLAGS) -o $@ $(INCLUDE) -I $(ROOT_DIR)/lib/node_modules/@stdlib/stats/base/dists/studentized-range/quantile/include $< $(LIBPATH) $(LIBRARIES)

# Clean.
#
# This target removes generated files.

clean:
$(QUIET) -rm -f *.o *.out

.PHONY: clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* @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/stats/base/dists/studentized_range/quantile.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sys/time.h>

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

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

/**
* Prints the TAP summary.
*
* @param total total number of tests
* @param passing total number of passing tests
*/
static void print_summary( int total, int passing ) {
printf( "#\n" );
printf( "1..%d\n", total );
printf( "# total %d\n", total );
printf( "# pass %d\n", passing );
printf( "#\n" );
printf( "# ok\n" );
}

/**
* Prints benchmarks results.
*
* @param elapsed elapsed time in seconds
*/
static void print_results( double elapsed ) {
double rate = (double)ITERATIONS / elapsed;
printf( " ---\n" );
printf( " iterations: %d\n", ITERATIONS );
printf( " elapsed: %0.9f\n", elapsed );
printf( " rate: %0.9f\n", rate );
printf( " ...\n" );
}

/**
* Returns a clock time.
*
* @return clock time
*/
static double tic( void ) {
struct timeval now;
gettimeofday( &now, NULL );
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
}

/**
* Generates a pseudorandom double on the interval [0,1].
*
* @return pseudorandom double
*/
static double rand_double( void ) {
int r = rand();
return (double)r / ( (double)RAND_MAX + 1.0 );
}

/**
* Runs a benchmark.
*
* @return elapsed time in seconds
*/
static double benchmark( void ) {
double elapsed;
double p;
double y;
double t;
int i;

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
p = rand_double();
y = stdlib_base_dists_studentized_range_quantile( p, 3.0, 5.0, 1 );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( y != y ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
int main( void ) {
double elapsed;
int i;

print_version();
for ( i = 0; i < REPEATS; i++ ) {
printf( "# c::%s\n", NAME );
elapsed = benchmark();
print_results( elapsed );
printf( "ok %d benchmark finished\n", i+1 );
}
print_summary( REPEATS, REPEATS );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"targets": [
{
"target_name": "addon",
"sources": [
"src/addon.cpp",
"src/quantile.c"
],
"include_dirs": [
"include",
"<!@(node -p \"require('@stdlib/utils-library-manifest').include()\")"
],
"libraries": [
"<!@(node -p \"require('@stdlib/utils-library-manifest').libraries()\")"
],
"ldflags": [
"<!@(node -p \"require('@stdlib/utils-library-manifest').ldflags()\")"
]
}
]
}
Loading