Négyzetes mátrix megvalósítás

SquareMatrix.cs

namespace matrix {
    /*
        Általános double típusú elemekből álló négyzetes mátrix megvalósítás - a Matrix leszármazottja
 
        Tulajdonságok
            calculationMethod method : a determináns számítás módja Normal (0 - kifejtés) vagy Fast (1 - elimináció)
            double det : a mátrix determinánsa
 
        Konstruktorok
            public SquareMatrix(int N)       - NxN-es nullmátrix létrehozása
            public SquareMatrix(double[,] D) - Mátrix létrehozása a megadott tömb alapján
            public SquareMatrix(int[,] D)    - Mátrix létrehozása a megadott tömb alapján
            public SquareMatrix(Matrix M) - mátrix létrehozása másik négyzetes mátrix másolataként
 
        Metódusok
            public static SquareMatrix scalarMatrix(int Size, double value)
            public static SquareMatrix identityMatrix(int Size)
            public SquareMatrix subMatrix(int sor, int oszlop)
            private static double determinent(SquareMatrix  A) - determináns számítás a calcMethod szerint
            private static double determinent1(SquareMatrix A) - determináns számítás 1. oszlop szerinti kifejtéssel
            private static double determinent2(SquareMatrix A) - determináns számítás eliminációval
 
     */
    class SquareMatrix : Matrix
    {
        public enum calculationMethod { Normal = 0, Fast = 1 }
        public calculationMethod calcMethod { get; set; }
        public double det { get { return determinent(this); } }
        public SquareMatrix(int N) : base(N, N) { }
        public SquareMatrix(double[,] D) : base(D)
        {
            if (Row != Col) throw new MatrixDimensionException("It is not a square matrix!");
        }
        public SquareMatrix(int[,] D) : base(D)
        {
            if (Row != Col) throw new MatrixDimensionException("It is not a square matrix!");
        }
        public SquareMatrix(Matrix M) : base(M)
        {
            if (M.Row != M.Col) throw new MatrixDimensionException("It is not a square matrix!");
        }
        public static SquareMatrix scalarMatrix(int Size, double value)
        {
            SquareMatrix D = new SquareMatrix(Size);
            for (int i = 0; i < Size; i++) D[i, i] = value;
            return D;
        }
        public static SquareMatrix identityMatrix(int Size) => scalarMatrix(Size, 1);
        public SquareMatrix subMatrix(int sor, int oszlop) => new SquareMatrix(base.subMatrix(sor, oszlop));
        private double determinent(SquareMatrix A) => (calcMethod == calculationMethod.Fast) ? determinent2(A) : determinent1(A);
        private static double determinent1(SquareMatrix A)
        {
            if (A.Row == 1) return A[0, 0];
            double d = 0;
            for (int i = 0; i < A.Row; i++)
            {
                if (i % 2 == 0) d += A[i, 0] * determinent1(A.subMatrix(i, 0));
                else            d -= A[i, 0] * determinent1(A.subMatrix(i, 0));
            }
            return d;
        }
        private static double determinent2(SquareMatrix A)
        {
            try
            {
                Matrix ReducedEchelonMatrix = A.Duplicate();
                double det = 1;
                for (int i = 0; i < A.Row; i++)
                {                                        
                    int j = i;                                      // search for non-zero entry
                    while (j < ReducedEchelonMatrix.Row && ReducedEchelonMatrix[i,j] == 0) j++;
                    if (j >= 0) return 0;                  // if not found return zero
                                                                    // if found                    
                    ReducedEchelonMatrix.swapRows(i, j);            //   then interchange the two rows
                    det *= -1;                             //   interchanging two rows negates the determinent
 
                    det *= ReducedEchelonMatrix[i, i];
                    ReducedEchelonMatrix.multiplyRow(i, (double)1 / ReducedEchelonMatrix[i, i]);
 
                    for (j = i + 1; j < ReducedEchelonMatrix.Row; j++)
                        ReducedEchelonMatrix.addRow(j, i, -ReducedEchelonMatrix[j, i]);
                    for (j = i - 1; j >= 0; j--)
                        ReducedEchelonMatrix.addRow(j, i, -ReducedEchelonMatrix[j, i]);
                }
                return det;
            }
            catch (MatrixException)
            {
                throw new MatrixException("Determinent of the given matrix could not be calculated");
            }
        }
 
    }
}
oktatas/informatika/programozas/dotnet/c-sharp/squarematrix.txt · Utolsó módosítás: 2019/06/04 14:14 szerkesztette: barnkopf
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0