Skip to content

feat: add C implementation for math/base/special/kernel-betaincinv - #13418

Merged
Planeshifter merged 4 commits into
developfrom
philipp/kernel-betaincinv-c
Oct 2, 2026
Merged

Planeshifter merged 4 commits into
developfrom
philipp/kernel-betaincinv-c

Conversation

@Planeshifter

@Planeshifter Planeshifter commented Jul 11, 2026 •

Copy link
Copy Markdown
Member

resolves stdlib-js/metr-issue-tracker#1322

Description

What is the purpose of this pull request?

This pull request:

  • adds a C implementation for @stdlib/math/base/special/kernel-betaincinv
  • adds a native add-on binding, C benchmarks, C examples, native tests, and documentation for the C API
  • extends scripts/evalpoly.js to generate the C polynomial evaluation functions (spliced into src/main.c between auto-generated markers), keeping the JS and C polynomial coefficients in sync from a single source

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

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.

Implementation notes:

  • The implementation is a faithful port of the existing JavaScript implementation (Boost ibeta_inverse.hpp, t_distribution_inv.hpp, and roots.hpp), preserving branch structure, constants, and iteration behavior.
  • As @stdlib/math/base/special/betainc does not yet have a C implementation, the two call sites in the driver use stdlib_base_kernel_betainc( x, a, b, true, false, &out, &deriv ) directly, mirroring what the JS betainc wrapper does.
  • The JS implementation computes ldexp( 1.0, (2.0*53.0)/3.0 ); the JS ldexp truncates the fractional exponent, so the C port uses stdlib_base_ldexp( 1.0, 35 ) to match runtime behavior.
  • The C API follows the multi-output pointer pattern of @stdlib/math/base/special/kernel-betainc: void stdlib_base_kernel_betaincinv( const double a, const double b, const double p, const double q, double *out1, double *out2 ).
  • Native tests pass the full C++ (Boost) fixture set (2000 cases) within 16 ULP (empirical minimum, deterministic). Differential testing against the JS implementation over >150k random and edge-case inputs shows agreement, with residual-level analysis confirming remaining ULP spread is root-finder termination conditioning rather than porting divergence.

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.".

This PR was written primarily by Claude Code under my direction, using a multi-agent workflow: the JS implementation and existing C ports (gammaincinv, kernel-betainc) were studied in detail, the port was generated to mirror the JS line-by-line, and the result was verified via fixture tests, large-scale differential testing against the JS implementation, and multiple independent adversarial review passes. All design decisions and validation results were reviewed by me.


@stdlib-js/reviewers

@stdlib-bot stdlib-bot added the Math Issue or pull request specific to math functionality. label Jul 11, 2026
@stdlib-bot

stdlib-bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

No coverage information available.

@kgryte

kgryte commented Sep 16, 2026

Copy link
Copy Markdown
Member

@anandkaranubc Would you mind doing a first review of this package? Cheers!

@kgryte kgryte added Feature Issue or pull request for adding a new feature. C Issue involves or relates to C. labels Sep 16, 2026
@anandkaranubc

Copy link
Copy Markdown
Contributor

@anandkaranubc Would you mind doing a first review of this package? Cheers!

Of course, on it! Thanks!

@anandkaranubc anandkaranubc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The CI failure is in the downstream stats/base/dists/f/quantile tests, not in this package. The same issue would also show up in stats/base/dists/beta/quantile. Those fixture tolerances were set to the exact worst case of the old JS root-finder, with no headroom. The new root-finder in this PR lands a few ULP away on a handful of large-parameter entries, with no loss of accuracy. Updating those downstream tolerances is a separate change, so I'd prefer to handle it in a follow-up PR.

@anandkaranubc anandkaranubc added the METR Pull request associated with the METR project. label Sep 26, 2026
Planeshifter and others added 4 commits September 30, 2026 01:22
---
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_pkg_readmes
    status: passed
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - 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: passed
  - task: lint_c_examples
    status: passed
  - task: lint_c_benchmarks
    status: passed
  - 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_pkg_readmes
    status: na
  - task: lint_markdown_docs
    status: na
  - 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: passed
  - 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: na
  - 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_pkg_readmes
    status: na
  - task: lint_markdown_docs
    status: na
  - 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: passed
  - task: lint_javascript_benchmarks
    status: passed
  - 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: 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_pkg_readmes
    status: passed
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: passed
  - task: lint_repl_help
    status: passed
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: passed
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: passed
  - task: lint_r
    status: na
  - task: lint_c_src
    status: passed
  - task: lint_c_examples
    status: passed
  - task: lint_c_benchmarks
    status: passed
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: passed
  - task: lint_license_headers
    status: passed
---
@Planeshifter
Planeshifter force-pushed the philipp/kernel-betaincinv-c branch from 765ef04 to 9d25801 Compare September 30, 2026 06:22
@Planeshifter
Planeshifter merged commit 57dd677 into develop Oct 2, 2026
16 of 17 checks passed
@stdlib-bot stdlib-bot removed the Needs Review A pull request which needs code review. label Oct 2, 2026
@anandkaranubc
anandkaranubc deleted the philipp/kernel-betaincinv-c branch October 3, 2026 19:37
@anandkaranubc
anandkaranubc restored the philipp/kernel-betaincinv-c branch October 3, 2026 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C Issue involves or relates to C. Feature Issue or pull request for adding a new feature. Math Issue or pull request specific to math functionality. METR Pull request associated with the METR project.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clean-up math/base/special/kernel-betaincinv

4 participants