Exemplo n.º 1
0
 private void factorLUToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         if (OutputMatrix != null)
         {
             OutputMatrix.FactorLU_withP();
             MMatrix P = new MMatrix(OutputMatrix.LU_P);
             MMatrix L = new MMatrix(OutputMatrix.LU_L);
             MMatrix U = new MMatrix(OutputMatrix.LU_U);
             string s = "\n\n\nFactor LU:\n";
             s += "\nP=\n" + P.PrintToString();
             s += "\nL=\n" + L.PrintToString();
             s += "\nU=\n" + U.PrintToString();
             s += "\nA=P*LU\n" + (P.Transpose()*L*U).PrintToString();
             RichText_Display(s);
         }
     }
     catch (MMatrixException exp)
     {
         MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
        private void buttonGenerateMatrix_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            int rows = (int)numericUpDownRow.Value;
            int cols = (int)numericUpDownCol.Value;

            min = (int)numericUpDownMin.Value;
            max = (int)numericUpDownMax.Value;

            if (radioButtonRandom.Checked)
                MainMatrix = MMatrix.RandomMatrix(rows, cols, min, max);
            else if (radioButtonDiagonal.Checked)
            {
                MainMatrix = MMatrix.DiagonalMatrix(rows, rand.Next(max));
                cols = rows;
            }
            else if (radioButtonIdentity.Checked)
            {
                MainMatrix = MMatrix.DiagonalMatrix(rows, 1);
                cols = rows;
            }
            else if (radioButtonSymetric.Checked)
            {
                MainMatrix = MMatrix.RandomMatrix(rows, cols, min, (int)Math.Sqrt(max));
                MainMatrix = MainMatrix * MainMatrix.Transpose();
                cols = rows;
            }

            //Matrix A
            this.dataGridViewMatrix.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            dataGridViewMatrix.SuspendLayout();
            dataGridViewMatrix.Columns.Clear();
            DataGridViewColumn[] DCS = new DataGridViewColumn[cols];
            for (int i = 0; i < cols; ++i)
            {
                DataGridViewColumn column = new DataGridViewTextBoxColumn();
                column.HeaderText = string.Concat("A", (i + 1).ToString());
                DCS[i] = column;
            }
            this.dataGridViewMatrix.Columns.AddRange(DCS);
            dataGridViewMatrix.Rows.Add(rows);

            //for DataGrid: [column, row]
            for (int j = 0; j < MainMatrix.col; ++j)
                for (int i = 0; i < MainMatrix.row; ++i)
                    dataGridViewMatrix[j,i].Value = MainMatrix[i, j];

            //this.dataGridViewMatrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dataGridViewMatrix.ResumeLayout();
            numericUpDownCol.Value = cols;

            //Vector b
            b = Vector.RandomVector(rows, min, max);
            dataGridViewVectorb.SuspendLayout();
            dataGridViewVectorb.Columns.Clear();
            dataGridViewVectorb.Columns.Add("b","b");
            dataGridViewVectorb.Columns[0].Width = ColWidth;
            dataGridViewVectorb.Rows.Add(rows);
            for (int i = 0; i < b.Elements.Length; ++i)
                dataGridViewVectorb[0, i].Value = b[i];
            dataGridViewVectorb.ResumeLayout();
        }
Exemplo n.º 3
0
        public void MatrixTest()
        {
            double[,] Arr1 = new double[4, 4] { { 1, 1, 0, 3 }, { 2, 1, -1, 1 }, { 3, -1, -1, 2 }, { -1, 2, 3, -1 } };
            double[,] Arr2 = new double[3, 3] { { 4, -1, 1 }, { -1, 4.25, 2.75 }, { 1, 2.75, 3.5 } };
            MMatrix A = new MMatrix(Arr1);
            MMatrix B = new MMatrix();
            MMatrix C = new MMatrix();
            string s = "";

            try
            {
                //A = MMatrix.RandomMatrix(size,0,10);

                s += "\nMatrix A=\n" + A.PrintToString();
                s += "\nDet (A)=                    \n" + Convert.ToString(A.Determinant()) + "\n";
                s += "\nA^-1=                    \n" + A.Inverse().PrintToString();
                s += "\nA*A^-1=                     \n" + (A * A.Inverse()).PrintToString();
                C = A * A.Transpose();
                s += "\nC=A*A^T=\n" + C.PrintToString();

                /////////////////Factor A = LDL*

                MMatrix.FactorLDLt(C);
                s += "\nFactor LDL*(C): L=\n" + (new MMatrix(C.LDL_L)).PrintToString();
                s += "\nFactor LDL*(C): D=\n" + (new MMatrix(C.LDL_D)).PrintToString();
                B = (new MMatrix(C.LDL_L)) * (new MMatrix(C.LDL_D)) * (new MMatrix(C.LDL_L)).Transpose();
                s += "\nFactor LDL*: C=L*D*L^T\n" + B.PrintToString();
                B = (new MMatrix(C.LDL_L)) * (new MMatrix(C.LDL_L)).Transpose();
                s += "\nFactor LDL*: C=L*L^T\n" + B.PrintToString();

                /////////////////Factor A = LU

                C.FactorLU();
                s += "\nFactor LU(C): L=\n" + (new MMatrix(C.LU_L)).PrintToString();
                s += "\nFactor LU(C): U=\n" + (new MMatrix(C.LU_U)).PrintToString();
                s += "\nFactor LU: C=L*U\n" + ((new MMatrix(C.LU_L)) * (new MMatrix(C.LU_U))).PrintToString();

                ///////////////////Factor A = PLU

                A.FactorLU_withP();
                s += "\nFactor LU (A): P=\n" + (new MMatrix(A.LU_P)).PrintToString();
                s += "\nFactor LU (A): L=\n" + (new MMatrix(A.LU_L)).PrintToString();
                s += "\nFactor LU (A): U=\n" + (new MMatrix(A.LU_U)).PrintToString();
                s += "\nFactor LU: A=PLU\n" + ((new MMatrix(A.LU_P)).Transpose() * (new MMatrix(A.LU_L)) * (new MMatrix(A.LU_U))).PrintToString();

                this.RichText_Display(s);
                OutputMatrix = C;
            }
            catch (MMatrixException exp)
            {
                MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Exemplo n.º 4
0
 private void factorLDLtToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         if (OutputMatrix != null)
         {
             OutputMatrix.FactorLDLt();
             MMatrix L = new MMatrix(OutputMatrix.LDL_L);
             MMatrix D = new MMatrix(OutputMatrix.LDL_D);
             string s = "\n\n\nFactor LDL*:\n";
             s += "\nL=\n" + L.PrintToString();
             s += "\nD=\n" + D.PrintToString();
             s += "\nA=LDL*\n" + (L*D*L.Transpose()).PrintToString();
             RichText_Display(s);
         }
     }
     catch (MMatrixException exp)
     {
         MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Exemplo n.º 5
0
 public static double L2Norm(MMatrix A)
 {
     return Math.Sqrt(MMatrix.SpectralRadius(A.Transpose() * A));
 }
Exemplo n.º 6
0
        public void EigenvectorTest()
        {
            double[,] Arr1 = new double[4, 4] { { 4, 1, -2, 2 }, { 1, 2, 0, 1 }, { -2, 0, 3, -2 }, { 2, 1, -2, -1 } };
            double[,] Arr2 = new double[2, 3] { { 3,1,1 }, { -1, 3,1 }};
            double[,] Arr3 = new double[5, 5] { { 2, 0, 8, 6, 0 }, { 1, 6, 0, 1, 7 }, { 5, 0, 7, 4, 0 }, { 7, 0, 8, 5, 0 }, { 0, 10, 0, 0, 7 } };
            double[,] Arr4 = new double[4, 4] { { 3, 1, 0, 1 }, { 1, 3, 1, 1 }, { 0, 1, 3, 1 }, { 1, 1, 1, 3 } };
            MMatrix A = new MMatrix(Arr2);
            MMatrix B = new MMatrix();
            string s = "";
            Vector v = new Vector();
            MMatrix Eigenvectors = new MMatrix();

            try
            {
                //A = MMatrix.RandomMatrix(3,0,255);
                B = A*A.Transpose();
                s += "\nAA*=\n" + B.PrintToString();

                B.Jacobi_Cyclic_Method(ref v, ref Eigenvectors);
                s += "\nBefore sort:";
                s += "\nEigenvalues of(A*At)=" + v.PrintToString();
                s += "\nEigenvectors of A*At:\n" + Eigenvectors.PrintToString();

                MMatrix.SortEigen(ref v, ref Eigenvectors);
                s += "\nAfter sort:";
                s += "\nEigenvalues of(A*At)=" + v.PrintToString();
                s += "\nEigenvectors of A*At:\n" + Eigenvectors.PrintToString();

                //This is not neccesary anymore since the eigenvectors are already orthonomalized
                s += "\nOrthonormalize eigenvectors(Ax = 0)\n" + MMatrix.Orthonormalize(Eigenvectors).PrintToString();

                this.RichText_Display(s);
            }
            catch (MMatrixException exp)
            {
                MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Exemplo n.º 7
0
        public static bool IsOrthogonal(MMatrix matrix)
        {
            MMatrix I = MMatrix.DiagonalMatrix(matrix.row, 1);

            return (matrix * matrix.Transpose() == I);
        }
Exemplo n.º 8
0
        //A = U*S*Vt
        //A*At = U*S^2*Ut
        //At*A = V*S^2*Vt
        //A*At = (At*A)t
        //This method is not right
        public static void FactorSVD_LDLt(MMatrix A)
        {
            MMatrix AA = new MMatrix();
            MMatrix Eigenvectors = new MMatrix();
            Vector Eigenvalues = new Vector();
            int index = A.row;
            if (A.row > A.col)
                index = A.col;

            AA = A * A.Transpose();
            AA.FactorLDLt();
            Eigenvectors = new MMatrix(AA.LDL_L);
            Eigenvalues = new Vector(AA.row);
            for (int i = 0; i < index; ++i)
                Eigenvalues[i] = AA.LDL_D[i, i];
            SortEigen(ref Eigenvalues, ref Eigenvectors);
            A.SVD_U = Eigenvectors.MArray;

            if (A.SVD_S == null)
                A.SVD_S = new double[A.row, A.col];
            for (int i = 0; i < index; ++i)
                A.SVD_S[i, i] = Math.Sqrt(Eigenvalues[i]);

            AA = AA.Transpose();
            AA.FactorLDLt();
            Eigenvectors = new MMatrix(AA.LDL_L);
            Eigenvalues = new Vector(AA.row);
            for (int i = 0; i < index; ++i)
                Eigenvalues[i] = AA.LDL_D[i, i];
            SortEigen(ref Eigenvalues, ref Eigenvectors);
            A.SVD_Vt = Eigenvectors.Transpose().MArray;
        }
Exemplo n.º 9
0
        //A = U*S*Vt
        //This method is not right
        public static void FactorSVD(MMatrix A)
        {
            MMatrix B = new MMatrix();
            Vector Eigenvalues = new Vector();
            MMatrix Eigenvectors = new MMatrix();
            int index;

            //U
            B = A * A.Transpose();
            B.Jacobi_Cyclic_Method(ref Eigenvalues,ref Eigenvectors);
            SortEigen(ref Eigenvalues,ref Eigenvectors);
            A.SVD_U = Eigenvectors.MArray;

            //S
            A.SVD_S = new double[A.row, A.col];
            index = (A.row < A.col) ? (A.row) : (A.col);
            for (int i = 0; i < index; ++i)
                A.SVD_S[i, i] = Math.Sqrt(Eigenvalues[i]);

            //V
            B = B.Transpose();
            B.Jacobi_Cyclic_Method(ref Eigenvalues,ref Eigenvectors);
            SortEigen(ref Eigenvalues,ref Eigenvectors);
            A.SVD_Vt = Eigenvectors.Transpose().MArray;
        }
Exemplo n.º 10
0
        //A = L*D*Lt
        private static void FactorLDLt(MMatrix A, int size)
        {
            A.FactorLU();
            A.LDL_L = (double[,])A.LU_L.Clone();

            MMatrix MU = new MMatrix(A.LU_U);
            MU = MU.Transpose();
            MU.FactorLU();

            A.LDL_D = (double[,])MU.LU_U.Clone();
        }
        private void buttonGenerateMatrix_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            int rows = (int)numericUpDownRow.Value;
            int cols = (int)numericUpDownCol.Value;

            min = (int)numericUpDownMin.Value;
            max = (int)numericUpDownMax.Value;

            if (radioButtonRandom.Checked)
                MainMatrix = MMatrix.RandomMatrix(rows, cols, min, max);
            else if (radioButtonDiagonal.Checked)
            {
                MainMatrix = MMatrix.DiagonalMatrix(rows, rand.Next(max));
                cols = rows;
            }
            else if (radioButtonIdentity.Checked)
            {
                MainMatrix = MMatrix.DiagonalMatrix(rows, 1);
                cols = rows;
            }
            else if (radioButtonSymetric.Checked)
            {
                MainMatrix = MMatrix.RandomMatrix(rows, cols, min, (int)Math.Sqrt(max));
                MainMatrix = MainMatrix * MainMatrix.Transpose();
                cols = rows;
            }
            else if (radioButtonGaussian.Checked)
            {
                double std = 1.4;
                double theta = 0;
                MainMatrix = MMatrix.D2Gauss(rows, std, rows, std, theta);
                //MainMatrix = MMatrix.D2Gauss_Canny(rows, std, rows, std, theta);  //Sobel kernel
                cols = rows;
            }

            this.dataGridViewMatrix.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            dataGridViewMatrix.SuspendLayout();
            dataGridViewMatrix.Columns.Clear();
            /*
            int ColCount = dataGridViewMatrix.Columns.Count;
            for (int i = 0; i < ColCount; ++i)
                dataGridViewMatrix.Columns.RemoveAt(0);
            */
            DataGridViewColumn[] DCS = new DataGridViewColumn[cols];
            for (int i = 0; i < cols; ++i)
            {
                DataGridViewColumn column = new DataGridViewTextBoxColumn();
                column.HeaderText = string.Concat("A", (i + 1).ToString());
                DCS[i] = column;
            }
            this.dataGridViewMatrix.Columns.AddRange(DCS);
            dataGridViewMatrix.Rows.Add(rows);

            //for DataGrid: [column, row]
            for (int j = 0; j < MainMatrix.col; ++j)
                for (int i = 0; i < MainMatrix.row; ++i)
                    dataGridViewMatrix[j,i].Value = MainMatrix[i, j];

            //this.dataGridViewMatrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dataGridViewMatrix.ResumeLayout();
            numericUpDownCol.Value = cols;
        }