Message Boards Message Boards

0
|
4840 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Custom rules for user operators

Hi,

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?
 (* operator for letters *)
 SetAttributes[CircleTimes, SequenceHold];
 SetAttributes[CircleTimes, Flat];
 (* operator for words *)
 SetAttributes[CirclePlus, SequenceHold];
 SetAttributes[CirclePlus, Flat];
 (* initial sentance word1&word2&word3 *)
 (* word1 = word3 \[Epsilon]\[CircleTimes]x[0]*)
 (* word2 = \[Epsilon]\[CircleTimes]y[0] *)
M[0] = (\[Epsilon]\[CircleTimes]x[0])\[CirclePlus](\[Epsilon]\[CircleTimes]y[0])\[CirclePlus](\[Epsilon]\[CircleTimes] x[0]);
(* compose *)
M[n_Integer] := (M[n - 1] /. \[Epsilon] -> \[Epsilon]\[CircleTimes]x[n])\[CirclePlus](M[n - 1] /. \[Epsilon] -> \[Epsilon]\[CircleTimes]y[n])\[CirclePlus](M[n - 1] /. \[Epsilon] -> \[Epsilon]\[CircleTimes] x[n]) /; n > 0;

For example,  M[1]  'sentence':
\[Epsilon]\[CircleTimes]x[1]\[CircleTimes]x[0]\[CirclePlus]\[Epsilon]\[CircleTimes]x[1]\[CircleTimes]y[0]\[CirclePlus]\[Epsilon]\[CircleTimes]x[1]\[CircleTimes]x[0]\[CirclePlus]\[Epsilon]\[CircleTimes]y[1]\[CircleTimes]x[0]\[CirclePlus]\[Epsilon]\[CircleTimes]y[1]\[CircleTimes]y[0]\[CirclePlus]\[Epsilon]\[CircleTimes]y[1]\[CircleTimes]x[0]\[CirclePlus]\[Epsilon]\[CircleTimes]x[1]\[CircleTimes]x[0]\[CirclePlus]\[Epsilon]\[CircleTimes]x[1]\[CircleTimes]y[0]\[CirclePlus]\[Epsilon]\[CircleTimes]x[1]\[CircleTimes]x[0]
should be transformed into
\[Epsilon]\[CircleTimes]x[1]\[CircleTimes]x[0]\[CirclePlus]\[Epsilon]\[CircleTimes]x[1]\[CircleTimes]y[0]\[CirclePlus]\[Epsilon]\[CircleTimes](x[1] + y[1])\[CircleTimes]x[0]\[CirclePlus]\[Epsilon]\[CircleTimes]y[1]\[CircleTimes]y[0]\[CirclePlus]\[Epsilon]\[CircleTimes](x[1] + y[1])\[CircleTimes]x[0]\[CirclePlus]\[Epsilon]\[CircleTimes]x[1]\[CircleTimes]y[0]\[CirclePlus]\[Epsilon]\[CircleTimes]x[1]\[CircleTimes]x[0]

I.M.
POSTED BY: Ivan Morozov
4 Replies
Hi,

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

I.M.
POSTED BY: Ivan Morozov
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:
myTensorLikeProduct[a_ + b_, c_] := myTensorLikeProduct[a, c] + myTensorLikeProduct[b, c]
myTensorLikeProduct[a_, b_ + c_] := myTensorLikeProduct[a, b] + myTensorLikeProduct[a, c]
Once I know I've written out the rules correctly and tested it to make sure it works, I can then make an infix notation for "myTensorLikeProduct"
<<Notation`
Notation[x_ \[CirclePlus] y_  \[DoubleLongLeftRightArrow]  myTensorLikeProduct[x_,y_]]
The infix notation now works. 

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. 
POSTED BY: Sean Clarke
Sean, thanks for the information, I'll give it a try.
I hope for special cases Rule[], Pattern[]PatternTest [] and MatchQ[] should suffice.
 SetAttributes[CircleTimes, SequenceHold];
 SetAttributes[CircleTimes, Flat];
 SetAttributes[CirclePlus, SequenceHold];
 SetAttributes[CirclePlus, Flat];
 
 EXPR1 = a \[CircleTimes] b\[CircleTimes]  c\[CircleTimes] d\[CircleTimes] x[0]\[CirclePlus]i \[CircleTimes]j\[CircleTimes] k\[CircleTimes] l\[CircleTimes] x[0];
 EXPR1 /. (a_\[CirclePlus]b_) :> {
     a[[1]],
     b[[1]]
    } /. {a_, b_}?(
    "???"
    ) -> {"???"}

I.M.
POSTED BY: Ivan Morozov
Are you familiar with the notation package?

http://reference.wolfram.com/mathematica/Notation/tutorial/NotationSymbolizeAndInfixNotation.html

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. 
POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract