Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Product


On 14 Apr 2008, at 18:41, Szabolcs Horv=E1t wrote:
> Andrzej Kozlowski wrote:
>> The reason for the speed of Total unlike Plus is that it does not 
>> need
>> to unpack packed arrays. Here is how you can see what happens.
>>
>> SetSystemOptions["PackedArrayOptions" -> {"UnpackMessage" -> True}];
>>
>> mytestlist = Table[RandomInteger[{1, 9}], {1000000}];
>> Developer`PackedArrayQ[mytestlist]
>> True
>>
>> So mytestlist is a packed array. Now let's see what happens if we add
>> it up using Plus:
>>
>> In[4]:= Plus @@ mytestlist
>> During evaluation of In[4]:= Developer`FromPackedArray::"unpack1" :
>> "Unpacking array."
>> During evaluation of In[4]:= Developer`FromPackedArray::"punpack1" =
:
>> "Unpacking array to
>> Out[4]= 4998322
>>
>> Now the same with Total:
>>
>> In[5]:= Total[mytestlist]
>> Out[5]= 4998322
>>
>> It is the unpacking of packed arrays by Plus that makes adding up 
>> this
>> list using Plus much slower.
>
> But of course in principle there is nothing that would make it
> impossible to introduce a special case for functions like Times and
> Plus, and make them work with packed arrays.  So this alone does not
> make it necessary to introduce a new syntax/new function, like 
> Total[].

Well, I never claimed that it did. It seems to me that WRI wanted to 
add new functionality to Mathematica and modifying Plus would break 
too many things. For example, Total, in version 6 accepts level 
specifications, so:

Total[{{a, b}, {c, d}}, {2}]
{a + b, c + d}

while

Apply[Plus, {{a, b}, {c, d}}, {2}]
{{a, b}, {c, d}}

The key thing, in my opinion, is that Total[list] is just a spacial 
case of Total (=Total[list,1]) and the general case of Total would be =

quite hard or impossible to define using Apply and Plus.

Andrzej Kozlowski




>
>
> For example,
>
> Unprotect[Apply]
> Apply[Plus, arr_?Developer`PackedArrayQ] := Total[arr]
> Protect[Apply]
>
> (Of course this is just an example to show that it is possible to do
> this.  It is not a complete implementation and certainly not something
> that I would recommend for _users_ to do.  But it would be nice if WRI
> provided these special case algorithms unless there is some limitation
> in Mathematica's internal design that makes it difficult/impossible to
> do this in a proper way.)
>



  • References:
    • Product
      • From: "Steven" <steven_504@telenet.be>
    • RE: Product
      • From: "Jose Luis Gomez" <jose.luis.gomez@itesm.mx>
    • Re: Product
      • From: Szabolcs Horvát <szhorvat@gmail.com>
  • Prev by Date: Re: Re: Product
  • Next by Date: Re: How to solve this simple equation?
  • Previous by thread: Re: Re: Product
  • Next by thread: Re: Re: Product