ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 12 Nov 2018 21:11:56 +0100Express the derivative in the Hamiltonian operator in sagehttps://ask.sagemath.org/question/44260/express-the-derivative-in-the-hamiltonian-operator-in-sage/I want to compute the expectation value of the energy $ \langle H \rangle $ for a quantum system with the wave function $ \psi(x) $. This is done with
$$
\langle H \rangle = \int_{-\infty}^{\infty} \psi^* \hat{H} \psi d x
$$
where
$$
\hat{H} = - \frac{\hbar^2}{2 m} \frac{d^2}{d x^2} + V(x)
$$
is the Hamiltonian operator for some potential $V(x)$. How do I express $ \frac{d}{d x} $ in sage if I don't know on which function it will be applied?
I know it is possible to use psi.diff(x), but is there a way to declare the operator so that it automatically does the derivative when it is multiplied with some function?Mon, 12 Nov 2018 18:21:14 +0100https://ask.sagemath.org/question/44260/express-the-derivative-in-the-hamiltonian-operator-in-sage/Answer by rburing for <p>I want to compute the expectation value of the energy $ \langle H \rangle $ for a quantum system with the wave function $ \psi(x) $. This is done with
$$
\langle H \rangle = \int_{-\infty}^{\infty} \psi^* \hat{H} \psi d x
$$
where
$$
\hat{H} = - \frac{\hbar^2}{2 m} \frac{d^2}{d x^2} + V(x)
$$
is the Hamiltonian operator for some potential $V(x)$. How do I express $ \frac{d}{d x} $ in sage if I don't know on which function it will be applied?</p>
<p>I know it is possible to use psi.diff(x), but is there a way to declare the operator so that it automatically does the derivative when it is multiplied with some function?</p>
https://ask.sagemath.org/question/44260/express-the-derivative-in-the-hamiltonian-operator-in-sage/?answer=44263#post-id-44263In Sage it is usual manipulate function *values* rather than functions, e.g. $\frac{d}{dx}(f+g)$ can be expressed by working with symbolic *expressions*, evaluated at a symbolic variable $x$ (basically, the "abuse of notation" $f = f(x)$):
sage: var('x'); f = function('f')(x); g = function('g')(x)
sage: diff(f + g, x)
diff(f(x), x) + diff(g(x), x)
(I also used the `diff` function rather than the method, with the additional benefit that it works on constants.)
Similarly, I would write an operator by letting it act on an expression (we interpret the expression as a function evaluated at a symbolic variable):
sage: var('x'); var('hbar', latex_name='\hbar'); var('m')
sage: psi = function('psi')(x); V = function('V')(x)
sage: H = lambda expr: -(hbar^2/(2*m))*diff(expr, x, x) + V*expr
sage: (conjugate(psi)*H(psi)).expand()
V(x)*conjugate(psi(x))*psi(x) - 1/2*hbar^2*conjugate(psi(x))*diff(psi(x), x, x)/m
You will want to replace `psi` and/or `V` here by symbolic *expressions* in `x`.Mon, 12 Nov 2018 21:11:56 +0100https://ask.sagemath.org/question/44260/express-the-derivative-in-the-hamiltonian-operator-in-sage/?answer=44263#post-id-44263