private void buttonClearAll_Click(object sender, EventArgs e)
        {
            MainMatrix = null;
            dataGridViewMatrix.Columns.Clear();

            b = null;
            dataGridViewVectorb.Columns.Clear();
        }
 public MMatrix(MMatrix A)
 {
     if (A != null)
     {
         this.row = A.row;
         this.col = A.col;
         if (A.MArray != null)
             this.MArray = (double[,])A.MArray.Clone();
     }
 }
        public MMatrix(MMatrix A, int row, int column)
        {
            if ((row > A.row) || (column > A.col))
                throw new MMatrixException("Cannot create matrix. Exceed the number of row and/or column.");

            this.row = row;
            this.col = column;
            this.MArray = new double[row, column];
            for (int i = 0; i < row; ++i)
                for (int j = 0; j < col; ++j)
                    this.MArray[i, j] = A.MArray[i, j];
        }
        public static MMatrix CalculateArgumentMatrix(MMatrix CoefficientMatrix, Vector b)
        {
            //Form Argument matrix from CoefficientMatrix and b
            MMatrix ArgumentMatrix = new MMatrix(CoefficientMatrix.row, CoefficientMatrix.col + 1);
            for (int i = 0; i < CoefficientMatrix.row; ++i)
                for (int j = 0; j < CoefficientMatrix.col; ++j)
                    ArgumentMatrix[i, j] = CoefficientMatrix[i, j];
            for (int i = 0; i < CoefficientMatrix.row; ++i)
                ArgumentMatrix[i, ArgumentMatrix.col - 1] = b[i];

            return ArgumentMatrix;
        }
        //Trunkcate a matrix
        public MMatrix(MMatrix A, int FromRow, int ToRow, int FromColumn, int ToColumn)
        {
            if ((FromRow > A.row) || (FromColumn > A.col))
                throw new MMatrixException("Cannot create matrix. Wrong indices");

            if (ToRow > A.row)
                ToRow = A.row;
            if (ToColumn > A.col)
                ToColumn = A.col;

            if ((FromRow > ToRow) || (FromColumn > ToColumn))
                throw new MMatrixException("Cannot create matrix. Wrong indices");

            this.row = ToRow - FromRow;
            this.col = ToColumn - FromColumn;
            this.MArray = new double[this.row, this.col];
            for (int i = FromRow; i < ToRow; ++i)
                for (int j = FromColumn; j < ToColumn; ++j)
                    this.MArray[i - FromRow, j - FromColumn] = A.MArray[i, j];
        }
        public static string PrintSystemOfEquations(MMatrix CoefficientMatrix, Vector b)
        {
            StringBuilder sb = new StringBuilder("");

            for (int i = 0; i < CoefficientMatrix.row; ++i)
            {
                for (int j = 0; j < CoefficientMatrix.col; ++j)
                {
                    string sign = (CoefficientMatrix[i, j] >= 0) ? "+" : "";
                    sb.Append(sign);
                    sb.Append(CoefficientMatrix[i, j]);
                    sb.Append(" X");
                    sb.Append(j + 1);
                    sb.Append("\t\t");
                }
                sb.Append(" = ");
                sb.Append(b[i]);
                sb.Append("\r\n");
            }

            return sb.ToString();
        }
        //After solving the system, Argument Matrix will contain the reduced system
        public static string PrintSystemOfEquations(MMatrix AugmentedMatrix)
        {
            StringBuilder sb = new StringBuilder("");

            for (int i = 0; i < AugmentedMatrix.row; ++i)
            {
                for (int j = 0; j < AugmentedMatrix.col - 1; ++j)
                {
                    string sign = (AugmentedMatrix[i, j] >= 0) ? "+" : "";
                    sb.Append(sign);
                    sb.Append(Math.Round(AugmentedMatrix[i, j], GlobalMath.DIGITS));
                    sb.Append(" X");
                    sb.Append(j + 1);
                    sb.Append("\t\t");
                }
                sb.Append(" = ");
                sb.Append(Math.Round(AugmentedMatrix[i, AugmentedMatrix.col - 1], GlobalMath.DIGITS));
                sb.Append("\r\n");
            }

            return sb.ToString();
        }
        public void QRTest()
        {
            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[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(Arr2);
            string s = "";

            try
            {

                s += "\nA=\n" + A.PrintToString();

                A.Householder();
                s += "\nHouseholder (A): A(n-1)=\n" + A.PrintToString();
                s += "\nEigenvalues approximation: " + A.Eigenvalues().PrintToString();

                s += "\nEigenvalues: " + B.Eigenvalues().PrintToString();

                this.RichText_Display(s);
            }
            catch (MMatrixException exp)
            {
                MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        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);
            }
        }
Example #10
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);
            }
        }
Example #11
0
 private void newToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (this.pictureBox1.Image != null)
     {
         pictureBox1.Image.Dispose();
         pictureBox1.Image = null;
         hScrollBar1.Visible = false;
         vScrollBar1.Visible = false;
     }
     this.richTextBox1.Text = "";
     argumentMatrix = null;
     if (OutputImage != null)
     {
         OutputImage.Dispose();
         OutputImage = null;
     }
     if (InputImage != null)
     {
         InputImage.Dispose();
         InputImage = null;
     }
 }
Example #12
0
 private void factorSVDToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         if (OutputMatrix != null)
         {
             OutputMatrix.FactorSVD();
             MMatrix U = new MMatrix(OutputMatrix.SVD_U);
             MMatrix S = new MMatrix(OutputMatrix.SVD_S);
             MMatrix Vt = new MMatrix(OutputMatrix.SVD_Vt);
             string s = "\n\n\n";
             s += "Factor SVD:\n";
             s += "\nU=\n" + U.PrintToString();
             s += "\nS=\n" + S.PrintToString();
             s += "\nV'=\n" + Vt.PrintToString();
             //s += "\nA=USV*\n" + (U * S * Vt.Transpose()).PrintToString();
             RichText_Display(s);
         }
     }
     catch (MMatrixException exp)
     {
         MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Example #13
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);
     }
 }
Example #14
0
 private void addToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (InputMatrixForm.ShowDialog() == DialogResult.OK)
     {
         if (InputMatrixForm.MainMatrix != null)
         {
             string s = "\n\n\n";
             s += "Matrix B = \n" + InputMatrixForm.MainMatrix.PrintToString();
             RichText_Display(s);
             argumentMatrix = InputMatrixForm.MainMatrix;
         }
     }
     try
     {
         if ((OutputMatrix != null) && (argumentMatrix!=null))
         {
             string s = "\n\n\n";
             s += "A + B = \n" + (OutputMatrix + argumentMatrix).PrintToString();
             RichText_Display(s);
         }
     }
     catch (MMatrixException exp)
     {
         MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Example #15
0
        public void SaveMatrixText(MMatrix matrix)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Title = "Save Output Matrix";
            saveDlg.Filter = "Rich Text Format (*.rtf)|*.rtf|Text files (*.txt)|*.txt|All files (*.*)|*.*";
            string FileName = "";

            bSaveDone = false;
            //if(matrix != null)
            if (saveDlg.ShowDialog() == DialogResult.OK)
            {
                FileName = saveDlg.FileName;
            }
            if (FileName != "")
            {
                richTextBox1.SaveFile(FileName);
                /*
                string[] FileNamePart = new string[2];
                string FileExtension = ".txt";

                FileNamePart = FileName.Split('.');
                if (FileNamePart.Length > 1)
                    FileExtension = "." + FileNamePart[1];

                SaveText(FileName, matrix.PrintToString(), false);
                //SVD
                if (matrix.SVD_U != null)
                    SaveText(FileNamePart[0] + "_SVD_U" + FileExtension, (new MMatrix(matrix.SVD_U)).PrintToString(), false);
                if (matrix.SVD_S != null)
                    SaveText(FileNamePart[0] + "_SVD_S" + FileExtension, (new MMatrix(matrix.SVD_S)).PrintToString(), false);
                if (matrix.SVD_Vt != null)
                    SaveText(FileNamePart[0] + "_SVD_Vt" + FileExtension, (new MMatrix(matrix.SVD_Vt)).PrintToString(), false);
                //LU
                if (matrix.LU_P != null)
                    SaveText(FileNamePart[0] + "_LU_P" + FileExtension, (new MMatrix(matrix.LU_P)).PrintToString(), false);
                if (matrix.LU_L != null)
                    SaveText(FileNamePart[0] + "_LU_L" + FileExtension, (new MMatrix(matrix.LU_L)).PrintToString(), false);
                if (matrix.LU_U != null)
                    SaveText(FileNamePart[0] + "_LU_U" + FileExtension, (new MMatrix(matrix.LU_U)).PrintToString(), false);
                //LDL*
                if (matrix.LDL_L != null)
                    SaveText(FileNamePart[0] + "_LDL*_L" + FileExtension, (new MMatrix(matrix.LDL_L)).PrintToString(), false);
                if (matrix.LDL_D != null)
                    SaveText(FileNamePart[0] + "_LDL*_D" + FileExtension, (new MMatrix(matrix.LDL_D)).PrintToString(), false);
                if (matrix.LDL_L != null)
                    SaveText(FileNamePart[0] + "_LDL*_Lt" + FileExtension, (new MMatrix(matrix.LDL_L)).Transpose().PrintToString(), false);
                 * */
            }
            bSaveDone = true;
        }
        public static string PrintToString(MMatrix A)
        {
            StringBuilder sb = new StringBuilder("");

            for (int i = 0; i < A.row; ++i)
            {
                for (int j = 0; j < A.col; ++j)
                {
                    sb.Append(Math.Round(A[i, j], GlobalMath.DIGITS));
                    sb.Append("\t\t");
                }
                sb.Append("\r\n");
            }
            return sb.ToString();
        }
 public static MMatrix PowEntry(MMatrix A, double pow)
 {
     return PowEntry(A, A.row, A.col, pow);
 }
        //Orthonormalize all eigenvectors based on the first one
        public static MMatrix Orthonormalize(MMatrix Eigenvectors)
        {
            MMatrix A = new MMatrix(Eigenvectors);
            Vector v1 = new Vector(Eigenvectors.row);
            Vector v = new Vector(Eigenvectors.row);

            for (int i = 0; i < v1.Elements.Length; ++i)
                v1[i] = A[i, 0];
            v1 = v1.Normalize();
            for (int i = 0; i < v1.Elements.Length; ++i)
                A[i, 0] = v1[i];

            for (int i = 1; i < Eigenvectors.col; ++i)
            {
                for (int j = 0; j < Eigenvectors.row; ++j)
                    v[j] = Eigenvectors[j,i];
                v = v.Orthonormalize(v1);

                for (int j = 0; j < v.Elements.Length; ++j)
                    A[j, i] = v[j];
            }
            return A;
        }
        //Orthonormalize all eigenvectors based on the first one
        public static MMatrix Orthonormalize(Queue<Vector> Eigenvectors)
        {
            MMatrix A = new MMatrix(Eigenvectors.ElementAt(0).Elements.Length, Eigenvectors.Count);
            Vector v1, v;
            int column = 0;

            v1 = Eigenvectors.ElementAt(0);
            v1 = v1.Normalize();
            for (int i = 0; i < v1.Elements.Length; ++i)
            {
                A[i, 0] = v1[i];
            }
            for (int j = 1; j < Eigenvectors.Count; ++j)
            {
                v = Eigenvectors.ElementAt(j);
                v = v.Orthonormalize(v1);
                column++;
                for (int i = 0; i < v.Elements.Length; ++i)
                {
                    A[i, column] = v[i];
                }
            }
            return A;
        }
        public static MMatrix Minor(MMatrix A, int row, int col)
        {
            MMatrix minor = new MMatrix(A.row - 1, A.col - 1);
            int m = 0, n = 0;

            for (int i = 0; i < A.row; ++i)
            {
                if (i == row)
                    continue;
                n = 0;
                for (int j = 0; j < A.col; ++j)
                {
                    if (j == col)
                        continue;
                    minor[m, n] = A[i, j];
                    n++;
                }
                m++;
            }
            return minor;
        }
Example #21
0
        public void SVDMatrixTest()
        {
            double[,] Arr1 = new double[3, 3] { { 4, -1, 1 }, { -1, 4.25, 2.75 }, { 1, 2.75, 3.5 } };
            double[,] Arr2 = new double[4, 4] { { 1, 1, 0, 3 }, { 2, 1, -1, 1 }, { 3, -1, -1, 2 }, { -1, 2, 3, -1 } };
            double[,] Arr3 = new double[2, 3] { { 3, 1, 1 }, { -1, 3, 1 } };
            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 = "";

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

                s = "Factor SVD:\n";
                s += "\nMatrix A=\n" + A.PrintToString();
                s += "\nU=\n" + (new MMatrix(A.SVD_U)).PrintToString();
                s += "\nS=\n" + (new MMatrix(A.SVD_S)).PrintToString();
                s += "\nVt=\n" + (new MMatrix(A.SVD_Vt)).PrintToString();

                B = (new MMatrix(A.SVD_U)) * (new MMatrix(A.SVD_S)) * (new MMatrix(A.SVD_Vt));
                s += "\nA=USV*\n" + B.PrintToString();

                this.RichText_Display(s);
                OutputMatrix = A;
            }
            catch (MMatrixException exp)
            {
                MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        //Matrix must be trigdiagonal (using Householder)
        //This routine cannot find exactly eigenvalues
        public static Queue<double> QR(MMatrix matrix, double TOL, int MaxIterations)
        {
            int n = matrix.row;
            int M = MaxIterations;
            int k;
            double SHIFT, lamda,b,c,d,m1,m2,lamda1,lamda2,min,sigma;
            double[] A = new double[n];
            double[] B = new double[n];
            double[] C = new double[n];
            double[] D = new double[n];
            double[] Q = new double[n];
            double[] R = new double[n];
            double[] S = new double[n];
            double[] X = new double[n];
            double[] Y = new double[n];
            double[] Z = new double[n];
            double[] Sig = new double[n];
            Queue<double> QLamda=new Queue<double>();

            for (int i = 0; i < n; ++i)
                A[i] = matrix[i, i];
            for (int i = 1; i < n; ++i)
                B[i] = matrix[i,i-1];

            k = 1;
            SHIFT = 0;

            while (k <= M)
            {
                if (Math.Abs(B[n - 1]) <= TOL)
                {
                    lamda = A[n - 1] + SHIFT;
                    QLamda.Enqueue(lamda);
                    n = n - 1;
                }

                if (B[1] <= TOL)
                {
                    lamda = A[0] + SHIFT;
                    QLamda.Enqueue(lamda);
                    n = n - 1;
                    A[0] = A[1];
                    for (int j = 1; j < n; ++j)
                    {
                        A[j] = A[j + 1];
                        B[j] = B[j + 1];
                    }
                }

                if (n == 0)
                    return QLamda;

                if (n == 1)
                {
                    lamda = A[0] + SHIFT;
                    QLamda.Enqueue(lamda);
                    return QLamda;
                }
                /*
                for (int j = 2; j < n - 1; ++j)
                {
                    if (Math.Abs(B[j]) <= TOL)
                    {
                        throw new MMatrixException("Split into A[0]..A[j-1], B[1]..B[j-1] and A[j]..A[n-1], B[j+1]..B[n-1]"+SHIFT.ToString());
                    }
                }
                */
                b = -(A[n - 2] + A[n - 1]);
                c = A[n - 1] * A[n - 2] - B[n - 1]* B[n - 1];
                d = Math.Sqrt(b * b - 4 * c);

                if(b > 0)
                {
                    m1 = -2*c/(b+d);
                    m2 = -(b+d)/2;
                }
                else
                {
                    m1 = (d-b)/2;
                    m2 = 2*c/(d-b);
                }

                if (n == 2)
                {
                    lamda1 = m1 + SHIFT;
                    lamda2 = m2 + SHIFT;
                    QLamda.Enqueue(lamda1);
                    QLamda.Enqueue(lamda2);
                    return QLamda;
                }

                min = Math.Min(Math.Abs(m1 - A[n - 1]), Math.Abs(m2 - A[n - 1]));
                sigma = min + A[n - 1];
                SHIFT += sigma;

                for (int j = 0; j < n; ++j)
                    D[j] = A[j] - sigma;

                X[0] = D[0];
                Y[0] = B[1];

                for (int j = 1; j < n; ++j)
                {
                    Z[j - 1] = Math.Sqrt(X[j-1]*X[j-1]+B[j]*B[j]);
                    C[j] = X[j-1] / Z[j-1];
                    Sig[j] = B[j] / Z[j - 1];
                    Q[j - 1] = C[j] * Y[j - 1] + S[j] * D[j];
                    X[j] = -Sig[j] * Y[j - 1] + C[j] * D[j];
                    if (j != n - 1)
                    {
                        R[j - 1] = Sig[j] * B[j + 1];
                        Y[j] = C[j] * B[j + 1];
                    }
                }

                Z[n - 1] = X[n - 1];
                A[0] = Sig[1] * Q[0] + C[1] * Z[0];
                B[1] = Sig[1] * Z[1];

                for (int j = 1; j < n - 1; ++j)
                {
                    A[j] = Sig[j + 1] * Q[j] + C[j] * C[j + 1] * Z[j];
                    B[j + 1] = Sig[j + 1] * Z[j + 1];
                }

                A[n - 1] = C[n - 1] * Z[n - 1];

                k++;
            }
            throw new MMatrixException("Maximum number of iterations exceeded.");
        }
Example #23
0
 private void eigenvectorsToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         StringBuilder sb = new StringBuilder("");
         Vector eigenvalues = new Vector();
         MMatrix eigenvectors = new MMatrix();
         if (OutputMatrix != null)
         {
             MMatrix temp = OutputMatrix.Clone();
             temp.Jacobi_Cyclic_Method(ref eigenvalues, ref eigenvectors);
             sb.Append("\n\n\nEigenvectors (A) = \n");
             for (int i = 0; i < eigenvectors.col; ++i)
             {
                 sb.Append("V");
                 sb.Append(i + 1);
                 sb.Append("\t\t");
             }
             sb.Append("\n");
             sb.Append(eigenvectors.PrintToString());
             RichText_Display(sb.ToString());
         }
     }
     catch (MMatrixException exp)
     {
         MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
        public static MMatrix RandomMatrix(int row, int column, int minvalue, int maxvalue)
        {
            MMatrix A = new MMatrix(row, column);
            Random ranobj = new Random();
            for (int i = 0; i < A.row; ++i)
                for (int j = 0; j < A.col; ++j)
                    A[i, j] = ranobj.Next(minvalue, maxvalue);

            return A;
        }
Example #25
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);
     }
 }
        public static int Rank(MMatrix A)
        {
            MMatrix temp = A.EchelonForm();
            int index = Math.Min(temp.row, temp.col);
            int rank = 0;

            for (int i = 0; i < index; ++i)
            {
                if (temp[i, i] != 0)
                    rank++;
            }

            return rank;
        }
Example #27
0
 private void inputMatrixToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (InputMatrixForm.ShowDialog() == DialogResult.OK)
     {
         if (InputMatrixForm.MainMatrix != null)
         {
             string s = string.Concat("\n\n\nMatrix A = \n", InputMatrixForm.MainMatrix.PrintToString());
             RichText_Display(s);
             OutputMatrix = InputMatrixForm.MainMatrix;
         }
     }
 }
        public static MMatrix ReducedEchelonForm(MMatrix matrix)
        {
            try
            {
                MMatrix ReducedEchelonMatrix = new MMatrix(matrix);
                for (int i = 0; i < matrix.row; ++i)
                {
                    if (i < ReducedEchelonMatrix.col)
                    {
                        if (ReducedEchelonMatrix[i, i] == 0)	// if diagonal entry is zero,
                            for (int j = i + 1; j < ReducedEchelonMatrix.row; ++j)
                                if (ReducedEchelonMatrix[j, i] != 0)	 //check if some below entry is non-zero
                                    ReducedEchelonMatrix.InterchangeRow(i, j);	// then interchange the two rows
                        if (ReducedEchelonMatrix[i, i] == 0)	// if not found any non-zero diagonal entry
                            continue;	// increment i;
                        if (ReducedEchelonMatrix[i, i] != 1)	// if diagonal entry is not 1 ,
                            for (int j = i + 1; j < ReducedEchelonMatrix.row; ++j)
                                if (ReducedEchelonMatrix[j, i] == 1)	 //check if some below entry is 1
                                    ReducedEchelonMatrix.InterchangeRow(i, j);	// then interchange the two rows
                        ReducedEchelonMatrix.MultiplyRow(i, 1 / (ReducedEchelonMatrix[i, i]));

                        for (int j = i + 1; j < ReducedEchelonMatrix.row; ++j)
                            ReducedEchelonMatrix.AddRow(j, i, -ReducedEchelonMatrix[j, i]);
                        for (int j = i - 1; j >= 0; j--)
                            ReducedEchelonMatrix.AddRow(j, i, -ReducedEchelonMatrix[j, i]);
                    }
                }
                return ReducedEchelonMatrix;
            }
            catch (Exception)
            {
                throw new MMatrixException("MMatrix can not be reduced to Echelon form");
            }
        }
        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();
        }
        public static double RootMeanSquareError(MMatrix MActual,MMatrix MApproximate)
        {
            double err = 0;
            //err = MMatrix.LInfinitiveNorm(MActual - MApproximate) / MMatrix.LInfinitiveNorm(MActual);

            double sum = 0;
            double diff;
            for (int i = 0; i < MActual.row; ++i)
                for (int j = 0; j < MActual.col; ++j)
                {
                    diff = MActual[i, j] - MApproximate[i, j];
                    sum += diff * diff;
                }
            err = Math.Sqrt(sum / (MActual.row * MActual.col));

            return err;
        }