Facebook
From Whipped Motmot, 7 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 259
  1. using System;
  2. using System.Numerics;
  3. using System.Windows.Forms;
  4.  
  5. namespace gaussJordan{
  6.         public static class complexEq
  7.         {
  8.                 static private double precision=0.000001;
  9.                 static public void setPrecision(double val){
  10.                         if(val>0.0){
  11.                                 precision=val;
  12.                         }
  13.                 }
  14.  
  15.  
  16.                 static public void swapRows(int r1, int r2, Complex[,] mat, Complex[] vec){
  17.                         Complex tmp;
  18.  
  19.                         tmp=vec[r1];
  20.                         vec[r1]=vec[r2];
  21.                         vec[r2]=tmp;
  22.  
  23.                         int end=mat.GetLength(0);
  24.                         for(int i=0; i<end; ++i){
  25.                                 tmp=mat[i, r1];
  26.                                 mat[i,r1]=mat[i,r2];
  27.                                 mat[i,r2]=tmp;
  28.                         }
  29.                 }
  30.                 static public void multipleRow(int row, Complex mul, Complex[,]mat, Complex[] vec){
  31.                         vec[row]*=mul;
  32.  
  33.                         int end=mat.GetLength(0);    
  34.                         for(int i=0; i<end; ++i){
  35.                                 mat[i, row]*=mul;
  36.                         }
  37.                 }
  38.                 static public void eliminateInCol(int col, Complex[,] mat, Complex[] vec){
  39.                         Complex mul;
  40.                         int w=mat.GetLength(0);
  41.                         int h=mat.GetLength(1);
  42.  
  43.                         for(int y=0; y<h; ++y){
  44.                                 mul=mat[col, y];
  45.  
  46.                                 if(y==col || mul==0.0){
  47.                                         continue;
  48.                                 }
  49.  
  50.  
  51.                                 vec[y]-=vec[col]*mul;
  52.  
  53.                                 for(int x=0; x<w; ++x){
  54.                                         mat[x,y]-=mat[x,col]*mul;
  55.                                 }
  56.                         }
  57.                 }
  58.                 static public void zeroCorrection(Complex[,]mat, Complex[] vec){
  59.                         for (int x = 0; x < mat.GetLength(0); ++x){
  60.                                 for (int y = 0; y < mat.GetLength(1); ++y){
  61.                                         if (mat[x, y].Real > -precision && mat[x, y].Real < precision){
  62.                                                 mat[x, y] = new Complex(0.0, mat[x, y].Imaginary);
  63.                                         }
  64.                                         if (mat[x, y].Imaginary > -precision && mat[x, y].Imaginary < precision){
  65.                                                 mat[x, y] = new Complex(mat[x, y].Real, 0.0);
  66.                                         }
  67.                                 }
  68.                         }
  69.                 }
  70.  
  71.                 static public int getRowIdOfMaxElemInCol(Complex[,] mat, int col){      //szuka tylko pod główną przekątną
  72.                         int id=col;
  73.                         //Complex max=Complex.Abs(mat[col,col]);
  74.                         double max=Math.Abs(mat[col,col].Real)+Math.Abs(mat[col,col].Imaginary);
  75.  
  76.                         int end=mat.GetLength(1);
  77.                         for(int i=col+1; i<end; ++i){
  78.                                 if(Math.Abs(mat[col,i].Real)+Math.Abs(mat[col,i].Imaginary) > max){
  79.                                         max=Math.Abs(mat[col,col].Real)+Math.Abs(mat[col,col].Imaginary);
  80.                                         id=i;
  81.                                 }
  82.                         }
  83.                         return id;
  84.                 }
  85.                 static public bool dgv2MatAndVec(DataGridView dgvM, DataGridView dgvV, Complex[,] mat, Complex[] vec){
  86.                         int x=dgvM.ColumnCount;
  87.                         int y=dgvM.RowCount;
  88.  
  89.                         int i=0;
  90.                         int j=0;
  91.  
  92.                         string[] split;
  93.  
  94.                         try{
  95.                                 for(i=0; i<y; i++){
  96.                                         for(j=0; j<x; j++){
  97.                                                 split=dgvM[j,i].Value.ToString().Split(';');
  98.  
  99.                                                 mat[j,i]=new Complex(double.Parse(split[0]), double.Parse(split[1]));
  100.                                         }
  101.                                         split=dgvV[0,i].Value.ToString().Split(';');
  102.  
  103.                                         vec[i]=new Complex(double.Parse(split[0]), double.Parse(split[1]));
  104.                                 }
  105.                         }catch(Exception e){
  106.                                 MessageBox.Show("Pole "+(i+1)+" x "+(j+1)+"\n"+e.Message, "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
  107.                                 return false;
  108.                         }
  109.                         return true;
  110.                 }
  111.                 static public void matAndVec2Dgv(DataGridView dgvM, DataGridView dgvV, Complex[,] mat, Complex[] vec){
  112.                         int x=mat.GetLength(0);
  113.                         int y=mat.GetLength(1);
  114.  
  115.                         for(int i=0; i<y; i++){
  116.                                 for(int j=0; j<x; j++){
  117.                                         dgvM[j, i].Value = mat[j, i].Real +";"+ mat[j, i].Imaginary;
  118.  
  119.                                 }
  120.                                 dgvV[0,i].Value=vec[i].Real + ";" + vec[i].Imaginary;
  121.                         }
  122.                 }
  123.  
  124.                 static public bool gaussJordanElimination(Complex[,]mat, Complex[] vec){
  125.                         int end=mat.GetLength(0);
  126.                         int maxid=0;
  127.                         bool error=false;
  128.  
  129.                         for(int i=0; i<end; ++i){
  130.                                 maxid=complexEq.getRowIdOfMaxElemInCol(mat, i);
  131.  
  132.                                 if(maxid!=i){
  133.                                         complexEq.swapRows(i,maxid,mat,vec);
  134.                                 }
  135.  
  136.                                 if(mat[i,i]==0){
  137.                                         error=true;
  138.                                         break;
  139.                                 }
  140.                         }
  141.  
  142.                         if(!error){
  143.                                 end=mat.GetLength(1);
  144.                                 Complex mul;
  145.                                 for(int i=0; i<end; ++i){
  146.                                         if(mat[i,i]==0.0){
  147.                                                 error=true;
  148.                                                 break;
  149.                                         }
  150.                                         mul=1.0/mat[i,i];
  151.                                         complexEq.multipleRow(i,mul,mat,vec);
  152.                                         complexEq.eliminateInCol(i,mat,vec);
  153.                                 }
  154.                         }
  155.  
  156.                         if(error){
  157.                                 MessageBox.Show("Wyznacznik = 0", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
  158.                                 return false;
  159.                         }
  160.                         return true;
  161.                 }
  162.         }
  163.  
  164.         public static class normalEq
  165.         {
  166.  
  167.                 static private double precision=0.000001;
  168.                 static public void setPrecision(double val){
  169.                         if(val>0.0){
  170.                                 precision=val;
  171.                         }
  172.                 }
  173.  
  174.  
  175.                 static public void swapRows(int r1, int r2, double[,] mat, double[] vec){
  176.                         double tmp;
  177.  
  178.                         tmp=vec[r1];
  179.                         vec[r1]=vec[r2];
  180.                         vec[r2]=tmp;
  181.  
  182.                         int end=mat.GetLength(0);
  183.                         for(int i=0; i<end; ++i){
  184.                                 tmp=mat[i, r1];
  185.                                 mat[i,r1]=mat[i,r2];
  186.                                 mat[i,r2]=tmp;
  187.                         }
  188.                 }
  189.                 static public void multipleRow(int row, double mul, double[,]mat, double[] vec){
  190.                         vec[row]*=mul;
  191.  
  192.                         int end=mat.GetLength(0);    
  193.                         for(int i=0; i<end; ++i){
  194.                                 mat[i, row]*=mul;
  195.                         }
  196.                 }
  197.                 static public void eliminateInCol(int col, double[,] mat, double[] vec){
  198.                         double mul;
  199.                         int w=mat.GetLength(0);
  200.                         int h=mat.GetLength(1);
  201.  
  202.                         for(int y=0; y<h; ++y){
  203.                                 mul=mat[col, y];
  204.  
  205.                                 if(y==col || mul==0.0){
  206.                                         continue;
  207.                                 }
  208.  
  209.  
  210.                                 vec[y]-=vec[col]*mul;
  211.  
  212.                                 for(int x=0; x<w; ++x){
  213.                                         mat[x,y]-=mat[x,col]*mul;
  214.  
  215.                                         if(mat[x,y] > -precision && mat[x,y] < precision){
  216.                                                 mat[x,y]=0;
  217.                                         }
  218.                                 }
  219.                         }
  220.                 }
  221.  
  222.  
  223.  
  224.  
  225.                 static public int getRowIdOfMaxElemInCol(double[,] mat, int col){       //szuka tylko pod główną przekątną
  226.                         int id=col;
  227.                         double max=mat[col,col];
  228.  
  229.                         int end=mat.GetLength(1);
  230.                         for(int i=col+1; i<end; ++i){
  231.                                 if(Math.Abs(mat[col,i]) > max){
  232.                                         max=mat[col,i];
  233.                                         id=i;
  234.                                 }
  235.                         }
  236.  
  237.                         return id;
  238.                 }
  239.  
  240.  
  241.                 static public bool dgv2MatAndVec(DataGridView dgvM, DataGridView dgvV, double[,] mat, double[] vec){
  242.                         int x=dgvM.ColumnCount;
  243.                         int y=dgvM.RowCount;
  244.  
  245.                         int i=0;
  246.                         int j=0;
  247.  
  248.                         try{
  249.                                 for(i=0; i<y; i++){
  250.                                         for(j=0; j<x; j++){
  251.                                                 mat[j,i]=double.Parse(dgvM[j,i].Value.ToString());
  252.                                         }
  253.                                         vec[i]=double.Parse(dgvV[0,i].Value.ToString());
  254.                                 }
  255.                         }catch(Exception e){
  256.                                 MessageBox.Show("Pole "+(i+1)+" x "+(j+1)+"\n"+e.Message, "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
  257.                                 return false;
  258.                         }
  259.                         return true;
  260.                 }
  261.                 static public void matAndVec2Dgv(DataGridView dgvM, DataGridView dgvV, double[,] mat, double[] vec){
  262.                         int x=mat.GetLength(0);
  263.                         int y=mat.GetLength(1);
  264.  
  265.                         for(int i=0; i<y; i++){
  266.                                 for(int j=0; j<x; j++){
  267.                                         dgvM[j,i].Value=mat[j,i];
  268.                                 }
  269.                                 dgvV[0,i].Value=vec[i];
  270.                         }
  271.                 }
  272.  
  273.  
  274.  
  275.                 static public bool gaussJordanElimination(double[,]mat, double[] vec){
  276.                         int end=mat.GetLength(0);
  277.                         int maxid=0;
  278.                         bool error=false;
  279.  
  280.                         for(int i=0; i<end; ++i){
  281.                                 maxid=getRowIdOfMaxElemInCol(mat, i);
  282.  
  283.                                 if(maxid!=i){
  284.                                         swapRows(i,maxid,mat,vec);
  285.                                 }
  286.  
  287.                                 if(mat[i,i]==0){
  288.                                         error=true;
  289.                                         break;
  290.                                 }
  291.                         }
  292.  
  293.                         if(!error){
  294.                                 end=mat.GetLength(1);
  295.                                 double mul;
  296.                                 for(int i=0; i<end; ++i){
  297.                                         if(mat[i,i]==0.0){
  298.                                                 error=true;
  299.                                                 break;
  300.                                         }
  301.                                         mul=1.0/mat[i,i];
  302.                                         multipleRow(i,mul,mat,vec);
  303.                                         eliminateInCol(i,mat,vec);
  304.                                 }
  305.                         }
  306.  
  307.                         if(error){
  308.                                 //MessageBox.Show("Wyznacznik = 0", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
  309.                                 return false;
  310.                         }
  311.                         return true;
  312.                 }
  313.                 static public bool gaussJordanEliminationTrans(double[,]mat, double[] vec){
  314.                         double tmp;
  315.  
  316.                         for (int x = 0; x < mat.GetLength(0)-1; ++x){
  317.                                 for (int y = x+1; y < mat.GetLength(1); ++y){
  318.                                         tmp = mat[x, y];
  319.                                         mat[x, y] = mat[y, x];
  320.                                         mat[y, x] = tmp;
  321.                                 }
  322.                         }
  323.                                
  324.                         return gaussJordanElimination(mat, vec);
  325.                 }
  326.         }
  327.  
  328. }
  329.