Re: Redefine[Plus] - problem
- To: mathgroup at smc.vnet.net
- Subject: [mg41134] Re: Redefine[Plus] - problem
- From: bobhanlon at aol.com (Bob Hanlon)
- Date: Mon, 5 May 2003 02:40:55 -0400 (EDT)
- References: <b92ha1$li0$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
I do not know the cause of the timing difference other than that you modified
the definition of Plus. It is generally a bad idea to modify a built-in
function. Rather than modify Plus, set UpValues of fc using TagSet.
Clear[fc];
fc /: fc[x_]+fc[y_]:=fc[x+y];
fc /: k_?NumericQ + fc[x_] := fc[k+x];
Bob Hanlon
In article <b92ha1$li0$1 at smc.vnet.net>, Petr Kujan <kujanp at fel.cvut.cz> wrote:
<<
Subject: Redefine[Plus] - problem
From: Petr Kujan <kujanp at fel.cvut.cz>
To: mathgroup at smc.vnet.net
Date: Sun, 4 May 2003 08:00:01 +0000 (UTC)
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}
>><BR><BR>