Skip to content

feat: add math/base/special/besselk1 - #16982

Open
Udaygund wants to merge 2 commits into
stdlib-js:developfrom
Udaygund:feat/besselk1
Open

Udaygund wants to merge 2 commits into
stdlib-js:developfrom
Udaygund:feat/besselk1

Conversation

@Udaygund

@Udaygund Udaygund commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Progresses #9892.

Description

What is the purpose of this pull request?

This pull request:

  • adds @stdlib/math/base/special/besselk1, which computes the modified Bessel function of the second kind of order one, K₁(x), with JavaScript, C, and Node-API implementations.

This is the companion to besselk0 (#16981), following the suggestion in #9892 to start with orders zero and one. The two packages are independent, so they can be reviewed in either order.

The implementation is a port of the double-precision (53-bit) branch of Boost's bessel_k1.hpp:

  • for x <= 1, R2(x²) * x + 1/x + ln(x) * a, where a = ((R1(t) + Y1) * t² + t/2 + 1) * x/2 and t = x²/4;
  • for x > 1, (Y3 + R3(1/x)) * exp(-x) / sqrt(x), with the exponential split into two halves for x >= ln(DBL_MAX) to avoid premature underflow.

Boost writes Y1 and Y3 as float literals; both values are exactly representable in single precision, so the double-precision constants used here are identical. Special cases: NaN for x < 0 or NaN, +Infinity for x = ±0, and 0 for x = +Infinity. Since K₁(x) ~ 1/x as x → 0, the result overflows to +Infinity for 0 < x < 1/DBL_MAX (~5.6e-309), which includes all subnormal inputs.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

Reference Packages

Did the content of this pull request follow one or more existing packages? Answering is optional.

This pull request followed these packages:

What carried over from the reference(s)?

  • package layout, C/Node-API structure, manifest, generator script, benchmarks, and test structure from math/base/special/besselk0, which was itself scaffolded from math/base/special/bessely0

What differed, or what did you have to work out yourself?

  • the algorithm and coefficients, taken from Boost's bessel_k1.hpp (all 34 coefficients were checked programmatically against the Boost source); this order uses three rational functions and no plain polynomial
  • accuracy: against 50-digit mpmath values on 20,000 log-uniform random points in [1e-300, 745], the maximum relative error is 1.55 ε (98.4% of cases within 0.5 ε). Against the SciPy (scipy.special.k1) fixtures, the maximum difference is 4 ULP. Test tolerances are set one ULP above the measured maximum per range
  • there is no subnormal fixture, since every subnormal input overflows; instead, there is an explicit test that 5.0e-324, 1.0e-310, and 5.0e-309 return +Infinity
  • as a check that the tolerances are meaningful, changing the last digit of one coefficient makes 171 test assertions fail

Questions

Any questions for reviewers of this pull request?

Same as in #16981: are SciPy-generated fixtures acceptable, or would you prefer Julia fixtures (SpecialFunctions.besselk( 1, x )) as in the other Bessel packages?

Other

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

Ran locally: package tests (JS and native, 6,512 assertions each), examples-c, benchmark-c, the JS examples, and the JavaScript, Markdown, TypeScript declaration, repl.txt, EditorConfig, and license-header linters, plus pylint (with etc/pylint/.pylintrc), pycodestyle, and pydocstyle on the Python files. The only lint output is n/no-sync warnings in scripts/, which are also present in math/base/special/bessely0.

Checklist

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

  • Read, understood, and followed the [contributing guidelines][contributing].

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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_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: missing_dependencies
  - 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: passed
  - task: lint_license_headers
    status: passed
---
@Udaygund
Udaygund requested a review from a team November 29, 2026 11:14
@stdlib-bot stdlib-bot added Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. labels Nov 29, 2026
@stdlib-bot

stdlib-bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
math/base/special/besselk1 $\\color{red}403/420$
$\\color{green}+0.00\\%$
$\\color{red}30/37$
$\\color{green}+0.00\\%$
$\\color{green}5/5$
$\\color{green}+0.00\\%$
$\\color{red}403/420$
$\\color{green}+0.00\\%$

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants