# Subtracting equations from each other?

I have two equations

eqhy

-2 x + f[x, y[x]] - x^2 y[x]^3 == hy[y[x]]

eqfhx
-2 E^(4 y[x]) + f[x, y[x]] - x^2 y[x]^3 == hx[x]


I would like to subtract these equations from one another, like this

solu12 = eqhy - eqfhx


which currently yields

-(-2 E^(4 y[x]) + f[x, y[x]] - x^2 y[x]^3 == hx[x]) + (-2 x +
f[x, y[x]] - x^2 y[x]^3 == hy[y[x]])


I would like it to yield the actual remaining terms

How do I do this in mathematica?

• Inner[Subtract, eqhy, eqfhx, Equal]? Commented Sep 5, 2016 at 15:50

An easy way to munge equations is to convert them to lists and then convert them back to equations when you are done munging. In your case, like so:

eq1 = -2 x + f[x, y[x]] - x^2 y[x]^3 == hy[y[x]];
eq2 = -2 E^(4 y[x]) + f[x, y[x]] - x^2 y[x]^3 == hx[x];
w1 = List @@ eq1;
w2 = List @@ eq2;
w3 = w1 - w2;
eq3 = Equal @@ w3


2 E^(4 y[x]) - 2 x == -hx[x] + hy[y[x]]

I'm going to guess that your actual goal here is to eliminate one of the terms shared by the two equations. In that case, you could also use Eliminate:

Eliminate[{eqfhx,eqhy},f[x,y[x]]]


$\text{hy}(y(x))-2 e^{4 y(x)}+2 x=\text{hx}(x)$

This doesn't do the subtraction exactly the way you asked for, but usually the goal of such a subtraction is to eliminate something. And that's why this may be the thing you actually need. It has the advantage that it will figure out how to do the subtraction all by itself.

You could use Inner, which is similar to Dot operation on matrices (but with operation of your choice not Times and Plus).

In the following code, the first element (LHS) of the first argument (eqhy) is subtracted by the first element (LHS) of the second argument (eqfhx), and second element (RHS) of the first argument (eqhy) is subtracted by the second element (RHS) of the second argument (eqfhx):

eqhy = -2 x + f[x, y[x]] - x^2 y[x]^3 == hy[y[x]];
eqfhx = -2 E^(4 y[x]) + f[x, y[x]] - x^2 y[x]^3 == hx[x];
Inner[Subtract, eqhy, eqfhx, Equal]


2 E^(4 y[x]) - 2 x == -hx[x] + hy[y[x]]

This seemed merely cute at first, but, after thinking about it, it actually can be useful:

eqhy - eqfhx /. Equal -> List /. List -> Equal  (* remember that Plus threads over List *)
(*  2 E^(4 y[x]) - 2 x == -hx[x] + hy[y[x]]  *)


We can add multiples of equations this way, too:

(x + 2 y == 3) - 2 (2 x + y == 4) /. Equal -> List /. List -> Equal
Simplify[%]
(*
x + 2 y - 2 (2 x + y) == -5
3 x == 5
*)


Likewise, we can take the dot product of a system of equations with a vector of coefficients:

{1, -2, 1}.{2 x + 3 y - z == 3, x + y + 2 z == 4, 2 x - y + 2 z == 4} /.
Equal -> List /. List -> Equal // Expand
(*  2 x - 3 z == -1  *)


Thinking about it further, the trick effectively does the same computational steps as m_goldberg's answer, only without needed extra variables.