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.

I came across a bit of code that uses the syntax /: and I don't know what it does. I can't find its documentation, or maybe I'm just not looking properly. The code snippet is something like:

F/:F[A___,b_?NumberQ B_, C___] := b*F[A,B,C]

(i.e. part of the definition of multi-linearity). How does this behave differently than the following?:

G[A___,b_?NumberQ B_, C___] := b*G[A,B,C]

Thanks!

share|improve this question
8  
In the spirit of teaching people how to fish: highlight the /: part of the expression. Press F1. The help file should give links to the relevant functions. –  J. M. May 3 '12 at 17:42
2  
Or at the very least, try entering /: into the doc center and see what comes up –  rm -rf May 3 '12 at 17:43
1  
Check TagSet in docs –  kguler May 3 '12 at 17:44
    
@J.M.,@R.M,@kguler: Thanks for the tips, I didn't know about the F1 thing. Having read those documentation pages, I still don't understand the difference between my two lines of code. –  Ian Hincks May 3 '12 at 17:55
4  
@IanHincks In this case, there is none. The docs mention that in the Scope section. For OwnValues (variables) and DownValues (functions), you can use SetDelayed (:=) with the same effect. But for UpValues (which are another kind of global rules), you need to use TagSetDelayed (/:), or perhaps UpSetDelayed (although I favor the former). –  Leonid Shifrin May 3 '12 at 18:00
show 5 more comments

1 Answer

up vote 14 down vote accepted

/: is the short-hand notation for TagSetDelayed, which is creating UpValues. It's useful for over-loading how a particular function behaves with a specific head. For example:

In[1]:= h /: Plus[x : h[arg1_, arg2_], y : h[arg3_, arg4_]] := Plus[arg1, arg2, arg3, arg4]

In[2]:= h[1, 2] + h[3, 4]
Out[2]= 10

The benefit being you don't have to Unprotect[Plus] to set the definition, and if you Remove[h] this definition will be wiped out as well.

share|improve this answer
    
Thanks @nikko. What purpose do the x and y serve in this example? It seems to work without them. –  Ian Hincks May 3 '12 at 18:35
    
@IanHincks in this example, they don't serve any purpose, but they could be used to refer to the entire object matched by h[arg1_, arg2_]. So, if you had need of the entire pattern, you could use it. –  rcollyer May 3 '12 at 18:36
4  
In this case, it is TagSetDelayed rather than TagSet. The syntax in fact has to parts: s/:lhs:=rhs or s/:lhs=rhs, so /: alone is not enough to tell which one it is. Also, a correct statement is that TagSetDelayed (or TagSet) can create UpValues (but it can also create OwnValues, DownValues and SubValues, in which case, the tag part is redundant). –  Leonid Shifrin May 3 '12 at 18:43
    
yes, sorry the x: and y: are superfluous here. –  nikko May 3 '12 at 18:45
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.