RE: Re: Packed Arrays in version 4

*To*: mathgroup at smc.vnet.net*Subject*: [mg19130] RE: [mg19082] Re: [mg18963] Packed Arrays in version 4*From*: "Ersek, Ted R" <ErsekTR at navair.navy.mil>*Date*: Thu, 5 Aug 1999 23:58:36 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Earlier I gave a method that I thought would ensure that when a packed array is multiplied by a complex number one would get a packed array of complex numbers. Part of my message is given below. ---------------------------- > >The following should do what you want for >Times and you can make a similar definition >for Transpose. However, I don't recommend it >because it will slow done Times in every case >when this rule isn't used. > > >In[1]:= >HiddenSymbols`ModifyTimes=True; >Unprotect[Times]; > >Times[z_Complex,arr_?Developer`PackedArrayQ]/; > HiddenSymbols`ModifyTimes:= > Block[{HiddenSymbols`ModifyTimes}, > Developer`ToPackedArray[Times[z,arr]] > ]; > >Protect[Times]; > >------------------ >How does it work? > >The above definition is only used when >(HiddenSymbols`ModifyTimes=True). When the rule is used Block >temporarily clears the value of (HiddenSymbols`ModifyTimes), so >the kernel doesn't go back and use the rule again. > ----------------------- Then John Tanner wrote back: Thank you, Ted: this is a good solution for Transpose (see also my reply to Bruno Daniel). Just by replacing Transpose for Times everywhere (and a little mixing of arguments) gave a good result. However I still have great difficulty with Times: I cannot make your function above give satisfactory results. I give an example below, it actually seems to be counter-productive. Is this because Transpose does not have Listable (etc.) Attributes like Times? ------------------------ REPLY: I must admit I didn't test the performance of Times based on my solution. I assumed it would work as advertised. However, the same method can be used to do this sort of thing with a lots of kernel functions. In the program posted at http://www.mathsource.com/Content/Enhancements/Graphics/2D/0209-876 I modified the Plot function so it does some work before calling the built in Plot algorithm. To see why my attempt to change Times didn't work as I expect it helps to consider the what happens when one uses the code below to change Times. In[1]:= Unprotect[Times]; DownValues[Times]={}; Times[a_,b_]/;(Print["Boo"];True):= {h[a],h[b]} In[4]:= 3*x Boo Out[4]= {h[3],h[x]} My new definition of Times was used in the line above. In the next line the multiplication is done before the kernel checks to see if I made a special definition. Notice the kernel doesn't even get to the Print statement because the head Times is gone. Because of this the programming trick I gave earlier can't be used to change the way Times works with all numbers, or lists of numbers. I suspect Plus and possibly others work the same way. Notice this should have the advantage that simple arithmetic will work faster than it would if it checked for user specified definitions first. In[5]:= 3*4 Out[5]= 12 I didn't think this had anything to do with the Attributes Times has. Just to make sure I removed all of the attributes from Times and the lines above came out the same. -------------- Regards, Ted Ersek