点击这里可以跳转至

【1】矩阵汇总:http://www.cnblogs.com/HongYi-Liang/p/7287369.html

【2】矩阵生成:http://www.cnblogs.com/HongYi-Liang/p/7275278.html

【3】矩阵加减:http://www.cnblogs.com/HongYi-Liang/p/7287403.html

【4】矩阵点乘:http://www.cnblogs.com/HongYi-Liang/p/7287324.html

【5】矩阵化简:现在的位置

(待续)

...


C++语言:

高斯消元法:

继续使用这个矩阵

当我们使用高斯消元(无回代)化简这个矩阵,是这样算的:

上述过程归纳为:

  1. 找到第一行行的主元(第一行第一个数:1)
  2. 消除第而三行的的第一个数(r2-2*r1;r3-4*r1)
  3. 找到第二行的主元(第二行第二个数:-2)
  4. 消除第三行的第二个数(r3-3/2*r2)

可以发现实际上是1和2两个步骤的循环,所以写成循环的形式

  • 从第一行开始到最后一行
  1. 找主元:找出第i的主元(第i行第i个数)
  2. 消元:消除下面所有行的第i个数(下面每一行减去x倍的第一行来消除第i列)

到目前为止,基本达到消元的目的了,但是有一些小小的瑕疵

我们可能碰到一个这样矩阵,有一行全是0,例如这个:

那么我们在步骤1中搜索到主元为0的话,0的任意倍数都是0,会导致第2步无法进行。所以我们需要添加换行的操作,计算方法为:

所以我们把代码逻辑修改成这样:

  • 从第一行开始到最后一行
  1. 找主元:找出第i的主元(第i行第i个数),若主元为0,把第i行向下换行,直到找到有主元的行。若找不到主元,就开始找下一个
  2. 消元:消除下面所有行的第i个数(下面每一行减去x倍的第一行来消除第i列)

下面就是高斯消元的主程序:

template <typename T>
bool Matrix<T>::GaussianElimination()
{
Matrix<T> outputMatrix = *this; /*Gaussian elmiation*/
for(int k=;k<outputMatrix.m_iRows;k++)
{
/*if all the pivot have been found*/
if(k>=m_iColumns)
{
break;
} /*exchange rows downward to find the row's pivot*/
for(int i=k+;i<outputMatrix.m_iRows;i++)
{
/*pivot is non-zero*/
if(outputMatrix.m_vecMatrix[k][k] != )
{
//T temp = outputMatrix.m_vecMatrix[0][0];
break;
}
else
{
if(i < outputMatrix.m_iRows)
{
outputMatrix.exchangeRows(k,i);
}
}
} /*if there is no pivot in this row*/
if(outputMatrix.m_vecMatrix[k][k] == )
{
break;
} /*elimination:The rows below pivot row subtract times of pivot row*/
for(int i=k+;i<outputMatrix.m_iRows;i++)
{
double RowsfirstData = outputMatrix.m_vecMatrix[i][k]/outputMatrix.m_vecMatrix[k][k];//Save the first data of next(k+1) rows
if(RowsfirstData != )
{
outputMatrix.m_vecMatrix[i][k]=;
for(int j=k+;j<outputMatrix.m_iColumns;j++)
{
outputMatrix.m_vecMatrix[i][j] -= RowsfirstData*outputMatrix.m_vecMatrix[k][j] ;
}
}
}
} *this = outputMatrix;
return true;
}

高斯-若尔当法

若尔当在高斯消元的基础上加上了回代过程,把矩阵化简成行最简式。我们在高斯消元的基础上加上和回代,方法跟高斯消元相反,用上面的行减下面的行,这里就不详细描述(展开查看代码)

rref()//化简矩阵成行最简

template <typename T>
bool Matrix<T>::rref()
{
Matrix<T> outputMatrix = *this;
int rank=;//the rank of the matrix, how many columns's pivot will it has(-1) /*Gaussian elmiation*/
for(int k=;k<outputMatrix.m_iRows;k++)
{
/*if all the pivot elem have been found*/
if(k>=m_iColumns)
{
break;
} /*exchange rows downward to find the pivot row*/
for(int i=k+;i<outputMatrix.m_iRows;i++)
{
/*pivot is non-zero*/
if(outputMatrix.m_vecMatrix[k][k] != )
{
//T temp = outputMatrix.m_vecMatrix[0][0];
rank++;
break;
}
else
{
if(i < outputMatrix.m_iRows)
{
outputMatrix.exchangeRows(k,i);
}
}
} /*if there is no pivot in this row*/
if(outputMatrix.m_vecMatrix[k][k] == )
{
break;
} /*elimination:The rows below pivot row subtract times of pivot row*/
for(int i=k+;i<outputMatrix.m_iRows;i++)
{
double RowsfirstData = outputMatrix.m_vecMatrix[i][k]/outputMatrix.m_vecMatrix[k][k];//Save the first data of next(k+1) rows
if(RowsfirstData != )
{
outputMatrix.m_vecMatrix[i][k]=;
for(int j=k+;j<outputMatrix.m_iColumns;j++)
{
outputMatrix.m_vecMatrix[i][j] -= RowsfirstData*outputMatrix.m_vecMatrix[k][j] ;
}
}
}
} /*normalizing:set all pivots to 1*/
for(int i=;i<outputMatrix.m_iRows;i++)
{
for(int j=;j<outputMatrix.m_iColumns;j++)
{
if(outputMatrix.m_vecMatrix[i][j] != )//pivot has been foound
{
double pivot = outputMatrix.m_vecMatrix[i][j];//get pivot
for(int k=i;k<outputMatrix.m_iColumns;k++)
{
outputMatrix.m_vecMatrix[i][k] /=pivot;
}
break;
}
}
} /*Back substitution*/
for(int i = rank;i>=;i--)
{
/*find a first non-zero elem (It is pivot)*/
for(int j=;j<outputMatrix.m_iColumns;j++)
{
double times=;
if(outputMatrix.m_vecMatrix[i][j] !=)//pivot found
{
for(int l=i-;l>=;l--)
{
times = outputMatrix.m_vecMatrix[l][j]/outputMatrix.m_vecMatrix[i][j];
for(int k=j;k<outputMatrix.m_iColumns;k++)//tims of this row subtract by each columns in upon row
{
outputMatrix.m_vecMatrix[l][k] -= times*outputMatrix.m_vecMatrix[i][k];
}
}
break;
}
}
} *this = outputMatrix;
return true;
}

rrefmovie()//化简矩阵成行最简,并打印过程

template <typename T>
bool Matrix<T>::rrefmovie()
{
Matrix<T> outputMatrix = *this;
int rank=;//the rank of the matrix, how many columns's pivot will it has(-1) /*Gauss elmiation*/
cout<<"Gauss elimination:"<<endl;
outputMatrix.printfAll();
for(int k=;k<outputMatrix.m_iRows;k++)
{
/*If all the pivot elem have been found*/
if(k>=m_iColumns)
{
break;
} /*Exchange rows downward to find the pivot row*/
for(int i=k+;i<outputMatrix.m_iRows;i++)
{
/*Pivot is non-zero*/
if(outputMatrix.m_vecMatrix[k][k] != )
{
rank++;
break;
}
else
{
if(i < outputMatrix.m_iRows)
{
outputMatrix.exchangeRows(k,i);
}
}
if(k!=i)
{
cout<<"row"<<k+<<" exchange row"<<i+<<endl;//Debug
outputMatrix.printfAll();
}
} /*If there is no pivot in this row*/
if(outputMatrix.m_vecMatrix[k][k] == )
{
break;
} /*Elimination:The rows below pivot row subtract times of pivot row*/
for(int i=k+;i<outputMatrix.m_iRows;i++)
{
double RowsfirstData = outputMatrix.m_vecMatrix[i][k]/outputMatrix.m_vecMatrix[k][k];//Save the first data of next(k+1) rows
if(RowsfirstData != )
{
outputMatrix.m_vecMatrix[i][k]=;
for(int j=k+;j<outputMatrix.m_iColumns;j++)
{
outputMatrix.m_vecMatrix[i][j] -= RowsfirstData*outputMatrix.m_vecMatrix[k][j] ;
}
}
cout<<"row"<<i+<<" - "<<RowsfirstData<<"*"<<"row"<<k+<<endl;//Debug
outputMatrix.printfAll();
}
} /*Normalizing:set all rows pivot to 1*/
for(int i=;i<outputMatrix.m_iRows;i++)
{
for(int j=;j<outputMatrix.m_iColumns;j++)
{
if(outputMatrix.m_vecMatrix[i][j] != )//pivot has been foound
{
double pivot = outputMatrix.m_vecMatrix[i][j];//get pivot
for(int k=i;k<outputMatrix.m_iColumns;k++)
{
outputMatrix.m_vecMatrix[i][k] /=pivot;
}
cout<<"row"<<i+<<" / "<<pivot<<endl;//Debug
outputMatrix.printfAll();//Debug
break;
}
}
} /*Back substitution*/
cout<<"Back substitution:"<<endl;
for(int i = rank;i>=;i--)
{
/*find a first non-zero elem (It is pivot)*/
for(int j=;j<outputMatrix.m_iColumns;j++)
{
double times=;
if(outputMatrix.m_vecMatrix[i][j] !=)//pivot found
{
for(int l=i-;l>=;l--)
{
times = outputMatrix.m_vecMatrix[l][j]/outputMatrix.m_vecMatrix[i][j];
for(int k=j;k<outputMatrix.m_iColumns;k++)//tims of this row subtract by each columns in upon row
{
outputMatrix.m_vecMatrix[l][k] -= times*outputMatrix.m_vecMatrix[i][k];
}
cout<<"row"<<l+<<" - "<<times<<"*"<<"row"<<i+<<endl;
outputMatrix.printfAll();
}
break;
}
}
} *this = outputMatrix;
return true;
}

使用我们开始的矩阵测试:

    Matrix<double> matrix(,);
matrix.setSpecifiedElem(,,);
matrix.setSpecifiedElem(,,);
matrix.setSpecifiedElem(,,);
matrix.setSpecifiedElem(,,);
matrix.setSpecifiedElem(,,);
matrix.setSpecifiedElem(,,);
matrix.setSpecifiedElem(,,);
matrix.setSpecifiedElem(,,);
matrix.setSpecifiedElem(,,);
matrix.printfAll(); matrix.rrefmovie();
matrix.printfAll();
system("pause");

结果:

线性代数-矩阵-【5】矩阵化简 C和C++实现的更多相关文章

  1. HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识

    求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...

  2. Google Code Jam 2008 Round 1A C Numbers(矩阵快速幂+化简方程,好题)

    Problem C. Numbers This contest is open for practice. You can try every problem as many times as you ...

  3. HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简

    http://acm.hdu.edu.cn/showproblem.php?pid=4565 首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数). 因为 ...

  4. 【BZOJ1048】分割矩阵(记忆化搜索,动态规划)

    [BZOJ1048]分割矩阵(记忆化搜索,动态规划) 题面 BZOJ 洛谷 题解 一个很简单的\(dp\),写成记忆化搜索的形式的挺不错的. #include<iostream> #inc ...

  5. 【线性代数】2-4:矩阵操作(Matrix Operations)

    title: [线性代数]2-4:矩阵操作(Matrix Operations) toc: true categories: Mathematic Linear Algebra date: 2017- ...

  6. 《Linear Algebra and Its Application》-chaper1-行化简法解决线性方程组

    在实际生产生活中,需要我们解大量的线性方程组,例如是有探测.线性规划.电路等,这里我们便从理论角度建立一套解决线性方程组的体系. 线性方程组: 形如下面形式的方程组称为线性方程组. 回想起解决二元线性 ...

  7. 【11.5校内测试】【倒计时5天】【DP】【二分+贪心check】【推式子化简+线段树】

    Solution 非常巧妙的建立DP方程. 据dalao们说题目明显暗示根号复杂度??(反正我是没看出来 因为每次分的块大小一定不超过$\sqrt n$,要不然直接每个位置开一个块答案都才为$n$. ...

  8. hdu 1588 Gauss Fibonacci(矩阵嵌矩阵)

    题目大意: 求出斐波那契中的 第 k*i+b 项的和. 思路分析: 定义斐波那契数列的矩阵 f(n)为斐波那契第n项 F(n) = f(n+1) f(n) 那么能够知道矩阵 A = 1 1 1  0 ...

  9. DFA与NFA的等价性,DFA化简

    等价性 对于每个NFA M存在一个DFA M',使得L(M)=L(M')--------等价性证明,NFA的确定化 假定NFA M=<S, Σ, δ, S 0 , F>,我们对M的状态转换 ...

随机推荐

  1. 使用Spring boot + jQuery上传文件(kotlin)

    文件上传也是常见的功能,趁着周末,用Spring boot来实现一遍. 前端部分 前端使用jQuery,这部分并不复杂,jQuery可以读取表单内的文件,这里可以通过formdata对象来组装键值对, ...

  2. Spring定时器实现(二)

    Spring结合quarzt可以实现更复杂的定时器,现做简单介绍相关配置: <?xml version="1.0" encoding="UTF-8"?&g ...

  3. JavaScript 学习推荐

    主要是个人的学习网站,书籍推荐,还有个人学习经历,以及一些学习经验或技巧 JavaScript学习网站推荐 如果想快速入门,这些是很推荐的网站      快速入门,很快能让你了解前端,有什么,做什么, ...

  4. 51nod 1008 N的阶乘 mod P

    输入N和P(P为质数),求N! Mod P = ? (Mod 就是求模 %)   例如:n = 10, P = 11,10! = 3628800 3628800 % 11 = 10   Input 两 ...

  5. 29. leetcode 167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...

  6. Django 1.9 admin 使用suit 小记

    最近项目做到了后台管理的部分.Django虽然提供了后台管理,但是界面不咋好看.所以我使用了suit.官网http://djangosuit.com/ 步骤: 1,安装suit 项目settings. ...

  7. Python [目录]

    [第一篇] Python 发展历史 [第二篇] Python 优缺点 [第三篇] Python 应用场景 [第四篇] Python Hello World程序 [第五篇] Python 基础知识 [第 ...

  8. Unity 发布的 WenGL 使用SendMessage传递多个参数

    如果要实现Unity与浏览器的数据交互一般都会采用两种方式 方法一: Application.ExternalCall("SayHello","helloworld&qu ...

  9. CronJobs

    一.概念     CronJobs提供了在特定的时间或者间隔内处理业务逻辑的方法.一般创建一个Cronjob有两种方式,第一种是定义Java类,由Hybris生成脚本并加入数据库.第二种是直接编写gr ...

  10. linux文件系统目录解析

    Linux下的文件系统为树形结构,入口为/ 树形结构下的文件目录: 无论哪个版本的Linux系统,都有这些目录,这些目录应该是标准的.各个Linux发行版本会存在一些小小的差异,但总体来说,还是大体差 ...