1 | initial version |

If you want to take two polynomials and match coefficients of like terms, you could do something like

```
from collections import defaultdict
def solve_matched_polynomials(eq, coeff_vars):
# get the non-coefficient variables
vv = set(eq.variables()).difference(coeff_vars)
d = defaultdict(Integer)
# loop over each term in the equation
for term in (eq.lhs()-eq.rhs()).operands():
# get the degrees of the variables
vsig = tuple(term.degree(v) for v in vv)
# find the non-variable part
d[vsig] += term/(prod(v**vs for v,vs in zip(vv, vsig)))
# get solutions with free variables
return solns = solve(d.values(), coeff_vars)
```

which will return the standard free variables form

```
sage: solve_matched_polynomials(5*a*x^2*y+b*x+y == 4*c*x**2*y-d*y, [a,b,c,d])
[[a == 4/5*r66, b == 0, c == r66, d == -1]]
```

and then if you really wanted to eliminate the free variables, you could.

2 | No.2 Revision |

If you want to take two polynomials and match coefficients of like terms, you could do something like

```
from collections import defaultdict
def solve_matched_polynomials(eq, coeff_vars):
# get the non-coefficient variables
vv =
```~~set(eq.variables()).difference(coeff_vars)
~~sorted(set(eq.variables()).difference(coeff_vars))
d = defaultdict(Integer)
# loop over each term in the equation
for term in (eq.lhs()-eq.rhs()).operands():
# get the degrees of the variables
vsig = tuple(term.degree(v) for v in vv)
# find the non-variable part
d[vsig] += term/(prod(v**vs for v,vs in zip(vv, vsig)))
# get solutions with free variables
return solns = solve(d.values(), coeff_vars)

which will return the standard free variables form

```
sage: solve_matched_polynomials(5*a*x^2*y+b*x+y == 4*c*x**2*y-d*y, [a,b,c,d])
[[a == 4/5*r66, b == 0, c == r66, d == -1]]
```

and then if you really wanted to eliminate the free variables, you could.

3 | No.3 Revision |

If you want to ~~take two polynomials and ~~match coefficients of like terms, you could do something like

```
from collections import defaultdict
def solve_matched_polynomials(eq, coeff_vars):
# get the non-coefficient variables
vv = sorted(set(eq.variables()).difference(coeff_vars))
d = defaultdict(Integer)
# loop over each term in the equation
for term in (eq.lhs()-eq.rhs()).operands():
# get the degrees of the variables
vsig = tuple(term.degree(v) for v in vv)
# find the non-variable part
d[vsig] += term/(prod(v**vs for v,vs in zip(vv, vsig)))
# get solutions with free variables
return solns = solve(d.values(), coeff_vars)
```

which will return the standard free variables form

```
sage: solve_matched_polynomials(5*a*x^2*y+b*x+y == 4*c*x**2*y-d*y, [a,b,c,d])
[[a == 4/5*r66, b == 0, c == r66, d == -1]]
```

and then if you really wanted to eliminate the free variables, you could.

4 | No.4 Revision |

If you want to match coefficients of like terms, you could do something like

```
from collections import defaultdict
def solve_matched_polynomials(eq, coeff_vars):
# get the non-coefficient variables
vv = sorted(set(eq.variables()).difference(coeff_vars))
d = defaultdict(Integer)
# loop over each term in the equation
for term in
```~~(eq.lhs()-eq.rhs()).operands():
~~(eq.lhs().operands())+(-(eq.rhs())).operands():
# get the degrees of the variables
vsig = tuple(term.degree(v) for v in vv)
# find the non-variable part
d[vsig] += term/(prod(v**vs for v,vs in zip(vv, vsig)))
# get solutions with free variables
return solns = solve(d.values(), coeff_vars)

which will return the standard free variables form

```
sage: solve_matched_polynomials(5*a*x^2*y+b*x+y == 4*c*x**2*y-d*y, [a,b,c,d])
[[a == 4/5*r66, b == 0, c == r66, d == -1]]
```

and then if you really wanted to eliminate the free variables, you could.

5 | No.5 Revision |

If you want to match coefficients of like terms, you could do something like

```
from collections import defaultdict
def solve_matched_polynomials(eq, coeff_vars):
# get the non-coefficient variables
vv = sorted(set(eq.variables()).difference(coeff_vars))
# make sure they're polynomials
assert all(eq.lhs().is_polynomial(v) and eq.rhs().is_polynomial(v) for v in vv)
d = defaultdict(Integer)
# loop over each term in the equation
for term in (eq.lhs().operands())+(-(eq.rhs())).operands():
# get the degrees of the variables
vsig = tuple(term.degree(v) for v in vv)
# find the non-variable part
d[vsig] += term/(prod(v**vs for v,vs in zip(vv, vsig)))
# get solutions with free variables
return solns = solve(d.values(), coeff_vars)
```

which will return the standard free variables form

```
sage: solve_matched_polynomials(5*a*x^2*y+b*x+y == 4*c*x**2*y-d*y, [a,b,c,d])
[[a == 4/5*r66, b == 0, c == r66, d == -1]]
```

and then if you really wanted to eliminate the free variables, you could.

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.