Define Custom Notation in Mathematica

前端 未结 2 1814
野趣味
野趣味 2021-01-07 06:13

I often need to extract to restrict value lists to sublists, ie if vals gives values of vars={x1,x2,x3,x4}, and I need values of svars={x2,x4

2条回答
  •  时光说笑
    2021-01-07 06:30

    Good notations can be very useful - but I'm not sure that this particular one is needed...

    That said, the Notation package makes this pretty easy. As there are many hidden boxes when you use the Notation palette, I'll use a screenshot: alt text

    You can see the underlying NotationMake* downvalues construct by using the Action -> PrintNotationRules option. In[4] in the screenshot generates

    NotationMakeExpression[
      SubscriptBox[vals_, RowBox[{vars_, "|", svars_}]], StandardForm] := 
     MakeExpression[
      RowBox[{"restrict", "[", RowBox[{vars, ",", svars, ",", vals}], 
        "]"}], StandardForm]
    
    NotationMakeBoxes[Subscript[vals_, vars_ | svars_], StandardForm] := 
     SubscriptBox[MakeBoxes[vals, StandardForm], 
      RowBox[{Parenthesize[vars, StandardForm, Alternatives], "|", 
        Parenthesize[svars, StandardForm, Alternatives]}]]
    

提交回复
热议问题