Ask Your Question
0

how to get the Values from an expression

asked 2014-02-09 06:10:18 +0100

srikanth ssk gravatar image

updated 2014-02-09 07:39:51 +0100

calc314 gravatar image
#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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-02-09 07:47:19 +0100

calc314 gravatar image

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
    sum=add(roots).n()
    print sum
edit flag offensive delete link more

Comments

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

srikanth ssk gravatar imagesrikanth ssk ( 2014-02-09 10:23:47 +0100 )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.

calc314 gravatar imagecalc314 ( 2014-02-09 15:09:46 +0100 )edit
0

answered 2014-02-09 07:14:54 +0100

ndomes gravatar image

updated 2014-02-09 08:20:11 +0100

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()
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-02-09 06:10:18 +0100

Seen: 1,302 times

Last updated: Feb 09 '14