怎麼用c++ builder 寫反矩陣 |
尚未結案
|
c0931121696
一般會員 發表:2 回覆:0 積分:0 註冊:2010-08-02 發送簡訊給我 |
如題:想請問大大們,我最近想寫一個反矩陣的程式,不知道該如何寫,是否可請大大指導,謝謝!
我貼程式碼上來!請大大們看一下!因為我看不懂他是用哪一種方法寫的!如果改成高斯消去法!該改哪一段程式! 程式碼如下: //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit2.h" #include <math.h></math.h> //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TRevrsematrix *Revrsematrix; float x[100][100], bas[100][100] ; //x矩陣為原始矩陣,bas為單位矩陣 //--------------------------------------------------------------------------- __fastcall TRevrsematrix::TRevrsematrix(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- int i, j, k, l, a, b, max; //a為矩陣之row值,b為矩陣之col值,max為換列時使用 float temp, tempdec; //-------------- void __fastcall TRevrsematrix::Button1Click(TObject *Sender) { a = StrToInt(Edii->Text);//矩陣之row b = StrToInt(Edij->Text);//矩陣之col StringGrid1->ColCount=a 1; //原始矩陣 StringGrid1->RowCount=b 1; StringGrid2->ColCount=a 1; //運算後原始矩陣 StringGrid2->RowCount=b 1; StringGrid3->ColCount=a 1; //運算後基本矩陣 StringGrid3->RowCount=b 1; if(a==0 && b==0) { ShowMessage("Error"); } else if(a!=b) { ShowMessage("Wrong Matrix"); } else if(a==1 && b==1) { ShowMessage("Wrong Matrix"); } for(int i=0;i<=a;i ) for(int j=0;j<=b;j ) { StringGrid1->Cells[0][i] = (i-1); StringGrid2->Cells[0][i] = (i-1); StringGrid3->Cells[0][i] = (i-1); } for(int i=0;i<=a;i ) for(int j=0;j<=b;j ) { StringGrid1->Cells[i][0] = (i-1); StringGrid2->Cells[i][0] = (i-1); StringGrid3->Cells[i][0] = (i-1); } StringGrid1->Cells[0][0] = " "; StringGrid2->Cells[0][0] = " "; StringGrid3->Cells[0][0] = " "; } //--------------------------------------------------------------------------- void __fastcall TRevrsematrix::Button4Click(TObject *Sender) { for(int i=0;i<=StrToInt(Edii->Text);i ) //清除矩陣內之值 for(int j=0;j<=StrToInt(Edij->Text);j ) { StringGrid1->Cells[i][j]=" "; StringGrid2->Cells[i][j]=" "; StringGrid3->Cells[i][j]=" "; Memo1->Text = " "; Memo2->Text = " "; } } //--------------------------------------------------------------------------- void __fastcall TRevrsematrix::Button2Click(TObject *Sender) { float b[5][5] = {{1, 4, 7, 8, 5}, //5*5demo之矩陣值 {3, 9, 2, 6, 1}, {2, 1, 4, 5, 6}, {2, 8, 1, 4, 9}, {5, 0, 4, 9, 2}}; StringGrid1->ColCount=6; StringGrid1->RowCount=6; for(int i=0;i<=4;i ) for(int j=0;j<=4;j ) { StringGrid1->Cells[j 1][i 1]=FloatToStr(b[i][j]); } /* float b[3][3] = {{1, -1, 0}, //3*3demo之矩陣值 {1, 0, -1}, {-6, 2, 3}}; StringGrid1->ColCount = 3; StringGrid1->RowCount = 3; for(int i=0;i<=2;i ) for(int j=0;j<=2;j ) StringGrid1->Cells[j][i]=b[i][j]; */ } //--------------------------------------------------------------------------- void __fastcall TRevrsematrix::Button5Click(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TRevrsematrix::Button3Click(TObject *Sender) { a = StrToInt(Edii->Text); b = StrToInt(Edij->Text); for(int i=0;i { for(int j=0;j { x[i][j]=StrToFloat(StringGrid1->Cells[j 1][i 1]); } } for(int i=0;i { for(int j=0;j if(i==j) bas[i][j]=1; else if(i!=j) bas[i][j]=0; } /* //Memo1->Lines->Add(FloatToStr(bas[i][j])); for(int i=0;i { max=i; for(j=1 1;j { if(abs(x[i][j])>abs(x[max][i])) max = j; } } if(max!=i) //若和原來值不同時 for(k=i;k { temp = x[i][k]; //x[i][k]和x[max][k]交換 x[i][k] = x[max][k]; x[max][k] = temp; { temp = bas[i][k]; //bas[i][k]和bas[max][k]交換 bas[i][k] = bas[max][k]; bas[max][k] = temp; } } //換列動作結束 */ temp = 0; //矩陣運算開始 tempdec = 0; for(int i=0;i { tempdec = x[i][i]; for(int k=0;k { x[i][k] = x[i][k]/tempdec; //x矩陣對角線變1 bas[i][k] = bas[i][k]/tempdec;//bas矩陣對角線變1 //Memo1->Lines->Add(FloatToStr(x[i][k]));//測試是否執行對角線變1 } for(int l=0;l<=a-1;l ) { temp = x[l][i]; if(i==l) { x[l][i] = x[l][i]; } else for(int j=0;j<=b-1;j ) { x[l][j] = x[l][j] - (temp*x[i][j]); //x矩陣相減 bas[l][j] = bas[l][j] - (temp*bas[i][j]); //bas矩陣相減 Memo1->Lines->Add("x" IntToStr(l) IntToStr(j) "=" FloatToStr(x[l][j])); //輸出原始矩陣運算過程於memo1 Memo2->Lines->Add("x" IntToStr(l) IntToStr(j) "=" FloatToStr(bas[l][j]));//輸出基本矩陣運算過程於memo2 } } } //矩陣運算結束 for(int i=0;i for(int j=0;j { StringGrid3->Cells[j 1][i 1]=FloatToStrF(bas[i][j],ffFixed,4,4); //輸出最後之反矩陣值 StringGrid2->Cells[j 1][i 1]=FloatToStr(x[i][j]); //測試原始矩陣是否已呈單位矩陣型式 } } //--------------------------------------------------------------------------- 編輯記錄
c0931121696 重新編輯於 2010-08-18 22:34:25, 註解 無‧
|
GrandRURU
站務副站長 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
http://www.wahas.com/archiver/?tid-729538.html
這邊有範例。 啊…什麼是反矩陣啊,有沒有簡單的介紹和實際應用的場合啊? ===================引 用 c0931121696 文 章=================== 如題:想請問大大們,我最近想寫一個反矩陣的程式,不知道該如何寫,是否可請大大指導,謝謝! |
istillloving
高階會員 發表:33 回覆:182 積分:183 註冊:2008-10-09 發送簡訊給我 |
定義: T*T^(-1)=單位矩陣
===================引 用 c0931121696 文 章=================== 如題:想請問大大們,我最近想寫一個反矩陣的程式,不知道該如何寫,是否可請大大指導,謝謝!
------
恩...
編輯記錄
istillloving 重新編輯於 2010-08-18 11:08:14, 註解 無‧
|
Ktop_Robot
站務副站長 發表:0 回覆:3511 積分:0 註冊:2007-04-17 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |