防止导数输出中的重新排序?

发布于 2024-12-22 21:18:52 字数 1025 浏览 7 评论 0原文

关于Wolfram Blog 提供了以下功能,以更传统的方式格式化导数。

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 的一般功能的情况下,有人可以改变它来维持给定的变量顺序,产生如下所示的输出吗 (当然,这纯粹是出于审美原因,使推导更容易让人理解)

在此处输入图像描述

我怀疑这一点实现起来并不简单——除非有人知道可以在Block内临时覆盖的神奇Global选项。

无论如何,这些问题可能是相关的:

A recent post on the Wolfram Blog offered the following function to format derivatives in a more traditional way.

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

An example use, Dt[d[x, a]] // pdConv gives:

enter image description here

Without breaking the general capabilities of pdConv, can someone alter it to maintain the given order of variables, producing the output shown below? (of course this is purely for asthetic reasons, making derivations easier for a human to follow)

enter image description here

I suspect this will be nontrivial to implement---unless someone knows of a magical Global option that can be temporarily overridden within a Block.

For what it's worth, these SO questions may be related:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2024-12-29 21:18:52

可能有一种更简洁的方法来执行 s,但如果纯粹出于演示目的,您可以执行类似的操作,

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]}]]

此处,额外参数 vvf 中的变量列表> 按照您希望偏导数出现的顺序。要使用此功能,您可以执行类似

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

equations in right order

的操作 基本上,此解决方案的作用是暂时替换列表变量 vv 包含按正确字典顺序排列的虚拟变量列表,应用转换,然后用原始变量替换虚拟变量,同时通过将转换后的表达式包装在中来保留所需的顺序HoldForm

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.

-小熊_ 2024-12-29 21:18:52

在看到 Heike 更先进的方法后进行了修改。希望不要打破它。

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

用途:

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

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

窄箱自动订购:

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

用途:

Dt[d[x, a, c, b]] // pdConvAuto

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
风情万种。 2024-12-29 21:18:52

我意识到 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

I realized that Dt[d[x, a, c, b]] already gives ordered output, just in reverse. I am probably misunderstanding the situation, but for some cases this appears to be sufficient:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文