Custom rules for user operators

Posted 10 years ago
4209 Views
|
4 Replies
|
4 Total Likes
|
 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.
4 Replies
Sort By:
Posted 10 years ago
 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*ba\[CirclePlus] b(* example-2 *)A.BA\[CirclePlus] BI.M.
Posted 10 years ago
 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"<
Posted 10 years ago
 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 10 years ago
 Are you familiar with the notation package?http://reference.wolfram.com/mathematica/Notation/tutorial/NotationSymbolizeAndInfixNotation.htmlMy 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.