Non-zero diagonals for sparse format [message #36806] |
Thu, 23 October 2003 11:12 |
the_cacc
Messages: 104 Registered: October 2001
|
Senior Member |
|
|
Hi,
Suppose you have a matrix with lots of zero elements:
IDL> n = 100
IDL> A = randomu(1,n,n) * (randomu(2,n,n) GT 0.95)
IDL> print,total(A EQ 0)
9449.00
Normally you don't have the full matrix A but only co-ordinates for
where the non-zeros are:
IDL> nz = WHERE(A NE 0)
IDL> sa = A[nz]
IDL> xa = nz MOD n
IDL> ya = nz / n
Then you can create a sparse array structure:
IDL> B = sprsin(xa,ya,sa,n)
Confirm it's identical to A:
IDL> print,TOTAL(ABS(fulstr(B) NE A))
0.000000
However, the sparse format in IDL *always* stores the diagonals,
regardless of whether they're zero, which results in extra stored
zeros:
IDL> help,nz
NZ LONG = Array[551]
IDL> help,B.sa
<Expression> FLOAT = Array[643]
In this example, there's about 20% inefficiency. The actual array I'm
using is around n = 10^5 and I'm carrying around ~10^5 extra zeros.
Since the array is created only once then used hundreds of times for
matrix multiplications it makes sense to spend some time trying to
swap the rows so that the diagonals contain non-zeros whenever
possible.
So the problem is:
How to swap rows of a matrix to ensure that the diagonals are mostly
non-zero?
|
|
|