MathGroup Archive 2012

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

Search the Archive

Re: fast summing alternative?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128358] Re: fast summing alternative?
  • From: Roland Franzius <roland.franzius at uos.de>
  • Date: Tue, 9 Oct 2012 00:39:48 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <k4tth1$sgq$1@smc.vnet.net>

Am 08.10.2012 08:57, schrieb Chris:
> Dear all
>
> The code below is part of a large compile function I wrote and the inputs, like the matrices weights and newData, are in fact large data files. Thus, I provide them in the following problem as random. Although, written as compile code (with CompilationTarget->"C") it is still too slow for the amounts of data I have to process. I provide two of my fastest alternatives, which sadly are not that fast. I do appreciate any help to make the code faster. Thanks in advance!
>
>
> (*
> What I do is simple: For each row of newMat and weights I construct a new row. The elements of this new row are the sums of the elements in weights if a certain requirement of the same element in newMat is met.
> *)
>
> Clear[bNum,nNum,weights,newMat,sortMat,tab];
>
> bNum;

Number missing

bNum=100;

> nNum=100;
>
> weights=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}];
> newMat=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}];
> sortMat=Table[i,{i, -10, 10, 0.1}]; (*elements always evenly spaced*)
>
> (*first alternative*)
> tab=Table[
> Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}],{k,1,bNum},
> {j,1,Length[sortMat]}];//AbsoluteTiming
>

{3.9624069, Null}

> (*second alternative*)
> mat=Block[{k=1},
> Reap[Do[
> Sow[Table[Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}],{j,1,Length[sortMat]}]];
> k++,{bNum}]]][[2,1]];//AbsoluteTiming
>
{3.9624069, Null}




List manipulations without references to elements and Replace instead of 
If's will save you more than half of the time

((projector = Outer[ #1 >= #2 &, sortMat, newMat] /.
    {True -> 1, False -> 0});
   pw = Plus @@@
     Transpose[ Map[(Plus @@ (weights*#) &), projector]];) // 
AbsoluteTiming

{1.5288027, Null}

I only checked dimensions of arrays for a short run time check.

The correct use of Outer, Transpose and element multiplication is up to 
you. For Plus@@@ a*b for vectors you can use Dot or Inner instead

-- 

Roland Franzius



  • Prev by Date: Re: Locator + EventHandler
  • Next by Date: SatisfiabilityInstances[] how to put variables
  • Previous by thread: fast summing alternative?
  • Next by thread: Re: fast summing alternative?