# How can I define a ⊕ operator?

What is wrong with the following code?

CirclePlus := SparseArray[Band[{1, 1}] -> {##}] &
a_ ⊕ b_ := CirclePlus[a, b]


SetDelayed::write: Tag Function in (SparseArray[Band[{1,1}]->{##1}]&)[a_,b_] is Protected. >>

I want to define the operator ⊕ to work like +,-,*,/, so it can be used as a ⊕ b.

-
CirclePlus := SparseArray[Band[{1, 1}] -> {##}] &; Normal[a\[CirclePlus]b] – Dr. belisarius Jan 8 '14 at 3:51
@belisarius Thanks! Can you explain why? and I want to define the operation \[CirclePlus] like +,-,*,/ which can be used as a \[CirclePlus] b. – Eden Harder Jan 8 '14 at 4:53
@EdenHarder Then you should define it as CirclePlus[a_, b_] := SparseArray[Band[{1, 1}] -> {a, b}] and this can be used as a ⊕ b. – R. M. Jan 8 '14 at 5:12
@rm-rf Thanks! Can you analyze the error in my original code to teach me a lesson? – Eden Harder Jan 8 '14 at 5:17
– Mr.Wizard Jan 8 '14 at 13:52

## 1 Answer

CirclePlus is a built-in symbol already with no meaning for the kernel, but meaning in the front-end.

The second definition tried to use the first definition (with head 'Function'), which is protected. (Note the pattern [a_,b_] appearing in the error message, which tells you the left-hand-side is the issue.)

Just one line is enough as @rm-rf said, like this...

a_ ⊕ b_ := SparseArray[Band[{1, 1} -> {a, b}]]


The front-end translates ⊕ for you into CirclePlus - this is what I meant when said that it has meaning in the front-end.

More detailed explanation

I was thinking that what I said may not be clear enough, in the sense that the workings of SetDelayed (aka :=) were not clearly exposed.

The fact that SetDelayed has attribute HoldAll does not mean that the expression on the left is not evaluated by the kernel in order to determine which head the defining rule gets attached to. No, it is actually evaluated!

Let me illustrate with a simple and quick example. If you execute the following

MyX[1] := Y;
MyX[x_] := Z;


then which heads are being defined here?

MyX[1][a_] := a^2;
MyX[x][a_] := a;


Ask the kernel!

?Y


?Z


Interesting, ah?

So, this is what happened in your case. CirclePlus was undefined for the kernel until you gave it the first definition without a pattern. When you attempted the second one, the kernel already knew something about the head and try to evaluate it and got Function. There the evaluation stopped and then it tried to associate the second rule with this Function head - error.

By the way, be careful your definition only knows about CirclePlus[a,b], but the front end treats a ⊕ b ⊕ c as a single CirclePlus[a,b,c], which is unknown to the kernel with the definition provided.

-
Thanks for your wonderful answer! But we should wait for @rm-rf waking up. He has promised to give an answer. – Eden Harder Jan 8 '14 at 11:38
"When you attempted the second one, the kernel already knew something about the head and try to evaluate it and got Function", can we avoid this? – Eden Harder Jan 8 '14 at 11:41
@EdenHarder caya has written a nice answer to your question and I have nothing more to add :) – R. M. Jan 8 '14 at 15:52
In the original two-line definition, one can also just switch the order of the lines. The reason why this works is exactly what caya wrote. – Jens Jan 8 '14 at 16:54
Thanks @rm-rf, I couldn't resist explaining this aspect of the Mathematica programming language :) – carlosayam Jan 8 '14 at 18:10