-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from lab-cosmo/torchexp1
Add PyTorch implementation of the exponential integral function
- Loading branch information
Showing
6 changed files
with
84 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 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,6 +1,6 @@ | ||
Math | ||
#### | ||
|
||
.. autofunction:: torchpme.lib.exp1 | ||
.. autofunction:: torchpme.lib.gamma | ||
.. autofunction:: torchpme.lib.torch_exp1 | ||
.. autofunction:: torchpme.lib.gammaincc_over_powerlaw |
This file contains 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 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 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 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,25 +1,31 @@ | ||
import numpy as np | ||
import scipy.special | ||
import torch | ||
from scipy.special import exp1 | ||
|
||
from torchpme.lib import torch_exp1 | ||
from torchpme.lib import exp1 | ||
|
||
|
||
def finite_difference_derivative(func, x, h=1e-5): | ||
return (func(x + h) - func(x - h)) / (2 * h) | ||
|
||
|
||
def test_torch_exp1_consistency_with_scipy(): | ||
x = torch.rand(1000, dtype=torch.float64) | ||
torch_result = torch_exp1(x) | ||
scipy_result = exp1(x.numpy()) | ||
assert np.allclose(torch_result.numpy(), scipy_result, atol=1e-6) | ||
seed = 42 | ||
torch.manual_seed(seed) | ||
np.random.seed(seed) | ||
random_tensor = torch.rand(100000) * 1000 | ||
random_array = random_tensor.numpy() | ||
scipy_result = scipy.special.exp1(random_array) | ||
torch_result = exp1(random_tensor) | ||
assert np.allclose(scipy_result, torch_result.numpy(), atol=1e-15) | ||
|
||
|
||
def test_torch_exp1_derivative(): | ||
x = torch.rand(1, dtype=torch.float64, requires_grad=True) | ||
torch_result = torch_exp1(x) | ||
torch_result = exp1(x) | ||
torch_result.backward() | ||
torch_exp1_prime = x.grad | ||
finite_diff_result = finite_difference_derivative(exp1, x.detach().numpy()) | ||
finite_diff_result = finite_difference_derivative( | ||
scipy.special.exp1, x.detach().numpy() | ||
) | ||
assert np.allclose(torch_exp1_prime.numpy(), finite_diff_result, atol=1e-6) |