9
$\begingroup$

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:

enter image description here

I then found a function pdConv in Wolfram blog 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:

enter image description here

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):

enter image description here

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.

$\endgroup$
7
  • 3
    $\begingroup$ Try adding the rule f /: Format[f[___]] := f; and similarly for g. $\endgroup$ Commented Nov 24, 2012 at 17:04
  • $\begingroup$ Isn't there a general expression? Thanks for the help. $\endgroup$ Commented Nov 24, 2012 at 17:17
  • $\begingroup$ 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]] $\endgroup$
    – Rojo
    Commented Nov 24, 2012 at 17:17
  • 3
    $\begingroup$ For reference, the function pdConv comes from this page on the Wolfram Blog. $\endgroup$
    – Jens
    Commented Nov 25, 2012 at 0:31
  • 1
    $\begingroup$ Related: mathematica.stackexchange.com/a/241634/1871 $\endgroup$
    – xzczd
    Commented Jan 30 at 8:29

4 Answers 4

6
$\begingroup$

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

Mathematica graphics

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

Mathematica graphics

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

$\endgroup$
10
  • $\begingroup$ Perfect! It's working very well. Thank you! :) $\endgroup$ Commented Nov 25, 2012 at 0:56
  • $\begingroup$ I added Log so that Log[f[x, y]] // clean works $\endgroup$
    – chris
    Commented Nov 25, 2012 at 7:39
  • $\begingroup$ @chris That's okay, but I intended this only as an example. Only Giovanni knows which functions he wants fully displayed. $\endgroup$
    – Mr.Wizard
    Commented Nov 25, 2012 at 7:43
  • $\begingroup$ @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 ;-) $\endgroup$
    – chris
    Commented Nov 25, 2012 at 7:45
  • $\begingroup$ In fact its probably less open-ended(?) to list variables you introduced rather than functions that might occur. $\endgroup$
    – chris
    Commented Nov 25, 2012 at 7:46
4
$\begingroup$

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:

formatEqn[expr_] := Interpretation[TraditionalForm[
    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...

$\endgroup$
1
  • $\begingroup$ Interesting. Worked well too. Thank you. $\endgroup$ Commented Nov 25, 2012 at 18:04
3
$\begingroup$

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

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Commented Nov 24, 2012 at 20:25
  • 1
    $\begingroup$ One would need to exclude Built in symbols as you probably don't want e.g. Log[f[r,z]] to become Log $\endgroup$
    – chris
    Commented Nov 24, 2012 at 21:31
3
$\begingroup$
Clear@pdConv;
pdConv[f_, pat_List] := pdConv[f, Alternatives @@ pat]

defaultpat = _Subscript | _Symbol?(Context[#] === Context[] &);

pdConv[f_, pat_ : defaultpat] := 
 f //. {Derivative[inds__][g : pat][vars__] :> 
     Apply[Defer@
        D[g, ##] &, {{vars}, {inds}}\[Transpose] /. {{var_, 0} :> 
         Sequence[], {var_, 1} :> {var}}], (g : pat)[___] :> g} // TraditionalForm

Please read the document carefully to understand why the code is modified in this manner.

The function can be used in 2 ways. The one-argument syntax should handle most of the usual cases:

D[Subscript[f, 1][x, y], x, x] + D[g[x, y], y] + 
  Subscript[f, 1][x, y] + g[x, y] == 0 // pdConv

enter image description here

For edge cases, set a second argument to specify functions whose independent variables should be omitted:

pdConv[D[\[FormalA][x, y], x] + D[OverTilde[v][x, y], y] + 
       f[\[FormalA][x, y], OverTilde[v][x, y]] == 0, {\[FormalA], OverTilde[v]}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.