Provide function to calculate the modified Bessel function of the second kind
Project description
logbesselk
Provide function to calculate the modified Bessel function of the second kind and its derivatives.
Reference
Takashi Takekawa, Fast parallel calculation of modified Bessel function of the second kind and its derivatives, SoftwareX, 17, 100923, 2022.
Author
TAKEKAWA Takashi takekawa@tk2lab.org
For Tensorflow
Require
- Python (>=3.10)
- Tensorflow (>=2.8)
Installation
pip install tensorflow logbesselk
Examples
import tensorflow as tf
from logbesselk.tensorflow import log_bessel_k as logk
from logbesselk.tensorflow import bessel_ke as ke
from logbesselk.tensorflow import bessel_kratio as kratio
v = 1.0
x = 1.0
a = logk(v, x)
v = tf.linspace(1, 10, 10)
x = tf.linspace(1, 10, 10)
b = logk(v, x)
# gradient
with tf.GradientTape() as g:
g.watch(v, x)
f = logk(v, x)
dlogkdv = g.gradient(f, v)
dlogkdx = g.gradient(f, x)
# use tf.function
logk = tf.function(logk)
# advanced version
from logbesselk.tensorflow import log_abs_deriv_bessel_k
logk = lambda v, x: log_abs_deriv_bessel_k(v, x, 0, 0)
logdkdv = lambda v, x: log_abs_deriv_bessel_k(v, x, 1, 0)
logdkdx = lambda v, x: log_abs_deriv_bessel_k(v, x, 0, 1)
For jax
Require
- Python (>=3.10)
- jax (>=0.4)
Installation
pip install jax[cuda] -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html
pip install logbesselk
Examples
import jax
import jax.numpy as jnp
from logbesselk.jax import log_bessel_k as logk
from logbesselk.jax import bessel_ke as ke
from logbesselk.jax import bessel_kratio as kratio
# scalar func and grad
v = 1.0
x = 1.0
a = logk(v, x)
# dlogK/dv = (dK/dv) / K
dlogkdv = jax.grad(logk, 0)
b = dlogkdv(v, x)
# dlogK/dx = (dK/dx) / K
dlogkdx = jax.grad(logk, 1)
c = dlogkdx(v, x)
# misc
d = ke(v, x)
e = kratio(v, x, d=1)
# vectorize
logk_vec = jax.vmap(logk)
v = jnp.linspace(1, 10, 10)
x = jnp.linspace(1, 10, 10)
f = logk_vec(v)
# use jit
logk_vec_jit = jax.jit(logk_vec)
# advanced version
from logbesselk.jax import log_abs_devel_bessel_k
log_dkdv = lambda v, x: log_abs_deriv_bessel_k(v, x, 1, 0)
log_dkdx = lambda v, x: log_abs_deriv_bessel_k(v, x, 0, 1)
log_dkdv_jit = jax.jit(jax.vmap(log_dkdv))
log_dkdx_jit = jax.jit(jax.vmap(log_dkdx))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
logbesselk-3.3.1.tar.gz
(937.6 kB
view details)
Built Distribution
File details
Details for the file logbesselk-3.3.1.tar.gz
.
File metadata
- Download URL: logbesselk-3.3.1.tar.gz
- Upload date:
- Size: 937.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f6413b30d90e44861694644da688bc2cc578e1eb65e08e9dfe829e07eb37d35 |
|
MD5 | 590cd4ea5ce6514127954964e4b2f9f3 |
|
BLAKE2b-256 | 6ad3533e1720769e29cfa62270a615d0a7ab9a97e230b876d0e46f3634637c5f |
File details
Details for the file logbesselk-3.3.1-py3-none-any.whl
.
File metadata
- Download URL: logbesselk-3.3.1-py3-none-any.whl
- Upload date:
- Size: 24.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6211da39eab9f64bc865522dab648ad08b43e4c54c7b4df68aa37158eb542f37 |
|
MD5 | 859ec2169cb3bb933eebc3981fc2e4ce |
|
BLAKE2b-256 | 9dd5cf125d68fa3271fe4791e75e0ec9314cb43446ff50d8e21c0904168fed38 |