Can I define a function over many "external" constants?

asked 2015-08-31 18:30:17 +0200

fho gravatar image

updated 2015-08-31 19:30:59 +0200

Basically I have a function that needs to access "external" but constant values. For example, simplified:

image description

The as are constant but I don't know n beforehand. My goal is to use SAGE to calculate the integral and derivative of a larger function. I know that there is a symbolic sum function, but I am not sure if it is possible to use the index to refer to something undefined and "external" to the function.

By now I have stumbled over the function ... function which I misuse to serve my purpose (can I link a sagecloud notebook here?):

%var x, y, z, i, n
ai = function('ai')
xi = function('xi')
yi = function('yi')
zi = function('zi')

gauss(a,x,y,z) = a * e^(-(x^2+y^2+z^2)/2)
f(x,y,z) = (sum(  gauss(ai(i),x-xi(i), y-yi(i), z-zi(i))  ,i,1,n))^2


Which results in:

(x, y, z) |--> integrate(integrate(integrate(sum(ai(i)*e^(-1/2*x^2 - 1/2*y^2 - 1/2*z^2 + x*xi(i) - 1/2*xi(i)^2 + y*yi(i) - 1/2*yi(i)^2 + z*zi(i) - 1/2*zi(i)^2), i, 1, n)^2, x), y), z)

Does this mean SAGE is not able to integrate this symbolically?

edit retag flag offensive close merge delete


If you define

f = (sum(  gauss(ai(i),x-xi(i), y-yi(i), z-zi(i))  ,i,1,n))^2

you should get the result without the "(x, y, z) |-->", which probably more properly expresses what you intend.

I think your hypothesis that SAGE doesn't know how to integrate this is also correct, since simpler examples using the same features do work:

sage: integrate(sum(x+ai(i),i,0,n),x)
sum(1/2*x^2 + x*ai(i), i, 0, n)
nbruin gravatar imagenbruin ( 2015-08-31 21:07:54 +0200 )edit

That's a shame :(

fho gravatar imagefho ( 2015-08-31 23:41:09 +0200 )edit

Have you tried specifying a specific value of n, by the way? If you define, for instance

n = 5

and then run your example, you do get an expression in terms of "erf" (which is the best you can hope for). So I think the problem is actually more that sage (or really maxima, which sage relies on for these things) does not know how to expand sum(...)^2 in this case, because the integration isn't the problem. If you manually expand the square, you'd be able to find an answer that doesn't explicitly mention integrals anymore.

nbruin gravatar imagenbruin ( 2015-09-01 02:25:35 +0200 )edit

@nbruin: yep I tried that. And that actually brought me a step closer to the solution. Basically I could extrapolate the real integral from several different ns ;)

fho gravatar imagefho ( 2015-09-02 02:33:05 +0200 )edit