Definition of special operators in mathematics

How do you define a special operator in Mathematica, for example a special type of additive or multiplicative operator? I have done this in the past, but I cannot remember where I put the code. I tried to define this filled small circle operator on two matrices:

A_\[FilledSmallCircle] B_ := Which[(MatrixQ[A] || VectorQ[A]) && (MatrixQ[B] || VectorQ[B]), AB, ! (MatrixQ[A] || VectorQ[A]) && (MatrixQ[B] || VectorQ[B]), A@B , (MatrixQ[A] || VectorQ[A]) && ! (MatrixQ[B] || VectorQ[B]), Transpose[ B@Transpose [A]]]; 

But that will not work. What am I doing wrong?

+4
source share
3 answers

So, you are trying to make an operator with an infix action. If you compare it with the built-in infix operators + , * , ** , \[CircleTimes] , etc .... you will see that they are all interpreted in their FullForm : Plus , Times , NonCommutativeMultiply , CircleTimes respectively.

You should probably try to create something similar. So start with

 BigDot[A_, B_] := Which[ (MatrixQ[A] || VectorQ[A]) && (MatrixQ[B] || VectorQ[B]), AB, !(MatrixQ[A] || VectorQ[A]) && (MatrixQ[B] || VectorQ[B]), A@B , (MatrixQ[A] || VectorQ[A]) && !(MatrixQ[B] || VectorQ[B]), Transpose[ B@Transpose [A]], True, HoldForm[BigDot[A, B]]]; 

Note that I added the last line as catch-all when neither A nor B are a matrix or a vector.

Then create the infix notation part. The hard way would be to make some definitions of MakeExpression and MakeBoxes . The easiest way is to use NotationPackage.

 Needs["Notation`"] InfixNotation[ParsedBoxWrapper["\[FilledSmallCircle]"], BigDot] 
+5
source

Try it (just cut and paste this):

 Needs["Notation`"] Notation[ParsedBoxWrapper[ RowBox[{"A_", " ", "\[FilledSmallCircle]", " ", "B_"}]] \[DoubleLongLeftRightArrow] ParsedBoxWrapper[ RowBox[{"Which", "[", RowBox[{ RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"MatrixQ", "[", "A_", "]"}], "||", RowBox[{"VectorQ", "[", "A_", "]"}]}], ")"}], "&&", RowBox[{"(", RowBox[{ RowBox[{"MatrixQ", "[", "B_", "]"}], "||", RowBox[{"VectorQ", "[", "B_", "]"}]}], ")"}]}], ",", RowBox[{"A_", " ", ".", "B_"}], ",", RowBox[{ RowBox[{"!", RowBox[{"(", RowBox[{ RowBox[{"MatrixQ", "[", "A_", "]"}], "||", RowBox[{"VectorQ", "[", "A_", "]"}]}], ")"}]}], "&&", RowBox[{"(", RowBox[{ RowBox[{"MatrixQ", "[", "B_", "]"}], "||", RowBox[{"VectorQ", "[", "B_", "]"}]}], ")"}]}], ",", RowBox[{"A_", "[", "B_", "]"}], ",", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"MatrixQ", "[", "A_", "]"}], "||", RowBox[{"VectorQ", "[", "A_", "]"}]}], ")"}], "&&", RowBox[{"!", RowBox[{"(", RowBox[{ RowBox[{"MatrixQ", "[", "B_", "]"}], "||", RowBox[{"VectorQ", "[", "B_", "]"}]}], ")"}]}]}], ",", RowBox[{"Transpose", "[", RowBox[{"B_", "[", RowBox[{"Transpose", "[", "A_", "]"}], "]"}], "]"}]}], "]"}]]] 

Now I entered this with the Notation palette, so on the screen it looks like this: enter image description here (if necessary, the palette inserts various boxes). It looks awful when I cut and paste due to the explicit string representation of everything.

EDIT: That is: type "Needs["Notation "]`, resulting in a palette. Click on the first button, and then

enter image description here

appears. Inside the first yellow rectangle is A_ \[FilledSmallCircle] B_ , and in the second -

 Which[(MatrixQ[A_]||VectorQ[A_])&&(MatrixQ[B_]||VectorQ[B_]),A_ .B_,!(MatrixQ[A_]||VectorQ[A_])&&(MatrixQ[B_]||VectorQ[B_]),A_[B_],(MatrixQ[A_]||VectorQ[A_])&&!(MatrixQ[B_]||VectorQ[B_]),Transpose[B_[Transpose[A_]]]] 

The result looks like this: enter image description here

and, when evaluated, determines what you want. Also, after the Needs bit, just cut and paste what I gave above.

+4
source

Mathematica has some operators without built-in definitions, such as CirclePlus and CircleTimes, which you can define. I'm an iPhone now, so I canโ€™t check, but I assume that FilledSmallCircle is just a character, not an operator. It is less trivial to define this as an operator, but you can check the Notation package.

+2
source

All Articles