示例#1
0
        private static void InnerRREchelon(MatrisBase <object> A, MatrisBase <object> result)
        {
            int lead = 0;
            int nr   = A.Row;
            int nc   = A.Col;

            for (int r = 0; r < nr; r++)
            {
                if (nc <= lead)
                {
                    break;
                }
                int i = r;
                while (float.Parse(result[i, lead].ToString()) == (float)0.0)
                {
                    i++;
                    if (nr == i)
                    {
                        i = r;
                        lead++;
                        if (nc == lead)
                        {
                            break;
                        }
                    }
                }

                if (nc == lead)
                {
                    break;
                }
                result.Swap(i, r, 0, 0);

                if (float.Parse(result[r, lead].ToString()) != (float)0.0)
                {
                    result.MulRow(r, (float)1.0 / float.Parse(result[r, lead].ToString()), 0);
                }
                for (int j = 0; j < nr; j++)
                {
                    if (j != r)
                    {
                        for (int k = 0; k < nc; k++)
                        {
                            result.MulThenSubFromOtherRow(r, float.Parse(result[j, lead].ToString()), j, 0);
                        }
                    }
                }
                lead++;
            }

            result.FixMinusZero();
        }
示例#2
0
        private void InnerEchelon(MatrisBase <object> A, MatrisBase <object> result)
        {
            int                   nr             = A.Row;
            int                   nc             = A.Col;
            List <int>            zeroCols       = new List <int>();
            List <List <object> > filteredResult = A.Copy().GetValues();

            for (int j = 0; j < nc; j++)
            {
                if (A.IsZeroCol(j, 0, (float)0.0))
                {
                    for (int i = 0; i < nr; i++)
                    {
                        filteredResult[i].RemoveAt(j - zeroCols.Count);
                    }

                    zeroCols.Add(j);
                    nc--;
                }
            }

            result.SetValues(filteredResult);

            for (int r = 0; r < nr; r++)
            {
                if (result.IsZeroRow(r, 0, (float)0.0))
                {
                    result.SwapToEnd(r, 0);
                    nr--;
                }
            }

            int  p = 0;
            bool next;
            int  swapCount = 0;

            while (p < nr && p < nc)
            {
                next = false;
                int r = 1;
                while (float.Parse(result.GetValues()[p][p].ToString()) == (float)0.0)
                {
                    if (p + 1 < nr)
                    {
                        if (result.IsZeroRow(p, 0, (float)0.0))
                        {
                            nr--;
                        }

                        if (!Sub(result, p, nr, p, p + 1, 0).IsZeroCol(0, 0))
                        {
                            for (int ri = p + 1; ri < nr; ri++)
                            {
                                if (Math.Abs(float.Parse(result.GetValues()[ri][p].ToString())) > 1e-6)
                                {
                                    result.Swap(p, ri, based: 0);
                                    swapCount++;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            p++;
                        }
                        next = true;
                        break;
                    }
                    else
                    {
                        // Zeros to bottom
                        for (int i = 0; i < A.Row; i++)
                        {
                            if (result.IsZeroRow(i, 0, (float)0.0))
                            {
                                result.SwapToEnd(i, 0);
                            }
                        }
                        // Restore zero columns
                        if (zeroCols.Count > 0)
                        {
                            foreach (int j in zeroCols)
                            {
                                for (int i = 0; i < result.Row; i++)
                                {
                                    result.GetValues()[i].Insert(j, (dynamic)(float)0.0);
                                }
                            }
                            result.SetCol(A.Col);
                        }
                        result.FixMinusZero();
                        result.SwapCount = swapCount;
                        return;
                    }
                }

                if (next)
                {
                    continue;
                }

                for (; r >= 1 && r < (nr - p); r++)
                {
                    if (float.Parse(result.GetValues()[p + r][p].ToString()) != (float)0.0)
                    {
                        float x = -(float.Parse(result.GetValues()[p + r][p].ToString()) / float.Parse(result.GetValues()[p][p].ToString()));
                        for (int c = p; c < nc; c++)
                        {
                            result.GetValues()[p + r][c] = (dynamic)((float.Parse(result.GetValues()[p][c].ToString()) * x) + float.Parse(result.GetValues()[p + r][c].ToString()));
                        }
                    }
                }
                p++;
            }

            // Zeros to bottom
            for (int i = 0; i < A.Row; i++)
            {
                if (result.IsZeroRow(i, 0, (float)0.0))
                {
                    result.SwapToEnd(i, 0);
                }
            }
            // Restore zero columns
            if (zeroCols.Count > 0)
            {
                foreach (int j in zeroCols)
                {
                    for (int i = 0; i < result.Row; i++)
                    {
                        result.GetValues()[i].Insert(j, (dynamic)(float)0.0);
                    }
                }
                result.SetCol(A.Col);
            }

            result.FixMinusZero();
            result.SwapCount = swapCount;
        }