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.Thu, 03 Oct 2019 06:56:33 -0500Get results of solvehttp://ask.sagemath.org/question/48143/get-results-of-solve/ Hi,
Given
var("a b")
solve([a+b==5,a-b==1],[a, b])
[[a == 3, b == 2]]
Is there a neat way to actually get a and b assigned to 3 and 2,
obviously this is a simplified equation.
Also sometimes one gets a == 3 and other times a = 3 why is that?
It would be good if the 'neat way' would work for both cases.
Thanks, Rob.Wed, 02 Oct 2019 10:45:09 -0500http://ask.sagemath.org/question/48143/get-results-of-solve/Answer by tmonteil for <p>Hi, </p>
<p>Given</p>
<p>var("a b")
solve([a+b==5,a-b==1],[a, b])</p>
<p>[[a == 3, b == 2]]</p>
<p>Is there a neat way to actually get a and b assigned to 3 and 2,</p>
<p>obviously this is a simplified equation.</p>
<p>Also sometimes one gets a == 3 and other times a = 3 why is that?</p>
<p>It would be good if the 'neat way' would work for both cases.</p>
<p>Thanks, Rob.</p>
http://ask.sagemath.org/question/48143/get-results-of-solve/?answer=48145#post-id-48145You can use the `solution_dict=True` option:
sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)
[{b: 2, a: 3}]
Here, you get a list of solutions, each solution is a dictionary. You get the first solution (here the only one) with:
sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0]
{b: 2, a: 3}
And it values as:
sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][a]
3
sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][b]
2
So, you just have to do:
sage: a = solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][a]
> Also sometimes one gets a == 3 and
> other times a = 3 why is that?
Are you sure about that ? Could you please provide a way to reproduce, since this is not expected ?
Wed, 02 Oct 2019 12:34:25 -0500http://ask.sagemath.org/question/48143/get-results-of-solve/?answer=48145#post-id-48145Comment by rb57cat for <p>You can use the <code>solution_dict=True</code> option:</p>
<pre><code>sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)
[{b: 2, a: 3}]
</code></pre>
<p>Here, you get a list of solutions, each solution is a dictionary. You get the first solution (here the only one) with:</p>
<pre><code>sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0]
{b: 2, a: 3}
</code></pre>
<p>And it values as:</p>
<pre><code>sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][a]
3
sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][b]
2
</code></pre>
<p>So, you just have to do:</p>
<pre><code>sage: a = solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][a]
</code></pre>
<blockquote>
<p>Also sometimes one gets a == 3 and
other times a = 3 why is that?</p>
</blockquote>
<p>Are you sure about that ? Could you please provide a way to reproduce, since this is not expected ?</p>
http://ask.sagemath.org/question/48143/get-results-of-solve/?comment=48164#post-id-48164Thanks, I have now worked out how to do this and I will try the ictionary tip soon.Thu, 03 Oct 2019 06:56:33 -0500http://ask.sagemath.org/question/48143/get-results-of-solve/?comment=48164#post-id-48164Comment by rb57cat for <p>You can use the <code>solution_dict=True</code> option:</p>
<pre><code>sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)
[{b: 2, a: 3}]
</code></pre>
<p>Here, you get a list of solutions, each solution is a dictionary. You get the first solution (here the only one) with:</p>
<pre><code>sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0]
{b: 2, a: 3}
</code></pre>
<p>And it values as:</p>
<pre><code>sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][a]
3
sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][b]
2
</code></pre>
<p>So, you just have to do:</p>
<pre><code>sage: a = solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][a]
</code></pre>
<blockquote>
<p>Also sometimes one gets a == 3 and
other times a = 3 why is that?</p>
</blockquote>
<p>Are you sure about that ? Could you please provide a way to reproduce, since this is not expected ?</p>
http://ask.sagemath.org/question/48143/get-results-of-solve/?comment=48153#post-id-48153The number of equals is just an artefact of print/show
var("a b")
show(solve([a+b-5,a-b-1],[a, b]))
print(solve([a+b-5,a-b-1],[a, b]))
[[a=3,b=2]
[
[a == 3, b == 2]
]Wed, 02 Oct 2019 17:35:03 -0500http://ask.sagemath.org/question/48143/get-results-of-solve/?comment=48153#post-id-48153Comment by rb57cat for <p>You can use the <code>solution_dict=True</code> option:</p>
<pre><code>sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)
[{b: 2, a: 3}]
</code></pre>
<p>Here, you get a list of solutions, each solution is a dictionary. You get the first solution (here the only one) with:</p>
<pre><code>sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0]
{b: 2, a: 3}
</code></pre>
<p>And it values as:</p>
<pre><code>sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][a]
3
sage: solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][b]
2
</code></pre>
<p>So, you just have to do:</p>
<pre><code>sage: a = solve([a+b==5,a-b==1],[a, b], solution_dict=True)[0][a]
</code></pre>
<blockquote>
<p>Also sometimes one gets a == 3 and
other times a = 3 why is that?</p>
</blockquote>
<p>Are you sure about that ? Could you please provide a way to reproduce, since this is not expected ?</p>
http://ask.sagemath.org/question/48143/get-results-of-solve/?comment=48148#post-id-48148Sorry I am unfamiliar with this forum I posted as an answer by mistake.Wed, 02 Oct 2019 13:30:48 -0500http://ask.sagemath.org/question/48143/get-results-of-solve/?comment=48148#post-id-48148Answer by rb57cat for <p>Hi, </p>
<p>Given</p>
<p>var("a b")
solve([a+b==5,a-b==1],[a, b])</p>
<p>[[a == 3, b == 2]]</p>
<p>Is there a neat way to actually get a and b assigned to 3 and 2,</p>
<p>obviously this is a simplified equation.</p>
<p>Also sometimes one gets a == 3 and other times a = 3 why is that?</p>
<p>It would be good if the 'neat way' would work for both cases.</p>
<p>Thanks, Rob.</p>
http://ask.sagemath.org/question/48143/get-results-of-solve/?answer=48147#post-id-48147My session does not paste very well but at the bottom is the solution I want to parse into A, B etc. I hope you will see what I am trying to do, and you will also see the singke '='
# ratfr2ser rational fraction to power series á la Euler Archive June 2005
def ratfr2ser(ratfr, num_terms):
var("A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z")
lets=[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
rnt=range(num_terms)
ser = [ lets[i] * x^i for i in rnt ]
ser=sum(ser) # converts from list to symbolic
show(ser)
ratfr_num = ratfr.numerator()
ratfr_den = ratfr.denominator()
eqn = (ser*ratfr_den - ratfr_num).expand().collect(x)
show(eqn)
cfs = [eqn.coefficient(x, n=p) for p in rnt]
show(cfs)
sol = [solve(ec, ec.variables()) for ec in cfs]
show(sol)
for i in rnt:
sol = solve(cfs[i], cfs[i].variables())
#sage_eval(str(sol[0]))
#show(A)
ratfr=(1-x)/(1-x-2*x^2)
ratfr.show()
ratfr2ser(ratfr, 4)
x−12x2+x−1
x
1
2
x
2
x
1
Dx3+Cx2+Bx+A
D
x
3
C
x
2
B
x
A
2Dx5+(2C+D)x4+(2B+C−D)x3+(2A+B−C)x2+(A−B−1)x−A+1
2
D
x
5
2
C
D
x
4
2
B
C
D
x
3
2
A
B
C
x
2
A
B
1
x
A
1
[−A+1,A−B−1,2A+B−C,2B+C−D]
A
1
A
B
1
2
A
B
C
2
B
C
D
[[A=1],[[A=r1+1,B=r1]],[[A=12r2−12r3,B=r3,C=r2]],[[B=12r4−12r5,C=r5,D=r4]]]
Wed, 02 Oct 2019 13:29:13 -0500http://ask.sagemath.org/question/48143/get-results-of-solve/?answer=48147#post-id-48147