Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Large sparse matrix

Status
Not open for further replies.

fanta2

Technical User
Apr 10, 2005
78
CA
I have a large matrix (360 * 720). Every row has only 11 columns which has a value the others are zeros. The columns whih has a value are determined during run time. How can I store this effectively in a matrix and output them?
 
First rough idea:

do not save the matrix but save the elements and teh corresponding index.

code would be something like

Code:
integer nrows, iline, irow, ivalue
integer,dimension(xxx):: index    

real,dimension(xxx)::a
!
! xxx to be the max number of nonzero elements in your
! matrix
!
nrows=360
ivalue=0

do while (evaluation not ready)
ivalue=ivalue+1

!
! routine to evaluate your values to be saved in
! row  number irow and line number iline of your matrix
!
a(ivalue) = ....
index(ivalue) = (iline - 1) * nrows + irow
!
! this is a unique index for your value and in fact 
! resembles the data saving sequence in your PC's memory
!
enddo

When you want to retrieve your data you just evaluate the required index and check through this index vector like

Code:
integer jndex,jj

jndex = (iline-1) * nrows + irow
jj=0

do j=1,xxx 
    if(index(j).eq.jndex)then
        jj = j
        exit
    endif
enddo

if(jj.ne.0)then  ! to make sure you have found data
!
!   Your routine now with a(jj) being the value you wanted to retrieve.
!
endif

This could be improved if you sort your data before retrieval.

This way you exchange memory saving for runtime performance. If it is worth the effort is for you to decide. After all, 720 x 360 real data is only about 1 megabyte.

Norbert
 
Thank you Norbert for your kind reply! I have a few questions to ask you: Is iline (line number) means the column number? How can I retrive the output to text file as actual matrix including the zeros? How can I multiply with another matrix in this structure? I would also appreciate if you point me a reference about it. Thanks again.
 
Is iline (line number) means the column number?


Being not a native speaker of English, I might have messed up the vocabulary:

I meant that a matrix element a(i,j) refers to the element in 'line' number i and 'row' number j. Maybe that is different in proper English.

When writing out a matrix

a11, a12, a13, a14
a21, a22, a23, a24
a31, a32, a33, a34
aa1, a42, a43, a44

I referenced the horizontal as 'line', the vertical as 'row'. What are the proper terms in English ??


How can I retrive the output to text file as actual matrix including the zeros?

Code:
real dummy(nrows)
integer j, jj, ind

do i=1,nlines
    do j=1,nrows
!
!   use the retrieval algorithm as above
! 
       dummy(j) = a(jj)
    enddo
    write(xxx,xxx)(dummy(j),j=1,nrows)
enddo

How can I multiply with another matrix in this structure?

If you actually want to handle this as a matrix and use it mathwise - forget the above. Save the matrix as a matrix. Then you can use the matrix operating routines that I feel sure can be downloaded from the net. The savings in memory do not justify the loss in performance. After all, your matrix is not that big (provided you do not have a computer from before the great flood).

There is a lot of theory around to do maths on special types of matrices, meaning with a any special arrangement of non zero elements - being in the diagonal or what. Obtain a good text book on linear algebra and work your way into it.

Norbert


 
Thanks so much Norbert for your reply. I have got what you say.

Regarding column and row ... the horizontal lines are the rows and the vertical lines are the columns for instance a11, a12, a13 are in the same row. Anyway I understood what you mean that is more important.

Now I am looking for a book which describe these stuff.

Thanks soooo much for your reply I appeciate it and it is useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top