The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Topic: Packed Arrays in version 4
Replies: 2   Last Post: Aug 5, 1999 2:28 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
John Tanner

Posts: 10
Registered: 12/7/04
Re: Packed Arrays in version 4
Posted: Aug 5, 1999 2:28 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

In article <7o5h0k$>, Bruno Daniel
<> writes
>Hi John
>You could use contexts: Define a new context "Efficient`" and put it
>in front of "System`" in the context search path. Then you can
>define your own "Transpose" etc inside this context and use the
>built-in "Transpose" by fully qualifying it (System`Transpose) in
>the implementation of your "Transpose".
>Here's an example how this can be done in the context "Global`":
>In[1]:= Global`Transpose[x_]:= System`Transpose[x] + a
>In[2]:= Transpose[{{c,d,e}}]
>Out[2]:= {{a+c},{a+d},{a+e}}
>Yours sincerely
> Bruno

Thank you: that is really neat, and I really appreciate not having to
Unprotect and modify the System definition.

This works well for Transpose, and for other functions which "sometimes"
unpack PackedArrays. My list so far of "higher level" functions with
this behaviour is:

(1) Transpose: a multidimensional Transpose always unpacks

(2) Outer: always unpacks

(3) Map: usually OK but can surprise you (see below)

What I intend to do at present is to modify Transpose and Outer (and
possibly also Map) as suggested above and/or by Ted Ersek (MG18963) -
thank you both.

I give some illustrations here of 3 different ways of doing exactly the
same thing, with 3 wholly different results (i.e. "Packed", "Unpacked"
and "List of Packed" for Dot, Outer and Map). Yes I know that Dot is
the best to use in this case, but the choice is not so simple for
functions other than Times!

avbytesize[arr_] := ByteCount[arr]/Length[Flatten[{arr}]] // N

arr1 = Range[1., 10.];
arr2 = Range[1., 100.];

arr1dot2 = Transpose[{arr1}].{arr2};
arr1outer2 = Outer[Times, arr1, arr2];
arr2map1 = arr2*# & /@ arr1;

Dimensions[#] & /@ {arr1, arr2, arr1dot2, arr1outer2, arr2map1}

{{10}, {100}, {10, 100}, {10, 100}, {10, 100}}

avbytesize[#] & /@ {arr1, arr2, arr1dot2, arr1outer2, arr2map1}

{13.6, 8.56, 8.06, 20.296, 8.616}

Developer`PackedArrayQ[#] & /@ {arr1, arr2, arr1dot2, arr1outer2,

{True, True, True, False, False}

A much more risky redefinition to get around the problem of
Times[I*realarray] is also possible, since redefinition of Times seems
to be very problematic (see my reply to Ted Ersek(MG18963)):

Global`I := N[System`I]

I am still struggling with the full implications of this - I suspect it
would seriously mess up symbolic simplifications.

Thank you,

from - John Tanner home -
mantra - curse Microsoft, curse... work -
I hate this 'orrible computer, I really ought to sell it:
It never does what I want, but only what I tell it.

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2017. All Rights Reserved.