I need to combine data structures in operations analogous to addition, subtraction, multiplication and division (and more). I need more than one operation of each type, i.e. more than one method of addition, subtraction, etc. I have made some progress. Using the Notation palette, i can for example define !/!
and %/%
as different operators for division. But how can I set precedence? My impression is that this is not possible. I have explored the existing symbols without in-built meanings, such as CirclePlus
and CircleMinus
. These symbols without in-built meanings have precedence while allowing the meaning to be defined by the user. But I can't find enough of them to meet my needs. For example, there is no "CircleDivide
".
Might it be possible to use subscripts and have $/_a$ and $/_b$ for different types of division while retaining the precedence of the division operator? If I could do it for division, then I could do it for the other operators of interest too.
ADDENDUM:
I've made some progress following the clue provided by Mr.Wizard. The following code creates subscripted operators for operations analogous to +
,-
,*
,/
. The conv
functions are highly simplified for test purposes and in the actual application would perform operations on data structures.
In[1]:= conv[x_, y_, op_ /; op == "+" || op == "-"] := Module[{},
If[op == "-", Return[conv[x, -y, "+"]]]; x + y]
conv[x_, y_, op_ /; op == "*" || op == "/"] := Module[{},
If[op == "/", Return[conv[x, 1/y, "*"]]]; x y]
MakeExpression[RowBox[{x_, SubscriptBox["+", "i"], y_}], StandardForm] :=
MakeExpression[RowBox[{"conv", "[", x, ",", y, ",", "\"+\"", "]"}], StandardForm]
MakeExpression[RowBox[{x_, SubscriptBox["-", "i"], y_}], StandardForm] :=
MakeExpression[RowBox[{"conv", "[", x, ",", y, ",", "\"-\"", "]"}], StandardForm]
MakeExpression[RowBox[{x_, SubscriptBox["*", "i"], y_}], StandardForm] :=
MakeExpression[RowBox[{"conv", "[", x, ",", y, ",", "\"*\"", "]"}], StandardForm]
MakeExpression[RowBox[{x_, SubscriptBox["/", "i"], y_}], StandardForm] :=
MakeExpression[RowBox[{"conv", "[", x, ",", y, ",", "\"/\"", "]"}], StandardForm]
Testing the code, it works at least up to a point and the subscripted operators appear to have the same precedence as the parent operators (+
,*
, etc.):
However, if I remove the parentheses from the second test expression, I get an error message. Why is that occurring and how do I fix it?