I provide a custom function in TransformationFunctions
options of FullSimplify
that can do a transformation only for some values of its argument. What is the proper way to bail out from the function and instruct FullSimplify
to skip this transformation for the current argument when the transformation is not applicable to it?
1 Answer
I usually set up a wrapper function that transforms if input is valid and otherwise acts like Identity
:
pickyTransform[expr_] := 0
helper[expr_] /; ! AtomQ[expr] && FreeQ[expr, _?NumericQ] := pickyTransform[expr]
helper[expr_] := expr
FullSimplify[a^3 + x^y, TransformationFunctions -> {helper}]
(* a^3 *)
This works fine most of the time, but in more annoying cases where the transformation function can be smart about expressions of the form Thing + x_ * AnotherThing
even if x
by itself would be considered non-applicable it can be useful to replace all non-applicable subexpressions with something inert and simplify that before replacing the inert subexpressions back to their original form.