Revision history [back]

Thanks; I don't quite understand what's broken, but I think I have a workaround:

sage: normal(x,av,sd)=((1/(sd*sqrt(2*pi)))*exp(-(x-av)^2/(2*sd^2)))
sage: f(x,y)= x*y^3*normal(y,1,2)
sage: g = f.integrate(y,-2,2)
sage: g
(x, y) |--> 1/4*sqrt(2)*x*integrate(y^3*e^(-1/8*(y - 1)^2), y, -2, 2)/sqrt(pi)


(note that sage seems to think g is a function of x and y, even though it shouldn't be)

sage: h(x) = f.integrate(y,-2,2)
sage: h
x |--> 1/4*sqrt(2)*x*integrate(y^3*e^(-1/8*(y - 1)^2), y, -2, 2)/sqrt(pi)


(this is looking better, but still doesn't work . . . the problem seems to be the integrate command in the definition of h)

sage: plot(h,0,5)
...
ValueError: free variable: y


(the first way I could think of to get Sage to evaluate the integral was to use the numerical_approx method; maybe there's a better way, but this works :)

sage: k = lambda x: h(x).numerical_approx()
sage: plot(k,0,5)


(graph is shown!)

Note that, unfortunately, the following does not work:

k(x) = h(x).numerical_approx()
...
TypeError: cannot evaluate symbolic expression numerically


Unless someone knows better, I'm inclined to think this should be filed as a Trac ticket.

Thanks; I don't quite understand what's broken, but I think I have a workaround:workaround.

sage: normal(x,av,sd)=((1/(sd*sqrt(2*pi)))*exp(-(x-av)^2/(2*sd^2)))
sage: f(x,y)= x*y^3*normal(y,1,2)
sage: g = f.integrate(y,-2,2)
sage: g
(x, y) |--> 1/4*sqrt(2)*x*integrate(y^3*e^(-1/8*(y - 1)^2), y, -2, 2)/sqrt(pi)


(note that sage seems to think g is a function of x and y, even though it shouldn't be)

sage: h(x) = f.integrate(y,-2,2)
sage: h
x |--> 1/4*sqrt(2)*x*integrate(y^3*e^(-1/8*(y - 1)^2), y, -2, 2)/sqrt(pi)


(this is looking better, but still doesn't work . . . the problem seems to be the integrate command in the definition of h)

sage: plot(h,0,5)
...
ValueError: free variable: y


(the first way I could think of to get Sage to evaluate the integral was to use the numerical_approx method; maybe there's a better way, but this works :)

sage: k = lambda x: h(x).numerical_approx()
sage: plot(k,0,5)


(graph is shown!)

Note that, unfortunately, the following does not work:

k(x) = h(x).numerical_approx()
...
TypeError: cannot evaluate symbolic expression numerically


Unless someone knows better, I'm inclined to think this should be filed as a Trac ticket.

EDIT: after writing all this, I think the problem is the "symbolic expression" integrate(...). The following works as expected:

sage: k(x) = 1/4*sqrt(2)*x*(integrate(y^3*e^(-1/8*(y - 1)^2), y, -2,2).numerical_approx())/sqrt(pi)
sage: plot(k,0,5)