20
$\begingroup$

The contrast in behavior between, say,

f[x_] = Sin[x^2];
f'[2]

vs.

u[x_, y_] = Cos[x + y^2];

enter image description here

has always bothered my students---and me! (Why does it do it this way?)

Anyway, I tell them to handle ${\partial u\over \partial x}\Big|_{x=2}$ via

enter image description here

Is this the "simplest" way? How else might we accomplish this?

I realize that this is a subjective question, but our context is this is a class where we use Mathematica as a tool rather than a class centered on Mathematica itself. Thus, I want to keep the commands and code required as reasonably elementary as possible (and preferably resembling their paper-and-pencil mathematical work).

$\endgroup$
8
  • $\begingroup$ Seems to be a dupe of this; anyway, look up SeriesCoefficient[]. $\endgroup$ Commented Nov 10, 2012 at 17:09
  • $\begingroup$ @texasAUtiger I have edited your question; but in the process may be answered it? In other words what's wrong with Derivative[1, 0][u][2, y]? $\endgroup$
    – chris
    Commented Nov 10, 2012 at 17:20
  • $\begingroup$ @J. M.: I see what you mean, but unlike the first answer in the link I don't want to appeal to limits since we aren't emphasizing what a partial derivative is but rather just need to evaluate partials at values to accomplish other things. The second answer does address the issue, but I'd say the code is more complicated than a replacement rule. YMMV of course. $\endgroup$
    – JohnD
    Commented Nov 10, 2012 at 17:21
  • $\begingroup$ @chris: Indeed I think you have. I re-edited it to show what I do, leaving you room to formally answer the question with what you had. I just did not know that Derivative[...] functioned differently than using the partial symbol in the palette (at least in the sense here). $\endgroup$
    – JohnD
    Commented Nov 10, 2012 at 17:29
  • 1
    $\begingroup$ Related: Wolfram Blog By the way, do you understand why there is contrast in behavior? $\endgroup$ Commented Nov 10, 2012 at 18:22

2 Answers 2

17
$\begingroup$

You could use as input:

 Derivative[1, 0][u][2, y]

You can implement the formating rule (Thanks to @Rojo)

 Format[Derivative[i_, j_][a_]] := Row[{"\[PartialD]"^(i + j), a}]/
   Row[Row[{"\[PartialD]", #}] & /@ {"x"^i, "y"^j}] 
 Format[(h : Derivative[i_, j_][a_])[x0_, y0_]] := 
   RawBoxes@SubscriptBox[RowBox[{MakeBoxes@h, "\[VerticalLine]"}], 
   MakeBoxes@Row[{Row[{x, "=", x0}], ",", Row[{y, "=", y0}]}]]

so that

   Derivative[1, 2][u][2, 3]

returns

Mathematica graphics

Note that I arbitrary assumed the variables were x,y since I have no way to know from the input alone what the variables are.

This solution also works with input such as

Derivative[1, 2][f][1, x]

Mathematica graphics

Derivative[0, 0][f][1, 3]

Mathematica graphics

Derivative[2, 0][f][1, 3] 

Mathematica graphics

More generally, provided you use (from @Jens see link below)

Derivative /:
MakeBoxes[Derivative[\[Alpha]__][f1_][vars__Symbol],
               TraditionalForm] :=
Module[{bb, dd, sp},
            MakeBoxes[dd, _] ^=
            If[Length[{\[Alpha]}] == 1, "\[DifferentialD]", "\[PartialD]"];
            MakeBoxes[sp, _] ^= "\[ThinSpace]";
            bb /: MakeBoxes[bb[x__], _] := RowBox[Map[ToBoxes[#] &, {x}]];
            FractionBox[ToBoxes[bb[dd^Plus[\[Alpha]], f1]],
            ToBoxes[Apply[bb,  Riffle[Map[bb[dd, #] &, 
            Select[({vars}^{\[Alpha]}), (# =!= 1 &)]], sp]]]]]


D[u[x,y,z],{x,2},{y,3},{z,2}]

Mathematica graphics

Or extending the above rule to arbitrary dimensions (again from @Rojo I am just a secretary here !)

Format[Derivative[i__][a_]] := 
Row[{"\[PartialD]"^Total[{i}], a}]/ 
Times @@ MapIndexed[Subscript["\[PartialD]x", First@#2]^#1 &, {i}]

and

Format[(h : Derivative[i__][a_])[vals__]] := 
RawBoxes@SubscriptBox[RowBox[{MakeBoxes@h, "\[VerticalLine]"}], 
ToBoxes@Row[ Riffle[MapIndexed[ 
Row[{Subscript["x", First@#2], "=", #1}] &, {vals}], ","]]]

so that

Derivative[2, 3, 1][u][1, 2, 3]

returns

Mathematica graphics

$\endgroup$
2
  • 1
    $\begingroup$ This formatting is also found in my answer here, where I give another link to the Wolfram Blog. $\endgroup$
    – Jens
    Commented Nov 10, 2012 at 17:54
  • $\begingroup$ @Jens right! I was trying to find out where I pinched that formatting. I used to use a package called DFormat which your method made obsolete. Thanks! $\endgroup$
    – chris
    Commented Nov 10, 2012 at 17:58
6
$\begingroup$

Perhaps you could add an input alias such as

AppendTo[CurrentValue[$FrontEndSession, InputAliases], 
 "der" -> SuperscriptBox["\[SelectionPlaceholder]", 
   TagBox[RowBox[{"(", "\[Placeholder]", ")"}], Derivative]]]

and then use Esc+der+Esc, and enter the derivatives separated by commas

Mathematica graphics

$\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.