Skip to content

feat: add C implementation for stats/base/ndarray/dnanmidrange#10203

Open
bhargava-d16 wants to merge 12 commits intostdlib-js:developfrom
bhargava-d16:feat-ndarray-nanmidrange
Open

feat: add C implementation for stats/base/ndarray/dnanmidrange#10203
bhargava-d16 wants to merge 12 commits intostdlib-js:developfrom
bhargava-d16:feat-ndarray-nanmidrange

Conversation

@bhargava-d16
Copy link
Copy Markdown
Contributor


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: passed
  • task: lint_package_json status: passed
  • 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: passed
  • task: lint_javascript_benchmarks status: passed
  • task: lint_python status: na
  • task: lint_r status: na
  • task: lint_c_src status: missing_dependencies
  • task: lint_c_examples status: missing_dependencies
  • task: lint_c_benchmarks status: missing_dependencies
  • task: lint_c_tests_fixtures status: na
  • task: lint_shell status: na
  • task: lint_typescript_declarations status: passed
  • task: lint_typescript_tests status: na
  • task: lint_license_headers status: passed ---

Resolves None

Description

What is the purpose of this pull request?

This pull request:

  • adds C implementation for stats/base/ndarray/dnanmidrange

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

  • #{{TODO: add related issue number}}

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".


@stdlib-js/reviewers

Signed-off-by: Bhargav Dabhade <bhargava2005dabhade@gmail.com>

---
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: passed
  - task: lint_package_json
    status: passed
  - 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: passed
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: missing_dependencies
  - task: lint_c_examples
    status: missing_dependencies
  - task: lint_c_benchmarks
    status: missing_dependencies
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@stdlib-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Feb 10, 2026
@stdlib-bot
Copy link
Copy Markdown
Contributor

stdlib-bot commented Feb 10, 2026

Coverage Report

Package Statements Branches Functions Lines
stats/base/ndarray/dnanmidrange $\color{green}170/170$
$\color{green}+0.00%$
$\color{green}7/7$
$\color{green}+0.00%$
$\color{green}2/2$
$\color{green}+0.00%$
$\color{green}170/170$
$\color{green}+0.00%$

The above coverage report was generated for the changes in this PR.

---
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: na
  - 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: na
  - task: lint_c_examples
    status: missing_dependencies
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
---
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: na
  - 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: na
  - task: lint_c_examples
    status: missing_dependencies
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@Sachinn-64 Sachinn-64 self-requested a review February 12, 2026 06:57
@bhargava-d16
Copy link
Copy Markdown
Contributor Author

@Planeshifter Just a gentle follow-up on this PR.

#include "stdlib/stats/base/ndarray/dnanmidrange.h"
```

stdlib_stats_dnanmidrange( arrays )
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be formatted as a #### heading to match our convention for C function signatures. See, e.g., the dmax README for reference.

Suggested change
stdlib_stats_dnanmidrange( arrays )
#### stdlib_stats_dnanmidrange( arrays )

#include <stdint.h>

// Create an ndarray:
const double data[] = { 1.0, NaN, -2.0, 3.0, -4.0 };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NaN isn't valid C -- the correct macro is NAN (from <math.h>). You'll also need to add #include <math.h> to this code block (similar to how you did it in the actual .c files).

Suggested change
const double data[] = { 1.0, NaN, -2.0, 3.0, -4.0 };
const double data[] = { 1.0, NAN, -2.0, 3.0, -4.0 };

Copy link
Copy Markdown
Member

@Sachinn-64 Sachinn-64 Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using NAN, we should prefer the expression 0.0/0.0 and remove the math.h include, as Athan mentioned here: 5a64342#r179146895


int main( void ) {
// Create a data buffer (includes NaN):
const double data[] = { 1.0, -2.0, 3.0, NaN, 5.0, -6.0, 7.0, -8.0 };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here -- NaN should be NAN.

Suggested change
const double data[] = { 1.0, -2.0, 3.0, NaN, 5.0, -6.0, 7.0, -8.0 };
const double data[] = { 1.0, -2.0, 3.0, NAN, 5.0, -6.0, 7.0, -8.0 };

@Planeshifter Planeshifter added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Mar 9, 2026
Signed-off-by: Sachin Pangal <151670745+Sachinn-64@users.noreply.github.com>
Signed-off-by: Sachin Pangal <151670745+Sachinn-64@users.noreply.github.com>
Comment on lines +39 to +41
var opts = {
'skip': ( dnanmidrange instanceof Error )
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var opts = {
'skip': ( dnanmidrange instanceof Error )
};
var opts = {
'skip': ( dnanmidrange instanceof Error )
};
var options = {
'dtype': 'float64'
};

var x;

xbuf = filledarrayBy( len, 'float64', rand );
x = new ndarray( 'float64', xbuf, [ len ], [ 1 ], 0, 'row-major' );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x = new ndarray( 'float64', xbuf, [ len ], [ 1 ], 0, 'row-major' );
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );

Signed-off-by: Sachin Pangal <151670745+Sachinn-64@users.noreply.github.com>

int main( void ) {
// Create a data buffer:
const double data[] = { 1.0, -2.0, 3.0, -4.0, NAN, 5.0, 0.0, 3.0 };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const double data[] = { 1.0, -2.0, 3.0, -4.0, NAN, 5.0, 0.0, 3.0 };
const double data[] = { 1.0, -2.0, 3.0, -4.0, 0.0/0.0, 5.0, 0.0, 3.0 };

We could do this.

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <math.h>

Signed-off-by: Sachin Pangal <151670745+Sachinn-64@users.noreply.github.com>
Signed-off-by: Sachin Pangal <151670745+Sachinn-64@users.noreply.github.com>
Signed-off-by: Sachin Pangal <151670745+Sachinn-64@users.noreply.github.com>
Signed-off-by: Sachin Pangal <151670745+Sachinn-64@users.noreply.github.com>
Signed-off-by: Sachin Pangal <151670745+Sachinn-64@users.noreply.github.com>

stdlib_stats_dnanmidrange( arrays )

Computes the mid-range of a one-dimensional double-precision floating-point ndarray, ignoring NaN values.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Computes the mid-range of a one-dimensional double-precision floating-point ndarray, ignoring NaN values.
Computes the [mid-range][mid-range] of a one-dimensional double-precision floating-point ndarray, ignoring NaN values.

#include <stdio.h>

int main( void ) {
// Create a data buffer (includes NaN):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Create a data buffer (includes NaN):
// Create a data buffer:

const int64_t nsubmodes = 1;

// Create an ndarray:
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );

// Define a list of ndarrays:
const struct ndarray *arrays[] = { x };

// Compute the NaN-ignoring midrange:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Compute the NaN-ignoring midrange:
// Compute the mid-range:

double v = stdlib_stats_dnanmidrange( arrays );

// Print the result:
printf( "midrange: %lf\n", v );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
printf( "midrange: %lf\n", v );
printf( "mid-range: %lf\n", v );

@kgryte
Copy link
Copy Markdown
Member

kgryte commented Apr 21, 2026

/stdlib merge

@kgryte kgryte added the Feature Issue or pull request for adding a new feature. label Apr 21, 2026
@kgryte kgryte added difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. review: 3 labels Apr 21, 2026
@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label Apr 21, 2026
@stdlib-bot stdlib-bot requested a review from a team April 21, 2026 03:36
@stdlib-bot stdlib-bot added Needs Review A pull request which needs code review. and removed bot: In Progress Pull request is currently awaiting automation. labels Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. Feature Issue or pull request for adding a new feature. Needs Changes Pull request which needs changes before being merged. Needs Review A pull request which needs code review. review: 3 Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants