# Verify if two functions are the same

I wrote the function in the figure in two different ways. How can I ask Mathematica to tell me if two functions are the same, beside FullSimplify[f[x] - g[x]]?

f[x_] = Abs[1 - Max[1 - x, x]]

g[x_] = Abs[Abs[x - 0.5] - 0.5]


]

• FullSimplify@Rationalize[f[x] - g[x]]?
– kglr
Commented Jan 18, 2018 at 20:34
• You didn't write the same function two different ways. One is exact, the other is approximate. Commented Jan 18, 2018 at 20:48
• FullSimplify[f[x] == g[x]] yields True; what's wrong with it? Commented Jan 18, 2018 at 20:48
• @corey979 Hah! Oh my. You're right... Commented Jan 18, 2018 at 20:51

## 4 Answers

PiecewiseExpand[f[x] == g[x]]


True

I strongly suspect the problem is undecidable (edit: actually it is, by Richardson's theorem), so in the most general case you have to look for an approximate/numerical solution.

What about this?

If[Quiet@NIntegrate[(f[x]-g[x])^2,{x,-Infinity,Infinity}]<10^-5,True,False]


This method will give false positives, whereas algebraic method will give false negatives.

• This is what I was thinking, although it only guarantees that the two functions are identical almost everywhere, and discrete points where the functions are different might matter. Commented Jan 18, 2018 at 20:57
• As I said the problem is undecidable (by Richardson's theorem), so you can just obtain approximate solutions. The integral will work in some cases FullSimplify[] and the algebraic machinery of Mathematica cannot simplify, whereas -- as you note -- the integral will miss functions identical almost everywhere, which may be clearly different when treated algebraically. It strongly depends on the kind of function you are comparing. In general algebraic methods will give you false negatives (won't be able to see that two function are the same) while the integral will give you false positives.
– zakk
Commented Jan 18, 2018 at 22:26

One of the possible ways is as follows.

f[x_] = Abs[1 - Max[1 - x, x]];
g[x_] = Abs[Abs[x - 0.5] - 0.5];
Maximize[Rationalize[RealAbs[f[x] - g[x]]], x]


{0,{x->1}}

FunctionDomain[f[x], x]


True

FunctionDomain[g[x], x]


True

 Simplify @ Rationalize[f[x] - g[x]]


0