Skip to content

Commit 95ce102

Browse files
Aarsh-WankarricardoV94
authored andcommitted
Add kn function as helper for modified Bessel function of the second kind and corresponding tests
1 parent b75c18f commit 95ce102

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pytensor/tensor/math.py

+6
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,11 @@ def kv(v, x):
24532453
return kve(v, x) * exp(-x)
24542454

24552455

2456+
def kn(n, x):
2457+
"""Modified Bessel function of the second kind of integer order v."""
2458+
return kv(n, x)
2459+
2460+
24562461
@scalar_elemwise
24572462
def sigmoid(x):
24582463
"""Logistic sigmoid function (1 / (1 + exp(-x)), also known as expit or inverse logit"""
@@ -4337,6 +4342,7 @@ def nan_to_num(x, nan=0.0, posinf=None, neginf=None):
43374342
"i1",
43384343
"iv",
43394344
"ive",
4345+
"kn",
43404346
"kv",
43414347
"kve",
43424348
"sigmoid",

tests/tensor/test_math_scipy.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pytensor import tensor as pt
1919
from pytensor.compile.mode import get_default_mode
2020
from pytensor.configdefaults import config
21-
from pytensor.tensor import gammaincc, inplace, kv, kve, vector
21+
from pytensor.tensor import gammaincc, inplace, kn, kv, kve, vector
2222
from tests import unittest_tools as utt
2323
from tests.tensor.utils import (
2424
_good_broadcast_unary_chi2sf,
@@ -1220,3 +1220,17 @@ def test_kv():
12201220
out.eval({v: test_v, x: test_x}),
12211221
scipy.special.kv(test_v[:, None], test_x[None, :]),
12221222
)
1223+
1224+
1225+
def test_kn():
1226+
n = vector("n")
1227+
x = vector("x")
1228+
1229+
out = kn(n[:, None], x[None, :])
1230+
test_n = np.array([-3, 4, 0, 5], dtype=n.type.dtype)
1231+
test_x = np.linspace(0, 512, 10, dtype=x.type.dtype)
1232+
1233+
np.testing.assert_allclose(
1234+
out.eval({n: test_n, x: test_x}),
1235+
scipy.special.kn(test_n[:, None], test_x[None, :]),
1236+
)

0 commit comments

Comments
 (0)