6
$\begingroup$

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.

$\endgroup$
6
  • 2
    $\begingroup$ CirclePlus := SparseArray[Band[{1, 1}] -> {##}] &; Normal[a\[CirclePlus]b] $\endgroup$ Jan 8, 2014 at 3:51
  • $\begingroup$ @belisarius Thanks! Can you explain why? and I want to define the operation \[CirclePlus] like +,-,*,/ which can be used as a \[CirclePlus] b. $\endgroup$ Jan 8, 2014 at 4:53
  • 3
    $\begingroup$ @EdenHarder Then you should define it as CirclePlus[a_, b_] := SparseArray[Band[{1, 1}] -> {a, b}] and this can be used as a ⊕ b. $\endgroup$
    – rm -rf
    Jan 8, 2014 at 5:12
  • $\begingroup$ @rm-rf Thanks! Can you analyze the error in my original code to teach me a lesson? $\endgroup$ Jan 8, 2014 at 5:17
  • 1
    $\begingroup$ Related: Specifics of usage and internal work of Set functions $\endgroup$
    – Mr.Wizard
    Jan 8, 2014 at 13:52

1 Answer 1

9
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ Thanks for your wonderful answer! But we should wait for @rm-rf waking up. He has promised to give an answer. $\endgroup$ Jan 8, 2014 at 11:38
  • $\begingroup$ "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? $\endgroup$ Jan 8, 2014 at 11:41
  • $\begingroup$ @EdenHarder caya has written a nice answer to your question and I have nothing more to add :) $\endgroup$
    – rm -rf
    Jan 8, 2014 at 15:52
  • 1
    $\begingroup$ 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. $\endgroup$
    – Jens
    Jan 8, 2014 at 16:54
  • $\begingroup$ Thanks @rm-rf, I couldn't resist explaining this aspect of the Mathematica programming language :) $\endgroup$
    – carlosayam
    Jan 8, 2014 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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