-
-
Notifications
You must be signed in to change notification settings - Fork 908
feat: add math/base/special/hyp2f1
#5140
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 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
438f8e0
feat: temp commit
anandkaranubc 2ebec87
feat: initial files
anandkaranubc 0d93dab
feat: update the JS implementation
anandkaranubc 5d17732
test: update test fixtures
anandkaranubc 078e839
chore: fix linting issues
anandkaranubc 7d81383
Merge remote-tracking branch 'upstream/develop' into feat/hyp2f1
stdlib-bot e438e2e
chore: fix python linting issues
anandkaranubc e6c5ce7
refactor: apply suggestions from PR review
anandkaranubc 42b5497
feat: add docs, benchmarks and examples
anandkaranubc a07d934
feat: add inline comments
anandkaranubc 146eecd
feat: add inline comments #2
anandkaranubc 140f6e7
refactor: apply suggestions from PR review
anandkaranubc 8f46980
docs: remove links from README
anandkaranubc bda7f1e
docs: follow consistent naming
anandkaranubc 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
215 changes: 215 additions & 0 deletions
215
lib/node_modules/@stdlib/math/base/special/hyp2f1/lib/hys2f1.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,215 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 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. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var PINF = require( '@stdlib/constants/float64/pinf' ); | ||
var round = require( '@stdlib/math/base/special/round' ); | ||
var abs = require( '@stdlib/math/base/special/abs' ); | ||
var isNonPositiveInteger = require( './utils.js' ).isNonPositiveInteger; | ||
|
||
|
||
// VARIABLES // | ||
|
||
var MACHEP = 1.11022302462515654042E-16; | ||
var EPS = 1.0e-13; | ||
var MAX_ITERATIONS = 10000; | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Evaluates the Gaussian hypergeometric function by two-term recurrence in `a`. | ||
* | ||
* @private | ||
* @param {number} a - input value | ||
* @param {number} b - input value | ||
* @param {number} c - input value | ||
* @param {number} x - input value | ||
* @param {number} loss - starting loss of significance | ||
* @returns {Object} the function value and error | ||
*/ | ||
function hyp2f1ra( a, b, c, x, loss ) { | ||
var f2Val; | ||
var f1Val; | ||
var f0Val; | ||
var err; | ||
var da; | ||
var f1; | ||
var f0; | ||
var t; | ||
var n; | ||
|
||
loss = 0.0; | ||
err = 0.0; | ||
|
||
if ( ( c < 0.0 && a <= c ) || ( c >= 0.0 && a >= c ) ) { | ||
da = round( a - c ); | ||
} | ||
else { | ||
da = round( a ); | ||
} | ||
|
||
t = a - da; | ||
if ( abs( da ) > MAX_ITERATIONS ) { | ||
loss = 1.0; | ||
return { | ||
'value': NaN, | ||
'error': loss | ||
}; | ||
} | ||
|
||
if ( da < 0.0 ) { | ||
f2Val = 0.0; | ||
f1 = hys2f1( t, b, c, x, err ); | ||
loss += f1.error; | ||
err = f1.error; | ||
f0 = hys2f1( t - 1.0, b, c, x, err ); | ||
loss += f0.error; | ||
t -= 1.0; | ||
f1Val = f1.value; | ||
f0Val = f0.value; | ||
for ( n = 1; n < -da; ++n ) { | ||
f2Val = f1Val; | ||
f1Val = f0Val; | ||
|
||
// eslint-disable-next-line max-len | ||
f0Val = -(((((2.0 * t) - c) - (t * x) + (b * x)) * f1Val) + ((t * (x - 1.0)) * f2Val)) / (c - t); | ||
t -= 1.0; | ||
} | ||
} | ||
else { | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
f2Val = 0.0; | ||
f1 = hys2f1( t, b, c, x, err ); | ||
loss += f1.error; | ||
err = f1.error; | ||
f0 = hys2f1( t + 1.0, b, c, x, err ); // CHECK IF err is THE SAME OR NEW ONE | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
loss += f0.error; | ||
t += 1.0; | ||
f1Val = f1.value; | ||
f0Val = f0.value; | ||
for ( n = 1; n < da; ++n ) { | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
f2Val = f1Val; | ||
f1Val = f0Val; | ||
|
||
// eslint-disable-next-line max-len | ||
f0Val = -(((((2.0 * t) - c) - (t * x) + (b * x)) * f1Val) + ((c - t) * f2Val)) / (t * (x - 1.0)); | ||
t += 1.0; | ||
} | ||
} | ||
|
||
return { | ||
'value': f0Val, | ||
'error': loss | ||
}; | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Evaluates the power series expansion of Gaussian hypergeometric function. | ||
* | ||
* @private | ||
* @param {number} a - input value | ||
* @param {number} b - input value | ||
* @param {number} c - input value | ||
* @param {number} x - input value | ||
* @param {number} loss - starting loss of significance | ||
* @returns {Object} the function value and error | ||
*/ | ||
function hys2f1( a, b, c, x, loss ) { | ||
var intFlag; | ||
var umax; | ||
var f; | ||
var g; | ||
var h; | ||
var k; | ||
var m; | ||
var s; | ||
var u; | ||
var i; | ||
|
||
intFlag = 0; | ||
|
||
if ( abs( b ) > abs( a ) ) { | ||
f = b; | ||
b = a; | ||
a = f; | ||
} | ||
|
||
if ( isNonPositiveInteger( b ) && ( abs( b ) < abs( a ) ) ) { | ||
f = b; | ||
b = a; | ||
a = f; | ||
intFlag = 1; | ||
} | ||
|
||
// eslint-disable-next-line max-len | ||
if ( ( ( abs( a ) > abs( c ) + 1.0 ) || intFlag ) && ( abs( c - a ) > 2.0 ) && ( abs( a ) > 2.0 ) ) { | ||
return hyp2f1ra( a, b, c, x, loss ); | ||
} | ||
|
||
i = 0.0; | ||
umax = 0.0; | ||
f = a; | ||
g = b; | ||
h = c; | ||
s = 1.0; | ||
u = 1.0; | ||
k = 0.0; | ||
|
||
do { | ||
if ( abs( h ) < EPS ) { | ||
loss = 1.0; | ||
return { | ||
'value': PINF, | ||
'error': loss | ||
}; | ||
} | ||
m = k + 1.0; | ||
u *= ( ( f + k ) * ( g + k ) * x / ( ( h + k ) * m ) ); | ||
s += u; | ||
k = abs( u ); | ||
if ( k > umax ) { | ||
umax = k; | ||
} | ||
k = m; | ||
i += 1.0; | ||
if ( i > MAX_ITERATIONS ) { | ||
loss = 1.0; | ||
return { | ||
'value': s, | ||
'error': loss | ||
}; | ||
} | ||
} while ( s === 0.0 || abs( u / s ) > MACHEP ); | ||
|
||
loss = ( ( MACHEP * umax ) / abs( s ) ) + ( MACHEP * i ); | ||
return { | ||
'value': s, | ||
'error': loss | ||
}; | ||
} | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = hys2f1; |
Oops, something went wrong.
Oops, something went wrong.
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.