I believe your function works correctly but the automatic transformation functions used by Simplify
lack a rule that converts (-1)^(1/4)
into Sqrt[I]
. Also, your observation that the reverse transformation happens automatically is correct, therefore even with the right transformation function you do not get the result you want. However, you can Hold
the expression to prevent this.
Attributes[f] = HoldAll;
f[expr_] := StringLength @ ToString @ HoldForm @ expr
tf = # /. HoldPattern[(-1)^(1/4)] :> Sqrt @ I &;
Simplify[Hold[(-1)^(1/4)], ComplexityFunction -> f, TransformationFunctions -> tf]
Hold[Sqrt[I]]
I used a transformation rule that explicitly performs the replacement you desire. It also operates inside of Hold
which the default transformations do not. This isn't particuarly helpful I fear but it does illustrate that with the right ComplexityFunction
and TransformationFunctions
settings Simplify
can perform the operation you want. Crafting those functions may be difficult however.
I took another look at this, and it seems that the custom ComplexityFunction
is not needed here. I also found that (-1)^(1/4)
may be represented in at least two different internal forms which illustrates the complexity of crafting your own rules. (In practice if any transformations can be done with built-in functions rather than manual pattern matching they should be done that way.)
auto = Replace[#, x_ :> With[{eval = FullSimplify[x]}, eval /; True], -1] &;
tf = # /. HoldPattern[(-1)^(1/4) | (-1)^Rational[1, 4]] :> Sqrt[I] &;
expr = Hold[1/(3 (1 + x)) - (-1 + 2 x)/(6 (1 - x + x^2)) +
2/(3 (1 + 1/3 (-1 + 2 x)^2)) + (-1)^(1/4)];
Simplify[expr, TransformationFunctions -> {auto, tf}]
Hold[Sqrt[I] + 1/(1 + x^3)]
Notice that in the rule auto
I am calling FullSimplify
to access the default rules, and I am using the Trott-Strzebonski method for In-Place Evaluation to make these apply inside Hold
.
(f@#&)
is redundant; you can use simplyf
. $\endgroup$ComplexityFunction
needed to beFunction
. $\endgroup$