How to attack the following problem efficiently?
I’m building a ‘text’ composed of ‘sentences’ that, in their turn, composed of ‘words’.
There are two types of words: ending with ‘letters’ x[0] or y[0] and I need to merge words ending with identical letters.
Is it possible to define, say, CirclePlus to act in such a way**?**
My first suggestion is to first get the computations working with a standard prefix syntax first. Once you’ve done this, you can use the notations package to the make the corresponding infex operators you are interested in.
First get the computations working with a standard prefix syntax first. Don’t use use infix notation for right now. Just get it to work with standard prefix syntax.
Here’s a short example where I make some tensor-like product. First I define how the tensor product works. I don’t use infix notation, but instead use the common prefix notation of Mathematica programming:
So in short, the first step is to get your algebra to work with prefix notation using common Mathematica programming. You can do this using UpValues, and Downvalues etc.
Only once you have it working with prefix notation should you begin to worry at all about making an infix notation.
Sean, this package is really nice, I was not aware of its mere existence, thank you so much.
Looks like it’s quite easy to define polymorph operators with this package. Or there exist a better way to do it?
<< Notation`
(* reals *)
a = 1.; b = 5.;
(* matrices *)
A = {{1, -1}, {3, 1}}; B = {{1, 2}, {3, 4}};
(* prefix syntax *)
f[x_, y_] := Module[{res},
If[Head[x] == Real, res = x y];
If[Head[x] == List, res = x.y];
res];
(* create notation *)
Notation[ParsedBoxWrapper[RowBox[{"x_", " ", "\[CirclePlus]", " ","y_"}]] \[DoubleLongLeftRightArrow] ParsedBoxWrapper[RowBox[{"f", "[",RowBox[{"x_", ",", "y_"}], "]"}]]];
(* example-1 *)
a*b
a\[CirclePlus] b
(* example-2 *)
A.B
A\[CirclePlus] B