ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 20 Mar 2021 05:19:10 +0100Taking elements in solve and multiplying to a polynomialhttps://ask.sagemath.org/question/56231/taking-elements-in-solve-and-multiplying-to-a-polynomial/So I want a function that solves a system of two equations and the move those numerical results to polynomials f and g.
def PolynomialCoefficient(fcn1, fcn2):
var ('a b')
eqn1 = 2*a + 2*b == 0
eqn2 = 3*a - b == 1
List = solve([eqn1, eqn2], a, b)
Now I want to move this to `a*f(x)+b*g(x)` without hitting solve each time
and manually typing what `a` and `b` are. For example,
`PolynomialCoefficient(x, y)` should output `1/4 x - 1/4 y`.
The issue is `len(List)` outputs 1. If it was length 2 with `a` in first slot
and `b` in 2nd slot, I could do
Poly = List[0]*fcn1 + List[1]*fcn2
return Poly
Any ideas on how to move `a` and `b`?Thu, 18 Mar 2021 18:56:33 +0100https://ask.sagemath.org/question/56231/taking-elements-in-solve-and-multiplying-to-a-polynomial/Answer by slelievre for <p>So I want a function that solves a system of two equations and the move those numerical results to polynomials f and g.</p>
<pre><code>def PolynomialCoefficient(fcn1, fcn2):
var ('a b')
eqn1 = 2*a + 2*b == 0
eqn2 = 3*a - b == 1
List = solve([eqn1, eqn2], a, b)
</code></pre>
<p>Now I want to move this to <code>a*f(x)+b*g(x)</code> without hitting solve each time
and manually typing what <code>a</code> and <code>b</code> are. For example,
<code>PolynomialCoefficient(x, y)</code> should output <code>1/4 x - 1/4 y</code>.</p>
<p>The issue is <code>len(List)</code> outputs 1. If it was length 2 with <code>a</code> in first slot
and <code>b</code> in 2nd slot, I could do</p>
<pre><code> Poly = List[0]*fcn1 + List[1]*fcn2
return Poly
</code></pre>
<p>Any ideas on how to move <code>a</code> and <code>b</code>?</p>
https://ask.sagemath.org/question/56231/taking-elements-in-solve-and-multiplying-to-a-polynomial/?answer=56233#post-id-56233The `solve` function has an optional argument `solution_dict`.
Using it, solutions are returned as dictionaries, which makes it
easier to access the solved values of each unknown.
Here is a way to use that in the function from the question.
def polynomial_coefficient(fcn1, fcn2):
a, b = SR.var('a, b')
eqn1 = 2*a + 2*b == 0
eqn2 = 3*a - b == 1
sol = solve([eqn1, eqn2], a, b, solution_dict=True)[0]
return sol[a]*fcn1 + sol[b]*fcn2Thu, 18 Mar 2021 22:02:59 +0100https://ask.sagemath.org/question/56231/taking-elements-in-solve-and-multiplying-to-a-polynomial/?answer=56233#post-id-56233Comment by whatupmatt for <p>The <code>solve</code> function has an optional argument <code>solution_dict</code>.</p>
<p>Using it, solutions are returned as dictionaries, which makes it
easier to access the solved values of each unknown.</p>
<p>Here is a way to use that in the function from the question.</p>
<pre><code>def polynomial_coefficient(fcn1, fcn2):
a, b = SR.var('a, b')
eqn1 = 2*a + 2*b == 0
eqn2 = 3*a - b == 1
sol = solve([eqn1, eqn2], a, b, solution_dict=True)[0]
return sol[a]*fcn1 + sol[b]*fcn2
</code></pre>
https://ask.sagemath.org/question/56231/taking-elements-in-solve-and-multiplying-to-a-polynomial/?comment=56280#post-id-56280Great thanksSat, 20 Mar 2021 05:19:01 +0100https://ask.sagemath.org/question/56231/taking-elements-in-solve-and-multiplying-to-a-polynomial/?comment=56280#post-id-56280Answer by Max Alekseyev for <p>So I want a function that solves a system of two equations and the move those numerical results to polynomials f and g.</p>
<pre><code>def PolynomialCoefficient(fcn1, fcn2):
var ('a b')
eqn1 = 2*a + 2*b == 0
eqn2 = 3*a - b == 1
List = solve([eqn1, eqn2], a, b)
</code></pre>
<p>Now I want to move this to <code>a*f(x)+b*g(x)</code> without hitting solve each time
and manually typing what <code>a</code> and <code>b</code> are. For example,
<code>PolynomialCoefficient(x, y)</code> should output <code>1/4 x - 1/4 y</code>.</p>
<p>The issue is <code>len(List)</code> outputs 1. If it was length 2 with <code>a</code> in first slot
and <code>b</code> in 2nd slot, I could do</p>
<pre><code> Poly = List[0]*fcn1 + List[1]*fcn2
return Poly
</code></pre>
<p>Any ideas on how to move <code>a</code> and <code>b</code>?</p>
https://ask.sagemath.org/question/56231/taking-elements-in-solve-and-multiplying-to-a-polynomial/?answer=56232#post-id-56232Your `List` is a list of solutions with respect to `a`,`b`. If you want just one solution you can re-define it as
List=solve([eqn1,eqn2],a,b)[0]
and then proceed as in your code;
or if you want to get `Poly` as a list of polynomials (one for each solution), you can define it as
Poly=[ List[0]*fcn1+List[1]*fcn2 for List in solve([eqn1,eqn2],a,b) ]Thu, 18 Mar 2021 21:39:21 +0100https://ask.sagemath.org/question/56231/taking-elements-in-solve-and-multiplying-to-a-polynomial/?answer=56232#post-id-56232Comment by whatupmatt for <p>Your <code>List</code> is a list of solutions with respect to <code>a</code>,<code>b</code>. If you want just one solution you can re-define it as</p>
<pre><code>List=solve([eqn1,eqn2],a,b)[0]
</code></pre>
<p>and then proceed as in your code;
or if you want to get <code>Poly</code> as a list of polynomials (one for each solution), you can define it as</p>
<pre><code>Poly=[ List[0]*fcn1+List[1]*fcn2 for List in solve([eqn1,eqn2],a,b) ]
</code></pre>
https://ask.sagemath.org/question/56231/taking-elements-in-solve-and-multiplying-to-a-polynomial/?comment=56281#post-id-56281Thanks for your help.Sat, 20 Mar 2021 05:19:10 +0100https://ask.sagemath.org/question/56231/taking-elements-in-solve-and-multiplying-to-a-polynomial/?comment=56281#post-id-56281