5

Wolfram 博客上最近的一篇文章提供了以下功能,以更传统的方式格式化衍生品。

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

一个示例使用,Dt[d[x, a]] // pdConv给出:

在此处输入图像描述

在不破坏 的一般功能的情况下pdConv,有人可以对其进行更改以保持给定的变量顺序,从而产生如下所示的输出吗?(当然这纯粹是出于美观的原因,使推导更容易让人理解)

在此处输入图像描述

我怀疑这将是不平凡的实现——除非有人知道Global可以在Block.

对于它的价值,这些 SO 问题可能是相关的:

4

3 回答 3

4

There is probably a cleaner way to do s, but if it's purely for presentation purposes, you could do something like

pdConv[f_, vv_] :=
 Module[{v},
  (HoldForm[
       Evaluate@
        TraditionalForm[((f /. Thread[vv -> #]) /. 
           Derivative[inds__][g_][vars__] :> 
            Apply[Defer[D[g[vars], ##]] &, 
             Transpose[{{vars}, {inds}}] /. {{var_, 0} :> 
                Sequence[], {var_, 1} :> {var}}])]] /. 
      Thread[# -> vv]) &@ Table[Unique[v], {Length[vv]}]]

Here, the extra parameter vv is a list of the variables in f in the order you want the partial derivatives to appear. To use this function, you would do something like

pdConv[Dt[d[x, c]], {x, c}]

equations in right order

Basically what this solution does is to temporarily replace the list of variables vv with a list of dummy variables which are in the right lexicographical order, apply the transformation, and then replace the dummy variables with the original variables while preserving the desired order by wrapping the transformed expression in HoldForm.

于 2011-12-24T12:37:43.007 回答
1

Revised after seeing Heike's far superior method. Hopefully without breaking it.

ClearAll[pdConv]

pdConv[order_List][f_] :=
  With[{R = Thread[order -> Sort@order]},
    HoldForm@TraditionalForm@# /. Reverse[R, 2] &[
     f /. R /. Derivative[inds__][g_][vars__] :>
        (Defer@D[g[vars], ##] & @@ Pick[{vars}, {inds}, 1])]
  ]

Use:

Dt[d[x, a]] // pdConv[{x, a}]

Dt[d[x, a, c, b]] // pdConv[{x, a, c, b}]

Automatic ordering for a narrow case:

ClearAll[pdConvAuto]
SetAttributes[pdConvAuto, HoldFirst]

pdConvAuto[f : Dt@_@syms__] :=
  With[{R = Thread[{syms} -> Sort@{syms}]},
    HoldForm@TraditionalForm@# /. Reverse[R, 2] &[
     f /. R /. Derivative[inds__][g_][vars__] :>
        (Defer@D[g[vars], ##] & @@ Pick[{vars}, {inds}, 1])]
  ]

Use:

Dt[d[x, a, c, b]] // pdConvAuto
于 2011-12-24T12:38:41.700 回答
0

我意识到Dt[d[x, a, c, b]]已经给出了有序的输出,只是相反。我可能误解了这种情况,但在某些情况下,这似乎就足够了:

ClearAll[pdConv]

pdConv[f_] :=
 Apply[Plus, HoldForm@TraditionalForm@#, {2}] &[
  Reverse[List @@ f] /. Derivative[inds__][g_][vars__] :>
    (Defer@D[g[vars], ##] & @@ Pick[{vars}, {inds}, 1])
  ]

Dt[d[x, a, r, c, b]] // pdConv
于 2011-12-24T14:42:42.953 回答