Re: Redefine[Plus] - problem
- To: mathgroup at smc.vnet.net
- Subject: [mg41132] Re: [mg41126] Redefine[Plus] - problem
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Mon, 5 May 2003 02:40:34 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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 > > > >