《原创利用克拉默法则求解多元一次方程组》由会员分享,可在线阅读,更多相关《原创利用克拉默法则求解多元一次方程组(8页珍藏版)》请在金锄头文库上搜索。
1、(原创)利用克拉默法则求解多元一次方程组packagezouyf.matrix;/*/*authorzouyf2008-4-7本程序利用克拉默法则求解多元一次方程组*/publicclassGetMatrix.privatedoublesavequot;/保存变量系数privatedoubleconstquot;/保存常量系数privatedoublesaveResult;/保存解的集合publicGetMatrix(doublequot).intcount=quot.length;savequot=newdoublecountcount;constquot=newdoublecount;sa
2、veResult=newdoublecount;inti=0,j=0;for(i=0;icount;i+).for(j=0;jcount;j+).savequotij=quotij;constquoti=quoticount;saveResulti=0;privatedoublegetMatrixResult(doubleinput)/递归的方法求得某个行列式的值.if(input.length=2)/递归出口,为二阶行列式时,直接返回.returninput00*input11-input01*input10;else.doubletemp=newdoubleinput.length;/存放
3、第一列的系数值doubletempinput=newdoubleinput.length-1input.length-1;doubleresult=0;for(inti=0;iinput.length;i+).tempi=inputi0;intm=0,n=0;for(intk=0;kinput.length;k+).if(k!=i).for(m=0;minput.length-1;m+).tempinputnm=inputkm+1;/删除当前变量系数所在的行和列,得到减少一阶的新的行列式n+;if(i%2=0)/递归调用,利用代数余子式与相应系数变量的乘积之和得到多阶行列式的值.result=
4、result+tempi*getMatrixResult(tempinput);else.result=result-tempi*getMatrixResult(tempinput);returnresult;privatedoublegetReplaceMatrix(inti)/用常数系数替换相应的变量系数,得到新的行列式.doubletempresult=newdoublesavequot.lengthsavequot.length;for(intm=0;msavequot.length;m+).for(intn=0;nsavequot.length;n+).if(i!=m).tempre
5、sultnm=savequotnm;else.tempresultni=constquotn;/用常量系数替换当前变量系数returntempresult;publicdoublegetResult().doublebasic=0;basic=getMatrixResult(savequot);/得到变量系数行列式的值if(Math.abs(basic)0.00001)/得到行列式的值为零时,表示此方程组没有唯一解.System.out.println(itdosenothavethequeueresult!);returnsaveResult;doubletemp=newdoublesave
6、Result.lengthsaveResult.length;for(inti=0;isaveResult.length;i+).temp=getReplaceMatrix(i);saveResulti=getMatrixResult(temp)/basic;/依次计算得到每一个变量的解returnsaveResult;publicstaticvoidmain(Stringargs)./*/*测试方程组*2a+b-5c+d=8*a-3b-6d=9*2b-c+2d=-5*a+4b-7c+6d=0*/doubletest=.2,1,-5,1,8,.1,-3,0,-6,9,.0,2,-1,2,-5,
7、.1,4,-7,6,0;GetMatrixgm=newGetMatrix(test);doubleuu=newdoubletest.length;/返回结果集uu=gm.getResult();for(inti=0;iuu.length;i+).System.out.println(uui+,);C+版#include stdafx.h#include #include #include /本程序利用克拉默法则求解多元一次方程组using namespace std;class GetMatrixpublic:GetMatrix(vector vector quot);double getMa
8、trixResult(vector vector input);vector vector getReplaceMatrix(int i);vector getResult();private:vector vector savequot;vector constquot;vector saveResult;GetMatrix:GetMatrix(vector vector quot)int count = quot.size();savequot.resize(count);for (int i=0;icount;i+)savequoti.resize(count);constquot.resize(count);saveResult.resize(count);int i = 0, j = 0;for (i = 0; i count; i+)for (j = 0; j count; j+)savequotij = quotij;constquoti = quoticount;saveResulti = 0;double GetMatrix:getMatrixResult(vector vector input)/ 递归的方法求得某