Пример #1
0
        public string GramSchmidt()
        {
            string output = "\\begin{align*}\n&" + GetRow(0).GetLaTeXLine() + "\\\\\n";

            for (int i = 1; i < n; i++)
            {
                MyMatrix    row    = GetRow(i);
                LaTeXExport export = new LaTeXExport(m + 1, "-", 1, begin: "&", end: (i < n - 1 ? "\\\\\n" : "\n"), new_line: "\\\\\n&");
                export.Add(row.GetLaTeXLine());
                for (int j = 0; j < i; j++)
                {
                    MyMatrix j_row = GetRow(j);
                    if (j_row.IsZero)
                    {
                        continue;
                    }
                    Rational scalar = ScalarProduct(GetRow(i), j_row),
                             square = ScalarProduct(j_row, j_row);
                    Rational.RemoveDivisor(ref scalar, ref square);
                    export.Add($"\\frac{{{scalar}}}{{{square}}}" + j_row.GetLaTeXLine());
                    row -= (scalar / square) * j_row;
                }
                export.Add(row.GetLaTeXLine(), "=", 1);
                output += export.Result;
                SetRow(i, row);
            }
            return(output + "\\end{align*}");
        }
Пример #2
0
        public string ImBasis()
        {
            MyMatrix    row_echelon = Clone();
            string      output      = row_echelon.GaussLaTeX(revert: false) + '\n';
            LaTeXExport export      = new LaTeXExport(1, ",", 1, false, "$$\\text{Im basis}:\\ ");

            for (int i = 0; i < n; i++)
            {
                int j_s = 0;
                for (; j_s < m && row_echelon[i, j_s] == 0; j_s++)
                {
                    ;
                }
                if (j_s >= m)
                {
                    break;
                }
                export.Add(GetColumn(j_s).GetLaTeX());
            }
            if (export.Empty)
            {
                export.Add(GetColumn(0).GetLaTeX());
            }
            return(output + export.Result);
        }
Пример #3
0
        public string KerBasis()
        {
            MyMatrix    row_echelon = Clone();
            string      output      = row_echelon.GaussLaTeX() + '\n';
            LaTeXExport export      = new LaTeXExport(1, ",", 1, false, "$$\\ker\\ \\text{basis}:\\ ");
            List <int>  pivots      = new List <int>();

            for (int i = 0; i < n; i++)
            {
                int j_s = 0;
                for (; j_s < m && row_echelon[i, j_s] == 0; j_s++)
                {
                    ;
                }
                if (j_s >= m)
                {
                    break;
                }
                pivots.Add(j_s);
            }
            int j_free = 0;

            for (int j = 0; j < m - pivots.Count; j++, j_free++)
            {
                for (; pivots.Contains(j_free); j_free++)
                {
                    ;
                }
                MyMatrix basis_el = new MyMatrix(m, 1);
                basis_el[j_free, 0] = 1;
                for (int i = 0; i < pivots.Count; i++)
                {
                    basis_el[pivots[i], 0] = -row_echelon[i, j_free];
                }
                export.Add(basis_el.GetLaTeX());
            }
            if (export.Empty)
            {
                export.Add(new MyMatrix(m, 1).GetLaTeX());
            }
            return(output + export.Result);
        }
Пример #4
0
        public string GaussLaTeX(int max_m = -1, bool revert = true)
        {
            if (max_m == -1)
            {
                max_m = m;
            }
            LaTeXExport export = new LaTeXExport(m);

            export.Add(GetLaTeX(max_m));
            bool need_entry = false;

            for (int i = 0; i < Math.Min(n, max_m); i++)
            {
                int j_start = i - 1, i_start;
                do
                {
                    j_start++;
                    for (i_start = i; i_start < n &&
                         values[i_start, j_start] == 0; i_start++)
                    {
                        ;
                    }
                } while (j_start < max_m - 1 && i_start == n);
                if (i_start == n && j_start == max_m - 1)
                {
                    break;
                }
                ElementarySwap(i, i_start);
                if (values[i, j_start] != 1)
                {
                    need_entry = true;
                }
                ElementaryProd(i, 1 / values[i, j_start]);
                if (i != i_start)
                {
                    export.Add(GetLaTeX(max_m));
                    need_entry = false;
                }
                bool done_sum = false;
                for (int i_down = i + 1; i_down < n; i_down++)
                {
                    if (values[i_down, j_start] != 0)
                    {
                        ElementarySum(i_down, i, -values[i_down, j_start] / values[i, j_start]);
                        done_sum = true;
                    }
                }
                if (done_sum)
                {
                    export.Add(GetLaTeX(max_m));
                    need_entry = false;
                }
            }
            if (revert)
            {
                for (int i = n - 1; i >= 0; i--)
                {
                    int j_s = 0;
                    for (; j_s < max_m && values[i, j_s] == 0; j_s++)
                    {
                        ;
                    }
                    if (j_s >= max_m)
                    {
                        continue;
                    }
                    bool done_sum = false;
                    for (int i_up = i - 1; i_up >= 0; i_up--)
                    {
                        if (values[i_up, j_s] != 0)
                        {
                            ElementarySum(i_up, i, -values[i_up, j_s] / values[i, j_s]);
                            done_sum = true;
                        }
                    }
                    if (done_sum)
                    {
                        export.Add(GetLaTeX(max_m));
                        need_entry = false;
                    }
                }
            }
            if (need_entry)
            {
                export.Add(GetLaTeX(max_m));
            }
            return(export.Result);
        }
Пример #5
0
        public string SymmetricGauss()
        {
            if (!IsSquare)
            {
                throw new InvalidOperationException("Matrix is not square!");
            }
            if (!CheckSymmetry())
            {
                throw new InvalidOperationException("Matrix is not symmetric!");
            }
            LaTeXExport export = new LaTeXExport(m);

            export.Add(GetLaTeX());
            for (int i = 0; i < n; i++)
            {
                int i_start = i;
                for (; i_start < n && values[i_start, i_start] == 0; i_start++)
                {
                    ;
                }
                if (i_start == n)
                {
                    int j = i;
                    for (; j < n - 1; j++)
                    {
                        for (i_start = j + 1; i_start < n &&
                             values[i_start, j] == 0; i_start++)
                        {
                            ;
                        }
                        if (i_start < n)
                        {
                            break;
                        }
                    }
                    if (j == n - 1 && i_start == n)
                    {
                        break;
                    }
                    ElementarySum(j, i_start, 1);
                    ElementarySum(j, i_start, 1, false);
                    export.Add(GetLaTeX());
                    i_start = j;
                }
                if (i_start != i)
                {
                    ElementarySwap(i, i_start);
                    ElementarySwap(i, i_start, false);
                    export.Add(GetLaTeX());
                }
                for (int i_down = i + 1; i_down < n; i_down++)
                {
                    if (values[i_down, i] != 0)
                    {
                        ElementarySum(i_down, i, -values[i_down, i] / values[i, i]);
                        ElementarySum(i_down, i, -values[i, i_down] / values[i, i], false);
                        export.Add(GetLaTeX());
                    }
                }
            }
            bool need_entry = false;

            for (int i = 0; i < n; i++)
            {
                if (values[i, i] != 1 && values[i, i] != -1 && values[i, i] != 0)
                {
                    need_entry    = true;
                    values[i, i] /= values[i, i].Abs();
                }
            }
            if (need_entry)
            {
                export.Add(GetLaTeX());
            }
            return(export.Result);
        }