Re: Q: Thread behaviour

*To*: mathgroup at smc.vnet.net*Subject*: [mg57956] Re: [mg57933] Q: Thread behaviour*From*: Andrzej Kozlowski <andrzej at akikoz.net>*Date*: Tue, 14 Jun 2005 05:10:20 -0400 (EDT)*References*: <200506130950.FAA29336@smc.vnet.net> <8F02341B-0E9C-4E43-8C65-12FB52A7ACC0@akikoz.net>*Sender*: owner-wri-mathgroup at wolfram.com

On 13 Jun 2005, at 19:43, Andrzej Kozlowski wrote: > > On 13 Jun 2005, at 18:50, Dick Barends wrote: > > >> Hallo, >> >> k[x_,y_] = x^2+y^2 >> Thread[k[a+b,c d],Plus] >> gives: (a+b)^2+c^2 d^2 >> I would expect: a^2 + (c d)^2 + b^2 + (c d)^2 (See below) >> >> ----------------- >> When I use: >> Thread[m[a+b, c d],Plus] >> gives: m[a, c d] + m[b, c d] >> This is what should happen. >> >> What gives "m" a different solution than "k"???? >> >> I hoop some of you will answer:-) >> >> greetings >> Dick >> >> >> > > > > Compare > > In[39]:= > Thread[k[a + b, c*d], Plus] > > > (a + b)^2 + c^2*d^2 > > with > > > Thread[Unevaluated[k[a + b, c*d]], Plus] > > > a^2 + b^2 + 2*c^2*d^2 > > Alternatively, give k the attribute HoldAll > > > SetAttributes[k, HoldAll] > > > Thread[k[a + b, c*d], Plus] > > > (a + b)^2 + c^2*d^2 > > Andrzej Kozlowski > > Chiba, Japan > Of course giving k the attribute HoldAll does not help since the point is that Thread evaluates it"s arguemnts not that k does. (I really should have looked at the answer I got). So you could give Thread the HoldALl (or HoldFirst) attribute: SetAttributes[Thread, HoldAll] Thread[k[a + b, c*d], Plus] a^2 + b^2 + 2*c^2*d^2 However, it is not a good idea in general to modify built-in function in this way so it is much better just to use Unevaluated as in my first example. Andrzej Kozlowski

**References**:**Q: Thread behaviour***From:*Dick Barends <dick.barends@wxs.nl>