Ask Your Question

Taking elements in solve and multiplying to a polynomial

asked 2021-03-18 18:56:33 +0200

whatupmatt gravatar image

updated 2021-03-18 22:00:08 +0200

slelievre gravatar image

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?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-03-18 22:02:59 +0200

slelievre gravatar image

updated 2021-03-18 22:03:12 +0200

The 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]*fcn2
edit flag offensive delete link more


Great thanks

whatupmatt gravatar imagewhatupmatt ( 2021-03-20 05:19:01 +0200 )edit

answered 2021-03-18 21:39:21 +0200

Max Alekseyev gravatar image

updated 2021-03-18 21:39:40 +0200

Your List is a list of solutions with respect to a,b. If you want just one solution you can re-define it as


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


Thanks for your help.

whatupmatt gravatar imagewhatupmatt ( 2021-03-20 05:19:10 +0200 )edit

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2021-03-18 18:56:33 +0200

Seen: 41 times

Last updated: Mar 18