在 Mathematica 中使用 f@expr 和 expr//f 的不同结果 [英] Different results using f@expr and expr // f in Mathematica

查看:30
本文介绍了在 Mathematica 中使用 f@expr 和 expr//f 的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩 PrefixPostfix 运算符(分别为 @//),我遇到了以下问题.

I was playing around with the Prefix and Postfix operators (@ and // respectively) and I ran into the following issue.

给定以下代码,它们的计算方式完全相同:

Given the following code, they evaluate in the same exact way:

Hold[MatrixPlot@Sort@data] // FullForm
(* Hold[MatrixPlot[Sort[data]]] *)

Hold[data // Sort // MatrixPlot] // FullForm
(* Hold[MatrixPlot[Sort[data]]] *)

然而,给定以下表达式,我得到不同的结果:

However, given the following expressions, I get different results:

FunctionExpand@Abs'[0]
(* Abs'[0] *)

Abs'[0] // FunctionExpand
(* 0 *)

我不太确定这是为什么.在我拥有的许多其他代码片段中,f@exprexpr//ff[expr] 都计算为同样的结果.为什么这个特殊情况会给出这样的结果?

I'm not quite sure really why this is. In dozens of other snippets of code I've had, f@expr, expr // f, and f[expr] all evaluate to the same result. Why does this one particular case give this result?

推荐答案

这是一个优先级问题.@ 的优先级高于//.要查看发生了什么,请将光标放在两种情况下的 FunctionExpand 上,然后是 cmd+.(在 OS X 上)或 ctrl+.在其他任何事情上,您最终都会按优先顺序选择内容.

This is a precedence issue. @ has higher precedence than //. To see what is going on, place the cursor on FunctionExpand in both cases, then either cmd+. (on OS X) or ctrl+. on anything else, and you end up selecting things by precedence.

另一种查看方式是使用Trace:

Another way to see it is to use Trace:

FunctionExpand@Abs'[0] // Trace
(*
-> {{{FunctionExpand[Abs],Abs},Abs^\[Prime]},(Abs^\[Prime])[0]}
*)

同时

Abs'[0] // FunctionExpand//Trace
(*
-> {FunctionExpand[(Abs^\[Prime])[0]],0}
*)

特别注意,在第一种情况下,mma 如何首先评估 FunctionExpand[Abs],获得 Abs,然后继续.这正是由于 @// 相比绑定的强度.

In particular, notice how in the first case mma first evaluates FunctionExpand[Abs], obtaining Abs, then continuing. This is precisely due to how strongly @ binds as compared to //.

受@Leonid 评论的启发,这也很有用:

Inspired by @Leonid's comment, this is also informative:

Hold[FunctionExpand@Abs'[0]] // FullForm
Hold[Abs'[0] // FunctionExpand] // FullForm
(*
-> Hold[Derivative[1][FunctionExpand[Abs]][0]]
   Hold[FunctionExpand[Derivative[1][Abs][0]]]
*)

这更好地展示了正在发生的事情.

which is a much better demonstration of what is going on.

这篇关于在 Mathematica 中使用 f@expr 和 expr//f 的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆