RE: Packed Array Angst
- To: mathgroup at smc.vnet.net
- Subject: [mg20992] RE: [mg20962] Packed Array Angst
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Thu, 2 Dec 1999 21:41:07 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Mark Reeve wrote:
-------------------------
<snip>
I've been experimenting with packed arrays and have observed that a
number of common functional programming constructs force unpacking, thus
greatly lessening the utility of packed arrays.
<snip>
For example: Transpose, Inner, Outer, and MapThread. The latter is
particularly annoying. I should clarify that for a 2-D array (and
possibly for higher-dimensions as well), Transpose[arr], works just fine
and preserves packing. But if the user specifies a permutation vector,
even the trivial Transpose[arr2d,{1,2}], unpacking occurs.
Another nasty thing is that if "a" and "b" are two packed arrays, the
composite c={a,b} is not, although c[[1]] and c[[2]] are still each
packed.
Has anyone found a way around some of these limitations? A generic
replacement for MapThread which preserves packing? I have experimented
with several forms but have so far only run into further limitations.
---------------------------------
Come on there are hundreds of cases, and there are bound be some where this
could be improved, and I expect it will improve in future versions. In the
mean time it might help to use the functions Developer`ToPackedArray,
Developer`PackedArrayQ. They are documented in the Help Browser.
A more serious problem is a bug that was discussed here earlier. Below I
copy a summary that I posted earlier.
--------------------
I have more on the 4.0 bug reported by
(slinger at signal.dera.gov.uk). The following code gives a simple yet
practical demonstration of the problem.
In[1]:=
ideal={1.5 + 0.2 I, 2.3 + 0.4 I};
output=Fourier[ideal]
Out[2]=
{2.68701 + 0.424264 I, -0.565685 - 0.141421 I}
In[3]:=
Part[output,1]=Part[ideal,1];
output
Out[4]=
{1.5 + 1.5 I, -0.565685 - 0.141421 I}
Of course the first part in Out[4] should be
(1.5 + 0.2 I)
We all agree this is a bug.
--------------------------------------
As far as I can tell we don't have the problem when ReplacePart is used (as
below). This solution allows one to keep the advantages of packed arrays,
and I think it's more direct than using Part anyway.
In[5]:=
output=Fourier[ideal];
ReplacePart[output,ideal,1,1]
Out[6]=
{1.5 + 0.2*I, -0.565686 - 0.141421 I}
In[7]:=
output=Fourier[ideal];
ReplacePart[output, 1.5+0.2I, 1]
Out[8]=
{1.5 + 0.2 I, -0.565685 - 0.141421 I}
-------------------------
We can also unpack the complex array we get from Fourier, and we get the
right result using Part as below.
In[9]:=
output=Developer`FromPackedArray[Fourier[ideal]];
Part[output,1]=Part[ideal,1];
output
Out[11]=
{1.5 + 0.2 I, -0.565685 - 0.141421 I}
As indicated in an earlier message the error seems to occur when using Part
to change a value in a complex, packed array.
--------------------
Regards,
Ted Ersek
For Mathematica tips, tricks see
http://www.dot.net.au/~elisha/ersek/Tricks.html