C-矩陣基本運算代碼_第1頁
C-矩陣基本運算代碼_第2頁
C-矩陣基本運算代碼_第3頁
C-矩陣基本運算代碼_第4頁
C-矩陣基本運算代碼_第5頁
已閱讀5頁,還剩22頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

?#矩陣的運算代碼#region矩陣運算///<summary>〃/矩陣對應(yīng)行列式的值///</summary>///<param></param>///<returns></returns>privatedoubleMatrixValue(double[,]MatrixList)(intLevel=MatrixList.GetLength(1);double[,]dMatrix=newdouble[Level,Level];for(inti=0;i<Level;i++)(for(intj=0;j<Level;j++)(dMatrix[i,j]=MatrixList[i,j];))intsign=1;for(inti=0,j=0;i<Level&&j<Level;i++,j++)(〃判斷改行dMatrix[i,j]是否為0,若是,則尋找i后的行(m,m>i,切dMatrix[m,j]!=0)進行交換if(dMatrix[i,j]==0)(if(i==Level-1)(return0;)intm=i+1;〃獲取一個dMatrix[m,j]不為為0的行for(;dMatrix[m,j]==0;m++)(if(m==Level-1)(return0;))//判斷是否達到矩陣的最大行,若是,則返回0〃把i行和m行調(diào)換doubletemp;for(intn=j;n<Level;n++)(temp=dMatrix[i,n];dMatrix[i,n]=dMatrix[m,n];dMatrix[m,n]=temp;sign*=(-1);//把當前行以后的行所對應(yīng)的列變成0doubletmp;for(ints=Level-1;s>i;s--)tmp=dMatrix[s,j];//j行后面的所有行for(intt=j;t<Level;t++)'…";doubleresult=1;for(inti=0;i<Level;i++)if(dMatrix[i,i]!=0)result*=dMatrix[i,i];elsereturn0;returnsign*result;///<summary>///矩陣減法///</summary>///<param></param>///<param></param>///<param></param>///<param></param>privatedouble[]SubMatrix(double[]A1,double[]A2)//判斷矩陣的長短是否一致inta1=A1.GetLength(0);inta2=A2.GetLength(0);if(a1!=a2)(returnnull;)〃矩陣相減double[]B=newdouble[a1];for(inti=0;i<a1;i++)(B[i]=A1[i]-A2[i];)returnB;)///<summary>///矩陣乘法///</summary>///<param></param>///<param></param>///<returns></returns>privatedouble[,]MultiplyMatrix(double[,]firstMatrix,double[,]secondMatrix)(double[,]resultMatrix=newdouble[firstMatrix.GetLength(0),secondMatrix.GetLength(1)];〃判斷相乘矩陣是否合法,即第一個矩陣的列要等于第二個矩陣的行if(firstMatrix.GetLength(1)!=secondMatrix.GetLength(0))(returnnull;)//求結(jié)果矩陣for(introwIndex=0;rowIndex<firstMatrix.GetLength(0);rowIndex++)(for(intcolIndex=0;colIndex<secondMatrix.GetLength(1);colIndex++)(〃初始化結(jié)果矩陣的元素resultMatrix[rowIndex,colIndex]=0;for(inti=0;i<firstMatrix.GetLength(1);i++)(//求結(jié)果矩陣的元素值resultMatrix[rowIndex,colIndex]+=firstMatrix[rowIndex,i]*secondMatrix[i,colIndex];)))returnresultMatrix;)///<summary>///求逆矩陣///</summary>III<param></param>///<returns></returns>privatedouble[,]Athwart(double[,]dMatrix)〃獲取矩陣的行數(shù)intLevel=dMatrix.GetLength(l);double[,]dReverseMatrix=newdouble[Level,2*Level];//初始化矩陣Levelx(2*Level)for(inti=0;i<Level;i++)for(intj=0;j<2*Level;j++)if(j<Level)dReverseMatrix[i,j]=dMatrix[i,j];elseif(j-Level==i)dReverseMatrix[i,j]=1;else丁…。for(inti=0,j=0;i<Level&&j<Level;i++,j++)if(dReverseMatrix[i,j]==0)if(i==Level-1)returnnull;intm=i+1;for(;dMatrix[m,j]==0;m++)if(m==Level-1)returnnull;if(m==Level)returnnull;else〃把i行和m行相加for(intn=j;n<2*Level;n++) doubletemp=dReverseMatrix[i,j];if(temp!=1)〃把i行數(shù)據(jù),變成以1開始的一行數(shù)據(jù)for(intn=j;n<2*Level;n++)if(dReverseMatrix[i,n]!=0)di…心〃把i行后的所有行的j列變成0for(ints=Level-1;s>i;s--)temp=dReverseMatrix[s,j];for(intt=j;t<2*Level;t++)[—〃把矩陣Levelx(2*Level)前LevelxLevel轉(zhuǎn)變?yōu)閱挝痪仃噁or(inti=Level-2;i>=0;i--)for(intj=i+1;j<Level;j++)if(dReverseMatrix[i,j]!=0)(doubletmp=dReverseMatrix[i,j];for(intn=j;n<2*Level;n++)(dReverseMatrix[i,n]-=(tmp*dReverseMatrix[j,n])))))//返回逆矩陣double[,]dReturn=newdouble[Level,Level];for(inti=0;i<Level;i++)for(intj=0;j<Level;j++)dReturn[i,j]=dReverseMatrix[i,j+Level];returndReturn;)#endregion第二份:usingSystem;usingSystem.IO;usingSystem.Diagnostics;6namespaceAdjust7日田{8韓///<summary>9I///Matrix的摘要說明id///實現(xiàn)矩陣的基本運算11卜///</summary>121publicclassMatrix13中申(1415I〃構(gòu)造方陣161publicMatrix(introw)17中申(181m_data=newdouble[row,row];1920卜)21IpublicMatrix(introw,intcol)22中申(231m_data=newdouble[row,col];24卜)25I〃復制構(gòu)造函數(shù)261publicMatrix(Matrixm)27中申(281introw=m.Row;291intcol=m.Col;301m_data=newdouble[row,col];31321for(inti=0;i<row;i++)331for(intj=0;j<col;j++)341m_data[i,j]=m[i,j];3536卜)3738中申/*I〃分配方陣的大小I〃對于已含有內(nèi)存的矩陣,將清空數(shù)據(jù)IpublicvoidSetSize(introw)I(431m_data=newdouble[row,row];441)4546I〃分配矩陣的大小〃對于已含有內(nèi)存的矩陣,將清空數(shù)據(jù)491publicvoidSetSize(introw,intcol)501{Im_data=newdouble[row,col];I)卜*/54551//unitmatrix:設(shè)為單位陣561publicvoidSetUnit()57中申{581for(inti=0;i<m_data.GetLength(0);i++)591for(intj=0;j<m_data.GetLength(1);j++)601m_data[i,j]=((i==j)?1:0);61卜)62631〃設(shè)置元素值641publicvoidSetValue(doubled)65中申{661for(inti=0;i<m_data.GetLength(0);i++)671for(intj=0;j<m_data.GetLength(1);j++)681m_data[i,j]=d;69卜)7071I//Valueextraction:返中行數(shù)721publicintRow73申申{741get75中申{76returnm_data.GetLength(0);78卜)79卜)8081I〃返回列數(shù)821publicintCol83中申(841get85申申{861returnm_data.GetLength(1);87卜)88卜)8990I〃重載索引91〃存取數(shù)據(jù)成員921publicdoublethis[introw,intcol]93中申{941get95申申{961returnm_data[row,col];97卜)981set99中申(100Im_data[row,col]=value;101卜}102卜}103104I//primarychange1051//初等變換對調(diào)兩行:ri<-->rj106IpublicMatrixExchange(inti,intj)107申申{108Idoubletemp;109110Ifor(intk=0;k<Col;k++)111中申{112Itemp=m_data[i,k];113Im_data[i,k]=m_data[j,k];114Im_data[j,k]=temp;115卜}1161returnthis;117卜}118119120I〃初等變換第index行乘以mul121IMatrixMultiple(intindex,doublemul)122中申(1231for(intj=0;j<Col;j++)124申申{125Im_data[index,j]*=mul;126卜}127Ireturnthis;128卜}129130131I〃初等變換第src行乘以mul加到第index行132IMatrixMultipleAdd(intindex,intsrc,doublemul)133中申{134Ifor(intj=0;j<Col;j++)135中申{136Im_data[index,j]+=m_data[src,j]*mul;137卜}138139Ireturnthis;140卜}141142I//transpose轉(zhuǎn)置143IpublicMatrixTranspose()144中申(145IMatrixret=newMatrix(Col,Row);146147Ifor(inti=0;i<Row;i++)148Ifor(intj=0;j<Col;j++)149中申(150Iret[j,i]=m_data[i,j];151卜}152Ireturnret;153卜}154155I//binaryaddition矩陣力口156IpublicstaticMatrixoperator+(Matrixlhs,Matrixrhs)157中申{158Iif(lhs.Row!=rhs.Row)〃異常159中申{160ISystem.Exceptione=newException("相加的兩個矩陣的行數(shù)不等");161Ithrowe;162卜}163Iif(lhs.Col!=rhs.Col)//異常164中申{ISystem.Exceptione=newException("相加的兩個矩陣的歹ij數(shù)不等");Ithrowe;卜)168169Iintrow=lhs.Row;170Iintcol=lhs.Col;171IMatrixret=newMatrix(row,col);172173Ifor(inti=0;i<row;i++)174Ifor(intj=0;j<col;j++)175中申{176Idoubled=lhs[i,j]+rhs[i,j];177Iret[i,j]=d;178卜}179Ireturnret;180181卜}182183I//binarysubtraction矩陣減184IpublicstaticMatrixoperator-(Matrixlhs,Matrixrhs)185中申{186Iif(lhs.Row!=rhs.Row)〃異常187中申(188ISystem.Exceptione=newException("相減的兩個矩陣的行數(shù)不等");189Ithrowe;190卜}191Iif(lhs.Col!=rhs.Col)〃異常192中申(193ISystem.Exceptione=newException("相減的兩個矩陣的列數(shù)不等");194Ithrowe;195卜}196197Iintrow=lhs.Row;198Iintcol=lhs.Col;199IMatrixret=newMatrix(row,col);200201Ifor(inti=0;i<row;i++)202Ifor(intj=0;j<col;j++)203中申(204Idoubled=lhs[i,j]-rhs[i,j];205Iret[i,j]=d;206卜}207Ireturnret;208卜)209210211I//binarymultiple矩陣乘212IpublicstaticMatrixoperator*(Matrixlhs,Matrixrhs)213中年{214Iif(lhs.Col!=rhs.Row)〃異常215中申{216ISystem.Exceptione=newException。,相乘的兩個矩陣的行歹ij數(shù)不匹配");217Ithrowe;218卜)219220IMatrixret=newMatrix(lhs.Row,rhs.Col);221Idoubletemp;222Ifor(inti=0;i<lhs.Row;i++)223中申{224Ifor(intj=0;j<rhs.Col;j++)225中申{226Itemp=0;227Ifor(intk=0;k<lhs.Col;k++)228中申{229Itemp+=lhs[i,k]*rhs[k,j];230卜)231Iret[i,j]=temp;232卜}233卜}234235Ireturnret;236卜}237238239I//binarydivision矩陣除240IpublicstaticMatrixoperator/(Matrixlhs,Matrixrhs)241中申(242Ireturnlhs*rhs.Inverse();243卜}244245I//unaryaddition單目加246IpublicstaticMatrixoperator+(Matrixm)247中申(248IMatrixret=newMatrix(m);249Ireturnret;250卜}251252I//unarysubtraction單目減253IpublicstaticMatrixoperator-(Matrixm)254中申(255IMatrixret=newMatrix(m);256Ifor(inti=0;i<ret.Row;i++)257Ifor(intj=0;j<ret.Col;j++)258中申(259Iret[i,j]=-ret[i,j];260卜)261262Ireturnret;263卜}264265I//numbermultiple數(shù)乘266IpublicstaticMatrixoperator*(doubled,Matrixm)267中申{268IMatrixret=newMatrix(m);269Ifor(inti=0;i<ret.Row;i++)270Ifor(intj=0;j<ret.Col;j++)271Iret[i,j]*=d;272273Ireturnret;274卜)275276I//numberdivision數(shù)除277IpublicstaticMatrixoperator/(doubled,Matrixm)278申申{279Ireturnd*m.Inverse();280卜}281282I〃功能:返回列主元素的行號2831〃參數(shù):row為開始查找的行號2841〃說明:在行號[row,Col)范圍內(nèi)查找第row列中絕對值最大的元素,返回所在行號285IintPivot(introw)286中申{287Iintindex=row;288289Ifor(inti=row+1;i<Row;i++)290中申{291Iif(m_data[i,row]>m_data[index,row])292Iindex=i;293卜}294295Ireturnindex;296卜)2972981//inversion逆陣:使用矩陣的初等變換,列主元素消去法299IpublicMatrixInverse()300申申{301Iif(Row!=Col)//異常,非方陣302中申{303ISystem.Exceptione=newException("求逆的矩陣不是方陣");304Ithrowe;305卜}306IStreamWritersw=newStreamWriter("..\\annex\\close_matrix.txt");307IMatrixtmp=newMatrix(this);308IMatrixret=newMatrix(Row);//單位陣3091ret.SetUnit();310311IintmaxIndex;312IdoubledMul;313314Ifor(inti=0;i<Row;i++)315申申{316ImaxIndex=tmp.Pivot(i);317318Iif(tmp.m_data[maxlndex,i]==0)3199申(320ISystem.Exceptione=newException("求逆的矩陣的行歹ij式的值等于0,");321Ithrowe;322卜}3233241if(maxIndex!=i)//下三角陣中此列的最大值不在當前行,交換325中申(326Itmp.Exchange(i,maxIndex);327Iret.Exchange(i,maxIndex);328329卜}330331Iret.Multiple(i,1/tmp[i,i]);332333Itmp.Multiple(i,1/tmp[i,i]);334335Ifor(intj=i+1;j<Row;j++)336中申{337IdMul=-tmp[j,i]/tmp[i,i];338Itmp.MultipleAdd(j,i,dMul);339Iret.MultipleAdd(j,i,dMul);340341卜}342Isw.WriteLine("tmp=\r\n"+tmp);343Isw.WriteLine("ret=\r\n"+ret);344卜}//endfor345346347Isw.WriteLine("**=\r\n"+this*ret);348349Ifor(inti=Row-1;i>0;i--)350中申{351Ifor(intj=i-1;j>=0;j--)352中申{353IdMul=-tmp[j,i]/tmp[i,i];354Itmp.MultipleAdd(j,i,dMul);355Iret.MultipleAdd(j,i,dMul);356卜}357卜}//endfor358359360Isw.WriteLine("tmp=\r\n"+tmp);361Isw.WriteLine("ret=\r\n"+ret);362Isw.WriteLine("***=\r\n"+this*ret);363Isw.Close();364365Ireturnret;366367卜}//endInverse368369中中#region370中申/*371I//inversion逆陣:使用矩陣的初等變換,列主元素消去法372IpublicMatrixInverse()373I(374Iif(Row!=Col)〃異常,非方陣375I(376ISystem.Exceptione=newException("求逆的矩陣不是方陣");377Ithrowe;3781}379I///////////////380IStreamWritersw=newStreamWriter("..\\annex\\matrix_mul.txt");381I////////////////////382I///383IMatrixtmp=newMatrix(this);384IMatrixret=newMatrix(Row);//單位陣3851ret.SetUnit();386387IintmaxIndex;3881doubledMul;389390Ifor(inti=0;i<Row;i++)391I(392393ImaxIndex=tmp.Pivot(i);394395Iif(tmp.m_data[maxIndex,i]==0)3961{397ISystem.Exceptione=newException("求逆的矩陣的行歹ij式的值等于0,");398Ithrowe;399I)400401Iif(maxIndex!=i)〃下三角陣中此列的最大值不在當前行,交換4021{403Itmp.Exchange(i,maxIndex);404Iret.Exchange(i,maxIndex);4054061)407408Iret.Multiple(i,1/tmp[i,i]);409410I/////////////////////////411I//sw.WriteLine("nul\t"+tmp[i,i]+"\t"+ret[i,i]);412I////////////////413Itmp.Multiple(i,1/tmp[i,i]);414I//sw.WriteLine("mmm\t"+tmp[i,i]+"\t"+ret[i,i]);415Isw.WriteLine("111111111tmp=\r\n"+tmp);416Ifor(intj=i+1;j<Row;j++)417l(418IdMul=-tmp[j,i];419Itmp.MultipleAdd(j,i,dMul);420Iret.MultipleAdd(j,i,dMul);421422I}423Isw.WriteLine("222222222222=\r\n"+tmp);424425I}//endfor426427428429Ifor(inti=Row-1;i>0;i--)430I(431Ifor(intj=i-1;j>=0;j--)432I(433IdMul=-tmp[j,i];434Itmp.MultipleAdd(j,i,dMul);435Iret.MultipleAdd(j,i,dMul);4361)437I}//endfor438439I//////////////////////////440441442Isw.WriteLine("tmp=\r\n"+tmp.ToString());443444Isw.Close();445I///////////////////////////////////////446I///447Ireturnret;448449I}//endInverse450451卜*/452453卜#endregion454455I//determineifthematrixissquare:方陣456IpublicboolIsSquare()457中申(458IreturnRow==Col;459卜}460461I//determineifthematrixissymmetric對稱陣462IpublicboolIsSymmetric()463中申{464465Iif(Row!=Col)466Ireturnfalse;467468Ifor(inti=0;i<Row;i++)469Ifor(intj=i+1;j<Col;j++)470Iif(m_data[i,j]!=m_data[j,i])471Ireturnfalse;472473Ireturntrue;474卜}475476I〃一階矩陣,實數(shù)477IpublicdoubleToDouble()478中申(479ITrace.Assert(Row==1&&Col==1);480481Ireturnm_data[0,0];482卜}483484I//conerttostring485IpublicoverridestringToString()486申申{487488Istrings="";489Ifor(inti=0;i<Row;i++)490中申{491Ifor(intj=0;j<Col;j++)492Is+=string.Format("{0}",m_data[i,j]);493494Is+="\r\n";495卜}496Ireturns;497498卜}499500501 〃私有數(shù)據(jù)成員502Iprivatedouble[,]m_data;503504卜}//endclassMatrix505}///矩陣的乘publicboolMatrixMultiply(double[,]a,double。]b,refdouble。]c)(if(a.GetLength(1)!=b.GetLength(0))returnfalse;if(a.GetLength(0)!=c.GetLength(0)||b.GetLength(1)!=c.GetLength(1))returnfalse;for(inti=0;i<a.GetLength(0);i++)(for(intj=0;j<b.GetLength(1);j++)(c[i,j]=0;for(intk=0;k<b.GetLength(0);k++)(c[i,j]+=a[i,k]*b[k,j];returntrue;///矩陣的加publicboolMatrixAdd(double[,]a,double[,]b,refdouble[,]c)(if(a.GetLength(0)!=b.GetLength(0)||a.GetLength(1)!=b.GetLength(1)||a.GetLength(0)!=c.GetLength(0)||a.GetLength(1)!=c.GetLength(1))returnfalse;for(inti=0;i<a.GetLength(0);i++)(for(intj=0;j<a.GetLength(1);j++)(c[i,j]=a[i,j]+b[i,j];))returntrue;)///矩陣的減publicboolMatrixSubtration(double[,]a,double[,]b,refdouble[,]c)(if(a.GetLength(0)!=b.GetLength(0)||a.GetLength(1)!=b.GetLength(1)||a.GetLength(0)!=c.GetLength(0)||a.GetLength(1)!=c.GetLength(1))returnfalse;for(inti=0;i<a.GetLength(0);i++)(for(intj=0;j<a.GetLength(1);j++)(c[i,j]=a[i,j]-b[i,j];))returntrue;)///矩陣的行列式的值publicdoubleMatrixSurplus(double[,]a)(inti,j,k,p,r,m,n;m=a.GetLength(0);n=

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論