-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor: improve the implementation of math/base/special/hyp2f1
#11325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b359e62
chore: improve the implementation
officiallyanee c5fec0a
Merge remote-tracking branch 'upstream/develop' into improve/hyp2f1
stdlib-bot cc3d8a1
Merge remote-tracking branch 'upstream/develop' into improve/hyp2f1
stdlib-bot c9ec417
chore: update test to use ULP-based testing & move hyp2f1NegCEqualBC …
officiallyanee f39d34d
Apply suggestions from code review
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
lib/node_modules/@stdlib/math/base/special/hyp2f1/lib/hyp2f1negcequalbc.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * | ||
| * ## Notice | ||
| * | ||
| * The original C code and copyright notice are from the [Cephes Mathematical Library]{@link https://www.netlib.org/cephes/}. The implementation has been modified for JavaScript. | ||
| * | ||
| * ```text | ||
| * (C) Copyright Stephen L. Moshier 1984, 1987, 1992, 2000. | ||
| * | ||
| * Use, modification and distribution are subject to the | ||
| * Cephes Mathematical Library License. (See accompanying file | ||
| * LICENSE or copy at https://smath.com/en-US/view/CephesMathLibrary/license) | ||
| * ``` | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var abs = require( '@stdlib/math/base/special/abs' ); | ||
| var max = require( '@stdlib/math/base/special/max' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| /** | ||
| * 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; | ||
|
|
||
| collectorMax = 1.0; | ||
| collector = 1.0; | ||
| sum = 1.0; | ||
|
|
||
| if ( abs( b ) >= 1.0e5 ) { | ||
| return NaN; | ||
| } | ||
| 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; | ||
| } | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| module.exports = hyp2f1NegCEqualBC; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/node_modules/@stdlib/math/base/special/hyp2f1/test/fixtures/python/edge_cases3.json
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
lib/node_modules/@stdlib/math/base/special/hyp2f1/test/fixtures/python/edge_cases5.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"a": [1.5, 1.5, 2.5, 0.5, 1.5, 0.5, 2.0, 1.5, 2.5, 1.5], "b": [2.5, 2.5, 1.5, 2.5, 2.5, 1.5, 3.0, 3.5, 2.5, 3.5], "c": [3.0, 3.0, 3.0, 2.0, 2.0, 1.0, 4.0, 4.0, 4.0, 3.0], "x": [0.9, 0.95, 0.9, 0.92, 0.91, 0.9, 0.9, 0.88, 0.93, 0.91], "expected": [14.663397526525134, 30.949523656432504, 14.663397526525134, 6.507774205759818, 106.96827020610523, 7.033214388515233, 21.78942310292968, 13.41253423166626, 36.54504530314331, 88.86455455103395]} |
1 change: 1 addition & 0 deletions
1
lib/node_modules/@stdlib/math/base/special/hyp2f1/test/fixtures/python/edge_cases6.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"a": [2.0, 2.0, 3.0, 0.5, 1.5, 2.0, 4.0, 0.5], "b": [-1.0, -2.0, -2.0, -3.0, -3.0, -4.0, -2.0, -1.0], "c": [-1.0, -2.0, -2.0, -3.0, -3.0, -4.0, -2.0, -1.0], "x": [0.5, 0.5, 0.3, 0.7, -0.5, 0.6, 0.8, -0.9], "expected": [2.0, 2.75, 2.44, 1.6409375, 0.4453125, 4.792, 10.600000000000001, 0.55]} |
2 changes: 1 addition & 1 deletion
2
lib/node_modules/@stdlib/math/base/special/hyp2f1/test/fixtures/python/outliers.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| {"a": [-1.8, 1.8, 1.8, 1.8, 1.8, 5.0, 10.0, 10.0, 2.0, 2.0, -1.8, 1.8, 10.0, 10.0, 0.5, -8.0, -0.1156544430516313, -27.911722328444608, -10.184355055477743, -5.0083982772911, -0.7429095709749414, -14.55029516886238, -5.428179853908536, -1.8190858888165087, -0.9534205693212994, -31.70680212982947, -0.18350917503602782, -200.19574300099214, -8.837706832399117, -3.2052905962805456, -13.37614633859299], "b": [7.4, -2.5, 1.0, 7.4, 7.4, 7.4, 7.4, 7.4, 3.0, 2.5, -2.5, 7.4, 1.0, 7.4, -269.5, 18.016500331508873, 12.722140489351698, 14.018736008860946, 6.5450429328481174, 171.09919289684245, 11.725525989335528, 0.46553367087834685, 26.782963689614604, 21.025202609254364, 10.024920055648606, 0.11445767340979862, 14.696533286274349, 1.9444493115386567, 0.19096384651567044, 0.3048596186043564, 1.337786902832312], "c": [20.4, 20.4, 20.4, -1.8, 20.4, 20.4, 20.4, 20.4, 5.0, -3.25, 20.4, -1.8, -1.8, -1.8, 1.5, 10.805295997850628, -0.6030985338257255, -1.0168630024136034, -0.44051751620383994, -1.4923865283013589, -7.299203351011158, -0.5349901418098844, -4.162402828198655, -0.6960141045675334, -0.23845367675425955, -0.5570625216964069, -6.338990250643928, -0.6933677849479691, -2.3808990396202425, -0.7039276707364683, -0.05015281580946018], "x": [-10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0, 0.95, 0.99, 0.999, -10.0, -1.01, -0.99, -0.99, 0.998001, 0.90875647507, -0.0005448214561780684, 0.6228483452847298, 0.6555896492302491, 0.7073764056574465, 0.11534401417791407, 0.6086682776938632, 0.10047548647104909, 0.2570211738060222, 0.4419851803780206, 0.020574313007314604, -0.9003519736816863, 0.24492811068225362, -0.27624718180517105, 0.8496565195400692, -0.0022109253176725296], "expected": [16.334204105729413, 5.883122144128727, 0.5820646473844754, 0.010147094307759376, 0.07291374137469146, 0.0011628419587190388, 8.042653908304492e-06, 566.0092271657466, 27.699347904322664, 2.183739328012162e+26, -0.5786871007671348, 0.10921062752340924, -0.8752587026378827, 0.4234752106080307, 0.053963052503373715, -3.566216341436626e-09, 0.998681836639102, 5.33299803869569, 1.1177709865124041, 3.491332086988064e+82, 1.132062691110089, -0.0006927158077474787, 98.41698380414908, -221.57018712798285, 43.53554136829992, 1.0540592696289446, 0.5371016318543966, 0.0028040224553278668, -2.4067466563131665, 0.028751519134363426, 0.18419839456905535]} | ||
| {"a": [-1.8, 1.8, 1.8, 1.8, 1.8, 5.0, 10.0, 10.0, 2.0, 2.0, -1.8, 1.8, 10.0, 10.0, 0.5, -8.0, -0.1156544430516313, -27.911722328444608, -10.184355055477743, -5.0083982772911, -0.7429095709749414, -14.55029516886238, -5.428179853908536, -1.8190858888165087, -0.9534205693212994, -31.70680212982947, -0.18350917503602782, -200.19574300099214, -8.837706832399117, -3.2052905962805456, -13.37614633859299], "b": [7.4, -2.5, 1.0, 7.4, 7.4, 7.4, 7.4, 7.4, 3.0, 2.5, -2.5, 7.4, 1.0, 7.4, -269.5, 18.016500331508873, 12.722140489351698, 14.018736008860946, 6.5450429328481174, 171.09919289684245, 11.725525989335528, 0.46553367087834685, 26.782963689614604, 21.025202609254364, 10.024920055648606, 0.11445767340979862, 14.696533286274349, 1.9444493115386567, 0.19096384651567044, 0.3048596186043564, 1.337786902832312], "c": [20.4, 20.4, 20.4, -1.8, 20.4, 20.4, 20.4, 20.4, 5.0, -3.25, 20.4, -1.8, -1.8, -1.8, 1.5, 10.805295997850628, -0.6030985338257255, -1.0168630024136034, -0.44051751620383994, -1.4923865283013589, -7.299203351011158, -0.5349901418098844, -4.162402828198655, -0.6960141045675334, -0.23845367675425955, -0.5570625216964069, -6.338990250643928, -0.6933677849479691, -2.3808990396202425, -0.7039276707364683, -0.05015281580946018], "x": [-10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0, 0.95, 0.99, 0.999, -10.0, -1.01, -0.99, -0.99, 0.998001, 0.90875647507, -0.0005448214561780684, 0.6228483452847298, 0.6555896492302491, 0.7073764056574465, 0.11534401417791407, 0.6086682776938632, 0.10047548647104909, 0.2570211738060222, 0.4419851803780206, 0.020574313007314604, -0.9003519736816863, 0.24492811068225362, -0.27624718180517105, 0.8496565195400692, -0.0022109253176725296], "expected": [16.334204105729413, 5.883122144128727, 0.5820646473844754, 0.010147094307759376, 0.07291374137469146, 0.0011628419587190388, 8.042653908304492e-06, 566.0092271658314, 27.699347904322664, 2.1837393280121743e+26, -0.5786871007671348, 0.10921062752340924, -0.8752587026378827, 0.4234752106080307, 0.053963052503373715, -3.566216341436626e-09, 0.998681836639102, 5.33299803869569, 1.1177709865124041, 3.491332086988064e+82, 1.132062691110089, -0.0006927158077474787, 98.41698380414908, -221.57018712798285, 43.53554136829992, 1.0540592696289446, 0.5371016318543966, 0.0028040224553278668, -2.4067466563131665, 0.028751519134363426, 0.18419839456905535]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.9was chosen in the first place?There was a problem hiding this comment.
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:

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 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for digging up the reference!