Re: combinations of pure functions
- To: mathgroup at smc.vnet.net
- Subject: [mg16593] Re: [mg16532] combinations of pure functions
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Wed, 17 Mar 1999 23:55:03 -0500
- Sender: owner-wri-mathgroup at wolfram.com
There are undoubtedly lots of solutions. The way I see it, the most obvious reason why the above attempt does not work is that Mathematica does understand various algebraic operations on functions, e.g. it does not convert (f+g)[x] to f[x]+g[x] etc. I often need that so I add my own rules (see below). In your particular case only one rule is needed, we want Mathematica to understend that (-f)[x] is -f[x]. This rule shoudl be attached to Times. Here is how it works: In[1]:= Unprotect[Times]; Times/:(-f_)[x_]:=-f[x]; Protect[Times]; Now your example will work as you wanted: In[2]:= op2 = Identity - (D[#,x]&) Out[2]= Identity - (D[#1, x] & ) In[3]:= Through[op[x^2], Plus] Out[3]= 2 -2 x + x Of course, if you add all the function alebra rules, you won't need Through at all: In[1]:= Unprotect[{Plus, Times}]; Plus/: ((f_) + (g_))[x_] := f[x] + g[x]; Times/: ((k_)?NumberQ (f_))[x_] := k f[x]; Times/: ((f_) (g_))[x_] := f[x] g[x]; Now In[6]:= op1 = Identity + (D[#,x]&) Out[6]= Identity + (D[#1, x] & ) In[7]:= op2 = Identity - (D[#,x]&) Out[7]= Identity - (D[#1, x] & ) In[8]:= op1[x^2] Out[8]= 2 2 x + x In[9]:= op2[x^2] Out[9]= 2 -2 x + x I must confess to a personal bias here. I like this sort of programming style more than any other because it is more like doing mathematics than like programming and I like mathematics more than I like programming. On Tue, Mar 16, 1999, Gianluca Gorni <gorni at dimi.uniud.it> wrote: >Hello! > >Talking of operators, consider the example of the Book >at Section 2.2.9: > > op = Identity + (D[#,x]&) > >To find the value of op on the expression x^2 it is >suggested to use Through: > > Through[op[x^2], Plus] > >Unfortunately the suggestion fails if we just change + >into - in op: > > op2 = Identity - (D[#,x]&) > >What can one do? I have thought of a replacement rule: > > op2 /. {Identity -> x^2, f_Function -> f[x^2] } > >Anyone has a better idea? > >Thank you in advance, > > Gianluca Gorni > > > +---------------------------------+ > | Gianluca Gorni | > | Universita` di Udine | > | Dipartimento di Matematica | > | e Informatica | > | via delle Scienze 208 | > | I-33100 Udine UD | > | Italy | > +---------------------------------+ > | Ph.: (39) 0432-558422 | > | Fax: (39) 0432-558499 | > | mailto:gorni at dimi.uniud.it | > | http://www.dimi.uniud.it/~gorni | > +---------------------------------+ > > > Andrzej Kozlowski Toyama International University JAPAN http://sigma.tuins.ac.jp/ http://eri2.tuins.ac.jp/