Ask Your Question

Solve for all values of a variable

asked 2011-01-22 19:10:25 -0500

chaesloc gravatar image



Output: ([a == (c*x - b + d)/x], [1])

I need: [a==c, b==d] for all x.


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2011-01-22 20:30:23 -0500

DSM gravatar image

updated 2011-01-23 02:19:57 -0500

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( 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.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2011-01-22 19:10:25 -0500

Seen: 249 times

Last updated: Jan 23 '11