Пример #1
0
        public MatrisBase <object> MinorMatris(MatrisBase <object> A,
                                               int row,
                                               int col,
                                               int based = 0)
        {
            if (!A.IsSquare())
            {
                throw new Exception(CompilerMessage.MAT_NOT_SQUARE);
            }

            CompilerUtils.AssertMatrixValsAreNumbers(A);

            List <List <object> > newlis = new List <List <object> >();
            List <List <object> > vals   = A.GetValues();

            row -= based;
            col -= based;

            if (row < 0 || row >= A.Row)
            {
                throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("satır", based, A.Row - based));
            }

            if (col < 0 || col >= A.Col)
            {
                throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("sütun", based, A.Col - based));
            }

            int rowindex = 0;

            for (int i = 0; i < row; i++)
            {
                newlis.Add(new List <object>());
                for (int j = 0; j < col; j++)
                {
                    newlis[rowindex].Add(vals[i][j]);
                }
                for (int j = col + 1; j < A.Col; j++)
                {
                    newlis[rowindex].Add(vals[i][j]);
                }
                rowindex++;
            }

            for (int i = row + 1; i < A.Row; i++)
            {
                newlis.Add(new List <object>());
                for (int j = 0; j < col; j++)
                {
                    newlis[rowindex].Add(vals[i][j]);
                }
                for (int j = col + 1; j < A.Col; j++)
                {
                    newlis[rowindex].Add(vals[i][j]);
                }
                rowindex++;
            }

            return(new MatrisBase <object>(newlis));
        }
Пример #2
0
        public MatrisBase <object> Inverse(MatrisBase <object> A)
        {
            if (!A.IsSquare())
            {
                throw new Exception(CompilerMessage.MAT_NOT_SQUARE);
            }

            if (Determinant(A) == (float)0.0)
            {
                throw new Exception(CompilerMessage.MAT_DET_ZERO_NO_INV);
            }

            using MatrisBase <object> temp = Concatenate(A is Dataframe ? ((Dataframe)A.Copy()) : A.Copy(),
                                                         (dynamic) new SpecialMatricesService().Identity(A.Row),
                                                         1);

            return(A is Dataframe df
                    ? new Dataframe(RREchelon(temp)[new Range(new Index(0), new Index(temp.Row)), new Range(new Index(A.Col), new Index(temp.Col))],
                                    df.Delimiter,
                                    df.NewLine,
                                    Dataframe.GetCopyOfLabels(df.GetRowLabels()),
                                    Dataframe.GetCopyOfLabels(df.GetColLabels()),
                                    df.GetRowSettings().Copy(),
                                    df.GetColSettings().Copy())
                    : new MatrisBase <object>(vals: RREchelon(temp)[new Range(new Index(0), new Index(temp.Row)), new Range(new Index(A.Col), new Index(temp.Col))]));
        }
Пример #3
0
        public MatrisBase <object> Adjoint(MatrisBase <object> A)
        {
            if (!A.IsSquare())
            {
                throw new Exception(CompilerMessage.MAT_NOT_SQUARE);
            }

            CompilerUtils.AssertMatrixValsAreNumbers(A);

            List <List <object> > adj = new List <List <object> >();
            int r = A.Row;
            int c = A.Col;

            for (int i = 0; i < r; i++)
            {
                adj.Add(new List <object>());
                for (int j = 0; j < c; j++)
                {
                    adj[i].Add((dynamic)(((i + j) % 2 == 1 ? -1 : 1) * Minor(A, i, j, 0)));
                }
            }

            return(A is Dataframe df
                ? new Dataframe(Transpose(new MatrisBase <object>(adj)).GetValues(),
                                df.Delimiter,
                                df.NewLine,
                                Dataframe.GetCopyOfLabels(df.GetRowLabels()),
                                Dataframe.GetCopyOfLabels(df.GetColLabels()),
                                df.GetRowSettings().Copy(),
                                df.GetColSettings().Copy())
                : Transpose(new MatrisBase <object>(adj)));
        }
Пример #4
0
        public float Determinant(MatrisBase <object> A)
        {
            if (!A.IsSquare())
            {
                throw new Exception(CompilerMessage.MAT_NOT_SQUARE);
            }

            if (A.IsZero((float)0.0))
            {
                CompilerUtils.AssertMatrixValsAreNumbers(A);
                return((float)0.0);
            }

            if (A.Row == 1)
            {
                CompilerUtils.AssertMatrixValsAreNumbers(A);
                return(float.Parse(A.GetValues()[0][0].ToString()));
            }

            if (A.Row == 2)
            {
                CompilerUtils.AssertMatrixValsAreNumbers(A);
                return((float.Parse(A.GetValues()[0][0].ToString()) * float.Parse(A.GetValues()[1][1].ToString()))
                       - (float.Parse(A.GetValues()[0][1].ToString()) * float.Parse(A.GetValues()[1][0].ToString())));
            }

            using MatrisBase <object> ech = Echelon(A.Copy());

            float det = float.Parse(ech.GetValues()[0][0].ToString());

            if (ech.SwapCount % 2 == 1)
            {
                det *= -1;
            }

            int dim = A.Row;

            for (int i = 1; i < dim; i++)
            {
                det *= float.Parse(ech.GetValues()[i][i].ToString());
            }
            return(det);
        }