Re: adding sparse arrays [message #54295] |
Thu, 07 June 2007 11:13  |
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.
>
>> Thearraysthat 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 thesparsearraysA and B to get anothersparsearray (A
>> +B). The non-zero elements of the twoarraysare 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 twosparsearrays? The fact that asparsefunction 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
>>> arraysare. 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 -
I just typed out a long reply, and it didn't post it to the board...
so trying again.
Here is a piece of my code:
; img- 3d image, tmat- 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)]])
stan_indx=long(stan_pts[0,*]+stan_pts[1,*]*sz[0]+stan_pts[2, *]*sz1)
img_est=fltarr(sz[0],sz[1],sz[2])
for j=0,tmat_sz[2]=1 do begin
mat=tmat[*,*,j]
rp=mat#stan_pts
res_indx=round(rp[0,*])+round(rp[1,*])*sz[0]+round(rp[2,*])* sz1
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 want to add the tij matrices for all transformations
tmat[*,*,j]. But I cannot do tijsum = tijsum+tij for the foll.
reasons:
- Can't initialize tijsum since I don't know the size (size varies
with each iteration). Besides, initializing w/ create_struct doesn't
seem to work (I think a sparse array structure created using sprsin is
different from a structure created using create_struct)
- tijsum+tij doesn't work since structures can't be directly added.
I would like to avoid loops since my arrays are really huge. ANy
ideas?
|
|
|