自定义后缀表示法,应用/功能 [英] custom postfix notation, Apply / Function

查看:43
本文介绍了自定义后缀表示法,应用/功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Mathematica 7中设置以下自定义符号.

该符号本身并不是特别有用,因此请不要提出任何建议,或者指出这仅节省了一些击键.

我想知道是否以及如何做.


目前,可以输入

  f =#2 +#^ 2/#3&@@#&;f [{a,b,c}]Out [] = b + a ^ 2/c 

内部函数#^ 2/#3 +#2& Apply '应用于第一个参数.


我想实现语法

  f =#2 +#^ 2/#3 @@&; 

,并使其行为完全相同.也就是说, @@& 表示自动应用于其第一个参数的 Function .

它将需要具有与 & 符号相同的绑定.


最好使用 Notifications 包来完成此操作,而不是手动使用 MakeBoxes ,以便于设置相似的内容符号,即使使用 Notifications 更难以通过文本进行交流.

解决方案

您无法使用自己发明的运算符语法(例如 @@& )来执行此操作.Mathematica只是没有能力在运行时像这样修改语言语法.

您可以至少使用 Notation 包来实现此目的,但是您必须使用在Mathematica中没有任何意义的符号,并且在大多数情况下都可以使用文档可能会给您带来短暂的错误希望,但是正如文档所述,它只会影响打印,而不会影响评估.

HTH!

I would like to set up the following custom notation in Mathematica 7.

This notation is not particularly useful in itself, so please do not suggest existing alternatives, or point out that this only saves a few keystrokes.

I want to know if and how it may be done.


At present, one may enter

f = #2 + #^2 / #3 & @@ # & ;

f[ {a, b, c} ]

Out[]= b + a^2 / c

Where the inner function #^2 / #3 + #2 & is Apply'd to the first argument.


I would like to implement the syntax

f = #2 + #^2 / #3 @@& ;

and have it behave exactly the same. That is, @@& to represent a Function that is automatically applied to its first argument.

It will need to have the same binding as the & symbol.


It is preferable that this is done with the Notations package, to whatever extent that is possible, rather than manual MakeBoxes, for the sake of ease in setting up similar notations, even though the use of Notations is more difficult to communicate via text.

解决方案

You can't do this with an operator syntax of your own invention (like @@&). Mathematica just doesn't have the capability to modify the language grammar at runtime like that.

You can get at least partway there with the Notation package, but you have to use a symbol that has no meaning in Mathematica, and possibly most of the way there with one of the operators without built-in meanings, but most (if any) of them don't bind as postfix operators.

Here, for example, I'll use the Notations package to define the \[Wolf] character as an admittedly pseudo-postfix operator in place of @@&:

In[1]:= Needs["Notation`"]

In[2]:= Notation[x_ \[Wolf] \[DoubleLongLeftRightArrow] (x_ @@ # &)]

In[3]:=  f=#2+#^2/#3& \[Wolf]
Out[3]= (#2+#1^2/#3&) \[Wolf]

In[4]:= f[{a,b,c}]
Out[4]= b+a^2/c

I'll include a screenshot too since this involves notation:

Where this approach may fail is in the fact that you can't set an operator precedence for an arbitrary symbol like \[Wolf]. You can instead use one of the meaningless operators I linked to above, but those also have a fixed precedence that can't be changed.

If you found PrecedenceForm in the documentation you might get a brief false hope, but as the docs say, it only effects printing and not evaluation.

HTH!

这篇关于自定义后缀表示法,应用/功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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