PLU Decomposition,pludecomposition

來源:互聯網
上載者:User

PLU Decomposition,pludecomposition

PLU分解的好處是,可以將Ax=b的矩陣,轉換成Ly=b, Ux = y

的形式,當我們改變係數矩陣b時,此時由於矩陣L和U均是固定

的,所以總能高效的求出矩陣的解。

// LU.cpp : Defines the entry point for the console application.///*************************************************   Author:     JohnsonDu*   From:       Institute of Computing Technology*               University of Chinese Academy of Science*   Time:       2014-10-7*   Content:    PLU decomposition*************************************************/#include "stdafx.h"#define MAXN 5005#define eps 1e-9         // 精度int n, m;double mat[MAXN][MAXN];  // 輸入矩陣double matL[MAXN][MAXN]; // 矩陣Ldouble matU[MAXN][MAXN]; // 矩陣Uint matP[MAXN][MAXN];    // 矩陣Pint seq[MAXN];           // 記錄行變換//double vecB[MAXN];//double vecY[MAXN];//double vecX[MAXN];//double matPb[MAXN];void menu(){printf("----------------PLU Factorization---------------\n");printf("|         Please follow the instruction        |\n");printf("|         to determine the LU decomposition    |\n");printf("|         PA = LU                              |\n");printf("------------------------------------------------\n\n");}void initLMatrix(){memset(matU, 0, sizeof(matU));memset(matL, 0, sizeof(matL));memset(matP, 0, sizeof(matP));}void padLMatrix(){for(int i = 0; i < n; i ++)matL[i][i] = 1.0;}inline double Abs(double x){return x < 0 ? -x : x;}void displayLU(){// 輸出矩陣Lprintf("\n----------------------\n");printf("Matrix L follows: \n");for(int i = 0; i < n; i ++){for(int j = 0; j < (n < m ? n : m); j ++)printf("%.3f  ", matL[i][j]);printf("\n");}// 輸出矩陣Uprintf("\nMatrix U follows: \n");for(int i = 0; i < (n < m ? n : m); i ++){for(int j = 0; j < m; j ++)printf("%.3f  ", matU[i][j]);printf("\n");}// 輸出矩陣Pprintf("\nMatrix P follows: \n");for(int i = 0; i < n; i ++){for(int j = 0; j < n; j ++)printf("%d  ", matP[i][j]);printf("\n");}printf("----------------------\n");}/*// 輸出LU的過程及最終解void displaySolution(){// 輸出矩陣Pbprintf("\nMatrix Pb follows: \n");for(int i = 0; i < n; i ++){printf("%.3f\n", matPb[i]);}// 輸出向量yprintf("\nVector Y follows: \n");for(int i = 0; i < n; i ++){printf("%.3f\n", vecY[i]);}printf("\n");// 輸出解向量xprintf("\Vector X follows: \n");for(int i = 0; i < n; i ++){printf("%.3f\n", vecX[i]);}printf("\n");}*/// 交換元素inline void swap(int &a, int &b){int t = a;a = b;b = t;}// 高斯消元部分void gauss(){int i;int col;int max_r;  col = 0;    //處理的當前列// 從第一行開始進行消元// k為處理的當前行for(int k = 0; k < n && col < min(n, m); k ++, col ++){// 尋找當前col列的絕對值最大值max_r = k;for(i = k + 1; i < n; i ++)if(Abs(mat[i][col]) > Abs(mat[max_r][col]))max_r = i;// 進行行交換if(max_r != k){for(int j = col; j < m; j ++)swap(mat[k][j], mat[max_r][j]);swap(seq[k], seq[max_r]);for(int j = 0; j < n; j ++)swap(matL[k][j], matL[max_r][j]);}// 當前主元為零, 繼續if(Abs(mat[k][col]) < eps){continue;}// 消元部分,並獲得L矩陣for(int i = k + 1; i < n; i ++){double t = mat[i][col] / mat[k][col];matL[i][col] = t;for(int j = col; j < m; j ++)mat[i][j] -= t * mat[k][j];}}// 為矩陣U進行賦值for(int i = 0; i < n; i ++)for(int j = 0; j < m; j ++)matU[i][j] = mat[i][j];// 產生矩陣Pfor(int i = 0; i < n; i ++)  matP[i][seq[i]] = 1.0;// 為矩陣L添加對角線元素padLMatrix();}/*// 計算Pb的值void calcPb(){for(int i = 0; i < n; i ++)matPb[i] = 0.0;//cout << "-----------" << endl;for(int i = 0; i < n; i ++){double t = 0.0;for(int j = 0; j < n; j ++){t = t + 1.0 * matP[i][j] * vecB[j];//cout << t << endl;//cout << matP[i][j] * vecB[j] << "---" << endl;}matPb[i] = t;//cout << matPb[i] << endl;}}// 計算Ly = Pb, y向量void calcY(){vecY[0] = matPb[0];for(int i = 1; i < n; i ++){double t = 0.0;for(int j = 0; j < i; j ++) t += vecY[j] * matL[i][j];vecY[i] = matPb[i] - t;}}// 計算Ux = y, y向量void calcX(){vecX[n-1] = vecY[n-1] / matU[n-1][n-1];for(int i = n-2; i >= 0; i --){double t = 0.0;for(int j = n-1; j > i; j --) t += vecX[j] * matU[i][j];vecX[i] = (vecY[i] - t) / matU[i][i];}}*/int _tmain(int argc, _TCHAR* argv[]){menu();while(true){printf("Please input the matrix's dimension n & m: ");// 輸入矩陣的行n和列mscanf("%d%d", &n, &m);  printf("Please input the matrix: \n");// 輸入矩陣for(int i = 0; i < n; i ++){for(int j = 0; j < m; j ++)cin >> mat[i][j];seq[i] = i;}// 初始化為0initLMatrix();// 高斯消元gauss();// 輸出P, L, U矩陣displayLU();system("pause");system("cls");menu();/*        //此處是輸入b,求取x, y 和 pbwhile(true){printf("please input vector b(whose length equals to %d): \n", n);for(int i = 0; i < n; i ++) cin >> vecB[i];calcPb();calcY();calcX();displaySolution();}*/}return 0;}
其中stdafx.h的標頭檔:

// stdafx.h : include file for standard system include files,// or project specific include files that are used frequently, but// are changed infrequently//#pragma once#define  _CRT_SECURE_NO_WARNINGS#include "targetver.h"#include <stdio.h>#include <tchar.h>#include <iostream>using namespace std;






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.