Example #1
0
        public float[] GaussElimination(float[][] Matrix, float accuracy, EnumsContainer.GaussType gaussType, int[] indexArray = null)
        {
            float multiplier       = 0;
            float sumOfMultipliers = 0;

            float[] VectorX = new float[NumberOfElements];
            //bool r = false;
            for (var i = 0; i < NumberOfElements - 1; i++)
            {
                switch (gaussType)
                {
                case EnumsContainer.GaussType.Normal:
                {
                    if (Math.Abs(Matrix[i][i]) < accuracy)
                    {
                        var success = SwapRowWithZero(i, accuracy, Matrix);
                        if (success == false)
                        {
                            VectorX[i] = 0;
                            break;
                        }
                    }
                    break;
                }

                case EnumsContainer.GaussType.PartialPivot:
                {
                    SwapRowWithRowWithMaxValue(i, Matrix);
                    break;
                }

                case EnumsContainer.GaussType.FullPivot:
                {
                    SwapWithhMaxValue(i, Matrix, ref indexArray);
                    break;
                }
                }

                for (var j = i + 1; j < NumberOfElements; j++)
                {
                    multiplier = -1 * Matrix[j][i] / Matrix[i][i];

                    for (var k = 0; k <= NumberOfElements; k++)
                    {
                        Matrix[j][k] += multiplier * Matrix[i][k];
                    }
                }

                //Console.WriteLine("MATRIX");
                //PrintMatrix(Matrix);
                //Console.WriteLine("==========");
            }

            var dupaaa = 3;

            if (gaussType == EnumsContainer.GaussType.FullPivot)
            {
                for (var i = NumberOfElements - 1; i >= 0; i--)
                {
                    sumOfMultipliers = Matrix[i][NumberOfElements];
                    Console.WriteLine(sumOfMultipliers);
                    for (var j = NumberOfElements - 1; j >= i + 1; j--)
                    {
                        sumOfMultipliers -= Matrix[i][j] * VectorX[indexArray[j]];
                        Console.WriteLine(sumOfMultipliers);
                    }

                    if (Math.Abs(Matrix[i][i]) > accuracy)
                    {
                        VectorX[indexArray[dupaaa]] = sumOfMultipliers / Matrix[i][i];
                        dupaaa--;
                    }

                    //Console.WriteLine("RESULT:");
                    //PrintResult(VectorX);
                    //Console.WriteLine("============");
                }
            }
            else
            {
                for (var i = NumberOfElements - 1; i >= 0; i--)
                {
                    sumOfMultipliers = Matrix[i][NumberOfElements];

                    for (var j = NumberOfElements - 1; j >= i + 1; j--)
                    {
                        sumOfMultipliers -= Matrix[i][j] * VectorX[j];
                    }

                    if (Math.Abs(Matrix[i][i]) > accuracy)
                    {
                        VectorX[i] = sumOfMultipliers / Matrix[i][i];
                    }
                }
            }

            return(VectorX);
        }
        public T[] GaussElimination(T[][] Matrix, T accuracy, EnumsContainer.GaussType gaussType, int[] indexArray = null)
        {
            T multiplier       = default(T);
            T sumOfMultipliers = default(T);

            T[] VectorX = new T[NumberOfElements];
            switch (this.GetTypeOfData <T>())
            {
            case EnumsContainer.DataType.Float:
                multiplier = (T)Convert.ChangeType(multiplier, typeof(float));
                break;

            case EnumsContainer.DataType.Double:
                multiplier = (T)Convert.ChangeType(multiplier, typeof(double));
                break;

            case EnumsContainer.DataType.FractionType:
                multiplier = (T)Convert.ChangeType(multiplier, typeof(FractionType));
                break;
            }

            //bool r = false;
            for (var i = 0; i < NumberOfElements - 1; i++)
            {
                switch (gaussType)
                {
                case EnumsContainer.GaussType.Normal:
                {
                    if (Matrix[i][i].Equals(0))
                    {
                        var success = SwapRowWithZero(i, accuracy, Matrix);
                        if (success == false)
                        {
                            //VectorX[i] = 0;
                            break;
                        }
                    }
                    break;
                }

                case EnumsContainer.GaussType.PartialPivot:
                {
                    SwapRowWithRowWithMaxValue(i, Matrix);
                    break;
                }

                case EnumsContainer.GaussType.FullPivot:
                {
                    SwapWithMaxValue(i, Matrix, ref indexArray);
                    break;
                }
                }

                dynamic minusOne;
                if (typeof(T) == typeof(FractionType))
                {
                    minusOne = new FractionType {
                        Denominator = 1, Numerator = -1
                    }
                }
                ;
                else
                {
                    minusOne = -1;
                }
                for (var j = i + 1; j < NumberOfElements; j++)
                {
                    multiplier = Divide <T>(Multiply <T>(minusOne, Matrix[j][i]), Matrix[i][i]);
                    for (var k = 0; k <= NumberOfElements; k++)
                    {
                        Matrix[j][k] = Add <T>(Matrix[j][k], Multiply <T>(multiplier, Matrix[i][k]));
                    }
                }
            }

            int index0 = VectorX.Length - 1;

            if (gaussType == EnumsContainer.GaussType.FullPivot)
            {
                for (var i = NumberOfElements - 1; i >= 0; i--)
                {
                    sumOfMultipliers = Matrix[i][NumberOfElements];
                    for (var j = NumberOfElements - 1; j >= i + 1; j--)
                    {
                        sumOfMultipliers = Subtract <T>(sumOfMultipliers, Multiply <T>(Matrix[i][j], VectorX[indexArray[j]]));
                    }

                    if (CompareBigger(Abs(Matrix[i][i]), accuracy))
                    {
                        VectorX[indexArray[index0]] = Divide <T>(sumOfMultipliers, Matrix[i][i]);
                        index0--;
                    }
                }
            }
            else
            {
                for (var i = NumberOfElements - 1; i >= 0; i--)
                {
                    sumOfMultipliers = Matrix[i][NumberOfElements];

                    for (var j = NumberOfElements - 1; j >= i + 1; j--)
                    {
                        sumOfMultipliers = Subtract <T>(sumOfMultipliers, Multiply <T>(Matrix[i][j], VectorX[j]));
                    }

                    if (CompareBigger <T>(Abs(Matrix[i][i]), accuracy))
                    {
                        VectorX[i] = Divide <T>(sumOfMultipliers, Matrix[i][i]);
                    }
                }
            }

            return(VectorX);
        }
        public double[] GaussElimination(double[][] Matrix, double accuracy, EnumsContainer.GaussType gaussType, int[] indexArray = null)
        {
            double multiplier       = 0;
            double sumOfMultipliers = 0;

            double[] VectorX = new double[NumberOfElements];
            //bool r = false;
            for (var i = 0; i < NumberOfElements - 1; i++)
            {
                switch (gaussType)
                {
                case EnumsContainer.GaussType.Normal:
                {
                    if (Matrix[i][i] == 0.0d)
                    {
                        var success = SwapRowWithZero(i, accuracy, Matrix);
                        if (success == false)
                        {
                            VectorX[i] = 0;
                            break;
                        }
                    }
                    break;
                }

                case EnumsContainer.GaussType.PartialPivot:
                {
                    SwapRowWithRowWithMaxValue(i, Matrix);
                    break;
                }

                case EnumsContainer.GaussType.FullPivot:
                {
                    SwapWithhMaxValue(i, Matrix, ref indexArray);
                    break;
                }
                }

                for (var j = i + 1; j < NumberOfElements; j++)
                {
                    multiplier = -1 * Matrix[j][i] / Matrix[i][i];

                    for (var k = 0; k <= NumberOfElements; k++)
                    {
                        Matrix[j][k] += multiplier * Matrix[i][k];
                    }
                }
            }

            int index0 = VectorX.Length - 1;

            if (gaussType == EnumsContainer.GaussType.FullPivot)
            {
                for (var i = NumberOfElements - 1; i >= 0; i--)
                {
                    sumOfMultipliers = Matrix[i][NumberOfElements];
                    for (var j = NumberOfElements - 1; j >= i + 1; j--)
                    {
                        sumOfMultipliers -= Matrix[i][j] * VectorX[indexArray[j]];
                    }

                    if (Math.Abs(Matrix[i][i]) > accuracy)
                    {
                        VectorX[indexArray[index0]] = sumOfMultipliers / Matrix[i][i];
                        index0--;
                    }
                }
            }
            else
            {
                for (var i = NumberOfElements - 1; i >= 0; i--)
                {
                    sumOfMultipliers = Matrix[i][NumberOfElements];

                    for (var j = NumberOfElements - 1; j >= i + 1; j--)
                    {
                        sumOfMultipliers -= Matrix[i][j] * VectorX[j];
                    }

                    if (Math.Abs(Matrix[i][i]) > accuracy)
                    {
                        VectorX[i] = sumOfMultipliers / Matrix[i][i];
                    }
                }
            }

            return(VectorX);
        }
Example #4
0
        public FractionType[] GaussElimination(FractionType[][] Matrix, BigInteger accuracy, EnumsContainer.GaussType gaussType, int[] indexArray = null)
        {
            FractionType multiplier       = new FractionType();
            FractionType sumOfMultipliers = new FractionType();

            FractionType[] VectorX = new FractionType[NumberOfElements];
            for (var i = 0; i < NumberOfElements - 1; i++)
            {
                switch (gaussType)
                {
                case EnumsContainer.GaussType.Normal:
                {
                    if (Matrix[i][i].Numerator == accuracy)
                    {
                        var success = SwapRowWithZero(i, accuracy, Matrix);
                        if (success == false)
                        {
                            VectorX[i].Denominator = 0;
                            VectorX[i].Numerator   = 0;
                            break;
                        }
                    }
                    break;
                }

                case EnumsContainer.GaussType.PartialPivot:
                {
                    SwapRowWithRowWithMaxValue(i, Matrix);
                    break;
                }

                case EnumsContainer.GaussType.FullPivot:
                {
                    SwapWithhMaxValue(i, Matrix, ref indexArray);
                    break;
                }
                }

                for (var j = i + 1; j < NumberOfElements; j++)
                {
                    multiplier            = Matrix[j][i] / Matrix[i][i];
                    multiplier.Numerator *= -1;

                    for (var k = 0; k <= NumberOfElements; k++)
                    {
                        Matrix[j][k] += multiplier * Matrix[i][k];
                    }
                }
            }

            var index0 = VectorX.Length - 1;

            if (gaussType == EnumsContainer.GaussType.FullPivot)
            {
                for (var i = NumberOfElements - 1; i >= 0; i--)
                {
                    sumOfMultipliers = Matrix[i][NumberOfElements];
                    for (var j = NumberOfElements - 1; j >= i + 1; j--)
                    {
                        sumOfMultipliers -= Matrix[i][j] * VectorX[indexArray[j]];
                    }

                    if (CustomAbs(Matrix[i][i].Numerator) > accuracy)
                    {
                        VectorX[indexArray[index0]] = sumOfMultipliers / Matrix[i][i];
                        index0--;
                    }
                }
            }
            else
            {
                for (var i = NumberOfElements - 1; i >= 0; i--)
                {
                    sumOfMultipliers = Matrix[i][NumberOfElements];

                    for (var j = NumberOfElements - 1; j >= i + 1; j--)
                    {
                        sumOfMultipliers -= Matrix[i][j] * VectorX[j];
                    }

                    if (CustomAbs(Matrix[i][i].Numerator) > accuracy)
                    {
                        VectorX[i] = sumOfMultipliers / Matrix[i][i];
                    }
                }
            }

            return(VectorX);
        }