Is there a fast product operation for PackedArrays?

后端 未结 3 845
执念已碎
执念已碎 2021-02-04 16:26

In Mathematica a vector (or rectangular array) containing all machine size integers or floats may be stored in a packed array. These objects take less memory, and some operatio

3条回答
  •  广开言路
    2021-02-04 16:57

    Simon's method is fast, but it fails on negative values. Combining it with his answer to my other question, here is a fast solution that handles negatives. Thanks, Simon.

    Function

    f = (-1)^(-1 /. Rule @@@ Tally@Sign@# /. -1 -> 0) * Exp@Total@Log@Abs@# &;
    

    Testing

    lst = RandomReal[{-50, 50}, 5000000];
    
    Times @@ lst // Timing
    f@lst // Timing
    
    lst = Developer`FromPackedArray[lst];
    
    Times @@ lst // Timing
    f@lst // Timing
    

    {0.844, -4.42943661963*10^6323240}
    
    {0.062, -4.4294366*10^6323240}
    
    {0.64, -4.42943661963*10^6323240}
    
    {0.203, -4.4294366*10^6323240}
    

提交回复
热议问题