Find values that make two expressions equivalent

35 views
Skip to first unread message

Renan Birck Pinheiro

unread,
May 25, 2015, 6:17:56 PM5/25/15
to sy...@googlegroups.com
Hi,

Say I have two expressions, for example, f(x) = (a+b)*x^2 + (b+c)*x + c and g(x) = 3*x^2 + 5*x + 1.

In this case one can see that a+b = 3, b+c = 5 and c = 1, then setting a = -1, b = 4 and c=1 makes the two expressions equivalent (that is, f(x) - g(x) = 0 for all x - I don't know the correct term).

How can I, using SymPy, find the values of a, b, c, that make those expressions equivalent?

Thanks!

Renan Birck Pinheiro

unread,
May 25, 2015, 7:52:59 PM5/25/15
to sy...@googlegroups.com
Hi,

I found that sympy.solvers.solvers.solve_undetermined_coeffs does what I want, e.g. using my example:

>>> # Sample problem (for testing)
>>> a, b, c, x = sym.var('a b c x')
>>> f = lambda x: (a+b)*x**2 + (b+c)*x + c
>>> g = lambda x: 3*x**2 + 5*x + 1
>>> solvers.solve_undetermined_coeffs(f(x) - g(x), [a, b, c], x)
{a: -1, b: 4, c: 1}


Chris Smith

unread,
May 26, 2015, 10:40:37 AM5/26/15
to sy...@googlegroups.com
It's good you found the method. solve will also try to detect such cases:

>>> solve(f(x)-g(x),(a,b,c))
{c: 1, b: 4, a: -1}
Reply all
Reply to author
Forward
0 new messages