Simplify function notation

I have some calculations with arbitrary functions. In the output, Mathematica always shows the functions with its arguments. I would like to tidy the notation a bit, by hiding the arguments in the output.

For example, I have something like this:

f[x,y] + (g[x,y])^2 = ...

and prefer like this:

f + g^2 = ...

I would like to create a function to do this work on any expression, involving all functions... is that possible?

Edit

I had outputs like this: I then found a function to transform in a more human way:

pdConv[f_] := TraditionalForm[f /. Derivative[inds__][g_][vars__] :>
Apply[Defer[D[g[vars], ##]] &, Transpose[{{vars}, {inds}}] /. {{var_, 0} :>
Sequence[], {var_, 1} :> {var}}]];

Much better now: Then I modified it, by taking out the [vars] from g[vars]:

pdConv[f_] := TraditionalForm[f /. Derivative[inds__][g_][vars__] :>
Apply[Defer[D[g, ##]] &, Transpose[{{vars}, {inds}}] /. {{var_, 0} :>
Sequence[], {var_, 1} :> {var}}]];

It now looks like this (notice that the functions arguments don't appear on functions inside the derivatives): Now the last step would be to take off the arguments that appear on the functions outside derivates. The code above was able to do some of the work in a general way... that's what I'm looking for.

• Try adding the rule f /: Format[f[___]] := f; and similarly for g. – J. M. will be back soon Nov 24 '12 at 17:04
• Isn't there a general expression? Thanks for the help. – Giovanni F. Nov 24 '12 at 17:17
• It is possible, but you need to know yourself clearly what you want before you can tell Mathematica. If you were given an output to format, how would you decide what's a function and what's not? Say Plus[x, f[y,z]] – Rojo Nov 24 '12 at 17:17
• Why, do you want this to happen for built-in functions as well? – J. M. will be back soon Nov 24 '12 at 17:18
• For reference, the function pdConv comes from this page on the Wolfram Blog. – Jens Nov 25 '12 at 0:31

I propose a whitelist approach for functions you do want to display the contents of:

clean[expr_] :=
HoldForm[expr] /.
(h : Except[HoldForm | Equal | Power | Times | Plus | Log | Subscript])[args___] :> h

eqn = Subscript[R, ttϕ]^ϕ ==
D[ξ[r, z], r] D[ξ[r, z], z] Exp[4 ψ[r, z] - 2 ν[r, z]]/ξ[r, z];

eqn // clean You have already shown how to handle the special case of Derivative which can be combined with this method.

Alternatively you could show only full expressions for functions in a given context, e.g. System:

clean2[expr_] :=
HoldForm[expr] /.
h_Symbol[args___] /; Context@Unevaluated@h =!= "System" :> h

eqn // clean2 Again this doesn't handle the case of Derivative or SubValues functions.

• Perfect! It's working very well. Thank you! :) – Giovanni F. Nov 25 '12 at 0:56
• I added Log so that Log[f[x, y]] // clean works – chris Nov 25 '12 at 7:39
• @chris That's okay, but I intended this only as an example. Only Giovanni knows which functions he wants fully displayed. – Mr.Wizard Nov 25 '12 at 7:43
• @Mr.Wizard yes; I don't see why its better to specify functions you don't want because its as endless as functions you want; right now BesselY[2, f[x, y]] // clean does to work either ;-) – chris Nov 25 '12 at 7:45
• In fact its probably less open-ended(?) to list variables you introduced rather than functions that might occur. – chris Nov 25 '12 at 7:46

Restrict on arguments

As others have mentioned replacing all functions with arguments with just their names wouldn't leave you with something very useful, so you have to make some restrictions. It has been shown how you can restrict for which functions such replacements should be made. What I would like to add is the possibility to make such redefinitions only for certain kinds of arguments. This could be as simple as:

eqn = Subscript[R, ttϕ]^ϕ ==
D[ξ[r, z], r] D[ξ[r, z],
z] Exp[4 ψ[r, z] - 2 ν[r, z]]/ξ[r, z];
(eqn // pdConv) /. {f_[r, z] :> f}

for the example you gave.

Formal Parameters

A feature that is available since Mathematica 7 are special names for "formal parameters" which are Protected so you can be reasonably sure they won't have values. I think it's just such formula type expressions as yours that these were introduced, so you might want to use them for your arguments. If you do so you could make this approach somewhat more general: First define the helper function:

formalQ[___] = False;
formalQ[x_Symbol] := StringMatchQ[
ToString[x, CharacterEncoding -> "ASCII"], "\\[Formal" ~~ __
];

and then define your expressions using those formal parameters (which you can enter with e.g. Esc-\$-x-Esc).

eqn = Subscript[R, ttϕ]^ϕ ==
D[ξ[\[FormalR], \[FormalZ]], \[FormalR]] D[ξ[\[FormalR], \
\[FormalZ]], \[FormalZ]] Exp[
4 ψ[\[FormalR], \[FormalZ]] -
2 ν[\[FormalR], \[FormalZ]]]/ξ[\[FormalR], \[FormalZ]];

and now use something like this to get the desired simplification:

(eqn // pdConv) /. {f_[__?formalQ] /; Context[f] =!= "System" :> f}

Interpretation

Another thing I would like to mention is that you could make your results look the way you want but still evaluate correctly by using Interpretation. The following does this for a combination of pdConv, the above replacement and some additional formatting so one can immediately see which of the variables are actually functions with left out parameters:

expr /. {
Derivative[inds__][g_][vars__] :> Interpretation[Apply[
HoldForm[D[g[vars], ##]] &, Transpose[{{vars}, {inds}}] /. {
{var_, 0} :> Sequence[],
{var_, 1} :> {var}
}],
Derivative[inds][g][vars]]
} /. {
\[FormalF]_Symbol[\[FormalA]__?formalQ] /;
Context[\[FormalF]] =!= "System" ->
Interpretation[
Style[\[FormalF], RGBColor[0, 0.6, 0],
Bold], \[FormalF][\[FormalA]]]
}
],
expr
];

You should now be able to copy the result (or parts of it), paste to a new input cell and evaluate that new cell. It would also be possible to programmatically do further work with the result given above with something like this:

formattedEqn = eqn // formatEqn
(ToExpression[ToBoxes[formattedEqn]] /. ξ->Function[Sin[#1]*Cos[#2]])// formatEqn

I'm guess that this will not exactly match your needs but think with this and the information others gave and some further reading of the documentation of the functions used/mentioned above you should be well prepared to exactly build what you need...

• Interesting. Worked well too. Thank you. – Giovanni F. Nov 25 '12 at 18:04

As suggested in the comments

eqn =
Subscript[R, ttϕ]^ϕ ==
D[ξ[r, z], r] D[ξ[r, z],
z] Exp[4 ψ[r, z] - 2 ν[r, z]]/ξ[r, z]

Using a Format rule:

Format[ξ[r, z]] = ξ;
Format[ψ[r, z]] = ψ;
Format[ν[r, z]] = ν;

eqn • Yes, it works, but again, I would like to know if there is a general expression that converts everything at once. I use different symbols from time to time and if this needs to be done every time, I prefer not doing it and leaving in the original format. Thanks anyway. – Giovanni F. Nov 24 '12 at 20:25
• One would need to exclude Built in symbols as you probably don't want e.g. Log[f[r,z]] to become Log – chris Nov 24 '12 at 21:31