示例#1
0
        /// <summary>
        /// Method is used to substitute matrix column with other values.
        /// </summary>
        /// <param name="matrix">Initial matrix for substitution.</param>
        /// <param name="columnIndex">Index of a column which will be substituted.</param>
        /// <param name="newColumn">New values of the column.</param>
        /// <returns></returns>
        public static MatrixT <T> SubstituteMatrixColumn(MatrixT <T> matrix, int columnIndex, List <T> newColumn)
        {
            if (newColumn.Count != matrix.Rows)
            {
                throw new ArgumentException($"Amount of matrix rows: {matrix.Rows} and amount of matrix columns: {newColumn.Count}");
            }

            MatrixT <T> result = new MatrixT <T>(matrix.Rows, matrix.Columns);

            MatrixT <T> .CopyMatrixItems(matrix, result);

            for (int i = 0; i < result.Rows; i++)
            {
                result[i, columnIndex] = newColumn[i];
            }

            return(result);
        }
示例#2
0
        private LAEAnswer CalculateGaussMethod(out List <LAEVariable> results)
        {
            results = null;

            MatrixT <double> currentMatrix = new MatrixT <double>(this.Matrix.Rows, this.Matrix.Columns);

            MatrixT <double> .CopyMatrixItems(this.Matrix, currentMatrix);

            double[] rightPart = new double[this.RightPartEquations.Count];
            for (int i = 0; i < this.RightPartEquations.Count; i++)
            {
                rightPart[i] = this.RightPartEquations[i];
            }

            double[] answer = new double[this.Variables.Count];

            if (currentMatrix.Rows != currentMatrix.Columns)
            {
                results = null;
                return(LAEAnswer.NoSolutions);
            }

            for (int i = 0; i < currentMatrix.Rows - 1; i++)
            {
                SortRows(currentMatrix, ref rightPart, i);

                for (int j = i + 1; j < currentMatrix.Rows; j++)
                {
                    if (currentMatrix[i, i] != 0)
                    {
                        double multElement = currentMatrix[j, i] / currentMatrix[i, i];

                        for (int k = i; k < currentMatrix.Columns; k++)
                        {
                            currentMatrix[j, k] -= currentMatrix[i, k] * multElement;
                        }

                        rightPart[j] -= rightPart[i] * multElement;
                    }
                }
            }

            for (int i = currentMatrix.Rows - 1; i >= 0; i--)
            {
                answer[i] = rightPart[i];

                for (int j = currentMatrix.Rows - 1; j > i; j--)
                {
                    answer[i] -= currentMatrix[i, j] * answer[j];
                }

                if (currentMatrix[i, i] == 0)
                {
                    if (rightPart[i] == 0)
                    {
                        return(LAEAnswer.ManySolutions);
                    }
                    else
                    {
                        return(LAEAnswer.NoSolutions);
                    }
                }

                answer[i] /= currentMatrix[i, i];
            }

            results = new List <LAEVariable>();
            for (int i = 0; i < this.Variables.Count; i++)
            {
                results.Add(new LAEVariable(this.Variables[i].Name, answer[i]));
            }

            return(LAEAnswer.OneSolution);
        }