using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
public class LinearEquation
{
#region Fields
private double _ReturnValue = 1.0;
internal List<double[]> _DoubleList = new List<double[]>();
#endregion
#region Property
///
/// Return the value of Linear Equation
///
public double ReturnValue
{
get { return _ReturnValue; }
}
///
///
///
public double[] Double
{
set
{
this._DoubleList.Add(value);
}
}
#endregion
#region Method
private bool CanRun()
{
if (this._DoubleList.Count == 0)
return false;
if (this._DoubleList[0].Length < 2)
return false;
if (this._DoubleList[0].Length != this._DoubleList.Count)
return false;
double[] flag = this._DoubleList[0];
foreach (double[] ds in this._DoubleList)
{
if (ds.Length != flag.Length)
{
return false;
}
}
return true;
}
public void Run()
{
if (!CanRun()) return;
for (int i = 0; i < this._DoubleList.Count; i++)
{
for (int j = i+1; j < this._DoubleList.Count; j++)
{
Get(ref this._DoubleList, i, j);
}
}
for (int i = 0; i < this._DoubleList.Count; i++)
{
this._ReturnValue = this._ReturnValue*this._DoubleList[i][i];
}
}
private void Get(ref List<double[]> doublelist,int fi, int fj)
{
if (doublelist[fi].Length.Equals(doublelist[fj].Length))
{
if (doublelist[fi].Length > 1)
{
int k = 0;
for (int j = 0; j < doublelist[fi].Length; j++)
{
if (doublelist[fi][j] == 0 && doublelist[fj][j] == 0)
{// 排除都是零的
continue;
}
else if (doublelist[fi][j] == 0 && doublelist[fj][j] != 0)
{// 两数组对换。
double[] flag1 = doublelist[fj];
doublelist[fj] = doublelist[fi];
doublelist[fi] = flag1;
}
else if(doublelist[fi][j] != 0 && doublelist[fj][j] == 0)
{ //第一个数组不为0,而第二个数组为0,不计算.
return;
}
k = j;
break;
}
double flag = double.MinValue;
if (doublelist[fj][k] / doublelist[fi][k] * doublelist[fi][k] + doublelist[fj][k] < 0.0005 && doublelist[fj][k] / doublelist[fi][k] * doublelist[fi][k] + doublelist[fj][k] > 0)
{
flag = doublelist[fj][k] / doublelist[fi][k];
}
else
{
flag = -(doublelist[fj][k] / doublelist[fi][k]);
}
for (int i = k; i < doublelist[fi].Length; i++)
{
doublelist[fj][i] = flag * doublelist[fi][i] + doublelist[fj][i];
}
}
}
}
private void Get(ref double[] one, ref double[] two)
{
if (one.Length.Equals(two.Length))
{
if (one.Length > 1)
{
int k = 0;
for (int j = 0; j < one.Length; j++)
{
if (one[j] == 0 && two[j] == 0)
{// 排除都是零的
continue;
}
else if (one[j] == 0 && two[j] != 0)
{// 两数组对换。
double[] flag1 = two;
two = one;
one = flag1;
}
k = j;
break;
}
double flag = double.MinValue;
if (two[k] / one[k] * one[k] + two[k] < 0.0005 && two[k] / one[k] * one[k] + two[k] > 0)
{
flag = two[k] / one[k];
}
else
{
flag = -(two[k] / one[k]);
}
for (int i = k; i < one.Length; i++)
{
two[i] = flag * one[i] + two[i];
}
}
}
}
#endregion
}
}