コード例 #1
0
ファイル: Matrix.cs プロジェクト: AnetaBaloyan/Matrices
        /// <summary>
        /// Cancelles the kth row of the given matrix.
        /// </summary>
        /// <returns>The matrix with cancelled row.</returns>
        /// <param name="a">The matrix.</param>
        /// <param name="k">The index of row to divide.</param>
        private static Matrix DivideRow(Matrix a, int k)
        {
            Matrix result;

            Matrix.Assign(out result, a);

            int last = result.rows - 1;

            while (result.matrix[k, k] == 0 && last != k)
            {
                for (int j = k; j < a.columns; j++)
                {
                    double temp = result.matrix[k, j];
                    result.matrix[k, j]    = result.matrix[last, j];
                    result.matrix[last, j] = temp;
                }
                //Console.WriteLine("switch " + k + "th with the " + last + "th" + "\n");
                //result.print();

                last--;
            }

            if (result.matrix[k, k] != 0)
            {
                double divisor = result.matrix[k, k];
                for (int j = k; j < a.columns; j++)
                {
                    result.matrix[k, j] /= divisor;
                }
            }

            return(result);
        }
コード例 #2
0
ファイル: Matrix.cs プロジェクト: AnetaBaloyan/Matrices
        /// <summary>
        /// Subtracts the row from all other rows of the given matrix.
        /// </summary>
        /// <returns>The the resulting matrix.</returns>
        /// <param name="a">The matrix.</param>
        /// <param name="k">The index of the row.</param>
        private static Matrix SubtractAll(Matrix a, int k)
        {
            Matrix result;

            Matrix.Assign(out result, a);

            for (int i = 0; i < a.rows; i++)
            {
                if (i != k)
                {
                    double argument = result.matrix[i, k];
                    for (int j = 0; j < result.columns; j++)
                    {
                        if (argument != 0)
                        {
                            result.matrix[i, j] -= (argument * result.matrix[k, j]);
                        }
                    }
                }
            }

            return(result);
        }