Exemple #1
0
        // Function to calculate and store inverse, returns false if  Matrix is singular
        public MatrixSlow Inverse()
        {
            int N = InnerMatrix.GetUpperBound(0) + 1;

            double[,] inverse = new double[N, N];

            // Find determinant of A[][]
            double det = Determinant(InnerMatrix, N);

            if (det == 0)
            {
                throw new Exception("Singular Matrix, can't find its inverse");
            }

            // Find adjoint
            double[,] adj = Adjoint(InnerMatrix);

            // Find Inverse using formula "inverse(A) = adj(A)/det(A)"
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    inverse[i, j] = adj[i, j] / det;
                }
            }

            return(MatrixSlow.CreateWithArray(inverse));
        }
Exemple #2
0
        public static MatrixSlow CreateWithArray(double[,] arr)
        {
            MatrixSlow m = new MatrixSlow();

            m._Row        = arr.GetUpperBound(0) + 1;
            m._Column     = arr.GetUpperBound(1) + 1;
            m.InnerMatrix = arr;

            return(m);
        }
Exemple #3
0
        public static MatrixSlow Create(int a, int b)
        {
            MatrixSlow m = new MatrixSlow();

            m._Row        = a;
            m._Column     = b;
            m.InnerMatrix = new double[a, b];

            return(m);
        }
Exemple #4
0
        public MatrixSlow Transpose()
        {
            double[,] transpose = new double[_Column, _Row];

            for (int i = 0; i < _Row; i++)
            {
                for (int j = 0; j < _Column; j++)
                {
                    transpose[j, i] = InnerMatrix[i, j];
                }
            }

            return(MatrixSlow.CreateWithArray(transpose));
        }
Exemple #5
0
        public static MatrixSlow operator *(MatrixSlow a, MatrixSlow b)
        {
            if (a._Column != b._Row)
            {
                throw new Exception("Dimension mismatch");
            }

            MatrixSlow c = MatrixSlow.Create(a._Row, b._Column);

            for (int i = 0; i < c._Row; i++)
            {
                for (int j = 0; j < c._Column; j++)
                {
                    c.InnerMatrix[i, j] = 0;
                    for (int k = 0; k < b._Row; k++)
                    {
                        c.InnerMatrix[i, j] += a.InnerMatrix[i, k] * b.InnerMatrix[k, j];
                    }
                }
            }

            return(c);
        }