Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
2  
CirclePlus := SparseArray[Band[{1, 1}] -> {##}] &; Normal[a\[CirclePlus]b] –  belisarius Jan 8 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 at 4:53
3  
@EdenHarder Then you should define it as CirclePlus[a_, b_] := SparseArray[Band[{1, 1}] -> {a, b}] and this can be used as a ⊕ b. –  rm -rf Jan 8 at 5:12
    
@rm-rf Thanks! Can you analyze the error in my original code to teach me a lesson? –  Eden Harder Jan 8 at 5:17
1  
show 1 more comment

1 Answer

up vote 8 down vote accepted

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

Y defs

?Z

Z defs

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.

share|improve this answer
    
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 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 at 11:41
    
@EdenHarder caya has written a nice answer to your question and I have nothing more to add :) –  rm -rf Jan 8 at 15:52
1  
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 at 16:54
    
Thanks @rm-rf, I couldn't resist explaining this aspect of the Mathematica programming language :) –  caya Jan 8 at 18:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.