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?
|
|
|
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.
|
|
|
Re: adding sparse arrays [message #54300 is a reply to message #54296] |
Thu, 07 June 2007 08:23   |
Vince Hradil
Messages: 574 Registered: December 1999
|
Senior Member |
|
|
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?
|
|
|
Re: adding sparse arrays [message #54301 is a reply to message #54300] |
Thu, 07 June 2007 07:32   |
nivedita.raghunath
Messages: 15 Registered: July 2006
|
Junior Member |
|
|
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 -
|
|
|
|
|
|
Re: adding sparse arrays [message #54435 is a reply to message #54295] |
Fri, 08 June 2007 00:42  |
Paolo Grigis
Messages: 171 Registered: December 2003
|
Senior Member |
|
|
nivedita.raghunath@gmail.com wrote:
> [...]
> 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*/
Why don't you keep track of the indices you are using (stan_indx, res_indx) in each
iteration? In the next iteration you can compare the new row & column indices with
the old ones: if they are different, add the new indices & values to the old list,
if they are equal, add the new value to the corresponding old value.
You can then use sprsin only once at the end.
Ciao,
Paolo
>
> 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?
>
>
|
|
|