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.
CirclePlus := SparseArray[Band[{1, 1}] -> {##}] &; Normal[a\[CirclePlus]b]
$\endgroup$\[CirclePlus]
like+,-,*,/
which can be used asa \[CirclePlus] b
. $\endgroup$CirclePlus[a_, b_] := SparseArray[Band[{1, 1}] -> {a, b}]
and this can be used asa ⊕ b
. $\endgroup$