I want to use hankle function with torch that can through grid. There only have first kind of bessel function and haven’t second kind of bessel function and hankel function. I don’t if it is right. Thank you very munch.
KFrank
(K. Frank)
July 29, 2023, 12:40am
2
Hi Dong!
It appears that pytorch does implement bessel functions of the second
kind for orders 0 and 1 as torch.special.bessel_y0()
and
torch.special.bessel_y1()
, but only for real arguments and
differentiation (autograd) is not supported. (The torch.special documentation
seems not to mention y0()
and y1()
.) I do not believe that pytorch currently
implements hankel functions themselves.
Here’s an illustration for the current stable version, pytorch 2.0.1:
>>> import torch
>>> torch.__version__
'2.0.1'
>>> tc = torch.tensor ([1.23], requires_grad = True)
>>> tg = torch.tensor ([1.23], requires_grad = True, device = 'cuda')
>>> torch.special.bessel_j0 (tc)
tensor([0.6561])
>>> torch.special.bessel_j1 (tc)
tensor([0.5058])
>>> torch.special.bessel_y0 (tc)
tensor([0.2464])
>>> torch.special.bessel_y1 (tc)
tensor([-0.5990])
>>> torch.special.bessel_j0 (tg)
tensor([0.6561], device='cuda:0')
>>> torch.special.bessel_j1 (tg)
tensor([0.5058], device='cuda:0')
>>> torch.special.bessel_y0 (tg)
tensor([0.2464], device='cuda:0')
>>> torch.special.bessel_y1 (tg)
tensor([-0.5990], device='cuda:0')
Note the absence of a grad_fn
for the results of the bessel functions.
For some context, see this github issue:
opened 06:10PM - 25 Apr 22 UTC
feature
triaged
module: special
# Bessel and Related Functions
A brief proposal for providing a complete suit… e of Bessel and related functions as PyTorch operators. Enjoy!
One of a five-part series of special functions issues:
- Gamma and Related Functions (#78065)
- Bessel and Related Functions (#76324)
- Orthogonal Polynomials (#80152)
- Elliptic Functions and Integrals (#80157)
-
## API
### Bessel Functions
- [ ] `bessel_j(input: Tensor, n: int = 0) → Tensor`
Bessel function of the first kind, $J_{n}\left(\text{input}\right)$.
- [x] `bessel_j_0(input: Tensor) → Tensor`
Bessel function of the first kind at order $0$, $J_{0}\left(\text{input}\right)$.
- [x] `bessel_j_1(input: Tensor) → Tensor`
Bessel function of the first kind at order $1$, $J_{1}\left(\text{input}\right)$.
- [ ] `bessel_y(input: Tensor, n: int = 0) → Tensor`
Bessel function of the second kind, $Y_{n}\left(\text{input}\right)$.
- [x] `bessel_y_0(input: Tensor) → Tensor`
Bessel function of the second kind at order $0$, $Y_{0}\left(\text{input}\right)$.
- [x] `bessel_y_1(input: Tensor) → Tensor`
Bessel function of the second kind at order $1$, $Y_{1}\left(\text{input}\right)$.
### Modified Bessel Functions
- [ ] `modified_bessel_i(input: Tensor, n: int = 0) → Tensor`
Modified Bessel function of the first kind, $I_{n}\left(\text{input}\right)$.
- [x] `modified_bessel_i_0(input: Tensor) → Tensor`
Modified Bessel function of the first kind at order $0$, $I_{0}\left(\text{input}\right)$.
- [x] `modified_bessel_i_1(input: Tensor) → Tensor`
Modified Bessel function of the first kind at order $1$, $I_{1}\left(\text{input}\right)$.
- [ ] `modified_bessel_k(input: Tensor, n: int = 0) → Tensor`
Modified Bessel function of the second kind, $K_{n}\left(\text{input}\right)$.
- [x] `modified_bessel_k_0(input: Tensor) → Tensor`
Modified Bessel function of the second kind at order $0$, $K_{0}\left(\text{input}\right)$.
- [x] `modified_bessel_k_1(input: Tensor) → Tensor`
Bessel function of the second kind at order $1$, $K_{1}\left(\text{input}\right)$.
### Exponentially Scaled Modified Bessel Functions
- [ ] `scaled_modified_bessel_i(input: Tensor, n: int = 0) → Tensor`
Exponentially scaled modified Bessel function of the first kind:
$$\text{exp}\left(|-\text{input}|\right)I_{n}\left(\text{input}\right)$$
- [ ] `scaled_modified_bessel_i_0(input: Tensor) → Tensor`
Exponentially scaled modified Bessel function of the first kind at order $0$:
$$\text{exp}\left(|-\text{input}|\right)I_{0}\left(\text{input}\right).$$
- [ ] `scaled_modified_bessel_i_1(input: Tensor) → Tensor`
Exponentially scaled modified Bessel function of the first kind at order $1$:
$$\text{exp}\left(|-\text{input}|\right)I_{1}\left(\text{input}\right)$$
- [ ] `scaled_modified_bessel_k(input: Tensor, n: int = 0) → Tensor`
Exponentially scaled modified Bessel function of the second kind:
$$\text{exp}\left(|-\text{input}|\right)K_{n}\left(\text{input}\right)$$
- [ ] `scaled_modified_bessel_k_0(input: Tensor) → Tensor`
Exponentially scaled modified Bessel function of the second kind at order $0$:
$$\text{exp}\left(|-\text{input}|\right)K_{0}\left(\text{input}\right)$$
- [x] `scaled_modified_bessel_k_1(input: Tensor) → Tensor`
Exponentially scaled modified Bessel function of the second kind at order $1$:
$$\text{exp}\left(|-\text{input}|\right)K_{1}\left(\text{input}\right)$$
### Spherical Bessel Functions
- [ ] `spherical_bessel_j(input: Tensor, n: int = 0) → Tensor`
Spherical Bessel function of the first kind, $j_{n}\left(\text{input}\right)$.
- [ ] `spherical_bessel_j_0(input: Tensor) → Tensor`
Spherical Bessel function of the first kind at order $0$, $j_{0}\left(\text{input}\right)$.
- [ ] `spherical_bessel_j_1(input: Tensor) → Tensor`
Spherical Bessel function of the first kind at order $1$, $j_{1}\left(\text{input}\right)$.
- [ ] `spherical_bessel_y(input: Tensor, n: int = 0) → Tensor`
Spherical Bessel function of the second kind $y_{n}\left(\text{input}\right)$.
- [ ] `spherical_bessel_y_0(input: Tensor) → Tensor`
Spherical Bessel function of the second kind at order $0$, $y_{0}\left(\text{input}\right)$.
- [ ] `spherical_bessel_y_1(input: Tensor) → Tensor`
Spherical Bessel function of the second kind at order $1$, $y_{1}\left(\text{input}\right)$.
### Hankel Functions
- [ ] `hankel_1(input: Tensor, a) → Tensor`
Hankel function of the first kind $H_{\alpha}^{1}\left(\text{input}\right)$.
- [ ] `hankel_2(input: Tensor, a) → Tensor`
Hankel function of the second kind $H_{\alpha}^{2}\left(\text{input}\right)$.
### Spherical Hankel Functions
- [ ] `spherical_hankel_1(input: Tensor, a) → Tensor`
Spherical Hankel function of the first kind $h_{\alpha}^{1}\left(\text{input}\right)$.
- [ ] `spherical_hankel_2(input: Tensor, a) → Tensor`
Spherical Hankel function of the second kind $h_{\alpha}^{2}\left(\text{input}\right)$.
### Airy Functions and Derivatives
- [x] `airy_ai(input: Tensor) → Tensor`
Airy function $\text{Ai}\left(\text{input}\right)$.
- [ ] `airy_bi(input: Tensor) → Tensor`
Airy function $\text{Bi}\left(\text{input}\right)$.
- [ ] `airy_derivative_ai(input: Tensor) → Tensor`
Derivative of Airy function $\text{Ai}'\left(\text{input}\right)$.
- [ ] `airy_derivative_bi(input: Tensor) → Tensor`
Derivative of Airy function $\text{Bi}'\left(\text{input}\right)$.
### Scorer Functions
- [ ] `scorer_gi(input: Tensor) → Tensor`
Scorer function $\text{Gi}\left(\text{input}\right)$.
- [ ] `scorer_hi(input: Tensor) → Tensor`
Scorer function $\text{Hi}\left(\text{input}\right)$.
### Kelvin Functions
- [ ] `kelvin_ber(input: Tensor) → Tensor`
Kelvin function $\text{ber}\left(\text{input}\right)$.
- [ ] `kelvin_bei(input: Tensor) → Tensor`
Kelvin function $\text{bei}\left(\text{input}\right)$.
- [ ] `kelvin_ker(input: Tensor) → Tensor`
Kelvin function $\text{ker}\left(\text{input}\right)$.
- [ ] `kelvin_kei(input: Tensor) → Tensor`
Kelvin function $\text{kei}\left(\text{input}\right)$.
Best.
K. Frank
Hi K.Frank!
Thank you for your helping. Yeah,the bessel functions of the second
kind do not support differentiation. Maybe using the part of the scipy.special in the tensowflow can do differentiate.
Best.
Dong
KFrank
(K. Frank)
August 2, 2023, 5:12pm
4
Hi Dong!
You can wrap scipy.special
’s hankel function in a custom autograd function.
Put, for example, hankel1()
in your custom function’s forward()
method
and implement the rather straightforward expression for the derivative of the
hankel function (see, for example, MathWorld ), again using scipy
, in the custom
function’s backward()
method.
Good luck!
K. Frank