This is brilliant, thanks!

Vince

On Friday, 3 February 2012, Ivan Andrus <[email protected]> wrote:
> Defining it as
> sage: BB=lambda(c):
factorial(c-1)*(mu/(B*mu+1))^2*sum(((c-1/(B*mu+1))^i)/factorial(i),i,0,c-1)
> sage: BB(1)
> mu^2/(B*mu + 1)^2
> gives the evaluated sum.  In other words it's a function which returns a
symbolic expression instead of a symbolic expression which can be evaluated.
> I don't know if there are disadvantages to this in terms of evaluation
speed if you use BB a lot, or things like taking derivatives etc. but
hopefully it will work for what you need.
> -Ivan
> On Feb 3, 2012, at 6:17 PM, Vincent Knight wrote:
>
> Thanks kcrisman,
> I was holding out for someone to suggest something a bit more
straightforward but I'll try and implement this. Very much appreciated (as
always).
> Best wishes,
> Vince
>
> On 2 February 2012 19:32, kcrisman <[email protected]> wrote:
>
>
> 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
>
>
>
> --
> Dr Vincent Knight
> Cardiff School of Mathematics
> Senghennydd Road,
> Cardiff
> CF24 4AG
> (+44) 29 2087 5548
> www.vincent-knight.com
> @drvinceknight
> Skype: drvinceknight
>
> --
> 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:
>
> --
> 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
>

-- 
Sent from my phone.

Dr Vincent Knight
Cardiff School of Mathematics
Senghennydd Road,
Cardiff
CF24 4AG
(+44) 29 2087 5548
www.vincent-knight.com
@drvinceknight
Skype: drvinceknight

-- 
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

Reply via email to