Algorithm----Ternary group of sparse matrices

Source: Internet
Author: User

The representation of a ternary group

(1), the purpose: for the large sparse matrix in the actual problem, if the conventional allocation method in the computer storage, will generate a lot of memory waste, and in the access and operation of the time will also cause a lot of waste of time, in order to solve this problem, so good to produce a variety of solutions.

(2) Because of its sparse characteristics, the memory cost of sparse matrix can be greatly saved by compressing. This method can save storage space by composing a ternary group (I,J,V) of the row, column, and its value of the non-0 element, and then storing the triples in a certain way.

Specific example:

#define Smax-  N-struct{     int i,j;           // store row and column information for non-0 elements     // value    of non-0 element } Spnode;        // define ternary group types struct {     int// matrix rows, columns, and number  of non-0 elements     // Triples table  }spmatrix;

Transpose of sparse matrices

Operation: A n*m sparse matrix is transpose and will be a m*n matrix. In a nutshell, we're talking about the row and label exchanges in triples, but is that just the end? Of course not. The first rule of the ternary group is a row by line and the elements in each row are stored in the order of the column number from small to large, so B must also be implemented according to this law.

= "

Algorithm ideas:

The rows and columns of ①a are converted into columns and rows of B;
② The first column, the second column, and the last column in A.data, and stores the rows and columns of each ternary group found in the B.data in order.

Since the transpose of a is top-down, it is required that the position of the post-transpose positions of any element in a must be known, and that the original storage order is arranged by line number, so the previous transpose of the same column must still be in front of the peer before it is placed. It is determined that the position of the first element of each row after the transpose determines the entire order. Specific calculations such as

The code is as follows:

voidTransM1 (Spmatrix *A) {Spmatrix*B; intP,q,col; B=malloc (sizeof(Spmatrix));/*Request Storage Space*/B->mu=a->nu; b->nu=a->mu; B->tu=a->Tu; /*the number of rows, columns, and elements of a sparse matrix*/    if(B->tu! =0)/*with a non-0 element, the conversion*/{Q=0;  for(col=1; Col<= (A-&GT;NU); col++) {/*converting by a sequence of columns*/             for(p=1; P<= (A-&GT;TU); p++)/*Scan the entire ternary group table*/                if(a->data[p].j==Col) {B->data[q].i= a->DATA[P].J; B->data[q].j= a->data[p].i; B->data[q].v= a->data[p].v; Q++; }/*if*/}}/*if (b->tu>0)*/    returnB/*returns a pointer to the transpose matrix*/} /*TransM1*/  

Algorithm----Ternary group of sparse matrices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.