Last updated:
0 purchases
hermitefunction 2.0
Hermite Function Series
A Hermite function series package.
from hermitefunction import HermiteFunction
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4, +4, 1000)
for n in range(5):
f = HermiteFunction(n)
plt.plot(x, f(x), label=f'$h_{n}$')
plt.legend(loc='lower right')
plt.show()
Installation
pip install git+https://github.com/goessl/vector.git
pip install hermite-function
Usage
This package provides a single class, HermiteFunction, to handle Hermite function series.
HermiteFunction extends Vector from the [https://github.com/goessl/vector](vector module) and therefore provides the same functionality.
A series can be initialized in three ways:
With the constructor HermiteFunction(coef), that takes a non-negative integer to create a pure Hermite function with the given index, or an iterable of coefficients to create a Hermite function series.
With the random factory HermiteFunction.random(deg) for a random Hermite series of a given degree.
By fitting data with HermiteFunction.fit(x, y, deg).
The objects are immutable (coefficients are internally stored in a tuple).
f = HermiteFunction((1, 2, 3))
g = HermiteFunction.random(15)
h = HermiteFunction.fit(x, g(x), 10)
plt.plot(x, f(x), label='$f$')
plt.plot(x, g(x), '--', label='$g$')
plt.plot(x, h(x), ':', label='$h$')
plt.legend()
plt.show()
Container and sequence interfaces are implemented so the coefficients can be
accessed by indexing: f[2] (coefficients not set return to 0),
iterated over: for c in f (stops at last set coefficient),
counted: len(f) (number of set coefficients),
compared: f == g (tuple of coefficients get compared),
shifted: f >> 1, f << 2 &
trimmed: f.trim() (trailing non-zero coefficients get removed).
Methods for functions:
evaluation with f(x),
differentiation to an arbitrary degree f.der(n),
integration f.antider(),
Fourier transformation f.fourier() &
getting the degree of the series f.deg are implemented.
f_p = f.der()
f_pp = f.der(2)
plt.plot(x, f(x), label=rf"$f \ (\deg f={f.deg})$")
plt.plot(x, f_p(x), '--', label=rf"$f' \ (\deg f'={f_p.deg})$")
plt.plot(x, f_pp(x), ':', label=rf"$f'' \ (\deg f''={f_pp.deg})$")
plt.legend()
plt.show()
Hilbert space operations are also provided, where the Hermite functions are used as an orthonormal basis of the LR2 space:
Vector addition & subtraction f + g, f - g,
scalar multiplication & division 2 * f, f / 2,
inner product & norm f @ g, abs(f).
g = HermiteFunction(4)
h = f + g
i = 0.5 * f
plt.plot(x, f(x), label='$f$')
plt.plot(x, g(x), '--', label='$g$')
plt.plot(x, h(x), ':', label='$h$')
plt.plot(x, i(x), '-.', label='$i$')
plt.legend()
plt.show()
Because this package was intended as a tool to work with quantum mechanical wavefunctions, the expectation value for the kinetic energy is also implemented (⟨P^2⟩=12∫Rf∗(x)f″(x)dx, natural units):
f.kin
Proofs
In the following let
f=∑k=0∞fkhk, g=∑k=0∞gkhk.
where hk are the Hermite functions, defined by the Hermite polynomials Hk:
hk(x)=e−x222kk!πHk(x)
from Wikipedia - Hermite functions.
Differentiation
f′=∑kfkhk′ ∣hk′=k2hk−1−k+12hk+1 =∑kfk(k2hk−1−k+12hk+1) =∑k=0∞fkk2hk−1−∑k=0∞fkk+12hk+1 ∣k−1→k, k+1→k =∑k=−1∞k+12fk+1hk−∑k=1∞k2fk−1hk ∣−0+0=−−1+12f−1+1h−1+02f0−1h0 =∑k=0∞k+12fk+1hk−∑k=0∞k2fk−1hk =∑k(k+12fk+1−k2fk−1)hk
With hk′=k2hk+1−k+12hk−1 from Wikipedia - Hermite functions.
Integration
With the same relation as above we get
hk′=k2hk−1−k+12hk+1 ∣+k+12hk+1−hk′ k+12hk+1=k2hk−1−hk′ ∣⋅2k+1 hk+1=kk+1hk−1−2k+1hk′ ∣k+1→k hk=k−1khk−2−2khk−1′ ∣∫ Hk=k−1kHk−2−2khk−1
which can be applied from the highest to the lowest order. For h0 we then get
H0(x)=∫−∞xh0(x′)dx′=∫−∞xe−x′22π4dx′=π2erf(x2) (+π2)
Fourier transformation
Wikipedia - Hermite functions
Kinetic energy
⟨−P^22⟩=−12∫Rf∗(x)d2dx2f(x)dx=+12∫R|f′(x)|2dx=12||f′||LR22
License (MIT)
Copyright (c) 2022 Sebastian Gössl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.