Skip to content

Instantly share code, notes, and snippets.

@mgeier
Last active November 27, 2017 20:26
Show Gist options
  • Save mgeier/51d8758eb94e8f6acaa1249494dfd16e to your computer and use it in GitHub Desktop.
Save mgeier/51d8758eb94e8f6acaa1249494dfd16e to your computer and use it in GitHub Desktop.
Spherical Hankel function of 2nd kind
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy.special import spherical_jn, spherical_yn\n",
"from scipy.special import sph_jnyn\n",
"\n",
"def spherical_hn2(n, z):\n",
" return spherical_jn(n, z) - 1j * spherical_yn(n, z)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"z = 20\n",
"max_order = 150"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.21 ms ± 162 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"for n in range(max_order + 1):\n",
" spherical_hn2(n, z)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"501 µs ± 6.75 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"spherical_hn2(np.arange(max_order + 1), z)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/mg/.local/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: `sph_jnyn` is deprecated!\n",
"scipy.special.sph_jnyn is deprecated in scipy 0.18.0. Use scipy.special.spherical_jn and scipy.special.spherical_yn instead. Note that the new function has a different signature.\n",
" \"\"\"Entry point for launching an IPython kernel.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"51.3 µs ± 194 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"jn, jnp, yn, ynp = sph_jnyn(max_order, z)\n",
"jn - 1j * yn"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Do some more realistic work in the loop"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"phi0 = 0.2\n",
"phi = 0.1"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"14.4 ms ± 292 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"d = 0\n",
"for m in range(-max_order, max_order + 1):\n",
" d += spherical_hn2(abs(m), z) / spherical_hn2(abs(m), z) * np.exp(1j * m * (phi0 - phi))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.53 ms ± 17.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"hr = spherical_hn2(range(0, max_order + 1), z)\n",
"hr0 = spherical_hn2(range(0, max_order + 1), z)\n",
"d = 0\n",
"for m in range(-max_order, max_order + 1):\n",
" d += hr[abs(m)] / hr0[abs(m)] * np.exp(1j * m * (phi0 - phi))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/mg/.local/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: `sph_jnyn` is deprecated!\n",
"scipy.special.sph_jnyn is deprecated in scipy 0.18.0. Use scipy.special.spherical_jn and scipy.special.spherical_yn instead. Note that the new function has a different signature.\n",
" \"\"\"Entry point for launching an IPython kernel.\n",
"/home/mg/.local/lib/python3.6/site-packages/ipykernel_launcher.py:3: DeprecationWarning: `sph_jnyn` is deprecated!\n",
"scipy.special.sph_jnyn is deprecated in scipy 0.18.0. Use scipy.special.spherical_jn and scipy.special.spherical_yn instead. Note that the new function has a different signature.\n",
" This is separate from the ipykernel package so we can avoid doing imports until\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.46 ms ± 6.02 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"jn, jnp, yn, ynp = sph_jnyn(max_order, z)\n",
"hr = jn - 1j * yn\n",
"jn, jnp, yn, ynp = sph_jnyn(max_order, z)\n",
"hr0 = jn - 1j * yn\n",
"d = 0\n",
"for m in range(-max_order, max_order + 1):\n",
" d += hr[abs(m)] / hr0[abs(m)] * np.exp(1j * m * (phi0 - phi))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment