Re: Packed Arrays in version 4
- To: mathgroup at smc.vnet.net
- Subject: [mg19082] Re: [mg18963] Packed Arrays in version 4
- From: John Tanner <john at janacek.demon.co.uk>
- Date: Thu, 5 Aug 1999 01:35:04 -0400
- Organization: Peace with the World
- References: <7o5fd0$rgp@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <7o5fd0$rgp at smc.vnet.net>, Ersek, Ted R
<ErsekTR at navair.navy.mil> writes
<Snip>
>---------------------------------------
>
>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
>
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?
It has been suggested to me by e-mail that using (N[I]*array) instead of
(I*array) solves the problem, and indeed I had found that
((I+0.0)*array) also solved the problem. The real problem is that
multiplying a real array by I is so common in electronics/microwaves! I
have many, many notebooks with many,many,many occurrences of this and I
do not want to start chasing them all down. Thus I have a real
motivation for making this work as a redefinition of Times or
\[ImaginaryI]. A global redefinition of Global`I := N[System`I] (see my
reply to Bruno Daniel) is probably extremely risky!
***********example***************
In[1]:=
avbytesize[arr_] := ByteCount[arr]/Length[Flatten[{arr}]] // N
In[2]:=
tmp1 = Table[Random[], {100000}];
tmp2 = Developer`FromPackedArray[tmp1];
In[4]:=
{avbytesize[#], Developer`PackedArrayQ[#]} & /@
{tmp1, tmp2,
Times[tmp1, tmp1], tmp1*tmp2,
I*tmp1, N[I]*tmp1,
I*tmp2, N[I]*tmp2}
Out[4]=
{{8.00056, True}, {20.0002, False},
{8.00056, True}, {8.00056, True},
{60.0002, False}, {16.0006, True},
{60.0002, False}, {16.0006, True}}
*********************************
but I dont understand what happens after evaluating your function,
especially mystifying since the results for Transpose were so good:
*********************************
In[5]:=
HiddenSymbols`ModifyTimes = True;
Unprotect[Times];
Times[z_Complex, arr_?Developer`PackedArrayQ] /;
HiddenSymbols`ModifyTimes :=
Block[{HiddenSymbols`ModifyTimes},
Developer`ToPackedArray[Times[z, arr]]];
Protect[Times];
In[9]:=
{avbytesize[#], Developer`PackedArrayQ[#]} & /@
{tmp1, tmp2,
Times[tmp1, tmp1], tmp1*tmp2,
I*tmp1, N[I]*tmp1,
I*tmp2, N[I]*tmp2}
Out[9]=
{{8.00056, True}, {20.0002, False},
{20.0002, False}, {20.0002, False},
{60.0002, False}, {60.0002, False},
{60.0002, False}, {60.0002, False}}
--
from - John Tanner home - john at janacek.demon.co.uk
mantra - curse Microsoft, curse... work - john.tanner at 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.