Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Packed Arrays in version 4
Posted:
Jul 30, 1999 1:59 AM


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) multidimensional 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 builtin 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 builtin commands within my own commands (e.g. myTranspose).
With Transpose, I can get part way: the pattern matching works:
Unprotect[Transpose]; Transpose[arr_?Developer`PackedArrayQ, extraargs_] := myTranspose[arr, extraargs]; Protect[Transpose]
the problem is that this causes infinite recursion (i.e. once it identifies a PackedArrayQ argument, it keeps on calling Transpose recursively since myTranspose calls Transpose with a PackedArray argument....).
With \[ImaginaryI], it does not seem possible to redefine \[ImaginaryI] itself, and attempts to overload Times, Plus etc. fail since the pattern match "Times[\[ImaginaryI],a_?Developer`PackedArrayQ,b___]" fails (why?).
I must be missing something(s) simple, all suggestions welcome.  from  John Tanner home  john@janacek.demon.co.uk mantra  curse Microsoft, curse... work  john.tanner@gecm.com I hate this 'orrible computer, I really ought to sell it: It never does what I want, but only what I tell it.



