How to rename a built-in function?
I'm not sure I understand your request, but you can usually get what you want with some combination of $PreRead
and either Format
or MakeBoxes
definitions.
From my answers to other qeustions here are an example of $PreRead
behavior and control of output using Format
.
A direct replacement in input and output is possible with:
MakeBoxes[HurwitzZeta[x__], fmt_] := MakeBoxes[Zeta[x], fmt]
$PreRead = # /. {"Zeta" -> "HurwitzZeta"} &;
Then:
Zeta[3, -1/2] === HurwitzZeta[3, -1/2]
True
Sum[(n + a)^(-3/2), {n, 0, Infinity}]
Zeta[3/2, a] (* normally prints HurwitzZeta[3/2, a] *)
I'm not sure how you plan to avoid confusing HurwitzZeta
and Zeta
.
For example, the output of:
RSolve[f[a + 1] == f[a] - 1/Sqrt[a], f[a], a]
is normally:
{{f[a] -> C[1] + HurwitzZeta[1/2, a] - Zeta[1/2]}}
but with the rules above it prints:
{{f[a] -> C[1] + Zeta[1/2, a] - Zeta[1/2]}}
This does not seem correct. What behavior do you intend in this case?
This should be possible using MakeBoxes
and MakeExpression
, but I haven't found a way which I can confidently say is going to be safe.
So beware, the method below may break things and I do not recommend using it! I haven't noticed any breakage yet, but that doesn't mean it doesn't exist.
Example: suppose we want to use Γ
to write the Gamma
function in standard form. We can create a formatting rule for Gamma
:
MakeBoxes[Gamma, StandardForm] = "Γ"
And a parsing rule for Γ
:
MakeExpression[expr_ /; ! FreeQ[expr, "Γ"], StandardForm] :=
MakeExpression[expr /. "Γ" -> "Gamma", StandardForm]
Now you can do things like this:
Note though that the parsing rule I used is extremely aggressive and it will scan every single expression it encounters for occurrences of "Γ"
. This will surely impact the performance of parsing, how severely, I do not know, but I won't be surprised if someone finds that some operations got much slower. It may also replace something I haven't thought of and break things.
I needed to use this very aggressive rule because MakeExpression
is not applied to strings inside e.g. a RowBox
, only the complete RowBox
. So otherwise it would have been necessary to handle both a lonely string (as the sole input) and strings that appear in all the different types of boxes (RowBox
, GridBox
, and possibly many others I don't know about).
For display purposes, you could use Format
. Say you hate Sin
and want it to appear as Sqrt
:
Unprotect[Sin];
Format[Sin] := Sqrt
Format[Sin[x_], TraditionalForm] := Sqrt[x]
Protect[Sin];
Simplify[Cos[x]*Tan[x]]
FullForm[%]
%% /. x -> \[Pi]/4
Plot[%%%, {x, 0, \[Pi]}, AxesLabel -> {x, Sin[x]}]
So it displays as Sqrt
but is still Sin