Skip to content

chore: improve the implementation of math/base/special/hyp2f1#11325

Open
officiallyanee wants to merge 3 commits intostdlib-js:developfrom
officiallyanee:improve/hyp2f1
Open

chore: improve the implementation of math/base/special/hyp2f1#11325
officiallyanee wants to merge 3 commits intostdlib-js:developfrom
officiallyanee:improve/hyp2f1

Conversation

@officiallyanee
Copy link
Copy Markdown
Contributor

@officiallyanee officiallyanee commented Apr 7, 2026


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: passed
  • task: lint_javascript_benchmarks status: na
  • task: lint_python status: missing_dependencies
  • 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 ---

Resolves none.

Description

What is the purpose of this pull request?

This pull request:

  • This pull request improves the JS implementation of math/base/special/hyp2f1. It started with this issue:
image which is now image

Though the difference still exists but wolfram gives 0.0040689094468919286...
so output now is closer to wolfram.
It also uses some updated implementation from scipy's cephes hyp2f1 with added test cases to test the same.
I will work on C implementation once this gets the green light.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

  • None

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

{{TODO: add disclosure if applicable}}


@stdlib-js/reviewers

---
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: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: missing_dependencies
  - 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
---
@stdlib-bot stdlib-bot added Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. labels Apr 7, 2026
@stdlib-bot
Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
math/base/special/hyp2f1 $\color{red}1001/1069$
$\color{green}+93.64%$
$\color{red}119/130$
$\color{green}+91.54%$
$\color{green}8/8$
$\color{green}+100.00%$
$\color{red}1001/1069$
$\color{green}+93.64%$

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

Comment on lines +226 to +230
if ( id > 0.0 ) {
y *= q;
} else {
y1 *= q;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was wrong originally, cephes and scipy both have this but it was not discovered probably because else branch is never hit ( in test cov ) so I changed it.

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.

To clarify, the bug was in y1 *= q mistakenly being implemented as y = y1 * q, correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, the snippet from cephes for reference:

	if( id > 0.0 )
		y *= q;
	else
		y1 *= q;

	y += y1;

Comment on lines +55 to +89
// FUNCTIONS //

/**
* Evaluates 2F1(a, b; b; x) when `b = c` is a negative integer using AMS55 #15.4.2.
*
* @private
* @param {number} a - first parameter
* @param {number} b - second parameter (equals c, a non-positive integer)
* @param {number} x - argument
* @returns {number} function value, or NaN if precision is insufficient
*/
function hyp2f1NegCEqualBC( a, b, x ) {
var collectorMax;
var collector;
var sum;
var k;

if ( !( abs( b ) < 1.0e5 ) ) {
return NaN;
}
collectorMax = 1.0;
collector = 1.0;
sum = 1.0;
for ( k = 1; k <= -b; k++ ) {
collector *= ( a + k - 1.0 ) * x / k;
collectorMax = max( abs( collector ), collectorMax );
sum += collector;
}
if ( 1.0e-16 * ( 1.0 + ( collectorMax / abs( sum ) ) ) > 1.0e-7 ) {
return NaN;
}
return sum;
}


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will add this in a separate file if that would be better, the implementation for this was taken from scipy.

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.

Yes, if you don't mind going ahead and moving. Cheers!

id = round( d );

if ( x > 0.9 && !negIntA && !negIntB ) {
if ( x > 0.85 && !negIntA && !negIntB ) {
Copy link
Copy Markdown
Contributor Author

@officiallyanee officiallyanee Apr 7, 2026

Choose a reason for hiding this comment

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

0.85 was chosen arbitrarily through trial and error, this helped the case I mentioned in the description.

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.

@officiallyanee Do you happen to know where/why the 0.9 was chosen in the first place?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Its not explicitly mentioned in the cephes/scipy code but I went through some resources and found this:
image
ref: https://link.springer.com/article/10.1007/s11075-016-0173-0
I think it also explains why the initial case was performing badly too since |a| and |b| were much larger than |c|
and |x| is also pretty close to 1 so the power series was performing poorly.
In the range 0.85<x<0.9, it was true for every value as long as we keep a, b, c same.
( this is assuming wolfram as a better source of comparision )

@github-actions github-actions bot mentioned this pull request Apr 8, 2026
@kgryte
Copy link
Copy Markdown
Member

kgryte commented Apr 9, 2026

/stdlib merge

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label Apr 9, 2026
@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label Apr 9, 2026
for ( i = 0; i < x.length; i++ ) {
v = hyp2f1( a[ i ], b[ i ], c[ i ], x[ i ] );
delta = abs( v - expected[ i ] );
tol = 8.0 * EPS * abs( expected[ i ] );
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.

We just had another PR land which migrated hyp2f1 to use ULP-based testing. If you could follow suit here, that would be appreciated!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do!

@kgryte kgryte added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Apr 9, 2026
@kgryte
Copy link
Copy Markdown
Member

kgryte commented Apr 10, 2026

/stdlib merge

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label Apr 10, 2026
@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label Apr 10, 2026
@kgryte
Copy link
Copy Markdown
Member

kgryte commented Apr 10, 2026

@officiallyanee Merged in the latest develop, which should have the test fixes.

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 Changes Pull request which needs changes before being merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants