Skip to content

feat: add C ndarray interface and refactor implementation for stats/base/dnanmeanors #4248

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

Merged
merged 27 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c60ec02
refactor: replace built-ins by stdlib packages, update benchmarks in …
gunjjoshi Dec 18, 2024
bc629b6
feat: ndarray-dnanmeanors
Neerajpathak07 Dec 26, 2024
b06689b
fix: update manifest
Neerajpathak07 Dec 26, 2024
880ab1f
Merge branch 'develop' into ndarray-dnanmeanors
Neerajpathak07 Dec 26, 2024
e02a08b
fix: update manifest
Neerajpathak07 Dec 26, 2024
59cbb55
fix: lint error
Neerajpathak07 Dec 26, 2024
48bef6d
fix: lint error
Neerajpathak07 Dec 26, 2024
3abacf6
fix: lint error
Neerajpathak07 Dec 26, 2024
4d13c72
chore: adding essential files
Neerajpathak07 Dec 30, 2024
f497d31
chore: minor clean up
Neerajpathak07 Jan 2, 2025
13b3a4f
fix: lint copyright years
Neerajpathak07 Jan 2, 2025
75ddeb9
chore: bench clean up
Neerajpathak07 Jan 3, 2025
c884529
chore: changes from code review
Neerajpathak07 Jan 4, 2025
d493d42
chore: minor clean up
Neerajpathak07 Jan 18, 2025
884a28e
chore: update copyright years
stdlib-bot Jan 18, 2025
57f9dfc
chore: update addon
Neerajpathak07 Jan 18, 2025
dbe831f
chore: update retrun values
Neerajpathak07 Jan 19, 2025
b8814c6
chore: minor changes in bench
Neerajpathak07 Feb 1, 2025
681c57a
chore: handling NaN elements
Neerajpathak07 Feb 1, 2025
952bf78
fix: eslint max-len
Neerajpathak07 Feb 1, 2025
306ce69
fix: eslint max-len
Neerajpathak07 Feb 1, 2025
8ce4612
chore: update copyright years
Neerajpathak07 Feb 2, 2025
7f40a2a
chore: update copyright years
stdlib-bot Feb 2, 2025
599d246
chore: changes from code review
Neerajpathak07 Feb 3, 2025
2068af1
fix: lint error
Neerajpathak07 Feb 3, 2025
0d4a877
refactor: update bench file
Neerajpathak07 Feb 3, 2025
d17d759
docs: fix example
kgryte Feb 9, 2025
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
144 changes: 120 additions & 24 deletions lib/node_modules/@stdlib/stats/base/dnanmeanors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,84 +51,79 @@ The [arithmetic mean][arithmetic-mean] is defined as
var dnanmeanors = require( '@stdlib/stats/base/dnanmeanors' );
```

#### dnanmeanors( N, x, stride )
#### dnanmeanors( N, x, strideX )

Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array `x`, ignoring `NaN` values and using ordinary recursive summation.

```javascript
var Float64Array = require( '@stdlib/array/float64' );

var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
var N = x.length;

var v = dnanmeanors( N, x, 1 );
var v = dnanmeanors( x.length, x, 1 );
// returns ~0.3333
```

The function has the following parameters:

- **N**: number of indexed elements.
- **x**: input [`Float64Array`][@stdlib/array/float64].
- **stride**: index increment for `x`.
- **strideX**: stride length for `x`.

The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`,

The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`,
<!-- eslint-disable max-len -->

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN ] );
var N = floor( x.length / 2 );
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ] );

var v = dnanmeanors( N, x, 2 );
var v = dnanmeanors( 5, x, 2 );
// returns 1.25
```

Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.

<!-- eslint-disable stdlib/capitalized-comments -->
<!-- eslint-disable stdlib/capitalized-comments, max-len -->

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

var N = floor( x0.length / 2 );

var v = dnanmeanors( N, x1, 2 );
var v = dnanmeanors( 5, x1, 2 );
// returns 1.25
```

#### dnanmeanors.ndarray( N, x, stride, offset )
#### dnanmeanors.ndarray( N, x, strideX, offsetX )

Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.

```javascript
var Float64Array = require( '@stdlib/array/float64' );

var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
var N = x.length;

var v = dnanmeanors.ndarray( N, x, 1, 0 );
var v = dnanmeanors.ndarray( x.length, x, 1, 0 );
// returns ~0.33333
```

The function has the following additional parameters:

- **offset**: starting index for `x`.
- **offsetX**: starting index for `x`.

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [arithmetic mean][arithmetic-mean] for every other element in `x` starting from the second element

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [arithmetic mean][arithmetic-mean] for every other value in `x` starting from the second value
<!-- eslint-disable max-len -->

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
var N = floor( x.length / 2 );
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );

var v = dnanmeanors.ndarray( N, x, 2, 1 );
var v = dnanmeanors.ndarray( 5, x, 2, 1 );
// returns 1.25
```

Expand Down Expand Up @@ -181,6 +176,107 @@ console.log( v );

<!-- /.examples -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

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

#### stdlib_strided_dnanmeanors( N, \*X, strideX )

Computes the arithmetic mean of a double-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation.

```c
const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 };

double v = stdlib_strided_dnanmeanors( 6, x, 2 );
// returns ~4.6667
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] double*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.

```c
double stdlib_strided_dnanmeanors( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
```

#### stdlib_strided_dnanmeanors_ndarray( N, \*X, strideX, offsetX )

Computes the arithmetic mean of a double-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.

```c
const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 };

double v = stdlib_strided_dnanmeanors_ndarray( 6, x, 2, 0 );
// returns ~4.6667
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] double*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.

```c
double stdlib_strided_dnanmeanors_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
```

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

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

// Specify the number of elements:
const int N = 6;

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

// Compute the arithmetic mean:
double v = stdlib_strided_dnanmeanors( N, x, strideX );

// Print the result:
printf( "mean: %lf\n", v );
}
```

</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,16 +21,30 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
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 dnanmeanors = require( './../lib/dnanmeanors.js' );


// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.8 ) < 1 ) {
return NaN;
}
return uniform( -10.0, 10.0 );
}

/**
* Creates a benchmark function.
*
Expand All @@ -39,17 +53,7 @@ var dnanmeanors = require( './../lib/dnanmeanors.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = new Float64Array( len );
for ( i = 0; i < x.length; i++ ) {
if ( randu() < 0.2 ) {
x[ i ] = NaN;
} else {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
}
var x = filledarrayBy( len, 'float64', rand );
return benchmark;

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

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
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 @@ -40,6 +41,19 @@ var opts = {

// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.8 ) < 1 ) {
return NaN;
}
return uniform( -10.0, 10.0 );
}

/**
* Creates a benchmark function.
*
Expand All @@ -48,17 +62,7 @@ var opts = {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = new Float64Array( len );
for ( i = 0; i < x.length; i++ ) {
if ( randu() < 0.2 ) {
x[ i ] = NaN;
} else {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
}
var x = filledarrayBy( len, 'float64', rand );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,30 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
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 dnanmeanors = require( './../lib/ndarray.js' );


// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.8 ) < 1 ) {
return NaN;
}
return uniform( -10.0, 10.0 );
}

/**
* Creates a benchmark function.
*
Expand All @@ -39,17 +53,7 @@ var dnanmeanors = require( './../lib/ndarray.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = new Float64Array( len );
for ( i = 0; i < x.length; i++ ) {
if ( randu() < 0.2 ) {
x[ i ] = NaN;
} else {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
}
var x = filledarrayBy( len, 'float64', rand );
return benchmark;

function benchmark( b ) {
Expand Down
Loading