Skip to content

Commit

Permalink
feat: add C ndarray interface and refactor implementation
Browse files Browse the repository at this point in the history
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: passed
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
  • Loading branch information
aayush0325 committed Jan 13, 2025
1 parent abf124e commit 54367d5
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef STDLIB_STATS_BASE_DMEANORS_H
#define STDLIB_STATS_BASE_DMEANORS_H

#include <stdint.h>
#include "stdlib/blas/base/shared.h"

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
Expand All @@ -31,7 +31,12 @@ extern "C" {
/**
* Computes the arithmetic mean of a double-precision floating-point strided array using ordinary recursive summation.
*/
double stdlib_strided_dmeanors( const int64_t N, const double *X, const int64_t stride );
double API_SUFFIX(stdlib_strided_dmeanors)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );

/**
* Computes the arithmetic mean of a double-precision floating-point strided array using ordinary recursive summation and alternative indexing semantics.
*/
double API_SUFFIX(stdlib_strided_dmeanors_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );

#ifdef __cplusplus
}
Expand Down
18 changes: 6 additions & 12 deletions lib/node_modules/@stdlib/stats/base/dmeanors/lib/dmeanors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

// MODULES //

var dsumors = require( '@stdlib/blas/ext/base/dsumors' );
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -30,26 +31,19 @@ var dsumors = require( '@stdlib/blas/ext/base/dsumors' );
*
* @param {PositiveInteger} N - number of indexed elements
* @param {Float64Array} x - input array
* @param {integer} stride - stride length
* @param {integer} strideX - stride length
* @returns {number} arithmetic mean
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
* var N = x.length;
*
* var v = dmeanors( N, x, 1 );
* var v = dmeanors( x.length, x, 1 );
* // returns ~0.3333
*/
function dmeanors( N, x, stride ) {
if ( N <= 0 ) {
return NaN;
}
if ( N === 1 || stride === 0 ) {
return x[ 0 ];
}
return dsumors( N, x, stride ) / N;
function dmeanors( N, x, strideX ) {
return ndarray( N, x, strideX, stride2offset( N, strideX ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ var addon = require( './../src/addon.node' );
*
* @param {PositiveInteger} N - number of indexed elements
* @param {Float64Array} x - input array
* @param {integer} stride - stride length
* @param {integer} strideX - stride length
* @returns {number} arithmetic mean
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
* var N = x.length;
*
* var v = dmeanors( N, x, 1 );
* var v = dmeanors( x.length, x, 1 );
* // returns ~0.3333
*/
function dmeanors( N, x, stride ) {
return addon( N, x, stride );
function dmeanors( N, x, strideX ) {
return addon( N, x, strideX );
}


Expand Down
7 changes: 2 additions & 5 deletions lib/node_modules/@stdlib/stats/base/dmeanors/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@
* var dmeanors = require( '@stdlib/stats/base/dmeanors' );
*
* var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
* var N = x.length;
*
* var v = dmeanors( N, x, 1 );
* var v = dmeanors( x.length, x, 1 );
* // returns ~0.3333
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var floor = require( '@stdlib/math/base/special/floor' );
* var dmeanors = require( '@stdlib/stats/base/dmeanors' );
*
* var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
* var N = floor( x.length / 2 );
*
* var v = dmeanors.ndarray( N, x, 2, 1 );
* var v = dmeanors.ndarray( 4, x, 2, 1 );
* // returns 1.25
*/

Expand Down
16 changes: 7 additions & 9 deletions lib/node_modules/@stdlib/stats/base/dmeanors/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,26 @@ var dsumors = require( '@stdlib/blas/ext/base/dsumors' ).ndarray;
*
* @param {PositiveInteger} N - number of indexed elements
* @param {Float64Array} x - input array
* @param {integer} stride - stride length
* @param {NonNegativeInteger} offset - starting index
* @param {integer} strideX - stride length
* @param {NonNegativeInteger} offsetX - starting index
* @returns {number} arithmetic mean
*
* @example
* 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 ] );
* var N = floor( x.length / 2 );
*
* var v = dmeanors( N, x, 2, 1 );
* var v = dmeanors( 4, x, 2, 1 );
* // returns 1.25
*/
function dmeanors( N, x, stride, offset ) {
function dmeanors( N, x, strideX, offsetX ) {
if ( N <= 0 ) {
return NaN;
}
if ( N === 1 || stride === 0 ) {
return x[ offset ];
if ( N === 1 || strideX === 0 ) {
return x[ offsetX ];
}
return dsumors( N, x, stride, offset ) / N;
return dsumors( N, x, strideX, offsetX ) / N;
}


Expand Down
20 changes: 6 additions & 14 deletions lib/node_modules/@stdlib/stats/base/dmeanors/lib/ndarray.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

// MODULES //

var Float64Array = require( '@stdlib/array/float64' );
var addon = require( './dmeanors.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -31,27 +30,20 @@ var addon = require( './dmeanors.native.js' );
*
* @param {PositiveInteger} N - number of indexed elements
* @param {Float64Array} x - input array
* @param {integer} stride - stride length
* @param {NonNegativeInteger} offset - starting index
* @param {integer} strideX - stride length
* @param {NonNegativeInteger} offsetX - starting index
* @returns {number} arithmetic mean
*
* @example
* 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 ] );
* var N = floor( x.length / 2 );
*
* var v = dmeanors( N, x, 2, 1 );
* var v = dmeanors( 4, x, 2, 1 );
* // returns 1.25
*/
function dmeanors( N, x, stride, offset ) {
var view;
if ( stride < 0 ) {
offset += (N-1) * stride;
}
view = new Float64Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len
return addon( N, view, stride );
function dmeanors( N, x, strideX, offsetX ) {
return addon.ndarray( N, x, strideX, offsetX );
}


Expand Down
45 changes: 33 additions & 12 deletions lib/node_modules/@stdlib/stats/base/dmeanors/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"options": {
"task": "build"
"task": "build",
"wasm": false
},
"fields": [
{
Expand All @@ -27,17 +28,18 @@
"confs": [
{
"task": "build",
"wasm": false,
"src": [
"./src/dmeanors.c"
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared",
"@stdlib/strided/base/stride2offset",
"@stdlib/blas/ext/base/dsumors",
"@stdlib/napi/export",
"@stdlib/napi/argv",
Expand All @@ -48,33 +50,52 @@
},
{
"task": "benchmark",
"wasm": false,
"src": [
"./src/dmeanors.c"
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared",
"@stdlib/strided/base/stride2offset",
"@stdlib/blas/ext/base/dsumors"
]
},
{
"task": "examples",
"wasm": false,
"src": [
"./src/dmeanors.c"
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared",
"@stdlib/strided/base/stride2offset",
"@stdlib/blas/ext/base/dsumors"
]
},
{
"task": "",
"wasm": true,
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared",
"@stdlib/strided/base/stride2offset",
"@stdlib/blas/ext/base/dsumors"
]
}
Expand Down
26 changes: 22 additions & 4 deletions lib/node_modules/@stdlib/stats/base/dmeanors/src/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "stdlib/stats/base/dmeanors.h"
#include "stdlib/blas/base/shared.h"
#include "stdlib/napi/export.h"
#include "stdlib/napi/argv.h"
#include "stdlib/napi/argv_int64.h"
Expand All @@ -34,10 +35,27 @@
static napi_value addon( napi_env env, napi_callback_info info ) {
STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
STDLIB_NAPI_ARGV_INT64( env, stride, argv, 2 );
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, stride, argv, 1 );
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_dmeanors( N, X, stride ), v );
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 );
STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dmeanors)( N, X, strideX ), v );
return v;
}

STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
/**
* Receives JavaScript callback invocation data.
*
* @param env environment under which the function is invoked
* @param info callback data
* @return Node-API value
*/
static napi_value addon_method( napi_env env, napi_callback_info info ) {
STDLIB_NAPI_ARGV( env, info, argv, argc, 4 );
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 3 );
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 );
STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dmeanors_ndarray)( N, X, strideX, offsetX ), v );
return v;
}

STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )
39 changes: 0 additions & 39 deletions lib/node_modules/@stdlib/stats/base/dmeanors/src/dmeanors.c

This file was deleted.

Loading

0 comments on commit 54367d5

Please sign in to comment.