Skip to content

Commit 9dc2abb

Browse files
committed
Revised polynomials.py and created test_polynomial.py
1 parent a3e9993 commit 9dc2abb

File tree

3 files changed

+221
-40
lines changed

3 files changed

+221
-40
lines changed

deeptrack/backend/polynomials.py

Lines changed: 147 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,86 @@
1-
""" Expands the set of polynomials available through scipy
1+
"""Bessel and Riccati-Bessel polynomials.
2+
3+
This file defines a set of functions for computing Bessel and Riccati-Bessel
4+
polynomials and their derivatives. It expands the capabilities of `scipy`.
5+
6+
Functions
7+
---------
8+
besselj(
9+
l: Union[int, float],
10+
x: Union[int, float, np.ndarray]
11+
) -> Union[float, np.ndarray]
12+
Computes the Bessel polynomial of the first kind for a given order `l`
13+
and input `x`.
14+
15+
dbesselj(
16+
l: Union[int, float],
17+
x: Union[int, float, np.ndarray]
18+
) -> Union[float, np.ndarray]
19+
Computes the first derivative of the Bessel polynomial of the first kind.
20+
21+
bessely(
22+
l: Union[int, float],
23+
x: Union[int, float, np.ndarray]
24+
) -> Union[float, np.ndarray]
25+
Computes the Bessel polynomial of the second kind for a given order `l`
26+
and input `x`.
27+
28+
dbessely(
29+
l: Union[int, float],
30+
x: Union[int, float, np.ndarray]
31+
) -> Union[float, np.ndarray]
32+
Computes the first derivative of the Bessel polynomial of the second kind.
33+
34+
ricbesj(
35+
l: Union[int, float],
36+
x: Union[int, float, np.ndarray]
37+
) -> Union[float, np.ndarray]
38+
Computes the Riccati-Bessel polynomial of the first kind.
39+
40+
dricbesj(
41+
l: Union[int, float],
42+
x: Union[int, float, np.ndarray]
43+
) -> Union[float, np.ndarray]
44+
Computes the first derivative of the Riccati-Bessel polynomial of the
45+
first kind.
46+
47+
ricbesy(
48+
l: Union[int, float],
49+
x: Union[int, float, np.ndarray]
50+
) -> Union[float, np.ndarray]
51+
Computes the Riccati-Bessel polynomial of the second kind.
52+
53+
dricbesy(
54+
l: Union[int, float],
55+
x: Union[int, float, np.ndarray]
56+
) -> Union[float, np.ndarray]
57+
Computes the first derivative of the Riccati-Bessel polynomial of the
58+
second kind.
59+
60+
ricbesh(
61+
l: Union[int, float],
62+
x: Union[int, float, np.ndarray]
63+
) -> Union[float, np.ndarray]
64+
Computes the Riccati-Bessel polynomial of the third kind.
65+
66+
dricbesh(
67+
l: Union[int, float],
68+
x: Union[int, float, np.ndarray]
69+
) -> Union[float, np.ndarray]
70+
Computes the first derivative of the Riccati-Bessel polynomial of the
71+
third kind.
272
"""
373

474
import numpy as np
5-
from scipy.special import jv, spherical_jn, h1vp, jvp, yv
75+
from scipy.special import jv, h1vp, yv
76+
from typing import Union
677

778

8-
def ricbesj(l, x):
9-
"""The Riccati-Bessel polynomial of the first kind.
79+
def besselj(
80+
l: Union[int, float],
81+
x: Union[int, float, np.ndarray],
82+
) -> Union[float, np.ndarray]:
83+
"""The Bessel polynomial of the first kind.
1084
1185
Parameters
1286
----------
@@ -21,11 +95,14 @@ def ricbesj(l, x):
2195
The polynomial evaluated at x
2296
"""
2397

24-
return np.sqrt(np.pi * x / 2) * besselj(l + 0.5, x)
98+
return jv(l, x)
2599

26100

27-
def dricbesj(l, x):
28-
"""The first derivative of the Riccati-Bessel polynomial of the first kind.
101+
def dbesselj(
102+
l: Union[int, float],
103+
x: Union[int, float, np.ndarray],
104+
) -> Union[float, np.ndarray]:
105+
"""The first derivative of the Bessel polynomial of the first kind.
29106
30107
Parameters
31108
----------
@@ -40,13 +117,14 @@ def dricbesj(l, x):
40117
The polynomial evaluated at x
41118
"""
42119

43-
return 0.5 * np.sqrt(np.pi / x / 2) * besselj(l + 0.5, x) + np.sqrt(
44-
np.pi * x / 2
45-
) * dbesselj(l + 0.5, x)
120+
return 0.5 * (besselj(l - 1, x) - besselj(l + 1, x))
46121

47122

48-
def ricbesy(l, x):
49-
"""The Riccati-Bessel polynomial of the second kind.
123+
def bessely(
124+
l: Union[int, float],
125+
x: Union[int, float, np.ndarray],
126+
) -> Union[float, np.ndarray]:
127+
"""The Bessel polynomial of the second kind.
50128
51129
Parameters
52130
----------
@@ -61,11 +139,15 @@ def ricbesy(l, x):
61139
The polynomial evaluated at x
62140
"""
63141

64-
return -np.sqrt(np.pi * x / 2) * bessely(l + 0.5, x)
142+
return yv(l, x)
65143

66144

67-
def dricbesy(l, x):
68-
"""The first derivative of the Riccati-Bessel polynomial of the second kind.
145+
def dbessely(
146+
l: Union[int, float],
147+
x: Union[int, float, np.ndarray],
148+
) -> Union[float, np.ndarray]:
149+
150+
"""The first derivative of the Bessel polynomial of the second kind.
69151
70152
Parameters
71153
----------
@@ -79,14 +161,15 @@ def dricbesy(l, x):
79161
float, ndarray
80162
The polynomial evaluated at x
81163
"""
82-
83-
return -0.5 * np.sqrt(np.pi / 2 / x) * yv(l + 0.5, x) - np.sqrt(
84-
np.pi * x / 2
85-
) * dbessely(l + 0.5, x)
164+
165+
return 0.5 * (bessely(l - 1, x) - bessely(l + 1, x))
86166

87167

88-
def ricbesh(l, x):
89-
"""The Riccati-Bessel polynomial of the third kind.
168+
def ricbesj(
169+
l: Union[int, float],
170+
x: Union[int, float, np.ndarray],
171+
) -> Union[float, np.ndarray]:
172+
"""The Riccati-Bessel polynomial of the first kind.
90173
91174
Parameters
92175
----------
@@ -100,11 +183,16 @@ def ricbesh(l, x):
100183
float, ndarray
101184
The polynomial evaluated at x
102185
"""
103-
return np.sqrt(np.pi * x / 2) * h1vp(l + 0.5, x, False)
186+
187+
return np.sqrt(np.pi * x / 2) * besselj(l + 0.5, x)
104188

105189

106-
def dricbesh(l, x):
107-
"""The first derivative of the Riccati-Bessel polynomial of the third kind.
190+
def dricbesj(
191+
l: Union[int, float],
192+
x: Union[int, float, np.ndarray],
193+
) -> Union[float, np.ndarray]:
194+
"""The first derivative of the Riccati-Bessel polynomial of the first
195+
kind.
108196
109197
Parameters
110198
----------
@@ -118,14 +206,17 @@ def dricbesh(l, x):
118206
float, ndarray
119207
The polynomial evaluated at x
120208
"""
121-
xi = 0.5 * np.sqrt(np.pi / 2 / x) * h1vp(l + 0.5, x, False) + np.sqrt(
209+
210+
return 0.5 * np.sqrt(np.pi / x / 2) * besselj(l + 0.5, x) + np.sqrt(
122211
np.pi * x / 2
123-
) * h1vp(l + 0.5, x, True)
124-
return xi
212+
) * dbesselj(l + 0.5, x)
125213

126214

127-
def besselj(l, x):
128-
"""The Bessel polynomial of the first kind.
215+
def ricbesy(
216+
l: Union[int, float],
217+
x: Union[int, float, np.ndarray],
218+
) -> Union[float, np.ndarray]:
219+
"""The Riccati-Bessel polynomial of the second kind.
129220
130221
Parameters
131222
----------
@@ -140,11 +231,15 @@ def besselj(l, x):
140231
The polynomial evaluated at x
141232
"""
142233

143-
return jv(l, x)
234+
return -np.sqrt(np.pi * x / 2) * bessely(l + 0.5, x)
144235

145236

146-
def dbesselj(l, x):
147-
"""The first derivative of the Bessel polynomial of the first kind.
237+
def dricbesy(
238+
l: Union[int, float],
239+
x: Union[int, float, np.ndarray],
240+
) -> Union[float, np.ndarray]:
241+
"""The first derivative of the Riccati-Bessel polynomial of the second
242+
kind.
148243
149244
Parameters
150245
----------
@@ -158,11 +253,17 @@ def dbesselj(l, x):
158253
float, ndarray
159254
The polynomial evaluated at x
160255
"""
161-
return 0.5 * (besselj(l - 1, x) - besselj(l + 1, x))
256+
257+
return -0.5 * np.sqrt(np.pi / 2 / x) * yv(l + 0.5, x) - np.sqrt(
258+
np.pi * x / 2
259+
) * dbessely(l + 0.5, x)
162260

163261

164-
def bessely(l, x):
165-
"""The Bessel polynomial of the second kind.
262+
def ricbesh(
263+
l: Union[int, float],
264+
x: Union[int, float, np.ndarray],
265+
) -> Union[float, np.ndarray]:
266+
"""The Riccati-Bessel polynomial of the third kind.
166267
167268
Parameters
168269
----------
@@ -177,11 +278,14 @@ def bessely(l, x):
177278
The polynomial evaluated at x
178279
"""
179280

180-
return yv(l, x)
281+
return np.sqrt(np.pi * x / 2) * h1vp(l + 0.5, x, False)
181282

182283

183-
def dbessely(l, x):
184-
"""The first derivative of the Bessel polynomial of the second kind.
284+
def dricbesh(
285+
l: Union[int, float],
286+
x: Union[int, float, np.ndarray],
287+
) -> Union[float, np.ndarray]:
288+
"""The first derivative of the Riccati-Bessel polynomial of the third kind.
185289
186290
Parameters
187291
----------
@@ -195,4 +299,8 @@ def dbessely(l, x):
195299
float, ndarray
196300
The polynomial evaluated at x
197301
"""
198-
return 0.5 * (bessely(l - 1, x) - bessely(l + 1, x))
302+
303+
xi = 0.5 * np.sqrt(np.pi / 2 / x) * h1vp(l + 0.5, x, False) + np.sqrt(
304+
np.pi * x / 2
305+
) * h1vp(l + 0.5, x, True)
306+
return xi

deeptrack/test/backend/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .test_core import *
1+
from .test_core import *
2+
from .test_polynomials import *
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import unittest
2+
3+
from deeptrack.backend import polynomials
4+
5+
6+
class TestPolynomials(unittest.TestCase):
7+
8+
def test_Besselj(self):
9+
Besselj = polynomials.besselj
10+
11+
self.assertEqual(Besselj(0, 0), 1)
12+
self.assertEqual(Besselj(1, 0), 0)
13+
14+
self.assertEqual(Besselj(1, 5), -Besselj(-1, 5))
15+
self.assertTrue(Besselj(1, 5) < 0)
16+
17+
18+
def test_dBesselj(self):
19+
dBesselj = polynomials.dbesselj
20+
21+
self.assertEqual(dBesselj(1,0), 0.5)
22+
23+
24+
def test_Bessely(self):
25+
Bessely = polynomials.bessely
26+
27+
self.assertEqual(Bessely(1, 3), -Bessely(-1,3))
28+
self.assertTrue(Bessely(1, 3) > 0)
29+
30+
31+
def test_dBessely(self):
32+
dBessely = polynomials.dbessely
33+
34+
self.assertEqual(dBessely(1, 3), -dBessely(-1,3))
35+
self.assertTrue(dBessely(1, 3) > 0)
36+
37+
38+
def test_RicBesj(self):
39+
Ricbesj = polynomials.ricbesj
40+
41+
self.assertEqual(Ricbesj(4, 0), 0)
42+
self.assertTrue(abs(Ricbesj(1, 8)- 0.2691) < 1e-3)
43+
44+
45+
def test_dRicBesj(self):
46+
dRicBesj = polynomials.dricbesj
47+
48+
self.assertTrue(abs(dRicBesj(1, 1) - 0.5403) < 1e-3)
49+
50+
51+
def test_RicBesy(self):
52+
RicBesy = polynomials.ricbesy
53+
54+
self.assertTrue(abs(RicBesy(2, 3) - 0.8011) < 1e-3)
55+
56+
57+
def test_dRicBesy(self):
58+
dRicBesy = polynomials.dricbesy
59+
60+
self.assertTrue(abs(dRicBesy(1, 1) + 0.8414) < 1e-3)
61+
62+
63+
def test_RicBesh(self):
64+
RicBesh = polynomials.ricbesh
65+
66+
self.assertTrue(abs(RicBesh(3, 2) - (0.1214 - 2.968j)) < 1e-3)
67+
68+
69+
def test_dRicBesh(self):
70+
dRicBesh = polynomials.dricbesh
71+
72+
self.assertTrue(abs(dRicBesh(2, 6) - (-0.9321-0.2206j)) < 1e-3)

0 commit comments

Comments
 (0)