On 06/13/2016 11:02 AM, saad khalid wrote:
> Hey everyone:
>
> So, I start by making a symbolic function. Gamma(k) is the sum of some
> variables, u_i. Generating the function by hand is difficult, so I was
> hoping I could make it so that Sage generates the function and then I
> can give values for the u_i's, and have it compute the value for me.
> However, even after I give values to the u's, it doesn't substitute them
> into the equation.
You're not telling it to substitute... Here, you define "u" to be a list:
> u =[SR("u_%i"%x)forx in[0..6]]
And here, you set the *elements* of that list to -1:
> u[1]=-1
> u[2]=-1
> u[3]=-1
> u[4]=-1
That has no effect on the variables (u_i) that you defined earlier. It
just overwrites them with the object "-1".
Substitution with arrays of variables is a little bit weird. Try this:
sage: reset()
sage: G = 5
sage: u = [SR("u_%i"%x) for x in [0..6]]
sage: gamma(k) = (1/G)*sum(-(u[i])^(k-1)/(u[i]-1)^k for i in (1..G-1))
sage: f = 1/5 - 3*2/5 + 3*gamma(2) - gamma(3)
sage: f.subs({u[1]: -1, u[2]: -1, u[3]: -1, u[4]: -1})
-1/2