Is there a hankel function in the pytorch?

username: casual_dong
cooked: <p>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.</p>

username: KFrank
cooked: <p>Hi Dong!</p>
<aside class="quote no-group" data-username="casual_dong" data-post="1" data-topic="185224" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/casual_dong/48/48069_2.png" class="avatar"> casual_dong:</div>
<blockquote>
<p>There only have first kind of bessel function and haven’t second kind of bessel function and hankel function.</p>
</blockquote>
</aside>
<p>It appears that pytorch does implement  bessel functions of the second<br>
kind for orders 0 and 1 as <code>torch.special.bessel_y0()</code> and<br>
<code>torch.special.bessel_y1()</code>, but only for real arguments and<br>
differentiation (autograd) is not supported.  (The <a href="https://pytorch.org/docs/2.0/special.html" rel="noopener nofollow ugc">torch.special documentation</a><br>
seems not to mention <code>y0()</code> and <code>y1()</code>.)  I do not believe that pytorch currently<br>
implements hankel functions themselves.</p>
<p>Here’s an illustration for the current stable version, pytorch 2.0.1:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'2.0.1'
&gt;&gt;&gt; tc = torch.tensor ([1.23], requires_grad = True)
&gt;&gt;&gt; tg = torch.tensor ([1.23], requires_grad = True, device = 'cuda')
&gt;&gt;&gt; torch.special.bessel_j0 (tc)
tensor([0.6561])
&gt;&gt;&gt; torch.special.bessel_j1 (tc)
tensor([0.5058])
&gt;&gt;&gt; torch.special.bessel_y0 (tc)
tensor([0.2464])
&gt;&gt;&gt; torch.special.bessel_y1 (tc)
tensor([-0.5990])
&gt;&gt;&gt; torch.special.bessel_j0 (tg)
tensor([0.6561], device='cuda:0')
&gt;&gt;&gt; torch.special.bessel_j1 (tg)
tensor([0.5058], device='cuda:0')
&gt;&gt;&gt; torch.special.bessel_y0 (tg)
tensor([0.2464], device='cuda:0')
&gt;&gt;&gt; torch.special.bessel_y1 (tg)
tensor([-0.5990], device='cuda:0')
</code></pre>
<p>Note the absence of a <code>grad_fn</code> for the results of the bessel functions.</p>
<p>For some context, see this github issue:</p>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/76324">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/76324" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/76324" target="_blank" rel="noopener nofollow ugc">Bessel and Related Functions</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2022-04-25" data-time="18:10:09" data-timezone="UTC">06:10PM - 25 Apr 22 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/0x00b1" target="_blank" rel="noopener nofollow ugc">
          <img alt="0x00b1" src="https://avatars.githubusercontent.com/u/315821?v=4" class="onebox-avatar-inline" width="20" height="20">
          0x00b1
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          feature
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: special
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container"># Bessel and Related Functions

A brief proposal for providing a complete suit<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">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)$.</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Best.</p>
<p>K. Frank</p>

username: casual_dong
cooked: <p>Hi K.Frank！<br>
Thank you for your helping. Yeah,the bessel functions of the second<br>
kind do not support differentiation. Maybe using the part of the scipy.special in the tensowflow can do differentiate.<img src="https://discuss.pytorch.org/images/emoji/apple/thinking.png?v=12" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p>
<p>Best.<br>
Dong</p>

username: KFrank
cooked: <p>Hi Dong!</p>
<aside class="quote no-group" data-username="casual_dong" data-post="3" data-topic="185224" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/casual_dong/48/48069_2.png" class="avatar"> casual_dong:</div>
<blockquote>
<p>Maybe using the part of the scipy.special in the tensowflow can do differentiate.</p>
</blockquote>
</aside>
<p>You can wrap <code>scipy.special</code>’s <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.hankel1.html" rel="noopener nofollow ugc">hankel function</a> in a <a href="https://pytorch.org/docs/2.0/notes/extending.html" rel="noopener nofollow ugc">custom autograd function.</a><br>
Put, for example, <code>hankel1()</code> in your custom function’s <code>forward()</code> method<br>
and implement the rather straightforward expression for the derivative of the<br>
hankel function (see, for example, <a href="https://mathworld.wolfram.com/HankelFunctionoftheFirstKind.html" rel="noopener nofollow ugc">MathWorld</a>), again using <code>scipy</code>, in the custom<br>
function’s <code>backward()</code> method.</p>
<p>Good luck!</p>
<p>K. Frank</p>