Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to write partial derivatives of functions with many arguments. Why is it that when I type

f[x,y] ctrl+6 (0,1)

it turns out to be bad syntax? The output of

D[f[x,y],y]

looks very much like f with a superscript (0,1).

share|improve this question
add comment

5 Answers

up vote 8 down vote accepted

For a start, f[x,y]^(0,1) isn't the same as f^(0,1)[x,y].

But the real reason is that these expressions are very different in meaning, as revealed by their FullForm:

D[f[x, y], y] // FullForm
Derivative[0,1][f][x,y]

versus (and I had to use a simple symbolic expression as the exponent to show what was going on:

f[x,y]^z//FullForm
Power[f[x,y],z]

Similarly, Derivative doesn't correspond to Superscript. They are syntactically different despite the visual similarities.

Stick with D[f[x,y],y] and so on. If you need the vector derivative, you can use the syntax:

 D[f,{{x1,x2,x3...}}]

as described in the documentation.

share|improve this answer
    
But if I write D[f[x,y],y] and evaluate, I can copy the output (which looks like f^(0,1)) and use that. Mathematica understands that this is a derivative and not a power. –  yohbs Apr 1 '12 at 13:24
    
@yohbs that's what is displayed not how it is stored internally. As Verbeia said, internally it is Derivative[0,1][f][x,y], but it has a Format, or similar, that makes it displayed as f$^\text{(0,1)}$[x,y]. –  rcollyer Apr 1 '12 at 13:34
add comment

Verbeia is right. An alternative notation is to use escpdesc which gives a partial derivative; thus, typing escpdesc ctrl-t followed by f[x,t] will give the derivative of f with respect to its second argument.

For instance, this is a valid way to specify a differential equation:

enter image description here

This is closer to what you're after than D[f[x,t],t], for instance.

share|improve this answer
add comment

I have a function called "AbleitungsForm" (Ableitung is german for Derivative) which is based on an answer I found here in SE. I coudn't find the original answer. It looks like this:

AbleitungsForm::ON = "AbleitungsForm with Options \"AuchStandard\[Rule]`1`\" and \"MitArgumenten\[Rule]`2`\" is activ.";
AbleitungsForm::OFF = "AbleitungsForm has been deactivated.";
abFOpts={}

SyntaxInformation[AbleitungsForm]={"ArgumentsPattern"->{_,OptionsPattern[]}};
Options[AbleitungsForm]={AuchStandard->True,MitArgumenten->False};
AbleitungsForm[On,opt:OptionsPattern[]] :=
(Quiet[AbleitungsForm[Off]];
 abFOpts={OptionValue[AuchStandard],OptionValue[MitArgumenten]};
  If[OptionValue[AuchStandard] === True,
    If[OptionValue[MitArgumenten] === False,
     Derivative /: 
      MakeBoxes[Derivative[inds__][g_][vars__Symbol], 
       form : TraditionalForm | StandardForm | DAFX] :=
      Module[{bb, dd, sp},
        MakeBoxes[dd, _] ^= 
           If[Length[{inds}] == 1, "\[DifferentialD]", "\[PartialD]"];
        MakeBoxes[sp, _] ^= "\[ThinSpace]";
        bb /: MakeBoxes[bb[x__], _] := RowBox[Map[ToBoxes[#] &, {x}]];
        FractionBox[ToBoxes[bb[dd^Plus[inds], g]], 
          ToBoxes[Apply[bb, 
            Riffle[Map[bb[dd, #] &, Select[({vars}^{inds}), (# =!= 1 &)]],
              sp]]]]],
     Derivative /: 
      MakeBoxes[Derivative[inds__][g_][vars__Symbol], 
       form : TraditionalForm | StandardForm | DAFX] :=
      Module[{bb, dd, sp, vd},
        MakeBoxes[dd, _] ^= 
           If[Length[{inds}] == 1, "\[DifferentialD]", "\[PartialD]"];
        MakeBoxes[sp, _] ^= "\[ThinSpace]";
        vd[f_, v__, fmt_] := DisplayForm@ToBoxes[f[v], fmt];
        bb /: MakeBoxes[bb[x__], _] := RowBox[Map[ToBoxes[#] &, {x}]];
        FractionBox[ToBoxes[bb[dd^Plus[inds], vd[g, vars, form]]], 
          ToBoxes[Apply[bb, 
           Riffle[Map[bb[dd, #] &, Select[({vars}^{inds}), (# =!= 1 &)]],
             sp]]]]]
    ],
    If[OptionValue[MitArgumenten] === False,
     Derivative /: 
      MakeBoxes[Derivative[inds__][g_][vars__Symbol], 
       form : TraditionalForm | DAFX] :=
      Module[{bb, dd, sp},
        MakeBoxes[dd, _] ^= 
           If[Length[{inds}] == 1, "\[DifferentialD]", "\[PartialD]"];
        MakeBoxes[sp, _] ^= "\[ThinSpace]";
        bb /: MakeBoxes[bb[x__], _] := RowBox[Map[ToBoxes[#] &, {x}]];
        FractionBox[ToBoxes[bb[dd^Plus[inds], g]], 
          ToBoxes[Apply[bb, 
           Riffle[Map[bb[dd, #] &, Select[({vars}^{inds}), (# =!= 1 &)]],
             sp]]]]],
     Derivative /: 
      MakeBoxes[Derivative[inds__][g_][vars__Symbol], 
       form : TraditionalForm | DAFX] :=
      Module[{bb, dd, sp, vd},
        MakeBoxes[dd, _] ^= 
           If[Length[{inds}] == 1, "\[DifferentialD]", "\[PartialD]"];
        MakeBoxes[sp, _] ^= "\[ThinSpace]";
        vd[f_, v__, fmt_] := DisplayForm@ToBoxes[f[v], fmt];
        bb /: MakeBoxes[bb[x__], _] := RowBox[Map[ToBoxes[#] &, {x}]];
        FractionBox[ToBoxes[bb[dd^Plus[inds], vd[g, vars, form]]], 
          ToBoxes[Apply[bb, 
           Riffle[Map[bb[dd, #] &, Select[({vars}^{inds}), (# =!= 1 &)]],
              sp]]]]]
   ]
  ];
  Message[AbleitungsForm::ON,
    OptionValue[AuchStandard], OptionValue[MitArgumenten]];)

AbleitungsForm::noset="AbleitungsForm ist nicht aktiv.";  
AbleitungsForm[Off] :=
 (If[Position[FormatValues[Derivative],DAFX]!={},
    abFOpts={};
    (FormatValues[Derivative] =
       Delete[FormatValues[Derivative], 
           Position[FormatValues[Derivative], DAFX][[1, 1]]];);
    Message[AbleitungsForm::OFF], 
    Message[AbleitungsForm::noset]];)

AbleitungsForm[]:=
  If[abFOpts==={},
   Message[AbleitungsForm::noset],
   Message[AbleitungsForm::ON,abFOpts[[1]],abFOpts[[2]]]]; 

You may switch the display of derivatives on or off by calling AbleitungsForm[On] or AbleitungsForm[Off].
AbleitungsForm[] yelds a message displaying the status

Normally this changes the display for TraditionalForm only. There are two Options: Option AuchStandard->True makes it work in StandardForm too Option Mit Argumenten->True schows the functions Arguments.

Some Remarks

The function changes the FormatValues of Derivative. To see, where in the FormatValues these changes took place, I added the meaningless DAFX to the form parameter, which is used when switching Off.

As @Sjoerd pointed out, you can't paste the displayed expression as is (it's a Form). But this works:

equ = f''[x] == x f'[x]

equ // FullForm
DSolve[equ, f[x], x]

![displayed]: http://i.stack.imgur.com/SNEOs.png

share|improve this answer
    
Thanks for the code Peter. There are, however, several errors here (I counted at least 3). Also, AbleitungsForm[Off] doesn't work sometimes. And for the general public it would be helpful if you could translate the German text into English. A description of its use with some examples would be nice too. –  Sjoerd C. de Vries Apr 1 '12 at 19:56
    
@SjoerdC I edited my code after a full copy-paste. If you still get an error, please report –  Peter Breitfeld Apr 1 '12 at 20:35
    
They are gone now. Just Syntaxinformation has to be spelled right. I really feel you should say something about the usage of this function. If I copy the result of D[f[x], x] with AbleitungsForm[On] in, for instance, a DSolve it doesn't work. Seems it is for display purposes only. –  Sjoerd C. de Vries Apr 1 '12 at 20:44
    
yes I had a typo here. The FullForm of the expr is untouched. You cant paste it in eg. DSolve. But writing eq=stuff and Evaluating in place, you can use eq in DSolve –  Peter Breitfeld Apr 1 '12 at 21:09
    
this is really nice. This MakeBox business seems useful. If you find the original answer from which you were inspired for this, please add it to the answer (I'd like to find out how this works) –  acl Apr 1 '12 at 22:52
show 3 more comments

What internally makes the superscript behave as a Derivative seems to be implemented with TagBox, this is what the output looks like:

SuperscriptBox["f", 
TagBox[
  RowBox[{"(", 
     RowBox[{"0", ",", "1"}], ")"}],
  Derivative],
MultilineFunction -> None]

If you show this with DisplayForm you will get something that looks like a superscript but evaluates as a Derivative. There are some similar examples mentioned in the documentation of TagBox. There seems to be no way to give this as input (except for inserting the TagBox by hand in the raw cell expression, of course...).

Edit: Of course one should never say it can't be done. I just stumbled over InputAliases in a post to mathgroup, a feature I usually don't use and thus always forget. Of course that would let you define a custom shortcut to insert exactly that box-expression, e.g. by evaluating the following:

CurrentValue[EvaluationNotebook[], InputAliases] = 
 Append[CurrentValue[EvaluationNotebook[], InputAliases], 
  "drv" -> SuperscriptBox["\[Placeholder]", 
    TagBox[RowBox[{"(", RowBox[{"\[Placeholder]"}], ")"}], 
     Derivative], MultilineFunction -> None]
  ]

then use EscdrvEsc to insert a corresponding template which you can fill by jumping from placeholder to placeholder using Tab. You could of course alternatively add such rules to your preferred stylesheet.

share|improve this answer
    
@rcollyer: thanks for the edit, it helped me to learn how to input those nice keyboard shortcuts. Another question: are there any conventions on how one would enter such shortcuts, I have seen some variations and am not sure whether what I have now done (more as an excercise) is how one would do it. Feel free to correct it again... –  Albert Retey Apr 2 '12 at 16:18
    
Unfortunately, I don't see any consistency either. My primary goal is readability, and the sequence of keys is readable. So, I wouldn't worry about it. –  rcollyer Apr 2 '12 at 16:23
add comment

There is another way to input derivatives for people who really like TraditionalForm. I like TraditionalForm output to look as much as possible like a $\LaTeX$ typeset formula, and that's why I came up with the format that Peter is using in his answer. For example, I want partial derivatives to look like this: $\frac{\partial^2 f}{\partial x\partial y}$ or like this: $\frac{\partial^2 f(x,y)}{\partial x\partial y}$

The first one is achieved with the code I posted in this MathGroup thread.

But the more we tweak the output to look like classical math typesetting, the more incongruous the InputForm and StandardForm will look compared to the output.

I think the question in this post really is concerned with this disconnect. So I thought it's worth addressing how this can be bridged if you do decide to massage TraditionalForm into the above, more polished, form.

One possible way is to enter equations in TraditionalForm, too (not just output them that way). With the default settings, Mathematica doesn't exactly make this completely smooth (because it rightfully wants to avoid the potential ambiguities of TraditionalForm input). But it can be done.

To enter a partial derivative like the one above in the same form as above, the steps are as follows (trying to give a detailed description, but assuming you know how to input superscripts etc.):

  1. Start a new input cell
  2. Enter the name of the function f
  3. Use keyboard or mouse to highlight the f and go to the menu item Cell > Convert to > TraditionalForm (or use the keyboard shortcut)
  4. Now the f should have turned into an italic $f$ and you can continue by editing this cell:
  5. Create a fraction (ctrl-/), add partial derivative symbols $\partial$ (escpdesc) exactly following the visual form of the example displayed above (including powers $\partial^2$ entered exactly like normal powers). For function arguments, use round parentheses $(x,y)$.

Now you can evaluate the cell. Mathematica will ask if you want to evaluate the input, and we have to confirm that we do. The point of this exercise is that you can in principle input expressions for (partial) derivatives in exactly the same form as they look in the $\LaTeX$-like TraditionalForm output that you get with my modification or the one in Peter's answer.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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