Skip to content
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

feat: add C ndarray implementation for stats/base/dmeanvarpn #4720

Open
wants to merge 4 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
109 changes: 109 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dmeanvarpn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,115 @@ console.log( out );

<!-- /.examples -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/stats/base/dmeanvarpn.h"
```

#### stdlib_strided_dmeanvarpn( N, correction, \*X, strideX, \*Out, strideOut )

Computes the [mean][arithmetic-mean] and [variance][variance] of a double-precision floating-point strided array using a two-pass algorithm.

```c
double x[] ={ 1.0, -2.0, 2.0 };
double out[2];
stdlib_strided_dmeanvarpn( x.length, 1, x, 1, out, 1 );
// Out = [ ~0.3333, ~4.3333 ]
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **correction**: `[in] double` degrees of freedom adjustment.
- **X**: `[in] double*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
- **Out**: `[in] double*` Output array.
- **strideOut**: `[in] CBLAS_INT` stride length for `Out`.

```c
double stdlib_strided_dmeanvarpn( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, double *Out, const CBLAS_INT strideOut );
```

#### stdlib_strided_dmeanvarpn_ndarray( N, correction, \*X, strideX, offsetX, \*Out, strideOut, offsetOut )

Computes the [mean][arithmetic-mean] and [variance][variance] of a double-precision floating-point strided array using a two-pass algorithm and alternative indexing semantics.

```c
const double x[] = { 1.0, -2.0, 2.0 };

stdlib_strided_dmeanvarpn_ndarray( x.length, 1, x, 1, 0, out, 1, 0 );
// Out = [ ~0.3333, ~4.3333 ]
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **correction**: `[in] double` degrees of freedom adjustment.
- **X**: `[in] double*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
- **offsetX**: `[in] CBLAS_INT` `X` starting index.
- **Out**: `[in] double*` Output array.
- **strideOut**: `[in] CBLAS_INT` stride length for `Out`.
- **offsetOut**: `[in] CBLAS_INT` `Out` starting index.

```c
double stdlib_strided_dmeankbn2_ndarray( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Out, const CBLAS_INT strideOut, const CBLAS_INT offsetOut );
```

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

int main( void ) {
// Create a strided array:
const double x[] = { 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 };

// Specify the number of elements:
const CBLAS_INT N = 4;

// Specify the stride length:
const CBLAS_INT strideX = 2;

// Compute the arithmetic mean:
stdlib_strided_dmeanvarpn_ndarray( N, 1, x, 2, 1, out, 1, 0 );

// Print the result:
printf( "sample mean: %lf\n", out[ 0 ] );
printf( "sample variance: %lf\n", out[ 1 ] );
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

* * *

<section class="references">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +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 pow = require( '@stdlib/math/base/special/pow' );
var Float64Array = require( '@stdlib/array/float64' );
var pkg = require( './../package.json' ).name;
var dmeanvarpn = require( './../lib/dmeanvarpn.js' );


// VARIABLES //

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


// FUNCTIONS //

/**
Expand All @@ -39,15 +45,8 @@ var dmeanvarpn = require( './../lib/dmeanvarpn.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var out;
var x;
var i;

x = new Float64Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
out = new Float64Array( 2 );
var out = uniform( 2, 0.0, 0.0, options );
var x = uniform( len, -10.0, 10.0, options );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@

var resolve = require( 'path' ).resolve;
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 pow = require( '@stdlib/math/base/special/pow' );
var Float64Array = require( '@stdlib/array/float64' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;

Expand All @@ -36,6 +35,9 @@ var dmeanvarpn = tryRequire( resolve( __dirname, './../lib/dmeanvarpn.native.js'
var opts = {
'skip': ( dmeanvarpn instanceof Error )
};
var options = {
'dtype': 'float64'
};


// FUNCTIONS //
Expand All @@ -48,15 +50,8 @@ var opts = {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var out;
var x;
var i;

x = new Float64Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
out = new Float64Array( 2 );
var out = uniform( 2, 0.0, 0.0, options );
var x = uniform( len, -10.0, 10.0, options );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +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 pow = require( '@stdlib/math/base/special/pow' );
var Float64Array = require( '@stdlib/array/float64' );
var pkg = require( './../package.json' ).name;
var dmeanvarpn = require( './../lib/ndarray.js' );


// VARIABLES //

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


// FUNCTIONS //

/**
Expand All @@ -39,15 +45,8 @@ var dmeanvarpn = require( './../lib/ndarray.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var out;
var x;
var i;

x = new Float64Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
out = new Float64Array( 2 );
var out = uniform( 2, 0.0, 0.0, options );
var x = uniform( len, -10.0, 10.0, options );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@

var resolve = require( 'path' ).resolve;
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 pow = require( '@stdlib/math/base/special/pow' );
var Float64Array = require( '@stdlib/array/float64' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;

Expand All @@ -36,6 +35,9 @@ var dmeanvarpn = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' )
var opts = {
'skip': ( dmeanvarpn instanceof Error )
};
var options = {
'dtype': 'float64'
};


// FUNCTIONS //
Expand All @@ -48,15 +50,8 @@ var opts = {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var out;
var x;
var i;

x = new Float64Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
out = new Float64Array( 2 );
var out = uniform( 2, 0.0, 0.0, options );
var x = uniform( len, -10.0, 10.0, options );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static double rand_double( void ) {
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark( int iterations, int len ) {
static double benchmark1( int iterations, int len ) {
double elapsed;
double out[ 2 ];
double x[ len ];
Expand All @@ -109,6 +109,7 @@ static double benchmark( int iterations, int len ) {

t = tic();
for ( i = 0; i < iterations; i++ ) {
// cppcheck-suppress uninitvar
stdlib_strided_dmeanvarpn( len, 1, x, 1, out, 1 );
if ( out[ i%2 ] != out[ i%2 ] ) {
printf( "should not return NaN\n" );
Expand All @@ -122,6 +123,42 @@ static double benchmark( int iterations, int len ) {
return elapsed;
}

/**
* Runs a benchmark.
*
* @param iterations number of iterations
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark2( int iterations, int len ) {
double elapsed;
double out[ 2 ];
double x[ len ];
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
}
out[ 0 ] = 0.0;
out[ 1 ] = 0.0;

t = tic();
for ( i = 0; i < iterations; i++ ) {
// cppcheck-suppress uninitvar
stdlib_strided_dmeanvarpn_ndarray( len, 1, x, 1, 0, out, 1, 0 );
if ( out[ i%2 ] != out[ i%2 ] ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( out[ i%2 ] != out[ i%2 ] ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -144,7 +181,18 @@ int main( void ) {
for ( j = 0; j < REPEATS; j++ ) {
count += 1;
printf( "# c::%s:len=%d\n", NAME, len );
elapsed = benchmark( iter, len );
elapsed = benchmark1( iter, len );
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
}
for ( i = MIN; i <= MAX; i++ ) {
len = pow( 10, i );
iter = ITERATIONS / pow( 10, i-1 );
for ( j = 0; j < REPEATS; j++ ) {
count += 1;
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
elapsed = benchmark2( iter, len );
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
Expand Down
Loading
Loading