Suppose I have two functions $h_1(x)$ and $h_2(x)$, for which we know their sum and their difference: $$ h_1(x)+h_2(x) = d(x) \qquad h_1(x)-h_2(x) = i f(x) $$
I have a complicated expression, in which I'd like to replace these sums and subtractions. In principle, I could just define
h1[x_]:= 1/2 (d[x] + I f[x])
h2[x_]:= 1/2 (d[x] - I f[x])
and use Simplify
. However, this is not an option as I'd like to keep the $h_i(x)$ when they appear alone (i.e. not as a sum/subtraction). So I defined a function simp
as follows:
ClearAll[h1,h2,simp]
simp[a_. (b_. h1[x_] + b_. h2[x_]) + e_.] := simp[2 a b d[x] + e]
simp[a_. (b_. h1[x_] - b_. h2[x_]) + e_.] := simp[2 a b I f[x] + e]
This works for the addition, but not for the subtraction, as can be seen with the test expressions:
simp[1/2 a h1[x] + 1/2 a h2[x]]
simp[1/2 a h1[x] - 1/2 a h2[x]]
Why is this? Any idea how I can solve it?