Re: Setting upvalues, using ^:=, such that they get actually used

*To*: mathgroup at smc.vnet.net*Subject*: [mg96453] Re: Setting upvalues, using ^:=, such that they get actually used*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>*Date*: Fri, 13 Feb 2009 03:49:28 -0500 (EST)*Organization*: The Open University, Milton Keynes, UK*References*: <gn11lk$86h$1@smc.vnet.net>

In article <gn11lk$86h$1 at smc.vnet.net>, Niko <niko.schwarz at googlemail.com> wrote: > Ok, I stumbled across Upvalues today and I thought they were gorgeous > for my case. > > My case is this: I need the intersection of two really large sets, it > looks like this: > > Intersection[sym[l], sym[q]] > > Now, they are really two large to compute them. I can compute the > intersection faster myself. So, I tossed along and defined something > like this: > > > Intersection[sym[a_], sym[b_]] ^:= 42 > > Unfortunately, Mathematica has a strong preference for using the > downvalue of sym for the computation, regardless of the order I use > for input. > > If I undefine "sym", things work fine, but that is of course a whole > lot less elegant. Is there a way to "push" the priority of the upvalue > definition, so it gets used whenever possible? Not sure whether the following fully addresses your problem, but you could add the attribute *HoldAll* to the function *Intersection[]*. For instance, In[1]:= Intersection[sym[l], sym[q]] Out[1]= sym[] In[2]:= Intersection[sym[a_], sym[b_]] ^:= 42 In[3]:= Intersection[sym[l], sym[q]] Out[3]= 42 In[4]:= sym[x_] := RandomInteger[{1, 10}, RandomInteger[{1, 5}]] In[5]:= Intersection[sym[l], sym[q]] Out[5]= {1, 2} In[6]:= SetAttributes[Intersection, HoldAll] Intersection[sym[l], sym[q]] Out[7]= 42 Regards, --Jean-Marc