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


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

Sparse Array Question

Started bychristopher arthur <chris.arthur1@gmail.com>
First post2013-04-24 01:39 +0000
Last post2013-04-25 06:51 +0000
Articles 4 — 3 participants

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


Contents

  Sparse Array Question christopher arthur <chris.arthur1@gmail.com> - 2013-04-24 01:39 +0000
    Re: Sparse Array Question debguy <johnandsara2@cox.net> - 2013-04-24 11:02 +0000
    Re: Sparse Array Question christopher arthur <chris.arthur1@gmail.com> - 2013-04-25 06:52 +0000
    Re: Sparse Array Question Szabolcs Horvát <szhorvat@gmail.com> - 2013-04-25 06:51 +0000

#14280 — Sparse Array Question

Fromchristopher arthur <chris.arthur1@gmail.com>
Date2013-04-24 01:39 +0000
SubjectSparse Array Question
Message-ID<kl7d3o$kdc$1@smc.vnet.net>
Hello,

I have a list of rules for defining a SparseArray.  The list has some 
coordinates repeated with different values.  If a coordinate appears in 
the list more than once, I want to sum up the values, and then have a 
SparseArray

i.e., if my list were myList={{1,1}->1,{2,1}->2,{1,1}->3} then my 
SparseArray should have {1,1}->4.  I've tried using ReplacePart on an 
array to say myArray=ReplacePart[myArray,...] but this seems horribly 
inefficient as a way to change values in an array.

I'm using Mathematica 5.2

Cheers,

Chris

[toc] | [next] | [standalone]


#14294

Fromdebguy <johnandsara2@cox.net>
Date2013-04-24 11:02 +0000
Message-ID<kl8e4s$ona$1@smc.vnet.net>
In reply to#14280
Sort by the 2nd key if the ordering of the list is immaterial.  Then
the task is near as simple as walking a simle list.

Use of Dispatch with long lists of rules may speed things up.

[toc] | [prev] | [next] | [standalone]


#14305

Fromchristopher arthur <chris.arthur1@gmail.com>
Date2013-04-25 06:52 +0000
Message-ID<klajqj$3al$1@smc.vnet.net>
In reply to#14280
Thanks to everyone for the suggestions...it turned out that the 
duplicate entries were in different lists to start, so I just made 
several SparseArrays and added them up.

Chris

debguy a écrit :
> Sort by the 2nd key if the ordering of the list is immaterial.  Then
> the task is near as simple as walking a simle list.
>
> Use of Dispatch with long lists of rules may speed things up.
>
>
>   

[toc] | [prev] | [next] | [standalone]


#14312

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2013-04-25 06:51 +0000
Message-ID<klajp8$39g$1@smc.vnet.net>
In reply to#14280
On 2013-04-24 01:39:04 +0000, christopher arthur said:

> Hello,
> 
> I have a list of rules for defining a SparseArray.  The list has some
> coordinates repeated with different values.  If a coordinate appears in
> the list more than once, I want to sum up the values, and then have a
> SparseArray
> 
> i.e., if my list were myList={{1,1}->1,{2,1}->2,{1,1}->3} then my
> SparseArray should have {1,1}->4.  I've tried using ReplacePart on an
> array to say myArray=ReplacePart[myArray,...] but this seems horribly
> inefficient as a way to change values in an array.
> 
> I'm using Mathematica 5.2


There is a system option for this.  Hopefully it works in 5.2 too, but 
I cannot test.

In[1]:= Normal@SparseArray[{{1, 1} -> 1, {2, 1} -> 2, {1, 1} -> 3}]
Out[1]= {{1}, {2}}

In[2]:= SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" 
-> Total]

In[3]:= Normal@SparseArray[{{1, 1} -> 1, {2, 1} -> 2, {1, 1} -> 3}]
Out[3]= {{4}, {2}}

In[4]:=
(* set it back to the default *)
SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> First]


Be very very careful with this!  It modified global behaviour, so 
there's no telling what other indirect effects it will have.

[toc] | [prev] | [standalone]


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


csiph-web