Ejemplo n.º 1
0
        /// <summary>
        /// Put the matrix in sorted form
        /// </summary>
        /// <param name="m"></param>
        /// <param name="constructor">The constructor for a null element of T</param>
        /// <param name="toIgnore"></param>
        /// <returns></returns>
        private static Matrix <T> ToSortedForm <T>(
            this Matrix <T> m, Func <T> constructor, int toIgnore = 0)
            where T : FieldMember, new()
        {
            Matrix <T> n = m.SortNullRows(constructor, toIgnore);

            int rowsSorted = -1;

            // Sort all the rows by switching
            while (!n.IsSorted(constructor, toIgnore))
            {
                rowsSorted++;

                List <int> rows = Enumerable.Range(rowsSorted,
                                                   n.Height - n.AmountOfNullRows(toIgnore) - rowsSorted).ToList();
                int min  = rows.Select(i => n.GetPivot(i, constructor, toIgnore)).Min();
                int next = rows.First(i => n.GetPivot(i, constructor, toIgnore) == min);

                if (next == rowsSorted)
                {
                    continue;
                }

                RowOperation <T> .SwitchingOperation(next, rowsSorted).ActOn(n);
            }

            return(n);
        }
Ejemplo n.º 2
0
        public static Matrix <T> ToEchelonForm <T>(
            this Matrix <T> m,
            Func <T> constructor,
            int toIgnore = 0)
            where T : FieldMember, new()
        {
            Matrix <T> n = m.ToSortedForm(constructor);

            int from = -1;

            // Put the rows in echelon form (some substitution will be required)
            while (!n.IsEchelon(constructor, toIgnore))
            {
                from++;
                // Substitution can unsort the rows
                if (!n.IsSorted(constructor, toIgnore))
                {
                    n = n.ToSortedForm(constructor, toIgnore);
                }

                List <int> rows = Enumerable.Range(from + 1,
                                                   n.Height - n.AmountOfNullRows(toIgnore) - from - 1).ToList();
                int pivot = n.GetPivot(from, constructor, toIgnore);

                foreach (var to in rows)
                {
                    if (n.GetPivot(from, constructor, toIgnore) <
                        n.GetPivot(to, constructor, toIgnore))
                    {
                        continue;
                    }
                    if (n.GetPivot(from, constructor, toIgnore) ==
                        n.GetPivot(to, constructor, toIgnore))
                    {
                        try
                        {
                            T scale = n[to, pivot].Negative <T>().Multiply(n[from, pivot].Inverse <T>());
                            RowOperation <T> .SubstitutionOperation(from, to, scale).ActOn(n);
                        }
                        catch (IncompatibleOperationException)
                        {
                            throw new ImpossibleEchelonFormException();
                        }
                    }
                    if (n.GetPivot(from, constructor, toIgnore) >
                        n.GetPivot(to, constructor, toIgnore) &&
                        n.GetPivot(to, constructor, toIgnore) != -1)
                    {
                        RowOperation <T> .SwitchingOperation(from, to).ActOn(n);
                    }
                }
            }

            return(n);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Put all the null rows on the bottom of the matrix
        /// </summary>
        /// <param name="m"></param>
        /// <param name="constructor"></param>
        /// <param name="toIgnore"></param>
        /// <returns></returns>
        private static Matrix <T> SortNullRows <T>(
            this Matrix <T> m, Func <T> constructor, int toIgnore = 0)
            where  T : FieldMember, new()
        {
            // First clone the matrix so that the original is not changed
            Matrix <T> n = new Matrix <T>(m);

            // Put all null rows on the bottom of the matrix
            while (!n.AreNullRowsBelow(constructor, toIgnore))
            {
                List <int> toSwitch = n.MisplacedNullRows(constructor, toIgnore);

                int next = toSwitch.First();

                // Switch the null row with the last non-null row
                RowOperation <T> .SwitchingOperation(next, n.Height + toSwitch.Count -
                                                     n.AmountOfNullRows(toIgnore) - 1).ActOn(n);
            }

            return(n);
        }