Exemplo n.º 1
0
        public static FractionValue[,] Alied(FractionValue[,] a, ref StringBuilder inDetal)
        {
            var res = new FractionValue[a.GetLength(0), a.GetLength(1)];

            for (var i = 0; i < a.GetLength(0); i++)
            {
                for (var j = 0; j < a.GetLength(1); j++)
                {
                    var sign  = ((i + 1 + j + 1) % 2 == 0) ? 1 : -1;
                    var minor = GetMinor(a, i, j);

                    inDetal.Append($"+ A[{i+1},{j+1}]] = {sign} * \r\n");
                    inDetal.Append($"{GetStringMatrixByArray(minor)} = \r\n");

                    var determinate = Determinate(minor, ref inDetal);
                    var product     = determinate * sign;

                    inDetal.Append($"+ A[{i + 1},{j + 1}]] = {sign} * {determinate}\r\n");
                    inDetal.Append($"+ A[{i + 1},{j + 1}]] = {product} \r\n\r\n\r\n");
                    res[i, j] = product;
                }
            }

            return(res);
        }
Exemplo n.º 2
0
        public static FractionValue Determinate(FractionValue[,] a, ref StringBuilder inDetal)
        {
            if (a.GetLength(0) == 2)
            {
                var res = a[0, 0] * a[1, 1] - a[0, 1] * a[1, 0];
                return(res);
            }

            FractionValue sum = new FractionValue(0, 1);

            for (var i = 0; i < a.GetLength(0); i++)
            {
                var minor = GetMinor(a, 0, i);
                var sign  = (((1 + i + 1) % 2 == 0) ? 1 : -1);

                inDetal.Append($"+ [{i}] {sign} * {a[0, i]} * \r\n");
                inDetal.Append($"{GetStringMatrixByArray(minor)} = \r\n");

                var determinate = Determinate(minor, ref inDetal);
                var product     = a[0, i] * determinate * sign;


                inDetal.Append($"+ [{i}] {a[0, i] * sign} * {determinate} = \r\n");
                inDetal.Append($"+ [{i}] {product} \r\n\r\n\r\n");
                sum += product;
            }

            return(sum);
        }
Exemplo n.º 3
0
        public static FractionValue[,] GetMinor(FractionValue[,] a, int row, int col)
        {
            var res  = new FractionValue[a.GetLength(0) - 1, a.GetLength(1) - 1];
            var resi = 0;
            var resj = 0;

            for (var i = 0; i < a.GetLength(0); i++)
            {
                if (i == row)
                {
                    continue;
                }
                resj = 0;
                for (var j = 0; j < a.GetLength(1); j++)
                {
                    if (col == j)
                    {
                        continue;
                    }
                    res[resi, resj] = a[i, j];
                    resj++;
                }

                resi++;
            }

            return(res);
        }
Exemplo n.º 4
0
        private void mulOnDigitButton_Click(object sender, EventArgs e)
        {
            var value = "";

            if (TMP.InputBox("Input FractalValue", "Input FractalValue like \"1/12\" or \"7\":", ref value) != DialogResult.OK)
            {
                return;
            }


            var           arr1 = Matrix.GetFractialMatrixFromDataGrid(matrix1DataGridView);
            FractionValue fractionValue;

            try
            {
                fractionValue = new FractionValue(value);
            }
            catch
            {
                MessageBox.Show($"I can not create FractionValue from this string \"{value}\"");
                return;;
            }

            var result = Matrix.MultiplicateOnFractionValue(arr1, fractionValue);

            Matrix.PrintFractionMatrixToDataGrid(matrixResDataGridView, result);
            resTextBox.Text = $"A * {fractionValue} = \r\n{Matrix.GetStringMatrixByArray(result)}";
        }
Exemplo n.º 5
0
        public static FractionValue operator /(FractionValue fvA, BigInteger b)
        {
            var fvB = new FractionValue(b, 1);

            fvA.Numerator   *= fvB.Denominator;
            fvA.Denominator *= fvB.Numerator;
            return(DownSize(fvA));
        }
Exemplo n.º 6
0
        public static FractionValue[,] Transposition(FractionValue[,] a)
        {
            var res = new FractionValue[a.GetLength(0), a.GetLength(1)];

            for (var i = 0; i < a.GetLength(0); i++)
            {
                for (var j = 0; j < a.GetLength(1); j++)
                {
                    res[i, j] = a[j, i];
                }
            }

            return(res);
        }
Exemplo n.º 7
0
        public static FractionValue[,] MultiplicateOnFractionValue(FractionValue[,] a, FractionValue b)
        {
            var res = new FractionValue[a.GetLength(0), a.GetLength(1)];

            for (var i = 0; i < a.GetLength(0); i++)
            {
                for (var j = 0; j < a.GetLength(1); j++)
                {
                    res[i, j] = a[i, j] * b;
                }
            }

            return(res);
        }
Exemplo n.º 8
0
        public static FractionValue operator -(FractionValue fvA, int b)
        {
            var fvB = new FractionValue(b, 1);

            if (fvA.Denominator != fvB.Denominator)
            {
                fvA.Numerator   *= fvB.Denominator;
                fvB.Numerator   *= fvA.Denominator;
                fvA.Denominator *= fvB.Denominator;
                fvA.Numerator   -= fvB.Numerator;
                return(DownSize(fvA));
            }

            fvA.Numerator -= fvB.Numerator;
            return(DownSize(fvA));
        }
Exemplo n.º 9
0
        public static FractionValue DownSize(FractionValue fV)
        {
            while (true)
            {
                if (fV.Numerator > -1 && fV.Numerator < 2 || fV.Denominator > -1 && fV.Denominator < 2)
                {
                    return(fV);
                }


                var sign = 0;
                if (fV.Numerator < 0)
                {
                    sign++;
                    fV.Numerator *= -1;
                }

                if (fV.Denominator < 0)
                {
                    sign++;
                    fV.Denominator *= -1;
                }

                var res = gcd(fV.Numerator, fV.Denominator);
                if (res == 1 || res > fV.Numerator || res > fV.Denominator)
                {
                    if (sign == 1)
                    {
                        fV.Numerator *= -1;
                    }
                    return(fV);
                }

                if (sign == 1)
                {
                    fV.Numerator *= -1;
                }
                fV.Numerator   /= res;
                fV.Denominator /= res;
            }
        }
Exemplo n.º 10
0
        public static FractionValue[,] GetFractialMatrixFromDataGrid(DataGridView dataGrid)
        {
            var res = new FractionValue[dataGrid.RowCount, dataGrid.ColumnCount];

            for (var i = 0; i < dataGrid.RowCount; i++)
            {
                for (var j = 0; j < dataGrid.ColumnCount; j++)
                {
                    try
                    {
                        res[i, j] = new FractionValue(dataGrid[j, i].Value.ToString());
                    }
                    catch/* (Exception e)*/
                    {
                        res[i, j]            = new FractionValue(1, 1);
                        dataGrid[j, i].Value = "1";
                    }
                }
            }

            return(res);
        }
Exemplo n.º 11
0
        public static FractionValue[,] Multiplication(FractionValue[,] a, FractionValue[,] b, ref StringBuilder inDetal)
        {
            var rows1 = a.GetLength(0);
            var cols2 = b.GetLength(1);
            var rows2 = b.GetLength(0);
            var res   = new FractionValue[rows1, cols2];

            for (var i = 0; i < rows1; i++)
            {
                for (var j = 0; j < cols2; j++)
                {
                    res[i, j] = new FractionValue(0, 1);
                    for (var k = 0; k < rows2; k++)
                    {
                        res[i, j] += a[i, k] * b[k, j];
                        inDetal.Append($"[{i},{j}] += {a[i, k]} * {b[k, j]} = {res[i, j]}\r\n");
                    }
                }
            }

            return(res);
        }
Exemplo n.º 12
0
 public FractionValue(FractionValue fV)
 {
     this.Numerator   = fV.Numerator;
     this.Denominator = fV.Denominator;
 }
Exemplo n.º 13
0
        private void exceptButton_Click(object sender, EventArgs e)
        {
            var arr = Matrix.GetFractialMatrixFromDataGrid(matrix3DataGridView);

            if (arr.GetLength(0) != arr.GetLength(1))
            {
                MessageBox.Show($"RowCount != ColCount");
                return;
            }

            tableLayoutPanel1.RowCount = 0;
            tableLayoutPanel1.Height   = 0;
            tableLayoutPanel1.RowStyles.Clear();
            ClearCreatedItems(dataGridList);
            dataGridList = new List <Control>();
            tableLayoutPanel1.RowCount = arr.GetLength(0);
            tableLayoutPanel1.Height   = matrix3DataGridView.Height * tableLayoutPanel1.RowCount;
            var strRes = new StringBuilder("");

            progressBar1.Maximum = arr.GetLength(0) * arr.GetLength(0) * arr.GetLength(1) * arr.GetLength(0) * 2;

            new Thread(() =>
            {
                for (var k = 0; k < arr.GetLength(0); k++)
                {
                    var dataGridViewLeft               = CreateDataGridView("LEFT", k);
                    var dataGridViewRight              = CreateDataGridView("RIGHT", k);
                    dataGridViewLeft.ContextMenuStrip  = dataGridContextMenuStrip;
                    dataGridViewRight.ContextMenuStrip = dataGridContextMenuStrip;

                    Invoke(new Action(() =>
                    {
                        dataGridList.Add(dataGridViewLeft);
                        dataGridViewLeft.RowCount    = arr.GetLength(0);
                        dataGridViewLeft.ColumnCount = arr.GetLength(1);

                        dataGridList.Add(dataGridViewRight);
                        dataGridViewRight.RowCount    = arr.GetLength(0);
                        dataGridViewRight.ColumnCount = arr.GetLength(1);

                        tableLayoutPanel1.Controls.Add(dataGridViewLeft, 0, k);
                        tableLayoutPanel1.Controls.Add(dataGridViewRight, 1, k);

                        tableLayoutPanel1.RowStyles.Add(new RowStyle()
                        {
                            Height   = 100,
                            SizeType = SizeType.Percent
                        });
                    }));



                    var detFractionValue = new FractionValue($"{arr[k, k]}");
                    strRes.Append($"K[{k + 1}] det = {arr[k, k]}\r\n");

                    for (var i = 0; i < arr.GetLength(0); i++)
                    {
                        for (var j = 0; j < arr.GetLength(1); j++)
                        {
                            if (i == k || j == k)
                            {
                                continue;
                            }

                            strRes.Append($"A[{i + 1},{j + 1}] = {arr[i, j]} * {detFractionValue} - {arr[k, j]}  * {arr[i, k]} = ");

                            arr[i, j] = arr[i, j] * detFractionValue - arr[k, j] * arr[i, k];

                            strRes.Append($"{arr[i, j]}\r\n");
                            Invoke(new Action(() =>
                            {
                                progressBar1.Value = k * arr.GetLength(0) * arr.GetLength(1) * arr.GetLength(0) * 2 + i * arr.GetLength(1) + j;
                                progressLabel.Text = $"{progressBar1.Value} / {progressBar1.Maximum} ({progressBar1.Value * 100.0 / progressBar1.Maximum:00}%)";
                            }));
                        }
                    }

                    for (var j = 0; j < arr.GetLength(0); j++)
                    {
                        if (j == k)
                        {
                            arr[k, k] = new FractionValue("1");
                            continue;
                        }

                        arr[k, j] *= -1;
                    }

                    Invoke(new Action(() =>
                    {
                        Matrix.PrintFractionMatrixToDataGrid(dataGridViewLeft, arr);
                    }));

                    for (var i = 0; i < arr.GetLength(0); i++)
                    {
                        for (var j = 0; j < arr.GetLength(1); j++)
                        {
                            strRes.Append($"A[{i}, {j}] = {arr[i, j]} / {detFractionValue} = ");
                            arr[i, j] /= detFractionValue;
                            strRes.Append($"{arr[i, j]}\r\n");
                            Invoke(new Action(() =>
                            {
                                progressBar1.Value = k * arr.GetLength(0) * arr.GetLength(1) * arr.GetLength(0) * 2 + i * arr.GetLength(1) + j + arr.GetLength(1) * 2;
                                progressLabel.Text = $"{progressBar1.Value} / {progressBar1.Maximum} ({progressBar1.Value * 100.0 / progressBar1.Maximum:00}%)";
                            }));
                        }
                    }

                    Invoke(new Action(() =>
                    {
                        Matrix.PrintFractionMatrixToDataGrid(dataGridViewRight, arr);
                    }));
                }


                Invoke(new Action(() =>
                {
                    panel3.AutoScroll      = false;
                    panel3.AutoScroll      = true;
                    resJordansTextBox.Text = strRes.ToString();
                    progressBar1.Maximum   = 0;
                    progressLabel.Text     = "100%";
                }));
            }).Start();
        }