Gauss.java

  1. package Gauss;
  2. /**
  3. * @description TODO 父类,包含高斯列主元消去法和全主元消去法的共有属性和方法
  4. * @author PengHao
  5. * @date 2018年12月1日 上午9:44:40
  6. */
  7. public class Gauss {
  8. protected double[][] augmentedMatrix; // 增广矩阵
  9. protected int n = 0; // n阶方阵
  10. protected double[] root; // 方程组的根
  11. protected int solution = 1; // 方程组解的个数,1唯一解,0无解,-1无穷解
  12. /**
  13. * @description TODO 构造方法,初始化各个属性
  14. * @date 2018年12月1日 下午12:04:44
  15. * @param A 增广矩阵
  16. */
  17. public Gauss(double[][] A) {
  18. this.n = A.length; // 方阵的阶数
  19. this.augmentedMatrix = new double[n][n + 1]; // 申请内存
  20. for (int i = 0; i < this.n; i++) {
  21. System.arraycopy(A[i], 0, this.augmentedMatrix[i], 0, this.n + 1); // 拷贝A的第i行到augmentedMatrix的第i行,每行n+1个数据
  22. }
  23. }
  24. /**
  25. * @description TODO 将增广矩阵的两行互换,注意不是整行
  26. * @date 2018年12月1日 下午1:33:45
  27. * @param rowSmall 要交换的较小的行下标
  28. * @param rowBig 要交换的较大的行下标
  29. */
  30. protected void rowSwap(int rowSmall, int rowBig) {
  31. for(int j = rowSmall; j <= this.n; j++) { // 将这两行的第rowSmall列至第n列交换
  32. this.valueSwap(rowSmall, j, rowBig, j);
  33. }
  34. }
  35. /**
  36. * @description TODO 将增广矩阵的值交换
  37. * @date 2018年12月1日 下午1:31:05
  38. * @param rowOne 第1个值的行下标
  39. * @param colOne 第1个值的列下标
  40. * @param rowTwo 第2个值的行下标
  41. * @param colTwo 第2个值的列下标
  42. */
  43. protected void valueSwap(int rowOne, int colOne, int rowTwo, int colTwo) {
  44. double temp = this.augmentedMatrix[rowOne][colOne];
  45. this.augmentedMatrix[rowOne][colOne] = this.augmentedMatrix[rowTwo][colTwo];
  46. this.augmentedMatrix[rowTwo][colTwo] = temp;
  47. }
  48. /**
  49. * @description TODO 消元过程
  50. * @date 2018年12月1日 下午1:48:45
  51. * @param k 消去第k行第k列以下的元素
  52. */
  53. protected void elimination(int k) {
  54. double temp; // 记录第k列的第i行除以第k行的数,即倍数关系
  55. for(int i = k + 1; i < this.n; i++) {
  56. temp = this.augmentedMatrix[i][k] / this.augmentedMatrix[k][k];
  57. for(int j = k + 1; j <= this.n; j++) {
  58. this.augmentedMatrix[i][j] -= this.augmentedMatrix[k][j] * temp; // 第i行第j列等于它减去第k行第j列乘以除数temp
  59. }
  60. }
  61. }
  62. /**
  63. * @description TODO 计算并设置方程组的解的状态
  64. * @date 2018年12月1日 下午2:24:46
  65. */
  66. protected void setSolution() {
  67. int rankOfC = this.n; // 系数矩阵的秩
  68. int rankOfA = this.n; // 增广矩阵的秩
  69. boolean zero = false; // 系数矩阵的当前行是否全为0
  70. for(int i = this.n - 1; i >= 0; i--) { // n行
  71. zero = true; // 初始化全为0
  72. for(int j = i; j < this.n; j++) { // 遍历第i行的第i列至第n-1列
  73. if(0 != this.augmentedMatrix[i][j]) { // [i][j]不等于0
  74. zero = false; // 不全为0
  75. break; // 退出遍历
  76. }
  77. }
  78. if(true == zero) { // 如果全为0
  79. rankOfC--; // 系数矩阵的秩减1
  80. if(0 == this.augmentedMatrix[i][n]) { // 如果常数矩阵的第i行为0
  81. rankOfA--; //增广矩阵的秩减1
  82. }
  83. } else { // 不全为0
  84. break; // 退出求秩
  85. }
  86. }
  87. if(rankOfC < rankOfA) { // 系数矩阵的秩小于增广矩阵的秩
  88. this.solution = 0; // 无解
  89. } else if(rankOfC == rankOfA && rankOfC < this.n) { // 系数矩阵的秩等于增广矩阵的秩
  90. this.solution = -1; // 无穷解
  91. }
  92. }
  93. }

ColumnPivot.java

  1. package Gauss;
  2. /**
  3. * @description TODO 列主元消去法
  4. * @author PengHao
  5. * @date 2018年12月1日 下午12:15:15
  6. */
  7. public class ColumnPivot extends Gauss {
  8. /**
  9. * @description TODO 列主元的构造方法,初始化父类
  10. * @date 2018年12月1日 下午12:18:07
  11. * @param A
  12. */
  13. public ColumnPivot(double[][] A) {
  14. super(A); // 显式调用父类的构造方法
  15. }
  16. /**
  17. * @description TODO 计算方程组的根并返回
  18. * @date 2018年12月1日 下午12:38:48
  19. * @return 方程组的根
  20. */
  21. public double[] getRoot() {
  22. for(int k = 0; k < this.n - 1; k++) { // n阶循环n-1次
  23. this.columnPivoting(k); // 在第k列选择主元
  24. this.elimination(k); // 消元过程
  25. }
  26. this.setSolution(); // 计算解的个数
  27. if(1 == this.solution) { // 唯一解
  28. this.backSubstitution(); // 回代求根
  29. } else if(0 == this.solution) { // 无解
  30. System.out.println("方程组无解!!!");
  31. System.exit(0); // 退出程序
  32. } else if(-1 == this.solution) {
  33. System.out.println("方程组有无穷解!!!");
  34. System.exit(0); // 退出程序
  35. }
  36. return this.root;
  37. }
  38. /**
  39. * @description TODO 列选主元
  40. * @date 2018年12月1日 下午1:51:49
  41. * @param k 在第k列选主元
  42. */
  43. private void columnPivoting(int k) {
  44. int maxRow = k; // 记录第k列最大的行下标
  45. double max = Math.abs(this.augmentedMatrix[k][k]); // 记录[k][k]至[n-1][k]中最大的数
  46. double now; // 记录当前的值,用于和max比较
  47. for(int i = k + 1; i < this.n; i++) { // 第k行以下的都要比较
  48. now = Math.abs(this.augmentedMatrix[i][k]); // 第k列第i行的绝对值
  49. if(now > max) { // 第i行第k列的数比当前的最大值更大
  50. max = now; // 更新最大值为当前值
  51. maxRow = i; // 记录最大值所在行
  52. }
  53. }
  54. this.rowSwap(k, maxRow); // 行交换
  55. }
  56. /**
  57. * @description TODO 回代求根
  58. * @date 2018年12月1日 下午2:48:33
  59. */
  60. private void backSubstitution() {
  61. this.root = new double[this.n]; // 申请内存
  62. for(int i = this.n - 1; i >= 0; i--) { // 回代n次
  63. for(int j = this.n - 1; j > i; j--) {
  64. this.augmentedMatrix[i][n] -= this.augmentedMatrix[i][j] * this.root[j];
  65. }
  66. this.root[i] = this.augmentedMatrix[i][n] / this.augmentedMatrix[i][i];
  67. }
  68. }
  69. }

CompletePivot.java

  1. package Gauss;
  2. /**
  3. * @description TODO 全主元消去法
  4. * @author PengHao
  5. * @date 2018年12月1日 下午3:00:31
  6. */
  7. public class CompletePivot extends Gauss {
  8. private int[] rootOrder; // 全主元会改变根的顺序,用这个记录变化
  9. /**
  10. * @description TODO 全主元的构造方法,初始化属性
  11. * @date 2018年12月1日 下午3:00:31
  12. */
  13. public CompletePivot(double[][] A) {
  14. super(A); // 显式调用父类的构造方法
  15. rootOrder = new int[this.n]; // 申请内存
  16. for(int i = 0; i < this.n; i++) {
  17. rootOrder[i] = i; // 初始化根的顺序
  18. }
  19. }
  20. /**
  21. * @description TODO 计算方程组的根并返回
  22. * @date 2018年12月1日 下午3:49:02
  23. * @return 返回方程组的根
  24. */
  25. public double[] getRoot() {
  26. for(int k = 0; k < this.n - 1; k++) { // n阶循环n-1次
  27. this.completePivoting(k); // 在第k行第k列的右下方阵部分全选主元
  28. this.elimination(k); // 消元过程
  29. }
  30. this.setSolution(); // 计算解的个数
  31. if(1 == this.solution) { // 唯一解
  32. this.backSubstitution(); // 回代求根
  33. } else if(0 == this.solution) { // 无解
  34. System.out.println("方程组无解!!!");
  35. System.exit(0); // 退出程序
  36. } else if(-1 == this.solution) {
  37. System.out.println("方程组有无穷解!!!");
  38. System.exit(0); // 退出程序
  39. }
  40. return this.root;
  41. }
  42. /**
  43. * @description TODO 在第k行第k列的右下方阵部分全选主元
  44. * @date 2018年12月1日 下午3:37:56
  45. * @param k 全选主元部分的左上角的数的行下标和列下标
  46. */
  47. private void completePivoting(int k) {
  48. int maxRow = k, maxCol = k; //记录最大数的行下标和列下标
  49. double max = Math.abs(this.augmentedMatrix[k][k]); // 记录最大值
  50. double now; // 记录当前值,用于和max比较
  51. for(int i = k; i < this.n; i++) {
  52. for(int j = k; j < this.n; j++) {
  53. now = Math.abs(this.augmentedMatrix[i][j]); // 第i行第j列的绝对值
  54. if(now > max) { // 当前值比最大值更大
  55. max = now; // 更新最大值
  56. maxRow = i; // 记录最大值所在行
  57. maxCol = j; // 记录最大值所在列
  58. }
  59. }
  60. }
  61. this.rowSwap(k, maxRow); // 行交换
  62. this.colSwap(k, maxCol); // 列交换
  63. }
  64. /**
  65. * @description TODO 交换列
  66. * @date 2018年12月1日 下午3:36:14
  67. * @param colSmall 要交换的较小列下标
  68. * @param colBig 要交换的较大列下标
  69. */
  70. private void colSwap(int colSmall, int colBig) {
  71. for(int i = 0; i < this.n; i++) { // 将这两列的第0行至第n-1行交换
  72. this.valueSwap(i, colSmall, i, colBig);
  73. }
  74. // 交换根的顺序
  75. int temp = rootOrder[colSmall];
  76. rootOrder[colSmall] = rootOrder[colBig];
  77. rootOrder[colBig] = temp;
  78. }
  79. /**
  80. * @description TODO 回代求根
  81. * @date 2018年12月1日 下午3:45:17
  82. */
  83. private void backSubstitution() {
  84. this.root = new double[this.n]; // 申请内存
  85. for(int i = this.n - 1; i >= 0; i--) { // 回代n次
  86. for(int j = this.n - 1; j > i; j--) {
  87. this.augmentedMatrix[i][n] -= this.augmentedMatrix[i][j] * this.root[this.rootOrder[j]];
  88. }
  89. this.root[this.rootOrder[i]] = this.augmentedMatrix[i][n] / this.augmentedMatrix[i][i];
  90. }
  91. }
  92. }

Test.java

  1. package Test;
  2. import Gauss.ColumnPivot;
  3. import Gauss.CompletePivot;
  4. /**
  5. * @description TODO 测试类
  6. * @author PengHao
  7. * @date 2018年12月1日 上午9:56:31
  8. */
  9. public class Test {
  10. /**
  11. * @description TODO 主方法,程序入口
  12. * @date 2018年12月1日 上午9:56:31
  13. * @param args
  14. */
  15. public static void main(String[] args) {
  16. int ORDERS = 4; // 阶数
  17. double[][] augmentedMatrix = {
  18. { 2, 10, 0, -3, 10 },
  19. { -3, -4, -12, 13, 5 },
  20. { 1, 2, 3, -4, -2 },
  21. { 4, 14, 9, -13, 7 }
  22. }; // 根是1,2,3,4
  23. // double[][] augmentedMatrix = {
  24. // { 1, 2, 3, 10 },
  25. // { 4, 3, 1, 19 },
  26. // { 2, 5, 2, 18 }
  27. // }; // 根是3,2,1
  28. double[] root; // 保存方程组的根
  29. ColumnPivot col = new ColumnPivot(augmentedMatrix);
  30. root = col.getRoot(); // 获取列主元消去法的方程组的根
  31. printRoot(root, ORDERS, "列主元消去法");
  32. CompletePivot com = new CompletePivot(augmentedMatrix);
  33. root = com.getRoot();
  34. printRoot(root, ORDERS, "全主元消去法");
  35. }
  36. /**
  37. * @description TODO 输出根
  38. * @date 2018年12月1日 下午4:00:40
  39. * @param root 根的数组
  40. * @param n 数组的长度,即根的个数
  41. * @param description 输出文字描述
  42. */
  43. static void printRoot(double[] root, int n, String description) {
  44. System.out.println(description);
  45. for(int i = 0; i < n; i++) {
  46. System.out.println("X" + (i + 1) + "=" + String.format("% 6.2f", root[i]));
  47. }
  48. }
  49. }

列主元消去法&全主元消去法——Java实现的更多相关文章

  1. 基于上三角变换或基于DFS的行(列)展开的n阶行列式求值算法分析及性能评估

    进入大一新学期,看完<线性代数>前几节后,笔者有了用计算机实现行列式运算的想法.这样做的目的,一是巩固自己对相关概念的理解,二是通过独立设计算法练手,三是希望通过图表直观地展现涉及的两种算 ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. C# 列主元素(Gauss)消去法 计算一元多次方程组

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. (原创)列主元Gauss消去法的通用程序

    import numpy as np np.set_printoptions(precision=5) A = np.array([[31., -13., 0., 0., 0., -10., 0., ...

  5. 用列主元消去法分别解方程组Ax=b,用MATLAB程序实现(最有效版)

    数值分析里面经常会涉及到用MATLAB程序实现用列主元消去法分别解方程组Ax=b 具体的方法和代码以如下方程(3x3矩阵)为例进行说明: 用列主元消去法分别解方程组Ax=b,用MATLAB程序实现: ...

  6. C# 顺序高斯(Gauss)消去法计算一元多次方程组

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. JAVA求解线性方程组-列主元高斯消去法

    package MyMath; import java.util.Scanner; public class Gauss { /** * @列主元高斯消去法 */ static double x[]; ...

  8. Guass列主元、平方根法、追赶法求解方程组的C++实现

    一,要解决的问题 选用合适的算法,求解三种线性方程组:一般线性方程组,对称正定方程组,三对角线性方程组. 方程略. 二,数值方法 1,使用Guass列主元消去法求解一般线性方程组. Guass列主元是 ...

  9. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

随机推荐

  1. 20190710用控制台启动一个wcf服务

    快速阅读 如何用控制台启动一个wcf服务,已经wcf的配置和在类库中如何实现 . wcf类库 用vs新建一个类库,引用system.ServiceModel 定义接口实现服务契约和操作契约 [Serv ...

  2. 软件工程实践2019第五次作业——结对编程的编程实现 version1.1

    1.链接 我的博客链接https://github.com/S031402112 结对同学的博客https://www.cnblogs.com/jiabingge/ 我们队创建的仓库的Github项目 ...

  3. ffmpeg-php扩展

    php视频缩略图,较常用的是ffmpeg-php 1: 安装 ffmpeg ffmpeg的下载链接  http://ffmpeg.org/download.html 解压安装包 tar -jxvf f ...

  4. PHP curl put方式上传文件

    发送端: <?php function curlPut($destUrl, $sourceFileDir, $headerArr = array(), $timeout = ) { $ch = ...

  5. pythonUDP发送结构体,对齐到C++结构体

    给出程序先: import random import socket import struct import threading import pickle import json from str ...

  6. 【转载】 卷积神经网络(Convolutional Neural Network,CNN)

    作者:wuliytTaotao 出处:https://www.cnblogs.com/wuliytTaotao/ 本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可,欢迎 ...

  7. 【转】用python读写excel的强大工具:openpyxl

    最近看到好几次群里有人问xlwt.wlrd的问题,怎么说呢,如果是office2007刚出来,大家用xlsx文件用不习惯,还可以理解,这都10年过去了喂,就算没有进化到office2016,还在用of ...

  8. SUPERSOCKET.CLIENTENGINE 简单使用

    首先 引用 SuperSocket.ClientEngine.Core.dll和 SuperSocket.ClientEngine.Common.dll 然后 就可以使用ClientEngine了. ...

  9. linux安装Erlang

    Erlang一种通用的面向并发的编程语言. 1.安装Erlang编译依赖: yum -y install gcc glibc-devel make ncurses-devel openssl-deve ...

  10. QTableView加载数据

    void VCAdmin::searchAllUser() { strID_Index = ""; if (NULL == vcManageDatabaseObj) { vc_ad ...