6
$\begingroup$

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]

enter image description here]

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

4 Answers 4

7
$\begingroup$
PiecewiseExpand[f[x] == g[x]]

True

$\endgroup$
0
6
$\begingroup$

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.

$\endgroup$
2
  • 2
    $\begingroup$ 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. $\endgroup$
    – march
    Commented Jan 18, 2018 at 20:57
  • $\begingroup$ 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. $\endgroup$
    – zakk
    Commented Jan 18, 2018 at 22:26
3
$\begingroup$

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

$\endgroup$
3
$\begingroup$
 Simplify @ Rationalize[f[x] - g[x]] 

0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.