solving a linear problem with (multivariate) polynomial coefficients

24 views
Skip to first unread message

Michele Zaffalon

unread,
Jan 15, 2017, 11:28:45 PM1/15/17
to sympy
I am struggling to find a way of solving the following equation (simplified example)
```
f = x+1
g = 2*x+1
expr = a*f + b*g - 1
solve(expr, (a, b))
```
I would like to have `a=2`, `b=-1` which make `expr` identically zero.

I am interested in polynomials with higher powers: at the moment, I derive with respect to x, subs 0 for x, accumulate the result as column in a matrix and solve the linear problem associated. This method works for univariate polynomials, but it become impractical for multivariate linear systems, which is my ultimate goal, because of the need to derive with respect to all mixed terms x^m*y^n. (I also could not find a way of collecting the coefficients of the terms x^m*y^n.)

Is there a way to solve these linear problems?

Thank you,
michele

Aaron Meurer

unread,
Jan 15, 2017, 11:42:15 PM1/15/17
to sy...@googlegroups.com
Perhaps solve_undetermined_coeffs() can do what you want.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/e6b86d1d-fb00-4022-8f25-f80ba0404d2c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Michele Zaffalon

unread,
Jan 16, 2017, 1:19:33 AM1/16/17
to sympy
A few minutes after I posted my question, I found that Poly.coeffs does what I want and works for multivariate polynomials

```
lin = Poly(expr, x, y).coeffs()
solve(lin, [a,b,c,d,e])
```
with the following definitions
```
x, y = symbols("x, y")
a, b, c, d, e = symbols("a, b, c, d, e")

f = 3*x**2-2*y+1
g = x*y+x-2*y
h = x**2+2*y**2-2
j = x+3
k = x**2-x*y+2
expr = a*f + b*g + c*h + d*j + e*k - (7*x**2 + 2*x*y + 4*x - 4*y**2 - 14*y + 9)

```
Reply all
Reply to author
Forward
0 new messages