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, 29 May 2023 12:18:37 +02002D Gaussian integralshttps://ask.sagemath.org/question/68843/2d-gaussian-integrals/Hello. I have the following Gaussian function $$G(\mathbf r) = \frac{1}{\sqrt \pi R_0}e^{-\frac{r^2}{2R_0^2}} , \qquad R_0>0$$ of a 2D vector $\mathbf r=(x, y)$ as well as a 'transfer' function $$T_k(\mathbf r, \mathbf r', d)=\frac{e^{ikd+\frac{ik}{2d}|\mathbf r-\mathbf r'|^2}}{id(2\pi/k)}, \qquad d,k>0. $$ My objective is to performe the symbolic integration $$G'(\mathbf r_1)=\int d^2\mathbf r_0G(\mathbf r_0) T_k(\mathbf r)$$ in Sage. First of all: this integral is actually extremely simple to compute by hand, by applying the standard Gaussian formula. However, I'm using this example to learn how to do symbolic integration in Sage.
My first question: is there some particular syntax to do this 2D integral directly, or is it always necessary to do the integrals in $dx$ and $dy$ separately? Following this route, after expanding the modulus squared we get $$G'(\mathbf r_1) = \frac{1}{\sqrt pi R_0}\frac{e^{ikd}e^{\frac{ik}{2d}(x_1^2+y_1^2)}}{id(2\pi/k)}\int dx_0 \ e^{-\left(\frac{1}{2R_0^2}-\frac{il}{2d}\right)x_0^2-\frac{ik}{d}x_0x_1}\int dy_1 \ e^{-\left(\frac{1}{2R_0^2}-\frac{il}{2d}\right)y_0^2-\frac{ik}{d}y_0y_1}. $$ Since the two integrals are analogous, I focus on the first. The code I've written to try and solve it is the following:
#define all variables
var('x_0, x_1, y_0, y_1, d, k, R_0')
assume(d>0, k>0, R_0>0)
#define constant prefactors
a = 1/(2*R_0^2) - i*k/(2*d)
b = i*k*x_1 / d
#define integrand
I(x_0) = exp(-a*x_0^2 + b*x_0)
#perform Gaussian integral
result = I.integral(x_0, -oo, oo)
show(result)
However, this only returns several warnings of the type
Warning, need to choose a branch for the root of a polynomial with parameters. This might be wrong.
on top of the error `Error trying to find limit of -sqrt(pi)*(-2*i)/(2*i*sageVARR_0^4*sageVARd*sageVARk/(-2*sageVARR_0^2*sageVARd^2+2*sqrt(sageVARR_0^8*sageVARd^2*sageVARk^2+sageVARR_0^4*sageVARd^4))+1)/sqrt(-sageVARR_0^2*sageVARd^2+sageVARR_0^2*sageVARd*sqrt(sageVARR_0^4*sageVARk^2+sageVARd^2))*sageVARR_0^2*sageVARd*exp((-i)*sageVARR_0^2*sageVARk^2*sageVARx_1^2/(2*sageVARR_0^2*sageVARd*sageVARk+2*i*sageVARd^2))/2*erf(-1/(-2*i)*(2*i*sageVARR_0^4*sageVARd*sageVARk/(-2*sageVARR_0^2*sageVARd^2+2*sqrt(sageVARR_0^8*sageVARd^2*sageVARk^2+sageVARR_0^4*sageVARd^4))+1)*sqrt(-sageVARR_0^2*sageVARd^2+sageVARR_0^2*sageVARd*sqrt(sageVARR_0^4*sageVARk^2+sageVARd^2))/sageVARR_0^2/sageVARd*(sageVARx_0+i*sageVARk*sageVARx_1/sageVARd/(i*sageVARR_0^2*sageVARk-sageVARd)*2*sageVARR_0^2*sageVARd/2))`.
With a finite choice of extremal points, the integration is performed correctly. I assume the problem here is that Sage has trouble finding the limits of a complex functions, but how do I circumvent the issue?UnwiseMon, 29 May 2023 12:18:37 +0200https://ask.sagemath.org/question/68843/Tips for Symbolic Integration of Rational Functions?https://ask.sagemath.org/question/57028/tips-for-symbolic-integration-of-rational-functions/I am doing some work that requires me to do a lot of symbolic integration of rational functions over the interval [-1, 1]. These rational functions end up containing the integration variable, plus a few real symbolic parameters t1,...,tn. The issue is, as I add more parameters and things get more complicated, Sage's integrate function slows down a lot, and even occasionally seems to get completely stuck.
I am reasonably new to Sage, and my code is able to handle the simpler cases, so it might just be that these functions become too complicated to integrate as I add more parameters. But I'm wondering, are there any general things I could do that might make the integration a little easier/faster? For example, I tried calling .partial_fractions() on the integrand before passing it to the integrate function, but I didn't really notice much of an effect. If you can think of anything that might help speed up these computations, I would appreciate it.OgrelordSun, 09 May 2021 00:03:28 +0200https://ask.sagemath.org/question/57028/How to do quantum mechanics integrals in Sage?https://ask.sagemath.org/question/54774/how-to-do-quantum-mechanics-integrals-in-sage/Hi.
I am a newcomer to Sage. I am trying to do integrals of the form shown below. This is from an introductory course in quantum mechanics.
$
\psi(x, t) = \int_{-\infty}^{+\infty} f(p) \psi_p(x - x_0, t) d\mathrm{p}
$
where,
$
\psi_p(x, t) = \dfrac{1}{\sqrt{2\pi\hbar}}e^\left( -\dfrac{i}{\hbar}\left(\dfrac{p^2}{2m}t - px \right)\right)
$
$
f(p) = \dfrac{1}{(2\pi)^{1/4} \sqrt{\sigma_p}}e^\left( -\dfrac{(p - p_0)^2}{4{\sigma_p}^2}\right)
$
I have tried the following in sage thus far:
forget()
var('x,t,p,p_0, m,h,x_0,sigma_p')
psi_p(x, t) = 1/(2*pi*h)^(1/2)*exp(-i/h*(p^2*t/(2*m) - p*(x - x_0)))
f(p) = 1/(2*pi)^(1/4)*1/sqrt(sigma_p)*exp(-(p - p_0)^2/(4*sigma_p^2))
show(psi_p(x, t))
show(f(p))
assume(m, 'constant')
assume(m > 0)
assume(h, 'constant')
assume(h > 0)
assume(p_0, 'constant')
assume(p_0 > 0)
assume(sigma_p, 'constant')
assume(sigma_p > 0)
assume(x, 'real')
assume(t, 'real')
from sage.symbolic.integration.integral import definite_integral
definite_integral(f(p)*psi_p(x, t), p, -oo, +oo)
Sage keeps complaining about requiring assumptions that I can't easily provide. Here is a sample output:
ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(2*sigma_p^2*t
>0)', see `assume?` for more details)
Is 2*sigma_p^2*t
*sin(atan((2*sigma_p^2*t)/(h*m))
/2)
+h*m
*cos(atan((2*sigma_p^2*t)
/(h*m))
/2) positive, negative or zero?
My question: how would you do integrals like this in Sage? Is my fundamental approach correct?ARVSun, 20 Dec 2020 14:41:49 +0100https://ask.sagemath.org/question/54774/