On Feb 2, 1:53 pm, Vince <[email protected]> wrote:
> Hi all,
>
> I'm working with some formulas including summations and sage doesn't
> always do what I was hoping. I program a function called BB(c)
> including (factorials):
>
> sage: var('i,mu,B')
> sage: BB(c)=factorial(c-1)*(mu/(B*mu+1))^2*sum(((c-1/(B*mu+1))^i)/
> factorial(i),i,0,c-1)
>
> When I compute it I get the following output which sadly hasn't
> evaluated the symbolic summation:
>
> sage: BB(1)
> mu^2*sum((B*mu + 1)^(-i)*(B*mu)^i/factorial(i), i, 0, 0)/(B*mu + 1)^2
>
> However if I copy and past the previous output sage does indeed
> compute everything I expect:
>
> sage: mu^2*sum((B*mu + 1)^(-i)*(B*mu)^i/factorial(i), i, 0, 0)/(B*mu +
> 1)^2
> mu^2/(B*mu + 1)^2
>
> Am I missing something here? I'd obviously like BB(1) to directly
> compute my result.
>
I think the following simpler examples might be illuminating.
sage: var('i')
i
sage: g(x) = sum(i,i,0,x)
sage: g
x |--> 1/2*x^2 + 1/2*x
sage: g(x) = sum(i^2,i,0,x)
sage: g
x |--> 1/3*x^3 + 1/2*x^2 + 1/6*x
sage: g(x) = sum(factorial(i),i,0,x)
sage: g
x |--> sum(factorial(i), i, 0, x)
sage: g(3)
sum(factorial(i), i, 0, 3)
So the summing, if it knows a formula *before* you plug in i, will
work; otherwise it will be an unevaluated sum. This is undoubtedly
due to the preparser's syntax.
sage: preparse('g(x) = sum(factorial(i),i,0,x)')
'__tmp__=var("x"); g =
symbolic_expression(sum(factorial(i),i,Integer(0),x)).function(x)'
So we get a callable thing, and passing in x simply substitutes.
d = dict(zip(map(repr, self.arguments()), args))
d.update(kwds)
return SR(_the_element.substitute(**d))
So we're just doing
sage: SR(g.subs({x:3}))
sum(factorial(i), i, 0, 3)
The only workaround I see right now is to evaluate the string output
you got.
sage: h = SR(g.subs({x:3}))
sage: eval(str(h))
10
I really hope someone else sees a better way around this, though.
- kcrisman
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org