using System;
using System.Numerics;
using System.Windows.Forms;
namespace gaussJordan{
public static class complexEq
{
static private double precision=0.000001;
static public void setPrecision(double val){
if(val>0.0){
precision=val;
}
}
static public void swapRows(int r1, int r2, Complex[,] mat, Complex[] vec){
Complex tmp;
tmp=vec[r1];
vec[r1]=vec[r2];
vec[r2]=tmp;
int end=mat.GetLength(0);
for(int i=0; i<end; ++i){
tmp=mat[i, r1];
mat[i,r1]=mat[i,r2];
mat[i,r2]=tmp;
}
}
static public void multipleRow(int row, Complex mul, Complex[,]mat, Complex[] vec){
vec[row]*=mul;
int end=mat.GetLength(0);
for(int i=0; i<end; ++i){
mat[i, row]*=mul;
}
}
static public void eliminateInCol(int col, Complex[,] mat, Complex[] vec){
Complex mul;
int w=mat.GetLength(0);
int h=mat.GetLength(1);
for(int y=0; y<h; ++y){
mul=mat[col, y];
if(y==col || mul==0.0){
continue;
}
vec[y]-=vec[col]*mul;
for(int x=0; x<w; ++x){
mat[x,y]-=mat[x,col]*mul;
}
}
}
static public void zeroCorrection(Complex[,]mat, Complex[] vec){
for (int x = 0; x < mat.GetLength(0); ++x){
for (int y = 0; y < mat.GetLength(1); ++y){
if (mat[x, y].Real > -precision && mat[x, y].Real < precision){
mat[x, y] = new Complex(0.0, mat[x, y].Imaginary);
}
if (mat[x, y].Imaginary > -precision && mat[x, y].Imaginary < precision){
mat[x, y] = new Complex(mat[x, y].Real, 0.0);
}
}
}
}
static public int getRowIdOfMaxElemInCol(Complex[,] mat, int col){ //szuka tylko pod główną przekątną
int id=col;
//Complex max=Complex.Abs(mat[col,col]);
double max=Math.Abs(mat[col,col].Real)+Math.Abs(mat[col,col].Imaginary);
int end=mat.GetLength(1);
for(int i=col+1; i<end; ++i){
if(Math.Abs(mat[col,i].Real)+Math.Abs(mat[col,i].Imaginary) > max){
max=Math.Abs(mat[col,col].Real)+Math.Abs(mat[col,col].Imaginary);
id=i;
}
}
return id;
}
static public bool dgv2MatAndVec(DataGridView dgvM, DataGridView dgvV, Complex[,] mat, Complex[] vec){
int x=dgvM.ColumnCount;
int y=dgvM.RowCount;
int i=0;
int j=0;
string[] split;
try{
for(i=0; i<y; i++){
for(j=0; j<x; j++){
split=dgvM[j,i].Value.ToString().Split(';');
mat[j,i]=new Complex(double.Parse(split[0]), double.Parse(split[1]));
}
split=dgvV[0,i].Value.ToString().Split(';');
vec[i]=new Complex(double.Parse(split[0]), double.Parse(split[1]));
}
}catch(Exception e){
MessageBox.Show("Pole "+(i+1)+" x "+(j+1)+"\n"+e.Message, "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
static public void matAndVec2Dgv(DataGridView dgvM, DataGridView dgvV, Complex[,] mat, Complex[] vec){
int x=mat.GetLength(0);
int y=mat.GetLength(1);
for(int i=0; i<y; i++){
for(int j=0; j<x; j++){
dgvM[j, i].Value = mat[j, i].Real +";"+ mat[j, i].Imaginary;
}
dgvV[0,i].Value=vec[i].Real + ";" + vec[i].Imaginary;
}
}
static public bool gaussJordanElimination(Complex[,]mat, Complex[] vec){
int end=mat.GetLength(0);
int maxid=0;
bool error=false;
for(int i=0; i<end; ++i){
maxid=complexEq.getRowIdOfMaxElemInCol(mat, i);
if(maxid!=i){
complexEq.swapRows(i,maxid,mat,vec);
}
if(mat[i,i]==0){
error=true;
break;
}
}
if(!error){
end=mat.GetLength(1);
Complex mul;
for(int i=0; i<end; ++i){
if(mat[i,i]==0.0){
error=true;
break;
}
mul=1.0/mat[i,i];
complexEq.multipleRow(i,mul,mat,vec);
complexEq.eliminateInCol(i,mat,vec);
}
}
if(error){
MessageBox.Show("Wyznacznik = 0", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
}
public static class normalEq
{
static private double precision=0.000001;
static public void setPrecision(double val){
if(val>0.0){
precision=val;
}
}
static public void swapRows(int r1, int r2, double[,] mat, double[] vec){
double tmp;
tmp=vec[r1];
vec[r1]=vec[r2];
vec[r2]=tmp;
int end=mat.GetLength(0);
for(int i=0; i<end; ++i){
tmp=mat[i, r1];
mat[i,r1]=mat[i,r2];
mat[i,r2]=tmp;
}
}
static public void multipleRow(int row, double mul, double[,]mat, double[] vec){
vec[row]*=mul;
int end=mat.GetLength(0);
for(int i=0; i<end; ++i){
mat[i, row]*=mul;
}
}
static public void eliminateInCol(int col, double[,] mat, double[] vec){
double mul;
int w=mat.GetLength(0);
int h=mat.GetLength(1);
for(int y=0; y<h; ++y){
mul=mat[col, y];
if(y==col || mul==0.0){
continue;
}
vec[y]-=vec[col]*mul;
for(int x=0; x<w; ++x){
mat[x,y]-=mat[x,col]*mul;
if(mat[x,y] > -precision && mat[x,y] < precision){
mat[x,y]=0;
}
}
}
}
static public int getRowIdOfMaxElemInCol(double[,] mat, int col){ //szuka tylko pod główną przekątną
int id=col;
double max=mat[col,col];
int end=mat.GetLength(1);
for(int i=col+1; i<end; ++i){
if(Math.Abs(mat[col,i]) > max){
max=mat[col,i];
id=i;
}
}
return id;
}
static public bool dgv2MatAndVec(DataGridView dgvM, DataGridView dgvV, double[,] mat, double[] vec){
int x=dgvM.ColumnCount;
int y=dgvM.RowCount;
int i=0;
int j=0;
try{
for(i=0; i<y; i++){
for(j=0; j<x; j++){
mat[j,i]=double.Parse(dgvM[j,i].Value.ToString());
}
vec[i]=double.Parse(dgvV[0,i].Value.ToString());
}
}catch(Exception e){
MessageBox.Show("Pole "+(i+1)+" x "+(j+1)+"\n"+e.Message, "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
static public void matAndVec2Dgv(DataGridView dgvM, DataGridView dgvV, double[,] mat, double[] vec){
int x=mat.GetLength(0);
int y=mat.GetLength(1);
for(int i=0; i<y; i++){
for(int j=0; j<x; j++){
dgvM[j,i].Value=mat[j,i];
}
dgvV[0,i].Value=vec[i];
}
}
static public bool gaussJordanElimination(double[,]mat, double[] vec){
int end=mat.GetLength(0);
int maxid=0;
bool error=false;
for(int i=0; i<end; ++i){
maxid=getRowIdOfMaxElemInCol(mat, i);
if(maxid!=i){
swapRows(i,maxid,mat,vec);
}
if(mat[i,i]==0){
error=true;
break;
}
}
if(!error){
end=mat.GetLength(1);
double mul;
for(int i=0; i<end; ++i){
if(mat[i,i]==0.0){
error=true;
break;
}
mul=1.0/mat[i,i];
multipleRow(i,mul,mat,vec);
eliminateInCol(i,mat,vec);
}
}
if(error){
//MessageBox.Show("Wyznacznik = 0", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
static public bool gaussJordanEliminationTrans(double[,]mat, double[] vec){
double tmp;
for (int x = 0; x < mat.GetLength(0)-1; ++x){
for (int y = x+1; y < mat.GetLength(1); ++y){
tmp = mat[x, y];
mat[x, y] = mat[y, x];
mat[y, x] = tmp;
}
}
return gaussJordanElimination(mat, vec);
}
}
}