RE: Packed Arrays in version 4
- To: mathgroup at smc.vnet.net
- Subject: [mg18990] RE: [mg18963] Packed Arrays in version 4
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Tue, 3 Aug 1999 13:44:35 -0400
- Sender: owner-wri-mathgroup at wolfram.com
John Tanner wrote: ------------------------ The use of Packed Arrays on version 4 is a huge boon: this means that much larger problems can be handled directly in the kernel. There are a few cases however when "trivial" operations on PackedArrays mean that a conventional unpacked array results: while this is very "safe" it can be annoying, and quite difficult to modify existing code to make the best use of the new capabilities. The two most severe cases are: (1) multi-dimensional transposes (i.e. "Transpose[packedarray]" returns a packed array, but "Transpose[packedarray,{3,2,1}]" returns an unpacked List.) (2) conversion of an Integer or Real PackedArray to Complex (e.g. by multiplication by \[ImaginaryI] ) (i.e. "Times[I,realpackedarray]" returns an unpacked list, which is especially nasty for such classic constructs as "Exp[I*2*Pi*realpackedarray]") In both cases it seems only possible to recover some of the speed and storage space improvement by wrapping the offending construct with Developer`ToPackedArray[] as early as possible. At this point I get stuck! How is it possible to redefine built-in functions such as Transpose and \[ImaginaryI] (and similar, less important cases...) so that this occurs automatically? I really do not wish to completely redefine all of the Mathematica built-in commands within my own commands (e.g. myTranspose). --------------------------------------- John, 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. Regards, Ted Ersek