Skip to content

Commit

Permalink
add ability to use my FORTRAN implementation of the hypergeometric fu…
Browse files Browse the repository at this point in the history
…nction
  • Loading branch information
Guymer committed Aug 10, 2024
1 parent 9a793aa commit adf4ddf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions mod_f2py.F90
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MODULE mod_f2py
CONTAINS

! Include functions and subroutines ...
INCLUDE "mod_f2py/sub_hypergeometric.f90"
INCLUDE "mod_f2py/sub_t_CDF.f90"
INCLUDE "mod_f2py/sub_t_PDF.f90"
END MODULE mod_f2py
21 changes: 21 additions & 0 deletions mod_f2py/sub_hypergeometric.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
PURE SUBROUTINE sub_hypergeometric(a, b, c, z, ans)
!f2py threadsafe

! Import standard modules ...
USE ISO_C_BINDING

! Import my modules ...
USE mod_safe, ONLY: func_hypergeometric

IMPLICIT NONE

! Declare inputs/outputs ...
REAL(kind = C_DOUBLE), INTENT(in) :: a
REAL(kind = C_DOUBLE), INTENT(in) :: b
REAL(kind = C_DOUBLE), INTENT(in) :: c
REAL(kind = C_DOUBLE), INTENT(in) :: z
REAL(kind = C_DOUBLE), INTENT(out) :: ans

! Calculate answer ...
ans = func_hypergeometric(a, b, c, z)
END SUBROUTINE sub_hypergeometric
7 changes: 5 additions & 2 deletions tests/func_t_CDF.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from mod_f2py import mod_f2py

# Define function ...
def t_CDF(x, dof):
def t_CDF(x, dof, /, *, fortran = True):
"""This function calculates the CDF of Student's t-distribution in the same
way as the FORTRAN function in this repository.
"""
Expand Down Expand Up @@ -52,7 +52,10 @@ def t_CDF(x, dof):
z = - (x ** 2 / dof)

# Calculate CDF ...
ans = a + x * math.gamma(b) * scipy.special.hyp2f1(a, b, c, z) / (math.sqrt(dof * math.pi) * math.gamma(0.5 * dof))
if fortran:
ans = a + x * math.gamma(b) * mod_f2py.sub_hypergeometric(a, b, c, z) / (math.sqrt(dof * math.pi) * math.gamma(0.5 * dof))
else:
ans = a + x * math.gamma(b) * scipy.special.hyp2f1(a, b, c, z) / (math.sqrt(dof * math.pi) * math.gamma(0.5 * dof))

# Return answer ...
return ans
Expand Down
2 changes: 1 addition & 1 deletion tests/func_t_PDF.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from mod_f2py import mod_f2py

# Define function ...
def t_PDF(x, dof):
def t_PDF(x, dof, /):
"""This function calculates the PDF of Student's t-distribution in the same
way as the FORTRAN function in this repository.
"""
Expand Down

0 comments on commit adf4ddf

Please sign in to comment.