Skip to content

Commit 3ca878a

Browse files
feat: ndarray-dnanmeanwd
1 parent 8785e54 commit 3ca878a

File tree

15 files changed

+193
-283
lines changed

15 files changed

+193
-283
lines changed

lib/node_modules/@stdlib/stats/base/dnanmeanwd/include.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# Source files:
3838
'src_files': [
39-
'<(src_dir)/addon.cpp',
39+
'<(src_dir)/addon.c',
4040
'<!@(node -e "var arr = require(\'@stdlib/utils/library-manifest\')(\'./manifest.json\',{},{\'basedir\':process.cwd(),\'paths\':\'posix\'}).src; for ( var i = 0; i < arr.length; i++ ) { console.log( arr[ i ] ); }")',
4141
],
4242

lib/node_modules/@stdlib/stats/base/dnanmeanwd/include/stdlib/stats/base/dnanmeanwd.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef STDLIB_STATS_BASE_DNANMEANWD_H
2020
#define STDLIB_STATS_BASE_DNANMEANWD_H
2121

22-
#include <stdint.h>
22+
#include "stdlib/blas/base/shared.h"
2323

2424
/*
2525
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
@@ -31,7 +31,12 @@ extern "C" {
3131
/**
3232
* Computes the arithmetic mean of a double-precision floating-point strided array, using Welford's algorithm and ignoring `NaN` values.
3333
*/
34-
double stdlib_strided_dnanmeanwd( const int64_t N, const double *X, const int64_t stride );
34+
double API_SUFFIX(stdlib_strided_dnanmeanwd)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
35+
36+
/**
37+
* Computes the maximum absolute value of a double-precision floating-point strided array, ignoring `NaN` values and using alternative indexing semantics .
38+
*/
39+
double API_SUFFIX(stdlib_strided_dnanmeanwd_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
3540

3641
#ifdef __cplusplus
3742
}

lib/node_modules/@stdlib/stats/base/dnanmeanwd/lib/dnanmeanwd.js

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
'use strict';
2020

21+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
22+
var ndarray = require( './ndarray.js' );
23+
24+
2125
// MAIN //
2226

2327
/**
@@ -43,7 +47,7 @@
4347
*
4448
* @param {PositiveInteger} N - number of indexed elements
4549
* @param {Float64Array} x - input array
46-
* @param {integer} stride - stride length
50+
* @param {integer} strideX - stride length
4751
* @returns {number} arithmetic mean
4852
*
4953
* @example
@@ -52,41 +56,11 @@
5256
* var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
5357
* var N = x.length;
5458
*
55-
* var v = dnanmeanwd( N, x, 1 );
59+
* var v = dnanmeanwd( x.length, x, 1 );
5660
* // returns ~0.3333
5761
*/
58-
function dnanmeanwd( N, x, stride ) {
59-
var mu;
60-
var ix;
61-
var v;
62-
var n;
63-
var i;
64-
65-
if ( N <= 0 ) {
66-
return NaN;
67-
}
68-
if ( N === 1 || stride === 0 ) {
69-
return x[ 0 ];
70-
}
71-
if ( stride < 0 ) {
72-
ix = (1-N) * stride;
73-
} else {
74-
ix = 0;
75-
}
76-
mu = 0.0;
77-
n = 0;
78-
for ( i = 0; i < N; i++ ) {
79-
v = x[ ix ];
80-
if ( v === v ) {
81-
n += 1;
82-
mu += ( v-mu ) / n;
83-
}
84-
ix += stride;
85-
}
86-
if ( n === 0 ) {
87-
return NaN;
88-
}
89-
return mu;
62+
function dnanmeanwd( N, x, strideX ) {
63+
return ndarray( N, x, strideX, stride2offset( N, strideX ) );
9064
}
9165

9266

lib/node_modules/@stdlib/stats/base/dnanmeanwd/lib/dnanmeanwd.native.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var addon = require( './../src/addon.node' );
3030
*
3131
* @param {PositiveInteger} N - number of indexed elements
3232
* @param {Float64Array} x - input array
33-
* @param {integer} stride - stride length
33+
* @param {integer} strideX - stride length
3434
* @returns {number} arithmetic mean
3535
*
3636
* @example
@@ -39,11 +39,11 @@ var addon = require( './../src/addon.node' );
3939
* var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
4040
* var N = x.length;
4141
*
42-
* var v = dnanmeanwd( N, x, 1 );
42+
* var v = dnanmeanwd( x.length, x, 1 );
4343
* // returns ~0.3333
4444
*/
45-
function dnanmeanwd( N, x, stride ) {
46-
return addon( N, x, stride );
45+
function dnanmeanwd( N, x, strideX ) {
46+
return addon( N, x, strideX );
4747
}
4848

4949

lib/node_modules/@stdlib/stats/base/dnanmeanwd/lib/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,16 @@
3030
* var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
3131
* var N = 3;
3232
*
33-
* var v = dnanmeanwd( N, x, 1 );
33+
* var v = dnanmeanwd( 5, x, 1 );
3434
* // returns ~0.3333
3535
*
3636
* @example
3737
* var Float64Array = require( '@stdlib/array/float64' );
38-
* var floor = require( '@stdlib/math/base/special/floor' );
3938
* var dnanmeanwd = require( '@stdlib/stats/base/dnanmeanwd' );
4039
*
4140
* var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
42-
* var N = floor( x.length / 2 );
4341
*
44-
* var v = dnanmeanwd.ndarray( N, x, 2, 1 );
42+
* var v = dnanmeanwd.ndarray( 5, x, 2, 1 );
4543
* // returns 1.25
4644
*/
4745

lib/node_modules/@stdlib/stats/base/dnanmeanwd/lib/ndarray.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,17 @@
4343
*
4444
* @param {PositiveInteger} N - number of indexed elements
4545
* @param {Float64Array} x - input array
46-
* @param {integer} stride - stride length
47-
* @param {NonNegativeInteger} offset - starting index
46+
* @param {integer} strideX - stride length
47+
* @param {NonNegativeInteger} offsetX - starting index
4848
* @returns {number} arithmetic mean
4949
*
5050
* @example
5151
* var Float64Array = require( '@stdlib/array/float64' );
52-
* var floor = require( '@stdlib/math/base/special/floor' );
5352
*
5453
* var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
55-
* var N = floor( x.length / 2 );
5654
*
57-
* var v = dnanmeanwd( N, x, 2, 1 );
58-
* // returns 1.25
5955
*/
60-
function dnanmeanwd( N, x, stride, offset ) {
56+
function dnanmeanwd( N, x, strideX, offsetX ) {
6157
var mu;
6258
var ix;
6359
var v;
@@ -67,10 +63,10 @@ function dnanmeanwd( N, x, stride, offset ) {
6763
if ( N <= 0 ) {
6864
return NaN;
6965
}
70-
if ( N === 1 || stride === 0 ) {
71-
return x[ offset ];
66+
if ( N === 1 || strideX === 0 ) {
67+
return x[ offsetX ];
7268
}
73-
ix = offset;
69+
ix = offsetX;
7470
mu = 0.0;
7571
n = 0;
7672
for ( i = 0; i < N; i++ ) {
@@ -79,7 +75,7 @@ function dnanmeanwd( N, x, stride, offset ) {
7975
n += 1;
8076
mu += ( v-mu ) / n;
8177
}
82-
ix += stride;
78+
ix += strideX;
8379
}
8480
if ( n === 0 ) {
8581
return NaN;

lib/node_modules/@stdlib/stats/base/dnanmeanwd/lib/ndarray.native.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
// MODULES //
2222

23-
var Float64Array = require( '@stdlib/array/float64' );
24-
var addon = require( './dnanmeanwd.native.js' );
23+
var addon = require( './../src/addon.node' );
2524

2625

2726
// MAIN //
@@ -31,27 +30,20 @@ var addon = require( './dnanmeanwd.native.js' );
3130
*
3231
* @param {PositiveInteger} N - number of indexed elements
3332
* @param {Float64Array} x - input array
34-
* @param {integer} stride - stride length
35-
* @param {NonNegativeInteger} offset - starting index
33+
* @param {integer} strideX - stride length
34+
* @param {NonNegativeInteger} offsetX - starting index
3635
* @returns {number} arithmetic mean
3736
*
3837
* @example
3938
* var Float64Array = require( '@stdlib/array/float64' );
40-
* var floor = require( '@stdlib/math/base/special/floor' );
4139
*
4240
* var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
43-
* var N = floor( x.length / 2 );
4441
*
45-
* var v = dnanmeanwd( N, x, 2, 1 );
42+
* var v = dnanmeanwd( 5, x, 2, 1 );
4643
* // returns 1.25
4744
*/
48-
function dnanmeanwd( N, x, stride, offset ) {
49-
var view;
50-
if ( stride < 0 ) {
51-
offset += (N-1) * stride;
52-
}
53-
view = new Float64Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len
54-
return addon( N, view, stride );
45+
function dnanmeanwd( N, x, strideX, offsetX ) {
46+
return addon.ndarray( N, x, strideX, offsetX );
5547
}
5648

5749

lib/node_modules/@stdlib/stats/base/dnanmeanwd/manifest.json

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
2-
"options": {},
2+
"options": {
3+
"task": "build",
4+
"wasm": false
5+
},
36
"fields": [
47
{
58
"field": "src",
@@ -24,17 +27,73 @@
2427
],
2528
"confs": [
2629
{
30+
"task": "build",
31+
"wasm": false,
2732
"src": [
28-
"./src/dnanmeanwd.c"
33+
"./src/main.c"
2934
],
3035
"include": [
3136
"./include"
3237
],
33-
"libraries": [
34-
"-lm"
38+
"libraries": [],
39+
"libpath": [],
40+
"dependencies": [
41+
"@stdlib/blas/base/shared",
42+
"@stdlib/strided/base/stride2offset",
43+
"@stdlib/napi/export",
44+
"@stdlib/napi/argv",
45+
"@stdlib/napi/argv-int64",
46+
"@stdlib/napi/argv-strided-float64array",
47+
"@stdlib/napi/create-double"
48+
]
49+
},
50+
{
51+
"task": "benchmark",
52+
"wasm": false,
53+
"src": [
54+
"./src/main.c"
55+
],
56+
"include": [
57+
"./include"
58+
],
59+
"libraries": [],
60+
"libpath": [],
61+
"dependencies": [
62+
"@stdlib/blas/base/shared",
63+
"@stdlib/strided/base/stride2offset"
64+
]
65+
},
66+
{
67+
"task": "examples",
68+
"wasm": false,
69+
"src": [
70+
"./src/main.c"
71+
],
72+
"include": [
73+
"./include"
74+
],
75+
"libraries": [],
76+
"libpath": [],
77+
"dependencies": [
78+
"@stdlib/blas/base/shared",
79+
"@stdlib/strided/base/stride2offset"
80+
]
81+
},
82+
{
83+
"task": "",
84+
"wasm": true,
85+
"src": [
86+
"./src/main.c"
87+
],
88+
"include": [
89+
"./include"
3590
],
91+
"libraries": [],
3692
"libpath": [],
37-
"dependencies": []
93+
"dependencies": [
94+
"@stdlib/blas/base/shared",
95+
"@stdlib/strided/base/stride2offset"
96+
]
3897
}
3998
]
4099
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "stdlib/stats/base/dnanmeanwd.h"
20+
#include "stdlib/napi/export.h"
21+
#include "stdlib/napi/argv.h"
22+
#include "stdlib/napi/argv_int64.h"
23+
#include "stdlib/napi/argv_strided_float64array.h"
24+
#include "stdlib/napi/create_double.h"
25+
26+
/**
27+
* Receives JavaScript callback invocation data.
28+
*
29+
* @param env environment under which the function is invoked
30+
* @param info callback data
31+
* @return Node-API value
32+
*/
33+
static napi_value addon( napi_env env, napi_callback_info info ) {
34+
STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
35+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
36+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
37+
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 );
38+
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_dnanmeanwd( N, X, strideX ), v );
39+
return v;
40+
}
41+
42+
/**
43+
* Receives JavaScript callback invocation data.
44+
*
45+
* @param env environment under which the function is invoked
46+
* @param info callback data
47+
* @return Node-API value
48+
*/
49+
static napi_value addon_method( napi_env env, napi_callback_info info ) {
50+
STDLIB_NAPI_ARGV( env, info, argv, argc, 4 );
51+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
52+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
53+
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 3 );
54+
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 );
55+
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_dnanmeanwd_ndarray( N, X, strideX, offsetX ), v );
56+
return v;
57+
}
58+
59+
STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )

0 commit comments

Comments
 (0)