Maybe the definition of x1 and x2 had something wrong. I get the desired result using what was shown:
>>> a = (k*x_2 + m)/(x_2 + 1) + (k*x_1 + m)/(x_1 + 1)
>>> b = collect(collect(cancel(a), m), k)
>>> b.subs({x_1 + x_2: -(2*k*m - 8)/k**2, x_1*x_2: m**2/k**2}).simplify()
8*(k + m)/(k**2 - 2*k*m + m**2 + 8)
Another way to think of this problem is that it requires x_1
and x_2
to be eliminated from a
given the relationships you define. So if we solve the coupled relationship for x_1
and x_2
and substitute those into a
we will have the desired result:
>>> e2 # = Eq(x_1 + x_2 , (-2*k*m + 8)/k**2)
x_1 + x_2 == (-2*k*m + 8)/k**2
>>> e3
x_1*x_2 == m**2/k**2
>>> x1x2 = solve((e2,e3),x_1,x_2,dict=True) # two solutions are given
>>> a.subs(x1x2[0]).simplify() # use either solution; the result is the same
8*(k + m)/(k**2 - 2*k*m + m**2 + 8)