Orderless for Plus? was: Re: Redefine[Plus] - problem
- To: mathgroup at smc.vnet.net
- Subject: [mg41194] Orderless for Plus? was: [mg41132] Re: [mg41126] Redefine[Plus] - problem
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Wed, 7 May 2003 03:50:58 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Andrzej Kozlowski [mailto:akoz at mimuw.edu.pl] To: mathgroup at smc.vnet.net >Sent: Monday, May 05, 2003 8:41 AM >To: mathgroup at smc.vnet.net >Subject: [mg41194] [mg41132] Re: [mg41126] Redefine[Plus] - problem > > >This is indeed a mystery that I expect is not going to be resolved. >However, it is generally a bad idea to redefine basic arithmetical >operations like Plus and if you do that you can expect all kinds of >unexpected problems. > >Also, it is not necessary. You can easily define your own >function plus >that will work just like Plus and use your additional rules. Here is >one way: > >start by giving plus all the attributes that Plus has, except the >Protected attribute, otherwise you won't be able to define it: > > >Attributes[plus]=Drop[Attributes[Plus],-1] > >General::spell1: Possible spelling error: new symbol name "plus" is >similar \ >to existing symbol "Plus". > > >{Flat,Listable,NumericFunction,OneIdentity,Orderless} > >now we define plus in terms of Plus, but with the extra rules: > >plus[x___] := Block[{Plus}, > Plus[fc[u_], fc[v_]] := fc[ > u + v]; Plus[a_?NumericQ, fc[u_]] := fc[a + u]; Plus @@ {x}] > >with that > > >plus[3,4] > > >7 > > >plus[fc[x],fc[y]] > > >fc[x+y] > >You can now check that plus unlike your redefined Plus does not suffer >form the strange slowdown. > >The only thing missing is that x+y still means Plus[x,y] rather than >plus[x,y]. But even that can be changed. You have to load the Notation >package: > > ><< Utilities`Notation` > >And then using the palette that opens automatically when the >package is >loaded enter and evaluate > >Notation[(x_ + y_) <-> plus[x_, y_]] > >(This is not really what you must enter, just looks similar, >really you >must use the palette) > >Once you have done that you Mathematica will interpret + as plus >instead of Plus. > >Andrzej Kozlowski >Yokohama, Japan >http://www.mimuw.edu.pl/~akoz/ >http://platon.c.u-tokyo.ac.jp/andrzej/ > > > >On Sunday, May 4, 2003, at 04:56 pm, Petr Kujan wrote: > >> Hello all, >> >> I would be grateful if someone could explain the difference in >> time consummation. >> >> I need redefine standard function Plus[] for function fc[]: >> >> Unprotect[Plus]; >> Plus[a : fc[x_], b : fc[y_]] := fc[x + y] >> Plus[k : _?NumericQ, b : fc[y_]] := fc[k + y] >> Protect[Plus]; >> >> >> Now easy computation is very slow (time consummation is >exponential!): >> >> dat = Table[s, {20}]; >> Plus @@ dat // Timing >> >> {20.019 Second, 20 s} >> >> >> But if the symbol s replace by x only: >> >> dat = Table[x, {20}]; >> Plus @@ dat // Timing >> >> {0. Second, 20 x} >> >> >> Thank you, >> Petr Kujan >> -- >> kujanp at fel.cvut.cz >> >> >> >> > > Andrzej, blocking Plus avoids the problems reported, because the attributes too are blocked (Plus in Block simply is a different symbol). However I still detected some remains of predefined Plus being effective: In[1]:= f[b] + f[a] Out[1]= f[a] + f[b] In[2]:= f[a] + 3 Out[2]= 3 + f[a] ok, Orderless does what it should. In[3]:= Print["[before]: ", Attributes[Plus]]; Block[{Plus}, Print["[blocked]: ", Attributes[Plus]]; ]; Print["[after]: ", Attributes[Plus]]; >From In[3]:= [before]: {Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected} >From In[3]:= [blocked]: {} >From In[3]:= [after]: {Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected} No attributes for blocked Plus. We make a definition with blocked Plus: In[6]:= plus[x___] := Block[{Plus}, Hold[#] &[Plus[x]]] >From In[6]:= General::"spell1" : "Possible spelling error: new symbol name plus is similar to existing symbol Plus." In[7]:= plus[f[b], f[a]] Out[7]= Hold[f[b] + f[a]] ok. In[8]:= plus[f[a], 3] Out[8]= Hold[3 + f[a]] What's that? In[9]:= plus[f[a], 3] // Trace Out[9]= {plus[f[a], 3], Block[{Plus}, (Hold[#1] &)[f[a] + 3]], {{f[a] + 3, 3 + f[a]}, (Hold[#1] &)[ 3 + f[a]], Hold[3 + f[a]]}, Hold[3 + f[a]]} when Plus is evaluated within the argument of the Function, there happens to be a rearrangement of order! Obviously due to some shortcut. Just another reason to be wary of constructions of this kind. See also: In[10]:= plus[z] Out[10]= Hold[z] (I had expected Hold[Plus[x]] ??) In[11]:= plus[z] // Trace Out[11]= {plus[z], Block[{Plus}, (Hold[#1] &)[+z]], {{+z, z}, (Hold[#1] &)[z], Hold[z]}, Hold[z]} I think its *not* OneIdentity having become effective, but what else?? Using HoldAllComplete would make no difference, obviously. -- Hartmut Wolf