Re: Distribute

*To*: mathgroup at yoda.physics.unc.edu*Subject*: Re: Distribute*From*: Frank Zizza <zizza at pioneer.willamette.edu>*Date*: Sat, 6 Mar 93 09:35:09 -0800

[I have combined all the responses to the original message into one since most of the answers are similar. - steve c, moderator] 1) >I have a function that I want to be additively distributive. The >standard way for doing this > >f[a_+b_]:=f[a]+f[b]; > >But this is a problem when distributing over long sums, because it >leads to Recursion errors. Mathematica has a command Distribute, but >if I just say > >f[a_+b_]:=Distribute[f[a+b]] > >I am immediately in a loop. Thus, the simplest way I can see to use >it in this situation is > >f[a_+b_]=Distribute[temp[a+b]]/.{temp->f} > >Is there way to do this without the introduction of a temporary >variable? Thanks in advance. > > - Ian Robertson > Oberlin College > iroberts at cs.oberlin.edu Add the rule f[S_Plus] := f /@ S to the end of the definitions you have made for f. Here's an example: Mathematica 2.1 for NeXT Copyright 1988-92 Wolfram Research, Inc. -- NeXT graphics initialized -- In[1]:= f[S_Plus] := f /@ S In[2]:= f[a+b+c] Out[2]= f[a] + f[b] + f[c] Frank Zizza Willamette University Salem, OR 97301 zizza at willamette.edu 2) From: tgayley at wri.com (Todd Gayley) How about this: f[x_Plus] := f /@ x Todd Gayley WRI 3) From: John Lee <lee at math.washington.edu> Here is a simple solution, which avoids recursion: In[1]:= f[x_Plus] := f /@ x; In[2]:= f[a + b + c + d + e] Out[2]= f[a] + f[b] + f[c] + f[d] + f[e] Jack Lee Dept. of Mathematics University of Washington Seattle, WA 4) From: reiszig at e-technik.tu-dresden.dbp.de Hi, sb. asked how to define a function to be additive (orig. mail see below). The simplest way I know is: f[x_Plus] := Map[f,x] (use level specific. for special effects) If needed, use in addition: f[x_Plus y_] := f[ Expand[x y]] G.Reiszig, Dresden, 8 Mar 93 5) From: wmm at chem.wayne.edu (Martin McClain) At the top of your script, write SetAttributes[Distribute, HoldAll] Now Distribute will work the way you want. Of course, this may introduce undesirable behaviour in other aspects of your problem. If it does, define another operator identical to Distribute and give it the HoldAll attribute.