[数值算法]线性方程组的求解---平方根法及改进平方根法_EmilMatthew的博客-程序员秘密

技术标签: 算法  input  file  null  matrix  algorithm  

[数值算法]线性方程组的求解---平方根法及改进平方根法

                                                                                                         By EmilMatthew

                                                                                                                       05/9/10

平方根法主要用于求解对称正定矩阵方程:

首先要提到的是有个关于正定矩阵的定理,说的是若An阶地称正定矩阵,则存在一个实的非奇异下三角矩阵L,使A=LL’L’表示L的对称矩阵)

根据这个前提,在结合前面的LU分解算法,便有了这里的平方根算法:

 

平方根方法:

/*

squareRootMethod, coded by EmilMathew 05/9/10, you can modify and use these code as you wish , but there is no guarantee that it can fit all you need.

*/

void squareRootMethod(Type** inMatrixArr,Type* bList,Type* xAnsList,int size)

{

       /*Maths Reason:

       L*U=A*x=b

       When you meet a duiCheng and zheng ding matrix:

       it could be pation like this:

       l11                           l11  l21  ...  ln1

       l21  l22               *          l22  ...  ln2

       ....                                           ...  ...

       ln1  ln2  ...  lnn                           lnn

      

       i=j:

       lij=sqrt(aij-sigma_k1...j-1(ljk^2))

      

       i>j:

       lij=(aij-sigma_k1...j-1(lik*ljk))/ljj

      

       the steps below is very easy :

       L*y=b;

       U*x=y;

      

       Enjoy!:)

                                                                      by EmilMatthew

                                                                             05/9/10.

       */

       Type** l_Matrix,* yAnsList;

       Type tmpData;

       int i,j;

      

       /*pointer data assertion*/

       assertF(inMatrixArr!=NULL,"in squareRootMethod,matrixArr is NULL/n");

       assertF(bList!=NULL,"in squareRootMethod,bList is NULL/n");

       assertF(xAnsList!=NULL,"in squareRootMethod,xAnsList is NULL/n");   

      

       /*correct pass in matrix assertion*/

       assertF(duiChengMatrixCheck(inMatrixArr,size),"in squareRootMethod,the pass in matrix is not dui cheng/n");

      

       /*Mem Apply*/            

       listArrMemApply(&yAnsList,size);

       twoDArrMemApply(&l_Matrix,size,size);

      

       assertF(l_Matrix!=NULL,"in squareRootMethod,l_Matrix is null/n");

       assertF(yAnsList!=NULL,"in squareRootMethod,yAnsList is null/n");

      

       /*Core Program*/

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

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

              {

                     if(i==j)

                     {

                            tmpData=sumSomeRowPower(l_Matrix,j,0,j-1,2);

                     //     printf("tmpData:%f/n",tmpData);

                            l_Matrix[i][j]=(float)sqrt(inMatrixArr[i][i]-tmpData);

                     }

                     else

                     {

       l_Matrix[i][j]=(inMatrixArr[i][j]-sumTwoRowBy(l_Matrix,i,j,0,j-1))/l_Matrix[j][j];

                     }

              }

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

yAnsList[i]=(bList[i]-sumArr_JKByList_K(l_Matrix,yAnsList,i,0,i-1))/l_Matrix[i][i];

 

for(i=size-1;i>=0;i--)

xAnsList[i]=(yAnsList[i]-sumArr_KJByList_K(l_Matrix,xAnsList,i,i+1,size-1))/l_Matrix[i][i];

      

       twoDArrMemFree(&l_Matrix,size);

       free(yAnsList);

}

 

平方根算法的计算量约为普通三角分解算法的一半,但由于这里要用到开平方,效率不是很高,所以,便有了为效率而存在的改进版平方根算法:

改进的平方根算法:

/*

enhancedSquareRootMethod, coded by EmilMathew 05/9/10, you can modify and use these code as you wish , but there is no guarantee that it can fit all you need.

*/

 

如下:

void enhancedSquareRootMethod(Type** inMatrixArr,Type* bList,Type* xAnsList,int size)

{

       Type** l_Matrix,** t_Matrix;

       Type* yAnsList,* dList;

 

       int i,j;

      

       /*pointer data assertion*/

       assertF(inMatrixArr!=NULL,"in enhancedSquareRootMethod,matrixArr is NULL/n");

       assertF(bList!=NULL,"in enhancedSquareRootMethod,bList is NULL/n");

       assertF(xAnsList!=NULL,"in enhancedSquareRootMethod,xAnsList is NULL/n");   

      

       /*correct pass in matrix assertion*/

       assertF(duiChengMatrixCheck(inMatrixArr,size),"in enhancedSquareRootMethod,the pass in matrix is not dui cheng/n");

      

       /*Mem Apply*/            

       listArrMemApply(&yAnsList,size);

       listArrMemApply(&dList,size);

       twoDArrMemApply(&l_Matrix,size,size);

       twoDArrMemApply(&t_Matrix,size,size);

      

       assertF(t_Matrix!=NULL,"in enhancedSquareRootMethod,t_Matrix is null/n");

       assertF(l_Matrix!=NULL,"in enhancedSquareRootMethod,l_Matrix is null/n");

       assertF(yAnsList!=NULL,"in enhancedSquareRootMethod,yAnsList is null/n");

      

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

              l_Matrix[i][i]=1;

      

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

       {

              dList[j]=inMatrixArr[j][j]-sumArr1_IKByArr2_JK(t_Matrix,l_Matrix,j,j,0,j-1);

              for(i=j+1;i<size;i++)

              {

                     t_Matrix[i][j]=inMatrixArr[i][j]-sumArr1_IKByArr2_JK(inMatrixArr,l_Matrix,i,j,0,j-1);

                     l_Matrix[i][j]=t_Matrix[i][j]/dList[j];

              }

       }

      

      

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

              yAnsList[i]=bList[i]-sumArr_JKByList_K(l_Matrix,yAnsList,i,0,i-1);

 

for(i=size-1;i>=0;i--)

xAnsList[i]=yAnsList[i]/dList[i]-sumArr_KJByList_K(l_Matrix,xAnsList,i,i+1,size-1);

      

       /*mem free*/

       twoDArrMemFree(&t_Matrix,size);

       twoDArrMemFree(&l_Matrix,size);

       free(yAnsList);

       free(dList);

}

 

测试程序:

/*Square Method  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,methodIndex;

       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,"size=%d;/r/n",&len);

       fscanf(inputFile,"method=%d;/r/n",&methodIndex);

      

       /*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*************/

              switch(methodIndex)

              {

              case 1:

                            enhancedSquareRootMethod(matrixArr,bList,xAnsList,len);

                            printf("after the enhancedSquareRootMethod:the ans x rows is:/n");

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

                            break;

 

              case 2:

                    

                            squareRootMethod(matrixArr,bList,xAnsList,len);

                            printf("after the SquartRootPationMethod:the ans x rows is:/n");

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

                            break;

                    

              default:

                            printf("input method index error/n");

                            break;

              }

              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;

}

 

测试结果:

平方根法:

test1:

输入:

size=3;

5,-4,1,1;

-4,6,-4,2;

1,-4,6,-3;

输出:

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

   1.00000    1.00000    0.00000

 

改进平方根法:

test1:

输入:

size=3;

method=1;

5,-4,1,1;

-4,6,-4,2;

1,-4,6,-3;

 

输出:

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

   1.00000    1.00000    0.00000

 

Test2:

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

   2.00000    1.00000   -1.00000

 

网上关于这个主题的相关参考:

http://jpkc.ecnu.edu.cn/gdds/xsxz/ZhangGengYun.htm

http://www.ascc.net/pd-man/linpack/node14.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/EmilMatthew/article/details/477089

智能推荐

springboot使用JSR303对提交的参数进行校验包括分组校验,自定义校验_垂钓的小鱼1的博客-程序员秘密

一、如果我们想要对传入的参数进行校验,只需要在接收前端传入的实体类上使用注解即可/** * 品牌 * * @date 2019-10-01 21:08:49 */@[email protected]("pms_brand")public class BrandEntity implements Serializable { private static final long serialVersionUID = 1L; /** * 品牌id */ @NotNull(message

Vue项目webpack打包部署到Tomcat,刷新报404错_vue 打包 web-inf_smallsnail-wh的博客-程序员秘密

遇到的问题使用webpack打包vue后,将打包好的文件,发布到Tomcat上,访问成功,但是刷新后页面报404错。在网上查找了一下,原来是HTML5 History 模式引发的问题,具体为什么,vue官方已经给出了解释,你可以看https://router.vuejs.org/zh-cn/essentials/history-mode.html但是看完问题又来了,官方给出的解决方案中没有说tomc

2019年,异质图神经网络领域有哪些值得读的顶会论文?_PaperWeekly的博客-程序员秘密

本文主要梳理了 2019 年各大顶会上关于异质图神经网络的论文,包括算法研究及应用研究。同时,作者也整理了相关大牛老师/论文/资料/数据集供大家学习。作者丨纪厚业学校丨北京邮电大学博士生...

PageX、clientX、screenX、offsetX、layerX的区别_·Q·的博客-程序员秘密

一.PageX和clientXPageX:鼠标在页面上的位置,从页面左上角开始,即是以页面为参考点,不随滑动条移动而变化clientX:鼠标在页面上可视区域的位置,从浏览器可视区域左上角开始,即是以浏览器滑动条此刻的滑动到的位置为参考点,随滑动条移而变化.二.screenXscreenX:鼠标在屏幕上的位置,从屏幕左上角开始,这个没有任何争议三.offsetX和layerXoffsetX:IE特有,鼠标相比较于触发事件的元素的位置,以元素盒子模型的内容区域的左上角为参考点,如果有bode

ESXI删除状态为“无效”的虚拟机_esxi注册虚拟机后状态显示无效_酒癫儿的博客-程序员秘密

方法/步骤删除ESXI系统内标识为无效的虚拟机:SSH登录ESXI服务器,在在 /etc/vmware/hostd目录下输入vim-cmd vmsvc/getallvms列表中显示Skipping invalid VM '1' , Skipping为已失效的虚拟机,编号1为该虚拟机的编号。使用putty工具ssh登录上去,使用以下命令取消注册虚拟机vim-cmd vmsvc/unr...

rk3399 rv1126上使用wk2124_rk3399 wk2124_xian0gang的博客-程序员秘密

wk2124是一个通过spi扩充为4路串口的模块,在嵌入式设备上能充分利用资源,我在rk3399和rv1126上移植了wk2124,比较容易,我想它在其他linux平台使用起来也是可以的。设备树添加&amp;spi0 { status = "okay"; max-freq = &lt;48000000&gt;; /* spi internal clk, don't modify */ spi[email protected] { compatible = "wkmic,wk2124spi"; reg = &

随便推点

Apache OFBIZ快速上手(一)--简单例子__Emily的博客-程序员秘密

本篇文章主要介绍简单例子,OFBIZ的其他介绍就先不说了,放在后面的博文中。1、目录结构                               说明:在hot-deploy目录下建立文件夹learning,为例。注意先把文件扔到hot-deploy目录下再启动服务器。2、创建ofbiz-component.xml文件创建此文件,负责让OFBIZ知道learni

PHP函数-Improved MySQL 函数_weixin_30788239的博客-程序员秘密

mysqli_affected_rows 返回最后一次执行 select,insert,delete 或 update 语句所影响的记录数,如果查询执行失败则返回-1mysqli_autocommit 用于打开或关闭查询的自动提交mysqli_change_user 更改已经与 MySQL 数据库服务器建立的连接mysqli_character_set_name ...

open-messaging使用实例_csid_502的博客-程序员秘密

为什么80%的码农都做不了架构师?&gt;&gt;&gt; ...

Kinect开发学习笔记之(四)提取颜色数据并用OpenCV显示_颜色标度图提取数据_zouxy09的博客-程序员秘密

Kinect开发学习笔记之(四)提取颜色数据并用OpenCV显示[email protected]://blog.csdn.net/zouxy09 我的Kinect开发平台是:Win7 x86 + VS2010 + Kinect for Windows SDK v1.6 + OpenCV2.3.0开发环境的搭建见上一文: http://blog.csdn.net/zo

将kubernetes跑在本地LXD容器中(by quqi99)_quqi99的博客-程序员秘密

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99)问题本文将kubernetest跑在本地LXD容器中。Kubernetes是什么Kubernetes是什么,见我的博客。安装LXD如何安装LXD,见我的博客。 这篇文章和之前的在LXD上运行容器化的OpenStack类似,见我的博客。LXD上安装Kube

双端队列BFS_迷亭1213的博客-程序员秘密

问题模型1-1: 如果在一张图上(有向图和无向图),边权只可能是1或0,现在我们想从某个节点(假设为s)到另一个节点(假设为t),怎样才能使得路径上的权值和最大?这是最短路径问题中的特例,因为其边权只可能是0或1;同时这也是许多问题的抽象形式,很多问题可以抽象为上述模型1-1。我们的目标就是找到一个尽可能高效的算法解决上述问题模型。最短路径算法这是很容易想到的图论算法,解决此题的效率为O...

推荐文章

热门文章

相关标签