《稀疏矩阵乘法(神牛)》由会员分享,可在线阅读,更多相关《稀疏矩阵乘法(神牛)(11页珍藏版)》请在金锄头文库上搜索。
1、/* * copyright reserved by the author*author:-xuxuelong *data:-20009/12/18 *IDE:-visulac+ 6.0 *major:-CS-software engineering-08 *email address:- *attention:-if you find a bug in the *-operation or you have a good *-algorithm to achieve the same *-purpose ,just email me!*/ #include #include #include
2、 #define Max 100 #define OK 1 #define ERROR 0 /*/ typedefstruct node /each nodess info int row; intcolum; int value; data; typedefstruct _matrix /triples introw_size; intcolum_size; intnon_zero_amount; data matrixMax+1; tri_matrix; /*subfunctions*/ /*function definitions*/ intcreat_matrix(tri_matrix
3、 scanf(“%d“, /*input how many columsmatrixs have*/ printf(“The matrixs colum size:“); scanf(“%d“, printf(“Input the number of non_zero amount :“); scanf(“%d“, printf(“/*/n“); for(int count=1;count:“); scanf(“%d“, while(T.matrixcount.rowT.row_size); do /insure the colum mark is valid printf(“Input th
4、e mark of :“); scanf(“%d“, while(T.matrixcount.columT.colum_size); /*input the value of the matrix*/ do /insure the value is non zero printf(“Input the of the node:“); scanf(“%d“, while(T.matrixcount.value=0); printf(“/*/n“); /for return OK; /creat_matrix voidprint_matrix(tri_matrix T) /print out th
5、e info of the matrix printf(“ttRowtttColumtttValuen“); printf(“tt-n“); for(int count=1;count=i;k-) /backoff the elements in three three.matrixk+1.row=three.matrixk.row; three.matrixk+1.colum=three.matrixk.colum; three.matrixk+1.value=three.matrixk.value; /*twos insert element replace the origin one*
6、/ three.matrixi.row=two.matrixj.row; three.matrixi.colum=two.matrixj.colum; three.matrixi.value=two.matrixj.value; three.non_zero_amount+; break; else if(two.matrixj.row=three.matrixi.row) /row equal then compare the colums if(two.matrixj.colum=three.matrixi.colum) /all equal three.matrixi.row=three
7、.matrixi.row; three.matrixi.colum=three.matrixi.colum; three.matrixi.value+=two.matrixj.value; break;/twos element to the next one else if(two.matrixj.colum=i;k-) /backoff the elements in three three.matrixk+1.row=three.matrixk.row; three.matrixk+1.colum=three.matrixk.colum; three.matrixk+1.value=th
8、ree.matrixk.value; three.matrixi.row=two.matrixj.row; three.matrixi.colum=two.matrixj.colum; three.matrixi.value=two.matrixj.value; three.non_zero_amount+; break;/twos element to the next one else if(i=three.non_zero_amount) /insert to the last three.matrixthree.non_zero_amount+1.row=two.matrixj.row
9、; three.matrixthree.non_zero_amount+1.colum=two.matrixj.colum; three.matrixthree.non_zero_amount+1.value=two.matrixj.value;three.non_zero_amount+; else continue;/element in three to the next one /else /if else/two.matrixj.rowthree.matrixi.row if(i=three.non_zero_amount) /insert to the last three.mat
10、rixthree.non_zero_amount+1.row=two.matrixj.row; three.matrixthree.non_zero_amount+1.colum=two.matrixj.colum; three.matrixthree.non_zero_amount+1.value=two.matrixj.value; three.non_zero_amount+; break; else continue; /inner for /outer for three.non_zero_amount=three.non_zero_amount; three.row_size=on
11、e.row_size; three.colum_size=one.colum_size; return OK; /add_matrix void swap(int temp=a; a=b; b=temp; /swap intmulti_matrix(tri_matrix one, tri_matrixtwo,tri_matrix int k=1;/count threes non zero amount inti,j;/count in the loop data tempMax+1;/temporarily contain the info /*assignment*/ three.row_
12、size=one.row_size; three.colum_size=two.colum_size; three.non_zero_amount=0; /*start multiplication*/ for(one_count=1;one_counttempj.row) continue; /for /*sort the element have the same row script*/ for(i=1;itempj.colum) swap(tempi.colum,tempj.colum); swap(tempi.value,tempj.value); else continue; el
13、se break; /*assign the temp arrays info to three*/ for(i=1;ione.matrixj.colum) swap(T.matrixi.row,one.matrixj.colum); swap(T.matrixi.colum,one.matrixj.row); swap(T.matrixi.value,one.matrixj.value); continue; else /Ts row equal to ones colum if(T.matrixi.columone.matrixj.row) /only swap the colum of
14、T swap(T.matrixi.colum,one.matrixj.row); swap(T.matrixi.value,one.matrixj.value);continue; /for T.matrixi.row=T.matrixi.row; T.matrixi.colum=T.matrixi.colum; T.matrixi.value=T.matrixi.value; /for T.row_size=one.row_size; T.colum_size=one.colum_size; T.non_zero_amount=one.non_zero_amount; return OK; /trans_matrix /*the main function*/ /*the main function*/ int main(void)