Re: adding sparse arrays [message #54296 is a reply to message #54295] |
Thu, 07 June 2007 10:50   |
nivedita.raghunath
Messages: 15 Registered: July 2006
|
Junior Member |
|
|
On Jun 7, 11:23 am, hradilv <hrad...@yahoo.com> wrote:
> On Jun 7, 9:32 am, nivedita.raghun...@gmail.com wrote:
>
>
>
>
>
>> Hi all,
>
>> Thanks for the suggestions.
>
>> The arrays that I'm working with are really huge so there's no option
>> of A+B. I cannot convert to full matrix form using fulstr and have to
>> work only with the sparse arrays A and B to get another sparse array (A
>> +B). The non-zero elements of the two arrays are in different index
>> positions (ija), so the sa vectors cannot be added directly.
>
>> Under these constraints, whats the best (and the fastest) way to add
>> them?
>
>> -Nivedita
>
>> On Jun 7, 4:36 am, Paolo Grigis <pgri...@astro.phys.ethz.ch> wrote:
>
>>> nivedita.raghun...@gmail.com wrote:
>>>> Hello all,
>
>>>> How do I add two sparse arrays? The fact that a sparse function to add
>>>> doesn't exist makes me think its pretty simple, but I just can't get
>>>> it. I do not want to use any loops.
>
>>> Well, it depends where the non-zero, non-diagonal elements of the two
>>> arrays are. If they are located in the same positions, you just need
>>> to add the sa vectors while keeping the ija vectors fixed.
>
>>> Ciao,
>>> Paolo
>
>>>> Thanks in advance.
>
>>>> -Nivedita- Hide quoted text -
>
>>> - Show quoted text -
>
> I don't use sprsin, but can you try c = sprsin(fulstr(a)+fulstr(b))?
> or maybe c = sprsin(fulstr(temporary(a))+fulstr(temporary(b))) to
> delete a and b from memory?- Hide quoted text -
>
> - Show quoted text -
Ok, here is the REAL problem:
I generate a bunch of x,y,z indices, apply a transformation to the
indices and build a matrix of standard vs transformed indices
;img is a 3-d image, tmat is a set of transformation matrices
sz=size(img,/dimensions)
tmat_sz=size(tmat,/dimensions)
sz1=sz[0]*sz[1]
npix=sz1*sz[2]
stan_pts=transpose([[lindgen(npix) mod sz[0]], [lindgen(npix)/sz[0]
mod sz[1]], [lindgen(npix)/sz1],[replicate(1l,npix)]])
for j=0,tmat_sz[2]-1 do begin
mat=tmat[*,*,j] ; transfomation matrix
rp=mat#stan_pts
res_indx=round(rp[0,*])+round(rp[1,*])*sz[0]
valid_indx=where(res_indx ge 0 and res_indx lt npix)
tij=sprsin(stan_indx[valid_indx],res_indx[valid_indx],replic ate(1.,n_elements(valid_indx)),npix)
/* THIS IS WHAT I WANT TO DO BUT CANNOT DO */
tijsum=tijsum+tij
endfor
So basically I have made tij as a sparse matrix (stan_indx vs
res_indx). I would like to sum this matrix over all the
transformations tmat[*,*,i].
I cannot do tijsum=tijsum+tij for the foll reasons:
- can't initialize tijsum since I don't have a size (size varies for
each iteration). Besides I don't think a sparse structure (created
using sprsin) is the same as an ordinary structure (created using
create_struct)
- fails since it can't directly add structures.
I do have an idea w/ the use of for/ while but I wouldn't like to use
loops, and either ways I need to know how I can initialize tijsum.
|
|
|