Planck units in symbolics expressions

60 views
Skip to first unread message

Julian L

unread,
Dec 26, 2017, 3:40:20 PM12/26/17
to sympy
Hi,
I am using sympy.physics.qho_1d to get the eigenfunctions and evaluate them numerically with numpy and lambdify, and I want to use units where hbar = 1.
I've tried things like:

(qho.psi_n(0, 0, 1, 1)).subs(hbar, 1.0)
(expr.subs(hbar, 1.0)).evalf()
lambdify(hbar, expr, "numpy")

but it doesn't work and always returns  1/(hbar**(1/4)*pi**(1/4)) or an error.
How can I do it well?

Julian

Leonid Kovalev

unread,
Dec 27, 2017, 6:32:18 PM12/27/17
to sympy
hbar is a special constant; you refer to it as HBar(), not as hbar, and it needs to be imported first.  This is how you can replace it in an expression:

from sympy import *
from sympy.physics.qho_1d import psi_n
from sympy.physics.quantum.constants import HBar
n
, x, m, omega = symbols("n x m omega")
psi_n
(n, x, m, omega).subs(HBar(), 1)  

An attempt to lambdify the complete function psi_n fails because of the factorial; apparently it's not supported by code generation. 

psi = lambdify([n, x, m, omega], psi_n(n, x, m, omega).subs(HBar(), 1))  # error


But if the parameter n is fixed, you can lambdify the rest:

psi = lambdify([x, m, omega], psi_n(0, x, m, omega).subs(HBar(), 1))


Now psi is callable like psi(1, 2, 3). 


Leonid Kovalev

unread,
Dec 27, 2017, 6:50:04 PM12/27/17
to sympy
Actually, hbar also works if you import it (and this makes more sense than importing the class):

from sympy.physics.quantum.constants import hbar

and then ...subs(hbar, 1).  

HBar is a class name, but the module also creates an instance of that class, called hbar. 
Reply all
Reply to author
Forward
0 new messages