Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #16747 > unrolled thread

Re: Efficient function to accumulate a list of

Started byRay Koopman <koopman@sfu.ca>
First post2014-04-08 07:36 +0000
Last post2014-04-08 07:36 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Efficient function to accumulate a list of Ray Koopman <koopman@sfu.ca> - 2014-04-08 07:36 +0000

#16747 — Re: Efficient function to accumulate a list of

FromRay Koopman <koopman@sfu.ca>
Date2014-04-08 07:36 +0000
SubjectRe: Efficient function to accumulate a list of
Message-ID<li08uj$j3e$1@smc.vnet.net>
I found the reference to the undocumented SparseArray option 
that will sum multiple values assigned to the same location. It's
http://forums.wolfram.com/mathgroup/archive/2010/Dec/msg00070.html

In[1]:= SetSystemOptions[
        "SparseArrayOptions" -> {"TreatRepeatedEntries" -> 1}];

In[2]:= 
particles = { {3.6, 7,4}, {3.4, 8,6}, {2.1, 7,4} };
arr = SparseArray[Rest@# -> First@# & /@ particles, {10,10}]

Out[3]= SparseArray[< 2 >,{10,10}]

In[4]:= Normal[arr]

Out[4]= 
{{0,0,0,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0},
 {0,0,0,5.7,0,0,0,0,0,0},
 {0,0,0,0,0,3.4,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0}}

In[5]:= $Version

Out[5]= "6.0 for Mac OS X PowerPC (32-bit) (June 19, 2007)"

----- Ray Koopman <koopman@sfu.ca> wrote:

> In[1]:= 
> particles = { {3.6, 7,4}, {3.4, 8,6}, {2.1, 7,4} };
> arr = SparseArray[Rest@#[[1]] -> Tr@#[[All,1]] & /@ 
>       Last@Reap@Scan[Sow[#,{Rest@#}]&,particles], {10,10}]
> 
> Out[2]= SparseArray[< 2 >,{10,10}]
> 
> In[3]:= Normal[arr]
> 
> Out[3]=
> {{0,0,0,0,0,0,0,0,0,0},
>  {0,0,0,0,0,0,0,0,0,0},
>  {0,0,0,0,0,0,0,0,0,0},
>  {0,0,0,0,0,0,0,0,0,0},
>  {0,0,0,0,0,0,0,0,0,0},
>  {0,0,0,0,0,0,0,0,0,0},
>  {0,0,0,5.7,0,0,0,0,0,0},
>  {0,0,0,0,0,3.4,0,0,0,0},
>  {0,0,0,0,0,0,0,0,0,0},
>  {0,0,0,0,0,0,0,0,0,0}}
>  
>  In[4]:= $Version
> 
> Out[4]= 5.2 for Mac OS X (June 20, 2005)
> 
> With v7+, use GatherBy instead of Reap/Scan/Sow.
> 
> I remember reading somewhere about an undocumented option in SparseArray that will automatically sum multiple values assigned to the same location, but I don't know remember the details.
> 
> ----- julian w francis <julian.w.francis@gmail.com> wrote:
> 
>> Dear all,
>> 
>> I am struggling with how to convert a list of (value, coords) tuples into an array such that the an element in the array should represent the sum of all value elements in that list with matching coords, and zero if no matches, e.g.
>> 
>> { {3.6, 7,4}, {3,4, 8,6}, {2.1, 7,4} }
>> 
>> if it were a 10X10 element array would become all zero's, except element (7,4)->(3.6+2.1) and (8,6)->3.4
>> 
>> I've got some code which does what I want:
>> 
>> ParticleToDensity[particles_] := (
>>   arr = ConstantArray[0, {144, 300}];
>>   For[l = 1, l < NoParticles, l++,
>>    (tp = particles[[l]];
>>     If[tp[[2]] != 0,
>>      arr[[Round[tp[[3]]], Round[tp[[2]]]]] += tp[[1]]]
>>     )];
>>   arr)
>> 
>> So, the argument to the function is a list of "particles", and the first element of this particle is the value in the array I'd like to accumulate, and the next two elements in that particle say where in the array they should be accumulated.
>> 
>> It seems to work, but it is slow. I can see that this isn't really the Mathematica way of doing things, but I'm struggling to think of a better way.
>> 
>> For reference, NoParticles is typically set to something like 10,000, and it takes about 0.1 secs to process on my machine. However this needs to be done frequently, hence I am hoping for a better way.
>> 
>> Any help greatly appreciated.
>> 
>> Thanks,
>> Julian.

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web