示例#1
0
        /// <summary>
        /// The partial gauss with swapping rows of equation.
        /// </summary>
        /// <param name="matrix">
        /// The matrix.
        /// </param>
        /// <param name="vector">
        /// The vector.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="MyMatrix{T}"/>.
        /// </returns>
        public static MyMatrix <T> PartialGauss <T>(MyMatrix <T> matrix, MyMatrix <T> vector)
            where T : Value <T>, new()
        {
            var m = new MyMatrix <T>(matrix);
            var v = new MyMatrix <T>(vector);

            for (int i = 0; i < vector.Height; i++)
            {
#if DEBUG
                Console.WriteLine($"Partial GAUSS: ROW:{i}/{vector.Height}");
#endif
                int rowToSwap = i;
                for (int j = i + 1; j < vector.Height; j++)
                {
                    if (m[j, i].Absolute() > m[rowToSwap, i].Absolute())
                    {
                        rowToSwap = j;
                    }
                }

                if (rowToSwap != i)
                {
                    // Swap rows
                    m.SwapRows(rowToSwap, i);
                    v.SwapRows(rowToSwap, i);
                }

                CreateStep(m, v, i);
            }

            return(RetrieveResult(m, v));
        }
示例#2
0
        /// <summary>
        /// The full gauss with swapping rows and columns of equation.
        /// </summary>
        /// <param name="matrix">
        /// The matrix.
        /// </param>
        /// <param name="vector">
        /// The vector.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="MyMatrix{T}"/>.
        /// </returns>
        public static MyMatrix <T> FullGauss <T>(MyMatrix <T> matrix, MyMatrix <T> vector)
            where T : Value <T>, new()
        {
            var m = new MyMatrix <T>(matrix);
            var v = new MyMatrix <T>(vector);

            int[] columnsLocations = Enumerable.Range(0, vector.Height).ToArray();
            for (int i = 0; i < vector.Height; i++)
            {
#if DEBUG
                Console.WriteLine($"Full GAUSS: ROW:{i}/{vector.Height}");
#endif
                int rowToSwap    = i;
                int columnToSwap = i;

                // Select best column and row to swap.
                for (int j = i + 1; j < vector.Height; j++)
                {
                    for (int k = i; k < matrix.Width; k++)
                    {
                        if (m[j, k].Absolute() > m[rowToSwap, columnToSwap].Absolute())
                        {
                            rowToSwap    = j;
                            columnToSwap = k;
                        }
                    }
                }

                if (rowToSwap != i || columnToSwap != i)
                {
                    // Swap information about columns for future reading result.
                    columnsLocations.Swap(columnToSwap, i);

                    // Swap rows and columns
                    m.SwapRows(rowToSwap, i);
                    m.SwapColumns(columnToSwap, i);
                    v.SwapRows(rowToSwap, i);
                }

                CreateStep(m, v, i);
            }

            // Retrieve result and sort it according to the columns locations
            var result         = RetrieveResult(m, v);
            var originalResult = new MyMatrix <T>(result);
            for (int j = 0; j < v.Height; j++)
            {
                originalResult[columnsLocations[j], 0] = result[j, 0];
            }

            return(originalResult);
        }