稀疏矩阵乘法神牛.docx
- 文档编号:30175981
- 上传时间:2023-08-05
- 格式:DOCX
- 页数:17
- 大小:18.28KB
稀疏矩阵乘法神牛.docx
《稀疏矩阵乘法神牛.docx》由会员分享,可在线阅读,更多相关《稀疏矩阵乘法神牛.docx(17页珍藏版)》请在冰豆网上搜索。
稀疏矩阵乘法神牛
/*************************************************
**@copyrightreservedbytheauthor
**author:
-----------xuxuelong
**data:
-------------20009/12/18
**IDE:
--------------visulac++6.0
**major:
------------CS-softwareengineering-08
**emailaddress:
----xuxuelong0214@
**attention:
--------ifyoufindabuginthe
**------------------operationoryouhaveagood
**------------------algorithmtoachievethesame
**------------------purpose,justemailme!
***************************************************/
#include
#include
#include
#defineMax100
#defineOK1
#defineERROR0
/********************/
typedefstructnode
{//eachnodes'sinfo
introw;
intcolum;
intvalue;
}data;
typedefstruct_matrix
{//triples
introw_size;
intcolum_size;
intnon_zero_amount;
datamatrix[Max+1];
}tri_matrix;
/********subfunctions**********/
/*******functiondefinitions****/
intcreat_matrix(tri_matrix&T)
{//creatatri_matrixTwithcertainrowsandcolums
/*inputhowmanyrowsmatrixshave*/
printf("Thematrix'srowsize:
");
scanf("%d",&T.row_size);
/*inputhowmanycolumsmatrixshave*/
printf("Thematrix'scolumsize:
");
scanf("%d",&T.colum_size);
printf("Inputthenumberofnon_zeroamount:
");
scanf("%d",&T.non_zero_amount);
printf("/*****************************************/\n");
for(intcount=1;count<=T.non_zero_amount;count++)
{
do
{//insuretherowmarkisvalid
printf("Inputthemarkof
");
scanf("%d",&T.matrix[count].row);
}while(T.matrix[count].row>T.row_size);
do
{//insurethecolummarkisvalid
printf("Inputthemarkof
");
scanf("%d",&T.matrix[count].colum);
}while(T.matrix[count].colum>T.colum_size);
/*inputthevalueofthematrix*/
do
{//insurethevalueisnonzero
printf("Inputthe
");
scanf("%d",&T.matrix[count].value);
}while(T.matrix[count].value==0);
printf("/************************************/\n");
}//for
returnOK;
}//creat_matrix
voidprint_matrix(tri_matrixT)
{//printouttheinfoofthematrix
printf("\t\tRow\t\t\tColum\t\t\tValue\n");
printf("\t\t-------------------------------------------------\n");
for(intcount=1;count<=T.non_zero_amount;count++)
{
printf("\t\t%d\t\t\t%d\t\t\t%d\n",T.matrix[count].row,T.matrix[count].colum,T.matrix[count].value);
printf("\t\t-------------------------------------------------\n");
Sleep(500);
}
}//print_matrix
intadd_matrix(tri_matrixone,tri_matrixtwo,tri_matrix&three)
{//addmatrixoneandtwothenputtheresultinthree
inti,j,k;//countintheloop
for(i=1;i<=one.non_zero_amount;i++)
{//assignone'sinfotothree
three.matrix[i].row=one.matrix[i].row;
three.matrix[i].colum=one.matrix[i].colum;
three.matrix[i].value=one.matrix[i].value;
}
three.non_zero_amount=i-1;
/*inserttwo'sinfotothree*/
for(j=1;j<=two.non_zero_amount;j++)
{//insertonebyoneofthetwo'snodes
for(i=1;i<=three.non_zero_amount;i++)
{
if(two.matrix[j].row {//nodeintwoinsertintothree for(k=three.non_zero_amount;k>=i;k--) {//backofftheelementsinthree three.matrix[k+1].row=three.matrix[k].row; three.matrix[k+1].colum=three.matrix[k].colum; three.matrix[k+1].value=three.matrix[k].value; } /*two'sinsertelementreplacetheoriginone*/ three.matrix[i].row=two.matrix[j].row; three.matrix[i].colum=two.matrix[j].colum; three.matrix[i].value=two.matrix[j].value; three.non_zero_amount++; break; } else if(two.matrix[j].row==three.matrix[i].row) {//rowequalthencomparethecolums if(two.matrix[j].colum==three.matrix[i].colum) {//allequal three.matrix[i].row=three.matrix[i].row; three.matrix[i].colum=three.matrix[i].colum; three.matrix[i].value+=two.matrix[j].value; break;//two'selementtothenextone } else if(two.matrix[j].colum { for(k=three.non_zero_amount;k>=i;k--) {//backofftheelementsinthree three.matrix[k+1].row=three.matrix[k].row; three.matrix[k+1].colum=three.matrix[k].colum; three.matrix[k+1].value=three.matrix[k].value; } three.matrix[i].row=two.matrix[j].row; three.matrix[i].colum=two.matrix[j].colum; three.matrix[i].value=two.matrix[j].value; three.non_zero_amount++; break;//two'selementtothenextone } else { if(i==three.non_zero_amount) {//inserttothelast three.matrix[three.non_zero_amount+1].row=two.matrix[j].row; three.matrix[three.non_zero_amount+1].colum=two.matrix[j].colum; three.matrix[three.non_zero_amount+1].value=two.matrix[j].value; three.non_zero_amount++; } elsecontinue;//elementinthreetothenextone }//else }//if else//two.matrix[j].row>three.matrix[i].row { if(i==three.non_zero_amount) {//inserttothelast three.matrix[three.non_zero_amount+1].row=two.matrix[j].row; three.matrix[three.non_zero_amount+1].colum=two.matrix[j].colum; three.matrix[three.non_zero_amount+1].value=two.matrix[j].value; three.non_zero_amount++; break; } elsecontinue; } }//innerfor }//outerfor three.non_zero_amount=three.non_zero_amount; three.row_size=one.row_size; three.colum_size=one.colum_size; returnOK; }//add_matrix voidswap(int&a,int&b) {//exchangethevaluebetweenaandb inttemp; temp=a; a=b; b=temp; }//swap intmulti_matrix(tri_matrixone,tri_matrixtwo,tri_matrix&three) {//figureoutthree=one*two intone_count,two_count; intk=1;//countthree'snonzeroamount inti,j;//countintheloop datatemp[Max+1];//temporarilycontaintheinfo /*assignment*/ three.row_size=one.row_size; three.colum_size=two.colum_size; three.non_zero_amount=0; /*startmultiplication*/ for(one_count=1;one_count<=one.non_zero_amount;one_count++) for(two_count=1;two_count<=two.non_zero_amount;two_count++) { if(one.matrix[one_count].colum==two.matrix[two_count].row) {//validtomultiply /*puttheresultintemparray*/ temp[k].row=one.matrix[one_count].row; temp[k].colum=two.matrix[two_count].colum; temp[k].value=one.matrix[one_count].value*two.matrix[two_count].value; k++; } elsecontinue; } /*combinethesamerowandcolumelements*/ for(i=1;i for(j=i+1;j {//comparecurrentonewiththeelementbehindit if(temp[i].row==temp[j].row) {//rowequal if(temp[i].colum==temp[j].colum) {//columequal,thencombinethetwoelement temp[i].value+=temp[j].value; /*moveforwardtheelementbehind*/ for(intm=j;m temp[m]=temp[m+1]; /*theamountofthenonzeroelementdecrease*/ k--; } elsecontinue; } else/*jumptothenextelementintheoutterloop*/ if(temp[i].row break; if(temp[i].row>temp[j].row) continue; }//for /*sorttheelementhavethesamerowscript*/ for(i=1;i for(j=i+1;j { if(temp[i].row==temp[j].row) {//justswaptheelementhavethesamerowscript if(temp[i].colum>temp[j].colum) { swap(temp[i].colum,temp[j].colum); swap(temp[i].value,temp[j].value); } elsecontinue; } else break; } /*assignthetemparray'sinfotothree*/ for(i=1;i { three.matrix[i].row=temp[i].row; three.matrix[i].colum=temp[i].colum; three.matrix[i].value=temp[i].value; } three.non_zero_amount=k-1; returnOK; }//multi_matrix inttrans_matrix(tri_matrixone,tri_matrix&T) {//figureoutthetransfortmatrixofone /*sortthecolum(bubblesort)andassigntoT*/ for(inti=1;i<=one.non_zero_amount;i++) { /*initilizetobetheminimumone*/ T.matrix[i].row=one.matrix[i].colum; T.matrix[i].colum=one.matrix[i].row; T.matrix[i].value=one.matrix[i].value; for(intj=i+1;j<=one.non_zero_amount;j++) { if(T.matrix[i].row continue; if(T.matrix[i].row>one.matrix[j].colum) { swap(T.matrix[i].row,one.matrix[j].colum); swap(T.matrix[i].colum,one.matrix[j].row); swap(T.matrix[i].value,one.matrix[j].value); continue; }else {//T'srowequaltoone'scolum if(T.matrix[i].colum continue; if(T.matrix[i].colum>one.matrix[j].row) {//onlyswapthecolumofT swap(T.matrix[i].colum,one.matrix[j].row); swap(T.matrix[i].value,one.matrix[j].value); continue; } } }//for T.matrix[i].row=T.matrix[i].row; T.matrix[i].colum=T.matrix[i].colum; T.matrix[i].value=T.matrix[i].value; }//for T.row_size=one.row_size; T.colum_size=one.colum_size; T.non_zero_amount=one.non_zero_amount; returnOK; }//trans_matrix /*******themainfunction********/ /*******themainfunction********/ intmain(void) { /*declarethreetri_matrix*/ tri_matrixone,two,three; intchoice;//asamarkofselection charflag;//selectionmark while(OK) { system("cls"); system("colora4"); system("modeconcols=80lines=400"); system("title#TriplesToDealWithSparseMatrix#"); printf("\t@*************************************************************@\n"); putchar('\n'); printf("\t\t%c---Sparse-Matrix's--Application-System---%c\n",2,2); putchar('\n'); printf("\t@*************************************************************@\n"); printf("\t! ! tips: ifyouwanttobreakintheprocess,pressCtrl+C\n"); printf("\t$*************************************************************$\n"); printf("\t\t%c----------------Functions----------------%c\n",2,2); putchar('\n'); printf("\t\t%c-----------------------------------------%c\n",2,2); printf("\t\t%c<1>sparse--matrix---addition%c\n",2,2); printf("\t\t%c-----------------------------------------%c\n",2,2); printf("\t\t%c<2>sparse--matrix---multiplication%c\n",2,2); printf("\t\t%c-----------------------------------------%c\n",2,2); printf("\t\t%c<3>sparse--matrix---transformation%c\n",2,2); printf("\t\t%c-----------------------------------------%c\n",2,2); printf("\t\t%c<4>exit-----quit--
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 稀疏 矩阵 乘法
![提示](https://static.bdocx.com/images/bang_tan.gif)