Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/scipy/special/_spherical_bessel.py: 29%

17 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-12 06:31 +0000

1from ._ufuncs import (_spherical_jn, _spherical_yn, _spherical_in, 

2 _spherical_kn, _spherical_jn_d, _spherical_yn_d, 

3 _spherical_in_d, _spherical_kn_d) 

4 

5def spherical_jn(n, z, derivative=False): 

6 r"""Spherical Bessel function of the first kind or its derivative. 

7 

8 Defined as [1]_, 

9 

10 .. math:: j_n(z) = \sqrt{\frac{\pi}{2z}} J_{n + 1/2}(z), 

11 

12 where :math:`J_n` is the Bessel function of the first kind. 

13 

14 Parameters 

15 ---------- 

16 n : int, array_like 

17 Order of the Bessel function (n >= 0). 

18 z : complex or float, array_like 

19 Argument of the Bessel function. 

20 derivative : bool, optional 

21 If True, the value of the derivative (rather than the function 

22 itself) is returned. 

23 

24 Returns 

25 ------- 

26 jn : ndarray 

27 

28 Notes 

29 ----- 

30 For real arguments greater than the order, the function is computed 

31 using the ascending recurrence [2]_. For small real or complex 

32 arguments, the definitional relation to the cylindrical Bessel function 

33 of the first kind is used. 

34 

35 The derivative is computed using the relations [3]_, 

36 

37 .. math:: 

38 j_n'(z) = j_{n-1}(z) - \frac{n + 1}{z} j_n(z). 

39 

40 j_0'(z) = -j_1(z) 

41 

42 

43 .. versionadded:: 0.18.0 

44 

45 References 

46 ---------- 

47 .. [1] https://dlmf.nist.gov/10.47.E3 

48 .. [2] https://dlmf.nist.gov/10.51.E1 

49 .. [3] https://dlmf.nist.gov/10.51.E2 

50 .. [AS] Milton Abramowitz and Irene A. Stegun, eds. 

51 Handbook of Mathematical Functions with Formulas, 

52 Graphs, and Mathematical Tables. New York: Dover, 1972. 

53 

54 Examples 

55 -------- 

56 The spherical Bessel functions of the first kind :math:`j_n` accept 

57 both real and complex second argument. They can return a complex type: 

58 

59 >>> from scipy.special import spherical_jn 

60 >>> spherical_jn(0, 3+5j) 

61 (-9.878987731663194-8.021894345786002j) 

62 >>> type(spherical_jn(0, 3+5j)) 

63 <class 'numpy.complex128'> 

64 

65 We can verify the relation for the derivative from the Notes 

66 for :math:`n=3` in the interval :math:`[1, 2]`: 

67 

68 >>> import numpy as np 

69 >>> x = np.arange(1.0, 2.0, 0.01) 

70 >>> np.allclose(spherical_jn(3, x, True), 

71 ... spherical_jn(2, x) - 4/x * spherical_jn(3, x)) 

72 True 

73 

74 The first few :math:`j_n` with real argument: 

75 

76 >>> import matplotlib.pyplot as plt 

77 >>> x = np.arange(0.0, 10.0, 0.01) 

78 >>> fig, ax = plt.subplots() 

79 >>> ax.set_ylim(-0.5, 1.5) 

80 >>> ax.set_title(r'Spherical Bessel functions $j_n$') 

81 >>> for n in np.arange(0, 4): 

82 ... ax.plot(x, spherical_jn(n, x), label=rf'$j_{n}$') 

83 >>> plt.legend(loc='best') 

84 >>> plt.show() 

85 

86 """ 

87 if derivative: 

88 return _spherical_jn_d(n, z) 

89 else: 

90 return _spherical_jn(n, z) 

91 

92 

93def spherical_yn(n, z, derivative=False): 

94 r"""Spherical Bessel function of the second kind or its derivative. 

95 

96 Defined as [1]_, 

97 

98 .. math:: y_n(z) = \sqrt{\frac{\pi}{2z}} Y_{n + 1/2}(z), 

99 

100 where :math:`Y_n` is the Bessel function of the second kind. 

101 

102 Parameters 

103 ---------- 

104 n : int, array_like 

105 Order of the Bessel function (n >= 0). 

106 z : complex or float, array_like 

107 Argument of the Bessel function. 

108 derivative : bool, optional 

109 If True, the value of the derivative (rather than the function 

110 itself) is returned. 

111 

112 Returns 

113 ------- 

114 yn : ndarray 

115 

116 Notes 

117 ----- 

118 For real arguments, the function is computed using the ascending 

119 recurrence [2]_. For complex arguments, the definitional relation to 

120 the cylindrical Bessel function of the second kind is used. 

121 

122 The derivative is computed using the relations [3]_, 

123 

124 .. math:: 

125 y_n' = y_{n-1} - \frac{n + 1}{z} y_n. 

126 

127 y_0' = -y_1 

128 

129 

130 .. versionadded:: 0.18.0 

131 

132 References 

133 ---------- 

134 .. [1] https://dlmf.nist.gov/10.47.E4 

135 .. [2] https://dlmf.nist.gov/10.51.E1 

136 .. [3] https://dlmf.nist.gov/10.51.E2 

137 .. [AS] Milton Abramowitz and Irene A. Stegun, eds. 

138 Handbook of Mathematical Functions with Formulas, 

139 Graphs, and Mathematical Tables. New York: Dover, 1972. 

140 

141 Examples 

142 -------- 

143 The spherical Bessel functions of the second kind :math:`y_n` accept 

144 both real and complex second argument. They can return a complex type: 

145 

146 >>> from scipy.special import spherical_yn 

147 >>> spherical_yn(0, 3+5j) 

148 (8.022343088587197-9.880052589376795j) 

149 >>> type(spherical_yn(0, 3+5j)) 

150 <class 'numpy.complex128'> 

151 

152 We can verify the relation for the derivative from the Notes 

153 for :math:`n=3` in the interval :math:`[1, 2]`: 

154 

155 >>> import numpy as np 

156 >>> x = np.arange(1.0, 2.0, 0.01) 

157 >>> np.allclose(spherical_yn(3, x, True), 

158 ... spherical_yn(2, x) - 4/x * spherical_yn(3, x)) 

159 True 

160 

161 The first few :math:`y_n` with real argument: 

162 

163 >>> import matplotlib.pyplot as plt 

164 >>> x = np.arange(0.0, 10.0, 0.01) 

165 >>> fig, ax = plt.subplots() 

166 >>> ax.set_ylim(-2.0, 1.0) 

167 >>> ax.set_title(r'Spherical Bessel functions $y_n$') 

168 >>> for n in np.arange(0, 4): 

169 ... ax.plot(x, spherical_yn(n, x), label=rf'$y_{n}$') 

170 >>> plt.legend(loc='best') 

171 >>> plt.show() 

172 

173 """ 

174 if derivative: 

175 return _spherical_yn_d(n, z) 

176 else: 

177 return _spherical_yn(n, z) 

178 

179 

180def spherical_in(n, z, derivative=False): 

181 r"""Modified spherical Bessel function of the first kind or its derivative. 

182 

183 Defined as [1]_, 

184 

185 .. math:: i_n(z) = \sqrt{\frac{\pi}{2z}} I_{n + 1/2}(z), 

186 

187 where :math:`I_n` is the modified Bessel function of the first kind. 

188 

189 Parameters 

190 ---------- 

191 n : int, array_like 

192 Order of the Bessel function (n >= 0). 

193 z : complex or float, array_like 

194 Argument of the Bessel function. 

195 derivative : bool, optional 

196 If True, the value of the derivative (rather than the function 

197 itself) is returned. 

198 

199 Returns 

200 ------- 

201 in : ndarray 

202 

203 Notes 

204 ----- 

205 The function is computed using its definitional relation to the 

206 modified cylindrical Bessel function of the first kind. 

207 

208 The derivative is computed using the relations [2]_, 

209 

210 .. math:: 

211 i_n' = i_{n-1} - \frac{n + 1}{z} i_n. 

212 

213 i_1' = i_0 

214 

215 

216 .. versionadded:: 0.18.0 

217 

218 References 

219 ---------- 

220 .. [1] https://dlmf.nist.gov/10.47.E7 

221 .. [2] https://dlmf.nist.gov/10.51.E5 

222 .. [AS] Milton Abramowitz and Irene A. Stegun, eds. 

223 Handbook of Mathematical Functions with Formulas, 

224 Graphs, and Mathematical Tables. New York: Dover, 1972. 

225 

226 Examples 

227 -------- 

228 The modified spherical Bessel functions of the first kind :math:`i_n` 

229 accept both real and complex second argument. 

230 They can return a complex type: 

231 

232 >>> from scipy.special import spherical_in 

233 >>> spherical_in(0, 3+5j) 

234 (-1.1689867793369182-1.2697305267234222j) 

235 >>> type(spherical_in(0, 3+5j)) 

236 <class 'numpy.complex128'> 

237 

238 We can verify the relation for the derivative from the Notes 

239 for :math:`n=3` in the interval :math:`[1, 2]`: 

240 

241 >>> import numpy as np 

242 >>> x = np.arange(1.0, 2.0, 0.01) 

243 >>> np.allclose(spherical_in(3, x, True), 

244 ... spherical_in(2, x) - 4/x * spherical_in(3, x)) 

245 True 

246 

247 The first few :math:`i_n` with real argument: 

248 

249 >>> import matplotlib.pyplot as plt 

250 >>> x = np.arange(0.0, 6.0, 0.01) 

251 >>> fig, ax = plt.subplots() 

252 >>> ax.set_ylim(-0.5, 5.0) 

253 >>> ax.set_title(r'Modified spherical Bessel functions $i_n$') 

254 >>> for n in np.arange(0, 4): 

255 ... ax.plot(x, spherical_in(n, x), label=rf'$i_{n}$') 

256 >>> plt.legend(loc='best') 

257 >>> plt.show() 

258 

259 """ 

260 if derivative: 

261 return _spherical_in_d(n, z) 

262 else: 

263 return _spherical_in(n, z) 

264 

265 

266def spherical_kn(n, z, derivative=False): 

267 r"""Modified spherical Bessel function of the second kind or its derivative. 

268 

269 Defined as [1]_, 

270 

271 .. math:: k_n(z) = \sqrt{\frac{\pi}{2z}} K_{n + 1/2}(z), 

272 

273 where :math:`K_n` is the modified Bessel function of the second kind. 

274 

275 Parameters 

276 ---------- 

277 n : int, array_like 

278 Order of the Bessel function (n >= 0). 

279 z : complex or float, array_like 

280 Argument of the Bessel function. 

281 derivative : bool, optional 

282 If True, the value of the derivative (rather than the function 

283 itself) is returned. 

284 

285 Returns 

286 ------- 

287 kn : ndarray 

288 

289 Notes 

290 ----- 

291 The function is computed using its definitional relation to the 

292 modified cylindrical Bessel function of the second kind. 

293 

294 The derivative is computed using the relations [2]_, 

295 

296 .. math:: 

297 k_n' = -k_{n-1} - \frac{n + 1}{z} k_n. 

298 

299 k_0' = -k_1 

300 

301 

302 .. versionadded:: 0.18.0 

303 

304 References 

305 ---------- 

306 .. [1] https://dlmf.nist.gov/10.47.E9 

307 .. [2] https://dlmf.nist.gov/10.51.E5 

308 .. [AS] Milton Abramowitz and Irene A. Stegun, eds. 

309 Handbook of Mathematical Functions with Formulas, 

310 Graphs, and Mathematical Tables. New York: Dover, 1972. 

311 

312 Examples 

313 -------- 

314 The modified spherical Bessel functions of the second kind :math:`k_n` 

315 accept both real and complex second argument. 

316 They can return a complex type: 

317 

318 >>> from scipy.special import spherical_kn 

319 >>> spherical_kn(0, 3+5j) 

320 (0.012985785614001561+0.003354691603137546j) 

321 >>> type(spherical_kn(0, 3+5j)) 

322 <class 'numpy.complex128'> 

323 

324 We can verify the relation for the derivative from the Notes 

325 for :math:`n=3` in the interval :math:`[1, 2]`: 

326 

327 >>> import numpy as np 

328 >>> x = np.arange(1.0, 2.0, 0.01) 

329 >>> np.allclose(spherical_kn(3, x, True), 

330 ... - 4/x * spherical_kn(3, x) - spherical_kn(2, x)) 

331 True 

332 

333 The first few :math:`k_n` with real argument: 

334 

335 >>> import matplotlib.pyplot as plt 

336 >>> x = np.arange(0.0, 4.0, 0.01) 

337 >>> fig, ax = plt.subplots() 

338 >>> ax.set_ylim(0.0, 5.0) 

339 >>> ax.set_title(r'Modified spherical Bessel functions $k_n$') 

340 >>> for n in np.arange(0, 4): 

341 ... ax.plot(x, spherical_kn(n, x), label=rf'$k_{n}$') 

342 >>> plt.legend(loc='best') 

343 >>> plt.show() 

344 

345 """ 

346 if derivative: 

347 return _spherical_kn_d(n, z) 

348 else: 

349 return _spherical_kn(n, z)