Re: Adding sparse matrices [message #63875] |
Fri, 21 November 2008 11:24 |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
On Nov 21, 2:03 pm, Paolo <pgri...@gmail.com> wrote:
> Luis wrote:
>> On Nov 21, 4:10 pm, Vince Hradil <vincehra...@gmail.com> wrote:
>> :-) My mail wasn't very detail...
>
>> Unfortunately I can not use c = sprsin(fulstr(a) + fulstr(b)) (Out of
>> memory problems)
>
> Well, it seems that the algorithm is obvious enough:
> if an element is in a and not in b, copy it to c
> if an element is in b and not in a, copy it to c
> if an element is in a and in b, put the sum in c
>
> Ciao,
> Paolo
>
>
>
>>> On Nov 21, 9:59 am, Vince Hradil <vincehra...@gmail.com> wrote:
>
>>>> On Nov 21, 9:27 am, Luis <lgmen...@gmail.com> wrote:
>
>>>> > Hi,
>
>>>> > Does anyone know how to add two sparse matrices?
>
>>>> > Tks,
>>>> > Luis
>
>>>> c = a + b ?
>
>>> Sorry (removing foot from mouth and humbly retreating...)
>
>
The algorithm may be obvious, but reverse-engineering how the indices
are stored in the structure so you can figure out what's what isn't so
obvious (even with a copy of NR). :-)=
-Jeremy.
|
|
|
Re: Adding sparse matrices [message #63876 is a reply to message #63875] |
Fri, 21 November 2008 10:52  |
Jean H.
Messages: 472 Registered: July 2006
|
Senior Member |
|
|
Luis wrote:
> On Nov 21, 4:10 pm, Vince Hradil <vincehra...@gmail.com> wrote:
> :-) My mail wasn't very detail...
>
> Unfortunately I can not use c = sprsin(fulstr(a) + fulstr(b)) (Out of
> memory problems)
then try chopping it...
tmpA = fulstr(a)
a= 0
tmpB = fulstr(b)
b = 0
tmpC = tmpA+tmpB
tmpA=0
tmpB=0
c = sprsin(tmpC)
tmpC=0
Jean
|
|
|
Re: Adding sparse matrices [message #63878 is a reply to message #63876] |
Fri, 21 November 2008 11:03  |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
Luis wrote:
> On Nov 21, 4:10�pm, Vince Hradil <vincehra...@gmail.com> wrote:
> :-) My mail wasn't very detail...
>
> Unfortunately I can not use c = sprsin(fulstr(a) + fulstr(b)) (Out of
> memory problems)
Well, it seems that the algorithm is obvious enough:
if an element is in a and not in b, copy it to c
if an element is in b and not in a, copy it to c
if an element is in a and in b, put the sum in c
Ciao,
Paolo
>
>> On Nov 21, 9:59�am, Vince Hradil <vincehra...@gmail.com> wrote:
>>
>>> On Nov 21, 9:27�am, Luis <lgmen...@gmail.com> wrote:
>>
>>>> Hi,
>>
>>>> Does anyone know how to add two sparse matrices?
>>
>>>> Tks,
>>>> Luis
>>
>>> c = a + b ?
>>
>> Sorry (removing foot from mouth and humbly retreating...)
|
|
|
Re: Adding sparse matrices [message #63879 is a reply to message #63876] |
Fri, 21 November 2008 10:52  |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
On Nov 21, 12:15 pm, Luis <lgmen...@gmail.com> wrote:
> Unfortunately I can not use c = sprsin(fulstr(a) + fulstr(b)) (Out of
> memory problems)
>>> On Nov 21, 9:27 am, Luis <lgmen...@gmail.com> wrote:
>
>>>> Hi,
>
>>>> Does anyone know how to add two sparse matrices?
>
>>>> Tks,
>>>> Luis
Try this. It worked for my test cases, but I can't say I tested it
extensively or worked hard to make it particularly efficieint...
;+
; NAME:
; SPRSADD
;
; PURPOSE:
; Adds two sparse matrices (as generated by SPRSIN).
;
; CATEGORY:
; Math
;
; CALLING SEQUENCE:
; Result = SPRSADD(A,B)
;
; INPUTS:
; A: Sparse matrix to be added.
; B: Sparse matrix to be added.
;
; OUTPUTS:
; The addition of the matrices, in sparse format. This is
functionally
; equivalent to:
; SPRSIN(FULSTR(A) + FULSTR(B))
; but can be used even when the full matrices take up too much
memory
; for that operation.
;
; EXAMPLE:
; IDL> a = sprsin([[1,0,0],[0,1,0],[0,0,1]])
; IDL> b = sprsin([[0,0,2],[0,2,0],[2,0,0]])
; IDL> c = sprsadd(a,b)
; IDL> print, fulstr(c)
; 1.00000 0.00000 2.00000
; 0.00000 3.00000 0.00000
; 2.00000 0.00000 1.00000
;
; MODIFICATION HISTORY:
; Written by: Jeremy Bailin, November 2008
;
;-
function sprsadd, a, b
; what is the size of the matrix?
N = a.ija[0]-2
if b.ija[0]-2 ne N then message, 'SPRSADD: A and B must have the same
size.'
; figure out the full indices of all specified elements
a_i = lonarr(n_elements(a.sa)-1)
b_i = lonarr(n_elements(b.sa)-1)
; diagonal elements
a_i[0] = lindgen(N)*N+lindgen(N)
b_i[0] = lindgen(N)*N+lindgen(N)
an=N & bn=N
for i=0l,N-1 do begin ; loop through rows
a_non0 = a.ija[i+1]-a.ija[i]
b_non0 = b.ija[i+1]-b.ija[i]
if a_non0 gt 0 then a_i[an:an+a_non0-1] = i*N + a.ija[a.ija[i:i
+a_non0-1]-1]-1
an += a_non0
if b_non0 gt 0 then b_i[bn:bn+b_non0-1] = i*N + b.ija[b.ija[i:i
+b_non0-1]-1]-1
bn += b_non0
endfor
all_index = [a_i,b_i]
if n_elements(a.sa) eq N+1 then a_vals = a.sa[0:N-1] $
else a_vals = [a.sa[0:N-1],a.sa[N+1:*]]
if n_elements(b.sa) eq N+1 then b_vals = b.sa[0:N-1] $
else b_vals = [b.sa[0:N-1],b.sa[N+1:*]]
all_vals = [a_vals,b_vals]
indexsort = sort(all_index)
indexuniq = uniq(all_index[indexsort])
dupes = where(indexuniq-[-1,indexuniq] gt 1, comp=single, $
ncomp=nsingle)
; we know there are at least N dupes because the diagonals are
; always represented, so we don't need to test the where
combined_index = all_index[indexsort[indexuniq[dupes]]]
combined_vals = all_vals[indexsort[indexuniq[dupes]]]+all_vals
[indexsort[indexuniq[dupes]-1]]
if nsingle gt 0 then begin
combined_index = [combined_index, all_index[indexsort[indexuniq
[single]]]]
combined_vals = [combined_vals, all_vals[indexsort[indexuniq
[single]]]]
endif
; create the output array using SPRSIN in col, row, val form
return, sprsin(combined_index mod N, combined_index/N, combined_vals,
N)
end
|
|
|
Re: Adding sparse matrices [message #63883 is a reply to message #63879] |
Fri, 21 November 2008 09:15  |
colerbe
Messages: 6 Registered: March 2008
|
Junior Member |
|
|
On Nov 21, 4:10 pm, Vince Hradil <vincehra...@gmail.com> wrote:
:-) My mail wasn't very detail...
Unfortunately I can not use c = sprsin(fulstr(a) + fulstr(b)) (Out of
memory problems)
> On Nov 21, 9:59 am, Vince Hradil <vincehra...@gmail.com> wrote:
>
>> On Nov 21, 9:27 am, Luis <lgmen...@gmail.com> wrote:
>
>>> Hi,
>
>>> Does anyone know how to add two sparse matrices?
>
>>> Tks,
>>> Luis
>
>> c = a + b ?
>
> Sorry (removing foot from mouth and humbly retreating...)
|
|
|
Re: Adding sparse matrices [message #63891 is a reply to message #63883] |
Fri, 21 November 2008 08:22  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Vince Hradil writes:
> Sorry (removing foot from mouth and humbly retreating...)
Yeah, I learned a long time ago to stay far away from
anything that has the word "sparse" in the header. I don't
know a damn thing about it. :-(
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: Adding sparse matrices [message #63893 is a reply to message #63891] |
Fri, 21 November 2008 08:10  |
Vince Hradil
Messages: 574 Registered: December 1999
|
Senior Member |
|
|
On Nov 21, 9:59 am, Vince Hradil <vincehra...@gmail.com> wrote:
> On Nov 21, 9:27 am, Luis <lgmen...@gmail.com> wrote:
>
>> Hi,
>
>> Does anyone know how to add two sparse matrices?
>
>> Tks,
>> Luis
>
> c = a + b ?
Sorry (removing foot from mouth and humbly retreating...)
|
|
|
Re: Adding sparse matrices [message #63894 is a reply to message #63893] |
Fri, 21 November 2008 08:01  |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
On Nov 21, 10:27 am, Luis <lgmen...@gmail.com> wrote:
> Hi,
>
> Does anyone know how to add two sparse matrices?
>
> Tks,
> Luis
If nothing else, there's always the slow memory-intensive way:
c = sprsin(fulstr(a) + fulstr(b))
-Jeremy.
|
|
|
|