ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 09 Feb 2014 08:09:46 -0600how to get the Values from an expressionhttp://ask.sagemath.org/question/11013/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.
Sat, 08 Feb 2014 23:10:18 -0600http://ask.sagemath.org/question/11013/how-to-get-the-values-from-an-expression/Answer by ndomes for <pre><code>#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 ;
</code></pre>
<p>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.</p>
http://ask.sagemath.org/question/11013/how-to-get-the-values-from-an-expression/?answer=16023#post-id-16023It'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()Sun, 09 Feb 2014 00:14:54 -0600http://ask.sagemath.org/question/11013/how-to-get-the-values-from-an-expression/?answer=16023#post-id-16023Answer by calc314 for <pre><code>#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 ;
</code></pre>
<p>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.</p>
http://ask.sagemath.org/question/11013/how-to-get-the-values-from-an-expression/?answer=16024#post-id-16024Here 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
Sun, 09 Feb 2014 00:47:19 -0600http://ask.sagemath.org/question/11013/how-to-get-the-values-from-an-expression/?answer=16024#post-id-16024Comment by srikanth ssk for <p>Here is another option. I think you are trying to sum the roots of unity.</p>
<pre><code>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
</code></pre>
http://ask.sagemath.org/question/11013/how-to-get-the-values-from-an-expression/?comment=16297#post-id-16297Thank 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!! Sun, 09 Feb 2014 03:23:47 -0600http://ask.sagemath.org/question/11013/how-to-get-the-values-from-an-expression/?comment=16297#post-id-16297Comment by calc314 for <p>Here is another option. I think you are trying to sum the roots of unity.</p>
<pre><code>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
</code></pre>
http://ask.sagemath.org/question/11013/how-to-get-the-values-from-an-expression/?comment=16296#post-id-16296Glad 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.Sun, 09 Feb 2014 08:09:46 -0600http://ask.sagemath.org/question/11013/how-to-get-the-values-from-an-expression/?comment=16296#post-id-16296