分享
 
 
 

[数值算法]求解线性方程组的LU分解法

王朝other·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

由于求解三解方程较易,所以,考虑将系数矩阵A分解成两个三角矩阵的乘积,

即: A=LU的形式

其中,L为下三解矩阵,U为上三解矩阵,则线性方程组:

Ax=b

可改写为 LUx=b

令 Ux=y

得 Ly=b

然后,用前代方法求Ly=b得列矩阵y,再用回代方法求Ux=b,得到的列矩阵x即为所求的线性方程组的解.

分解的细节请参考相应的数值算法书籍,这里就不做过细的解释了.

首先是一个对下三解矩阵的前代方法,很简单的:

void downTriangleMethod(Type** matrixArr,Type* bList,Type* xAnsList,int len)

{

int i=0,j=0;

double e=0.0001;/*precise controller*/

Type tmpSum;

Type* tmpBList;

/*---assertion---*/

assertF(matrixArr!=NULL,"in upAngleMethod,matrixArr is NULL\n");

assertF(bList!=NULL,"in upAngleMethod,bList is NULL\n");

assertF(xAnsList!=NULL,"in upAngleMethod,xAnsList is NULL\n");

/*mem apply*/

tmpBList=(Type*)malloc(sizeof(Type)*len);

for(i=0;i<len;i++)

tmpBList[i]=bList[i];

/*copy the data*/

for(i=0;i<len;i++)

assertF(fabs(matrixArr[i][i])>=e,"in upAngleMethod ,matrixArr[i][i] has value closed to 0\n");

/*data initialiate*/

tmpBList[0]=tmpBList[0]/matrixArr[0][0];/*Get the first answer*/

i=1;

/*Core part of the up triangle method*/

while(i<len)

{

tmpSum=0;

for(j=0;j<i;j++)

tmpSum+=matrixArr[i][j]*tmpBList[j];

tmpBList[i]=(tmpBList[i]-tmpSum)/matrixArr[i][i];

i++;

}

/*copy to answer*/

for(i=0;i<len;i++)

xAnsList[i]=tmpBList[i];

/*End of down triangle method*/

free(tmpBList);

}

/*The core part of the LUPation Method.*/

void LUPationMethod(Type** matrixArr,Type* bList,Type* xAnsList,int len)

{

/*Core think of this algorithm:

matrix_L*yAnsList=bList;

matrix_U*xAnsList=yAnsList;

*/

Type** matrix_L,** matrix_U;/*The core two matrix object of the LU method.*/

Type* yAnsList;

int i,j,k;/*iterator num*/

int i2,j2;

Type sum;/*the temp tween data*/

/*input assertion*/

assertF(matrixArr!=NULL,"in LUPationMethod,matrixArr is NULL\n");

assertF(bList!=NULL,"in LUPationMethod,bList is NULL\n");

assertF(xAnsList!=NULL,"in LUPationMethod,xAnsList is NULL\n");

/*memory apply*/

matrix_L=(Type**)malloc(sizeof(Type*)*len);

matrix_U=(Type**)malloc(sizeof(Type*)*len);

for(i=0;i<len;i++)

{

matrix_L[i]=(Type*)malloc(sizeof(Type)*len);

matrix_U[i]=(Type*)malloc(sizeof(Type)*len);

}

yAnsList=(Type*)malloc(sizeof(Type)*len);

/*==end of memory apply==*/

/*---assertion after apply the mem---*/

assertF(matrix_L!=NULL,"in LUPationMethod,matrix_L is NULL\n");

assertF(matrix_U!=NULL,"in LUPationMethod,matrix_U is NULL\n");

assertF(yAnsList!=NULL,"in LUPationMethod,yAnsList is NULL\n");

/*---end of assertion---*/

// printf("arr mem applyed\n");

/*----data initialization----*/

for(i=0;i<len;i++)

for(j=0;j<len;j++)

{

matrix_L[i][j]=0;

matrix_U[i][j]=0;

}

for(i=0;i<len;i++)

matrix_L[i][i]=1;

for(i=0;i<len;i++)

{

/*matrixU make*/

for(j=i;j<len;j++)

{

sum=0;

for(k=0;k<i;k++)

sum+=matrix_L[i][k]*matrix_U[k][j];

matrix_U[i][j]=matrixArr[i][j]-sum;

}

/*matrixL make*/

if(i<len-1)

{

j2=i;

for(i2=j2+1;i2<len;i2++)

{

sum=0;

for(k=0;k<j2;k++)

sum+=matrix_L[i2][k]*matrix_U[k][j2];

matrix_L[i2][j2]=(matrixArr[i2][j2]-sum)/matrix_U[j2][j2];

}

}

}

printf("matrix L:\n");

show2DArrFloat(matrix_L,len,len);

printf("matrix U:\n");

show2DArrFloat(matrix_U,len,len);

downTriangleMethod(matrix_L,bList,yAnsList,len);

upTriangleMethod(matrix_U,yAnsList,xAnsList,len);

/*memory free*/

for(i=0;i<len;i++)

{

free(matrix_L[i]);

free(matrix_U[i]);

}

free(matrix_L);

free(matrix_U);

free(yAnsList);

}

/*The matrix’s LUPation Method

This version is implemnted by EmilMatthew 05/8/19

You can use these code freely , but I'm not assure them could totally fit your needs.

*/

/*upTiangleMethod Algorithm test program*/

#include "Global.h"

#include "Ulti.h"

#include "MyAssert.h"

#include "Matrix.h"

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

char *inFileName="inputData.txt";

/*

input data specification

len,

a00,a01,...,a0n-1,b0;

.....

an-10,an-11,...,an-1n-1,bn-1;

*/

char *outFileName="outputData.txt";

#define DEBUG 1

void main(int argc,char* argv[])

{

FILE *inputFile;/*input file*/

FILE *outputFile;/*output file*/

double startTime,endTime,tweenTime;/*time callopsed info*/

/*The read in data*/

int len;

Type** matrixArr;

Type* bList,* xAnsList;

int i,j;/*iterator index*/

/*input file open*/

if(argc>1)strcpy(inFileName,argv[1]);

assertF((inputFile=fopen(inFileName,"rb"))!=NULL,"input file error");

printf("input file open success\n");

/*outpout file open*/

if(argc>2)strcpy(outFileName,argv[2]);

assertF((outputFile=fopen(outFileName,"wb"))!=NULL,"output file error");

printf("output file open success\n");

fscanf(inputFile,"%d,",&len);

printf("len is:%d\n",len);

/*Memory apply*/

matrixArr=(Type**)malloc(sizeof(Type*)*len);

for(i=0;i<len;i++)

matrixArr[i]=(Type*)malloc(sizeof(Type)*len);

bList=(Type*)malloc(sizeof(Type)*len);

xAnsList=(Type*)malloc(sizeof(Type)*len);

/*Read info data*/

for(i=0;i<len;i++)

{

for(j=0;j<len;j++)

fscanf(inputFile,"%f,",&matrixArr[i][j]);

fscanf(inputFile,"%f;",&bList[i]);

}

/*Check the input data*/

showArrListFloat(bList,0,len);

show2DArrFloat(matrixArr,len,len);

#if DEBUG

printf("\n*******start of test program******\n");

printf("now is runnig,please wait...\n");

startTime=(double)clock()/(double)CLOCKS_PER_SEC;

/******************Core program code*************/

LUPationMethod(matrixArr,bList,xAnsList,len);

printf("after the LUPationMethod:the ans x rows is:\n");

fprintf(outputFile,"after the LUPationMethod:the ans x rows is:(from x0 to xn-1)\r\n");

showArrListFloat(xAnsList,0,len);

outputListArrFloat(xAnsList,0,len,outputFile);

/******************End of Core program**********/

endTime=(double)clock()/(double)CLOCKS_PER_SEC;

tweenTime=endTime-startTime;/*Get the time collapsed*/

/*Time collapsed output*/

printf("the collapsed time in this algorithm implement is:%f\n",tweenTime);

fprintf(outputFile,"the collapsed time in this algorithm implement is:%f\r\n",tweenTime);

printf("\n*******end of test program******\n");

#endif

for(i=0;i<len;i++)

free(matrixArr[i]);

free(matrixArr);

free(xAnsList);

free(bList);

printf("program end successfully,\n you have to preess any key to clean the buffer area to output,otherwise,you wiil not get the total answer.\n");

getchar();/*Screen Delay Control*/

return;

}

测试结果:

test-1:

//input

3,

2,0,1,8;

0,4,6,12;

1,1,1,30;

//output

after the upTriangleMethod:the ans x rows is:(from x0 to xn-1)

15.500000,37.500000,-23.000000;

the collapsed time in this algorithm implement is:0.000000

test-2:

//input

2,

0.0001,1,1;

1,1,3;

由于部分数据在分解后接近0,导致无法测试。

test-3:

//input

3,

2,2,3,3;

4,7,7,1;

-2,4,5,-7;

//output

after the upTriangleMethod:the ans x rows is:(from x0 to xn-1)

2.000000,-2.000000,1.000000;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有