Exemplo n.º 1
0
        private void Button1_Click(object sender, EventArgs e)
        {
            if (Operaciones.Text != "")
            {
                Operaciones.Clear();
                Operaciones.Focus();
            }

            GaussJordan gauss         = new GaussJordan();
            int         cantelementos = int.Parse(textBox1.Text);
            int         pointx        = 30;
            int         pointy        = 70;

            panel2.Controls.Clear();

            for (int j = 0; j < int.Parse(textBox1.Text); j++)
            {
                for (int i = 0; i < int.Parse(textBox1.Text); i++)
                {
                    TextBox text   = new TextBox();
                    string  nombre = "txt" + j + i;
                    text.Name      = nombre;
                    text.BackColor = Color.PaleVioletRed;
                    text.Location  = new Point(pointx, pointy);
                    text.Size      = new Size(60, 100);
                    panel2.Controls.Add(text);
                    pointy += 40;
                }

                pointx += 70;
                pointy  = 70;
            }
            for (int u = 0; u < cantelementos; u++)
            {
                TextBox text   = new TextBox();
                string  nombre = "txt" + u;
                text.Name      = nombre;
                text.BackColor = Color.Violet;
                text.Location  = new Point(pointx, pointy);
                text.Size      = new Size(60, 100);
                panel2.Controls.Add(text);
                pointy += 40;
            }
        }
Exemplo n.º 2
0
        private void Button2_Click(object sender, EventArgs e)
        {
            Salida sali     = new Salida();
            int    cantelem = int.Parse(textBox1.Text);

            double [] vect = new double [cantelem];
            double[,] matriz = new double[cantelem, cantelem + 1];

            double[] v = new double[(cantelem * cantelem) + cantelem];
            int      i = 0;

            foreach (TextBox txt in panel2.Controls.OfType <TextBox>())
            {
                v[i] = Convert.ToDouble(txt.Text);
                i++;
            }

            int xv = 0;

            for (int ym = 0; ym < cantelem + 1; ym++)
            {
                for (int xm = 0; xm < cantelem; xm++)
                {
                    matriz[xm, ym] = v[xv];
                    xv++;
                }
            }

            GaussJordan gauss = new GaussJordan();

            vect = gauss.Gauss(matriz, cantelem);
            for (int x = 0; x < cantelem; x++)
            {
                Operaciones.Text += "Valor x" + x + " : " + vect[x] + Environment.NewLine;
            }
        }
Exemplo n.º 3
0
 public static Matrix Invert(Matrix matrix)
 {
   return GaussJordan.Invert(matrix);
 }
Exemplo n.º 4
0
        public void T01_BasicLinearEquations()
        {
            const double Accuracy = 2.2205e-15, SvdAccuracy = 4.46e-15, DeterminantAccuracy = 1.2e-13;

            // given w=-1, x=2, y=-3, and z=4...
            // w - 2x + 3y - 5z = -1 - 4 - 9 - 20 = -34
            // w + 2x + 3y + 5z = -1 + 4 - 9 + 20 = 14
            // 5w - 3x + 2y - z = -5 - 6 - 6 - 4  = -21
            // 5w + 3x + 2y + z = -5 + 6 - 6 + 4  = -1
            double[] coefficientArray = new double[16]
            {
                1, -2, 3, -5,
                1, 2, 3, 5,
                5, -3, 2, -1,
                5, 3, 2, 1,
            };
            double[] valueArray = new double[4] {
                -34, 14, -21, -1
            };

            // first solve using Gauss-Jordan elimination
            Matrix4 coefficients = new Matrix4(coefficientArray);
            Matrix  gjInverse, values = new Matrix(valueArray, 1);
            Matrix  solution = GaussJordan.Solve(coefficients.ToMatrix(), values, out gjInverse);

            CheckSolution(solution, Accuracy);
            CheckSolution(gjInverse * values, Accuracy); // make sure multiplying by the matrix inverse also gives the right answer

            // then solve using LU decomposition
            LUDecomposition lud = new LUDecomposition(coefficients.ToMatrix());

            solution = lud.Solve(values);
            CheckSolution(solution, Accuracy);
            CheckSolution(lud.GetInverse() * values, Accuracy); // check that the inverse can be multiplied to produce a good solution

            solution.Multiply(1.1);                             // mess up the solution
            lud.RefineSolution(values, solution);               // test that refinement can fix it
            CheckSolution(solution, Accuracy);

            // check that the computed determinants are what we expect
            bool negative;

            Assert.AreEqual(coefficients.GetDeterminant(), lud.GetDeterminant(), DeterminantAccuracy);
            Assert.AreEqual(Math.Log(Math.Abs(coefficients.GetDeterminant())), lud.GetLogDeterminant(out negative), Accuracy);
            Assert.AreEqual(coefficients.GetDeterminant() < 0, negative);

            // check that the inverses are about the same from both methods
            Assert.IsTrue(gjInverse.Equals(lud.GetInverse(), Accuracy));

            // then solve using QR decomposition
            QRDecomposition qrd = new QRDecomposition(coefficients.ToMatrix());

            solution = qrd.Solve(values);
            CheckSolution(solution, Accuracy);
            CheckSolution(qrd.GetInverse() * values, Accuracy); // check that the inverse can be multiplied to produce a good solution
            // TODO: test qrd.Update()

            // finally, try solving using singular value decomposition
            SVDecomposition svd = new SVDecomposition(coefficients.ToMatrix());

            solution = svd.Solve(values);
            CheckSolution(solution, SvdAccuracy);
            CheckSolution(svd.GetInverse() * values, SvdAccuracy);
            Assert.IsTrue(gjInverse.Equals(svd.GetInverse(), Accuracy));
            Assert.AreEqual(4, svd.GetRank());
            Assert.AreEqual(0, svd.GetNullity());
        }