custom postfix notation, Apply / Function

后端 未结 1 1517
名媛妹妹
名媛妹妹 2021-02-04 13:34

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

相关标签:
1条回答
  • 2021-02-04 13:53

    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:

    example operator hackery

    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!

    0 讨论(0)
提交回复
热议问题