Re: Listable attribute (was Re: eval differential expressions)
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Listable attribute (was Re: eval differential expressions)
- From: Count Dracula <lk3a at kelvin.seas.virginia.edu>
- Date: Sat, 25 Jun 1994 12:30:55 -0400
<> Here is just one solution, using the listability, which ends with another
<> question:
<>
<> In[1]:= Diff[fun_,var_] := D[fun,var]^2 + fun
<>
<> In[2]:= Diff[ { Cos[x], Sin[x], Tan[x] }, x ]
<>
<> 2 2 4
<> Out[2]= {Cos[x] + Sin[x] , Cos[x] + Sin[x], Sec[x] + Tan[x]}
<>
<> Et voila, it can probably do what Geoff asked. But at my first try, I
<> thought at the Listable attribute, but I have discovered that since the
<> Plus[], D[] and Power[] have it, it is useless. In fact, it is not useless,
<> it is worse than that:
<>
<> In[3]:= Diff[ Exp[x+y^2], {x, y}]
<>
<> 2 2
<> x + y x + y 2
<> Out[3]= E + D[E , {x, y}]
<>
<> In[4]:= SetAttributes[Diff, Listable]
<>
<> In[5]:= Diff[ Exp[x+y^2], {x, y}]
<>
<> 2 2 2 2
<> x + y 2 x + 2 y x + y 2 x + 2 y 2
<> Out[5]= {E + E , E + 4 E y }
<>
<>
<> At that point, the "listability" seems extended to the second argument,
<> which can be nice. Alas, one can not pull the line too much:
<>
<> In[6]:= Diff[{1,x,x y},{x,y}]
<>
<> Thread::tdlen: Objects of unequal length in Diff[{1, x, x y}, {x, y}]
<> cannot be combined.
<>
<> 2 (y,0) 2
<> Out[6]= {1, x + D[x, {x, y}] , x y + Times [x, y] }
<>
<>
<> Thus my question: does somebody know how to limit the Listable attribute to
<> one of the arguments (a kind of "ListableOnFirstArg" attribute), or does
<> one have to explicit rules with Thread and so on ?
<>
<>
<> =====[ Eric LEWIN - lewin at ipgp.jussieu.fr - IPGP Geochimie ]=====
<> = Labo de Geochimie et Cosmochimie / Institut de Physique du Globe =
<> ========================== Paris - FRANCE ==========================
Hello Eric,
Listability can be limited to the first argument by the device given
by Roman Maeder:
f[fun_List, args___] := Map[ f[#, args]&, fun]
f[fun_, ....] := (* code for usual case where fun is not a list*)
For the example above, if you define
diff[fun_List, var_List] := Plus[ fun , #1] & /@ Transpose[Outer[D, fun, var]^2]
then you would have a kind of listability over the two lists, e.g.,
diff[{x y, x^3, x^2 y^2, y^3}, {x, y}] will give
2 3 4 2 2 2 4 3 2 3 2 2 4 2 3 4
{{x y + y , x + 9 x , x y + 4 x y , y }, {x + x y, x , x y + 4 x y , y + 9 y }
_______________________________________________________________
Levent Kitis
Department of Mechanical, Aerospace and Nuclear Engineering
Thornton Hall McCormick Road
University of Virginia
Charlottesville, Virginia 22903 USA
804-924-6230 lk3a at cars.mech.virginia.edu
804-924-3291 lk3a at kelvin.seas.virginia.edu
_______________________________________________________________