-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add general integer exponents #128
Changes from 3 commits
d67ff35
d2dccf0
ad9f194
1d2ed8a
54b6fd6
401e65d
738c8e9
60be4ac
cbf8d3b
323c920
cff0039
ee63871
60dcade
2418c37
f077ba6
99421a3
0f8d7e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from typing import Optional | ||
|
||
import torch | ||
from torch.special import gammainc, gammaincc, gammaln | ||
from torch.special import gammaln | ||
|
||
from .potential import Potential | ||
|
||
|
@@ -17,6 +17,30 @@ def gamma(x: torch.Tensor) -> torch.Tensor: | |
return torch.exp(gammaln(x)) | ||
|
||
|
||
# Auxilary function for stable Fourier transform implementation | ||
def gammainc_upper_over_powerlaw(exponent, zz): | ||
if exponent not in [1, 2, 3, 4, 5, 6]: | ||
raise ValueError(f"Unsupported exponent: {exponent}") | ||
|
||
if exponent == 1: | ||
return torch.exp(-zz) / zz | ||
if exponent == 2: | ||
return torch.sqrt(torch.pi / zz) * torch.erfc(torch.sqrt(zz)) | ||
if exponent == 3: | ||
return -torch.expi(-zz) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I know |
||
if exponent == 4: | ||
return 2 * ( | ||
torch.exp(-zz) - torch.sqrt(torch.pi * zz) * torch.erfc(torch.sqrt(zz)) | ||
) | ||
if exponent == 5: | ||
return torch.exp(-zz) + zz * torch.expi(-zz) | ||
if exponent == 6: | ||
return ( | ||
(2 - 4 * zz) * torch.exp(-zz) | ||
+ 4 * torch.sqrt(torch.pi) * zz**1.5 * torch.erfc(torch.sqrt(zz)) | ||
) / 3 | ||
PicoCentauri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we double-check that everything here is correct? With the new implementation, |
||
class InversePowerLawPotential(Potential): | ||
""" | ||
Inverse power-law potentials of the form :math:`1/r^p`. | ||
|
@@ -46,7 +70,7 @@ class InversePowerLawPotential(Potential): | |
|
||
def __init__( | ||
self, | ||
exponent: float, | ||
exponent: int, | ||
smearing: Optional[float] = None, | ||
exclusion_radius: Optional[float] = None, | ||
dtype: Optional[torch.dtype] = None, | ||
|
@@ -58,8 +82,8 @@ def __init__( | |
if device is None: | ||
device = torch.device("cpu") | ||
|
||
if exponent <= 0 or exponent > 3: | ||
raise ValueError(f"`exponent` p={exponent} has to satisfy 0 < p <= 3") | ||
# function call to check the validity of the exponent | ||
gammainc_upper_over_powerlaw(exponent, torch.tensor(1.0, dtype=dtype, device=device)) | ||
self.register_buffer( | ||
"exponent", torch.tensor(exponent, dtype=dtype, device=device) | ||
) | ||
|
@@ -103,7 +127,7 @@ def lr_from_dist(self, dist: torch.Tensor) -> torch.Tensor: | |
x = 0.5 * dist**2 / smearing**2 | ||
peff = exponent / 2 | ||
prefac = 1.0 / (2 * smearing**2) ** peff | ||
return prefac * gammainc(peff, x) / x**peff | ||
return self.from_dist(dist) - prefac * gammainc_upper_over_powerlaw(exponent, x) | ||
|
||
@torch.jit.export | ||
def lr_from_k_sq(self, k_sq: torch.Tensor) -> torch.Tensor: | ||
|
@@ -136,7 +160,7 @@ def lr_from_k_sq(self, k_sq: torch.Tensor) -> torch.Tensor: | |
return torch.where( | ||
k_sq == 0, | ||
0.0, | ||
prefac * gammaincc(peff, masked) / masked**peff * gamma(peff), | ||
prefac * gammainc_upper_over_powerlaw(exponent, masked), | ||
) | ||
|
||
def self_contribution(self) -> torch.Tensor: | ||
|
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.
Give the equation in the docstring here.