I often need to check if expr1==expr2, where checking for symbolic equality is hard, but a numeric check suffices
To deal with such cases it would be neat to have TildeTilde work like Equal but instead of checking symbolic equality it would substitute unknowns with numeric values and check for numeric equality at several points.
Unknowns are things that “look like” variables in the expression. The ones I can think of have form x,x[1,2] and Subscript[x,2,3]. Any tips welcome!
edit
usually I do something like below, but it requires specifying variables, sometimes requires changing Chop tolerance, and “10 samples” seems arbitrary. An ideal tester would be a function that works like Equals and guarantees meaningful False answers. (to complement Equals which has meaningful True answers)
approxEqual[expr1_, expr2_, vars_] :=
Chop[(expr1 - expr2 /. Thread[vars -> #]) & /@
RandomReal[{-1, 1}, {10, Length[vars]}]] == Table[0, {10}];
expr1 = 1/Sqrt[2] Log[Cosh[q + x/Sqrt[2]] Sech[q - x/Sqrt[2]]];
expr2 = Sqrt[2] ArcTanh[Tanh[q] Tanh[x/Sqrt[2]]];
approxEqual[expr1, expr2, {q, x}]
As a side-note, apparently Maple uses this algorithm for such equality testing
This is somewhat straightforward if you use
FindMaximumas a jumping-off point:Thus:
Obviously, in general this is subject to various numerical error issues that you can address with
AccuracyGoal/PrecisionGoal/WorkingPrecision/ etc. options toFindMaximum. You could also repeatFindMaximumfor multiple starting points for the variables.As an aside, note that
TildeTilde, (i.e.~~), is the infix operator forStringExpression.HTH!