Substituting values for variables

52 views
Skip to first unread message

saad khalid

unread,
Jun 13, 2016, 11:02:32 AM6/13/16
to sage-support
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. Sorry if this is basic, but what am I doing wrong? Ideally, I could write a function that would calculate the sum of many gammas, and then I could just supply values for the u's. Here's my code right now:

reset()
var("G")
var('i')
G
= 5
u
= [SR("u_%i"%x) for x in [0..6]]
gamma
(k) = (1/G)*sum(-(u[i])^(k-1)/(u[i]-1)^k for i in (1..G-1))
show
(1/5 - 3*2/5 + 3*gamma(2) - gamma(3))
u
[1] = -1
u
[2] = -1
u
[3] = -1
u
[4] = -1
show
(1/5 - 3*2/5 + 3*gamma(2) - gamma(3))

 
Message has been deleted

saad khalid

unread,
Jun 13, 2016, 11:53:37 AM6/13/16
to sage-support
I should add that moving the part where I assign values to the u's to above the definition of gamma, like so:


reset()
var("G")
var('i')
G
= 5

u
= [SR("u_%i"%x) for x in [0..G-1]]


u
[1] = -1
u
[2]= -1
u
[3]= -1
u
[4]= -1
gamma
(k) = (1/G)*sum(-(u[i])^(k-1)/(u[i]-1)^k for i in (1..G-1))
show
(1/5 - 3*2/5 + 3*gamma(2) - gamma(3))



Fixes the issue. But, I feel like there must be a more elegant solution. Is there any way to do this as more of a function? Here is how mathematica does it:

gamma0 = 1/5;
gamma1 = 2/5;
gamma2 = (-(mu1/(5 (-1 + mu1)^2)) - mu2/(5 (-1 + mu2)^2) - mu3/(
    5 (-1 + mu3)^2) - mu4/(5 (-1 + mu4)^2));
gamma3 = -(mu1^2/(5 (-1 + mu1)^3) + mu2^2/(5 (-1 + mu2)^3) + mu3^2/(
     5 (-1 + mu3)^3) + mu4^2/(5 (-1 + mu4)^3));
Then
f[mu1_, mu2_, mu3_, mu4_] = gamma0 - 3 gamma1 + 3 gamma2 - gamma3;

When you do something like f[-1, -1, -1, -1], it automatically substitutes the values you're inputting in for the mu's. 

Message has been deleted

Ralf Stephan

unread,
Jun 14, 2016, 1:32:59 AM6/14/16
to sage-support
It's a bit unclear to me what you want, but have you tried
substituting your expressions, or writing a Python function
instead of a symbolic function gamma?

Michael Orlitzky

unread,
Jun 16, 2016, 8:34:33 AM6/16/16
to sage-s...@googlegroups.com
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


Reply all
Reply to author
Forward
0 new messages