# how to get the Values from an expression

#Sum of n-th roots of unity is zero.
n=10;
for i in range(1,n):
v=solve(x^i - 1,x);
sum_roots = 0;
m=i;
for j in range(0,i):
sum_roots += v[j];  # error here .. I want the values, not the expression
print ' sum of n-th roots of ' ;
print m;
print ' is ';
print sum_roots ;


How do i get the values from the list of expressions returned by solve? I have another Question-: How can I see all the member functions of any type,say expression..I have tried putting ? sfter the command, but that does not give all the member functions.

edit retag close merge delete

Sort by ยป oldest newest most voted

It's not clear to me what you want to do. Looking at the first lines of your code: your for-loop ends up with the solutions of x^9 - 1 == 0 ; all other cases are lost. (ok, that's wrong, I can see it now after your code was properly formatted)

n=10
for i in range(1,n):
v=solve(x^i - 1,x)
print v
print solve(x^9 - 1,x)


As far as solve returns a list of equations you can run through the list applying the method rhs() (right hand side)

For example:

sols = solve(x^9 - 1,x)
for sol in sols:
print sol.rhs()


Or (if you are not interested in complex numbers ;-) )

sols = solve(x^9 - 1,x)
for sol in sols:
if sol.rhs() in RR:
print sol.rhs()

more

Here is another option. I think you are trying to sum the roots of unity.

n=10
for i in range(1,n):
v=solve(x^i - 1,x)
roots=map(lambda q: q.rhs(),v)
print roots
print sum

more

Thank YOU so much!! Could you please tell me where to look for such member functions.. I tried so much to get the rhs of the expression but did not know where to look ! Thanks man!!

( 2014-02-09 03:23:47 -0600 )edit

Glad to help! You can get the member functions by typing the element name, for example myvar, and then a period and hitting tab. So, it would be: myvar.<tab> This will pop up a list of the associated functions. You can then use a question mark to ask for help on that function.

( 2014-02-09 08:09:46 -0600 )edit