Example #1
0
 private int CompareTo(FractionType other)
 {
     if (this.Denominator == other.Denominator)
     {
         if (this.Numerator > other.Numerator)
         {
             return(1);
         }
         else if (this.Numerator < other.Numerator)
         {
             return(-1);
         }
         else
         {
             return(0);
         }
     }
     else
     {
         if (this.Numerator * other.Denominator > this.Denominator * this.Numerator)
         {
             return(1);
         }
         else if (this.Numerator * other.Denominator > this.Denominator * this.Numerator)
         {
             return(-1);
         }
         else
         {
             return(0);
         }
     }
 }
Example #2
0
        public static FractionType operator *(FractionType type1, FractionType type2)
        {
            var rtn = new FractionType
            {
                Numerator = type1.Numerator * type2.Numerator,

                Denominator = type1.Denominator * type2.Denominator
            };

            return(Euklides(rtn.Numerator, rtn.Denominator));
        }
Example #3
0
        public static bool CompareBigger <T>(T number1, T number2)
        {
            if (typeof(T) == typeof(FractionType))
            {
                FractionType fracNumb1 = (FractionType)Convert.ChangeType(number1, typeof(FractionType));
                FractionType fracNumb2 = (FractionType)Convert.ChangeType(number2, typeof(FractionType));

                return(fracNumb1 > fracNumb2);
            }
            dynamic dynNumber1 = number1;
            dynamic dynNumber2 = number2;

            return(dynNumber1 > dynNumber2);
        }
Example #4
0
        static FractionType Euklides(BigInteger num1, BigInteger num2)
        {
            FractionType rtn = new FractionType {
                Numerator = num1, Denominator = num2
            };
            BigInteger d = BigInteger.GreatestCommonDivisor(num1, num2);

            if (d != 0)
            {
                rtn.Numerator   = rtn.Numerator / d;
                rtn.Denominator = rtn.Denominator / d;
            }
            return(rtn);
        }
Example #5
0
        public static T Multiply <T>(T number1, T number2)
        {
            if (typeof(T) == typeof(FractionType))
            {
                FractionType fracNumb1 = (FractionType)Convert.ChangeType(number1, typeof(FractionType));
                FractionType fracNumb2 = (FractionType)Convert.ChangeType(number2, typeof(FractionType));

                var tmp = fracNumb1 * fracNumb2;
                return((T)Convert.ChangeType(tmp, typeof(T)));
            }
            dynamic dynNumber1 = number1;
            dynamic dynNumber2 = number2;

            return(dynNumber1 * dynNumber2);
        }
Example #6
0
        //public  float[][] GetMatrixWithFloatData(int numberOfRows)
        //{
        //    var floatRandomNumbers = new float[numberOfRows][];
        //
        //    for (int i = 0; i < numberOfRows; i++)
        //    {
        //        floatRandomNumbers[i] = new float[numberOfRows + 1];
        //        for (int j = 0; j < numberOfRows + 1; j++)
        //        {
        //            floatRandomNumbers[i][j] = (float)RandomNumbers[i][j]/65536;
        //        }
        //    }
        //
        //    return floatRandomNumbers;
        //}

        public T[][] GetMatrixData <T>(int numberOfRows)
        {
            switch (typeof(T).Name)
            {
            case "FractionType":
            {
                FractionType[][] randomNumbers = new FractionType[numberOfRows][];
                for (int i = 0; i < numberOfRows; i++)
                {
                    randomNumbers[i] = new FractionType[numberOfRows + 1];
                    for (int j = 0; j < numberOfRows + 1; j++)
                    {
                        randomNumbers[i][j].Numerator   = RandomNumbers[i][j];
                        randomNumbers[i][j].Denominator = 65536;
                    }
                }
                return((T[][])Convert.ChangeType(randomNumbers, typeof(T[][])));
            }

            case "Double":
            {
                double[][] randomNumbersDouble = new double[numberOfRows][];
                for (int i = 0; i < numberOfRows; i++)
                {
                    randomNumbersDouble[i] = new double[numberOfRows + 1];
                    for (int j = 0; j < numberOfRows + 1; j++)
                    {
                        randomNumbersDouble[i][j] = (double)RandomNumbers[i][j] / 65536;
                    }
                }
                return((T[][])Convert.ChangeType(randomNumbersDouble, typeof(T[][])));
            }

            default:
            {
                float[][] randomNumbersFloat = new float[numberOfRows][];
                for (int i = 0; i < numberOfRows; i++)
                {
                    randomNumbersFloat[i] = new float[numberOfRows + 1];
                    for (int j = 0; j < numberOfRows + 1; j++)
                    {
                        randomNumbersFloat[i][j] = (float)RandomNumbers[i][j] / 65536;
                    }
                }
                return((T[][])Convert.ChangeType(randomNumbersFloat, typeof(T[][])));
            }
            }
        }
Example #7
0
        public FractionType[][] GetMatrixWithSpecialTypeData(int numberOfRows)
        {
            var specialTypeRandomNumbers = new FractionType[numberOfRows][];

            for (int i = 0; i < numberOfRows; i++)
            {
                specialTypeRandomNumbers[i] = new FractionType[numberOfRows + 1];
                for (int j = 0; j < numberOfRows + 1; j++)
                {
                    specialTypeRandomNumbers[i][j].Numerator   = RandomNumbers[i][j];
                    specialTypeRandomNumbers[i][j].Denominator = 65536;
                }
            }

            return(specialTypeRandomNumbers);
        }
Example #8
0
        private void SwapRowWithRowWithMaxValue(int i, FractionType[][] Matrix)
        {
            FractionType maxValue = Matrix[i][i];
            int          position = i;

            for (var k = i + 1; k < NumberOfElements; k++)
            {
                if (maxValue < Matrix[k][i])
                {
                    maxValue = Matrix[k][i];
                    position = k;
                }
            }
            var tmp = Matrix[i];

            Matrix[i]        = Matrix[position];
            Matrix[position] = tmp;
        }
Example #9
0
        public FractionType[] CalculateError(FractionType[][] MatrixCopy, FractionType[] VectorX)
        {
            var          diff   = new FractionType[MatrixCopy.Length];
            FractionType result = new FractionType();

            for (var i = 0; i < MatrixCopy.Length; i++)
            {
                for (int j = 0; j < MatrixCopy[i].Length - 1; j++)
                {
                    result += VectorX[j] * MatrixCopy[i][j];
                }

                diff[i]            = MatrixCopy[i][MatrixCopy.Length] - result;
                result.Numerator   = 0;
                result.Denominator = 0;
            }
            return(diff);
        }
Example #10
0
        public static T Abs <T>(T number)
        {
            if (typeof(T) == typeof(FractionType))
            {
                FractionType tmp = (FractionType)Convert.ChangeType(number, typeof(FractionType));
                if (tmp.Numerator > 0)
                {
                    return(number);
                }
                tmp.Numerator *= (-1);
                return((T)Convert.ChangeType(tmp, typeof(T)));
            }

            dynamic dynNumber = number;

            if (dynNumber > 0)
            {
                return(number);
            }
            return(dynNumber * (-1));
        }
Example #11
0
        private void SwapWithhMaxValue(int i, FractionType[][] Matrix, ref int[] indexTable)
        {
            FractionType maxValue       = Matrix[i][i];
            int          rowPosition    = i;
            int          columnPosition = i;

            for (var k = i; k < NumberOfElements; k++)
            {
                for (int l = i; l < NumberOfElements; l++)
                {
                    if (maxValue < Matrix[k][l])
                    {
                        maxValue       = Matrix[k][l];
                        rowPosition    = k;
                        columnPosition = l;
                    }
                }
            }
            //swap rows
            var tmp = Matrix[i];

            Matrix[i]           = Matrix[rowPosition];
            Matrix[rowPosition] = tmp;

            int index = indexTable[i];

            indexTable[i] = indexTable[columnPosition];
            indexTable[columnPosition] = index;

            for (int k = 0; k < NumberOfElements; k++)
            {
                var cellValue = Matrix[k][i];
                Matrix[k][i] = Matrix[k][columnPosition];
                Matrix[k][columnPosition] = cellValue;
            }
        }
Example #12
0
        public void CheckH3(int numberOfElements, int numberOfTests)
        {
            #region Fractiontype
            long[] algTimes = new long[3];

            AlgorithmGen <FractionType> algorithmFracType = new AlgorithmGen <FractionType>(numberOfElements);
            AlgorithmGen <float>        algorithm         = new AlgorithmGen <float>(numberOfElements);

            for (int pf = 0; pf < numberOfTests; pf++)
            {
                FractionType[][] matrixFracCopyToCompare = DataSource.GetMatrixData <FractionType>(numberOfElements);
                FractionType[][] MatrixFracType          = DataSource.GetMatrixData <FractionType>(numberOfElements);
                FractionType[][] Matrix2FracType         = DataSource.GetMatrixData <FractionType>(numberOfElements);;
                FractionType[][] Matrix3FracType         = DataSource.GetMatrixData <FractionType>(numberOfElements);;


                float[][] matrixCopy = DataSource.GetMatrixData <float>(numberOfElements);


                #region GaussTypeNormal
                FractionType res1 = new FractionType();

                //get alg time
                var vectorResultFracType         = algorithmFracType.GaussElimination(MatrixFracType, new FractionType(), EnumsContainer.GaussType.Normal);
                var varctorResultFracTypeToFloat = new float[numberOfElements];
                if (vectorResultFracType != null)
                {
                    for (int m = 0; m < vectorResultFracType.Length; m++)
                    {
                        while (vectorResultFracType[m].Numerator.ToString().Length > 9 || vectorResultFracType[m].Denominator.ToString().Length > 9)
                        {
                            vectorResultFracType[m].Numerator   /= 10;
                            vectorResultFracType[m].Denominator /= 10;
                        }
                        varctorResultFracTypeToFloat[m] = ((float)vectorResultFracType[m].Numerator / (float)vectorResultFracType[m].Denominator);
                    }

                    var rtnFracTypeError = algorithmFracType.CalculateError(matrixFracCopyToCompare, vectorResultFracType);
                    algorithmFracType.PrintResult(rtnFracTypeError);
                }

                #endregion


                #region GaussPartialPivot

                var vectorResult2 = algorithmFracType.GaussElimination(Matrix2FracType, new FractionType(), EnumsContainer.GaussType.PartialPivot);
                var verctorResultFracTypePartialPivot = new float[numberOfElements];
                if (vectorResult2 != null)
                {
                    for (int m = 0; m < vectorResult2.Length; m++)
                    {
                        while (vectorResult2[m].Numerator.ToString().Length > 9 || vectorResult2[m].Denominator.ToString().Length > 9)
                        {
                            vectorResult2[m].Numerator   /= 10;
                            vectorResult2[m].Denominator /= 10;
                        }
                        verctorResultFracTypePartialPivot[m] = (float)((float)vectorResult2[m].Numerator / (float)vectorResult2[m].Denominator);
                    }

                    var rtnFracTypeError = algorithmFracType.CalculateError(matrixFracCopyToCompare, vectorResultFracType);
                    algorithmFracType.PrintResult(rtnFracTypeError);
                }


                #endregion


                #region GaussTypeFullPivot


                int[] indexArray = new int[numberOfElements];
                for (int i = 0; i < numberOfElements; i++)
                {
                    indexArray[i] = i;
                }
                var vectorResult3 = algorithmFracType.GaussElimination(Matrix3FracType, new FractionType(), EnumsContainer.GaussType.FullPivot, indexArray);
                var verctorResultFracTypeFullpivot = new float[numberOfElements];
                if (vectorResult3 != null)
                {
                    for (int m = 0; m < vectorResult3.Length; m++)
                    {
                        while (vectorResult3[m].Numerator.ToString().Length > 9 || vectorResult3[m].Denominator.ToString().Length > 9)
                        {
                            vectorResult3[m].Numerator   /= 10;
                            vectorResult3[m].Denominator /= 10;
                        }
                        verctorResultFracTypeFullpivot[m] = ((float)vectorResult3[m].Numerator / (float)vectorResult3[m].Denominator);
                    }

                    var rtnFracTypeError = algorithmFracType.CalculateError(matrixFracCopyToCompare, vectorResultFracType);
                    algorithmFracType.PrintResult(rtnFracTypeError);
                }


                #endregion
            }

            #endregion
        }
        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);
        }
Example #14
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);
        }