コード例 #1
0
        public MatrisBase <object> Head(MatrisBase <object> df,
                                        int n = 5)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            n = Math.Min(n, df.Row);
            if (n <= 0 || df.Row < n)
            {
                throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("satır", 1, df.Row));
            }

            return(df.Row == n
                ? df is Dataframe ? ((Dataframe)df.Copy()) : df.Copy()
                : df is Dataframe dataframe
                    ? new Dataframe(dataframe[new Range(0, n)],
                                    dataframe.Delimiter,
                                    dataframe.NewLine,
                                    null,
                                    Dataframe.GetCopyOfLabels(dataframe.GetColLabels()),
                                    dataframe.GetRowSettings().Copy(),
                                    dataframe.GetColSettings().Copy())
                    : new MatrisBase <object>(df[new Range(0, n)]));
        }
コード例 #2
0
        public MatrisBase <object> Set(MatrisBase <object> A,
                                       int i,
                                       int j,
                                       float value,
                                       int based = 0)
        {
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }
            if (i - based < 0 || i - based >= A.Row)
            {
                throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("satır", based, A.Row - 1));
            }
            if (j - based < 0 || j - based >= A.Col)
            {
                throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("sütun", based, A.Col - 1));
            }

            List <List <object> > newlis = A is Dataframe ? ((Dataframe)A.Copy()).GetValues() : A.Copy().GetValues();

            newlis[i - based][j - based] = (dynamic)value;

            return(A is Dataframe df
                ? new Dataframe(newlis,
                                df.Delimiter,
                                df.NewLine,
                                Dataframe.GetCopyOfLabels(df.GetRowLabels()),
                                Dataframe.GetCopyOfLabels(df.GetColLabels()),
                                df.GetRowSettings().Copy(),
                                df.GetColSettings().Copy())
                : new MatrisBase <object>(newlis));
        }
コード例 #3
0
        public int Rank(MatrisBase <object> A)
        {
            using MatrisBase <object> ech = Echelon(A.Copy());
            int zeroCount = 0;

            if (A.Row <= A.Col)
            {
                for (int i = ech.Row - 1; i >= 0; i--)
                {
                    if (ech.IsZeroRow(i, 0, (float)0.0))
                    {
                        zeroCount++;
                    }
                }
                return(ech.Row - zeroCount);
            }
            else
            {
                for (int i = ech.Col - 1; i >= 0; i--)
                {
                    if (ech.IsZeroCol(i, 0, (float)0.0))
                    {
                        zeroCount++;
                    }
                }
                return(ech.Col - zeroCount);
            }
        }
コード例 #4
0
        public MatrisBase <object> RREchelon(MatrisBase <object> A)
        {
            // Bad dimensions
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            // Zero matrix
            if (A.IsZero((float)0.0))
            {
                return(A);
            }

            if (A is Dataframe df)
            {
                CompilerUtils.AssertMatrixValsAreNumbers(A);

                Dataframe result = df.Copy();
                InnerRREchelon(df, result);
                return(result);
            }
            else
            {
                MatrisBase <object> result = A.Copy();
                InnerRREchelon(A, result);
                return(result);
            }
        }
コード例 #5
0
        /// <summary>
        /// Matrix multiplication of given <paramref name="_base"/> with itself <paramref name="_expo"/> times
        /// </summary>
        /// <param name="_base">Matrix to use</param>
        /// <param name="_expo">Exponential to use</param>
        /// <param name="matDict">Matrix dictionary to refer to if needed</param>
        public static void OPMatMulByExpo(Token _base,
                                          Token _expo,
                                          Dictionary <string, MatrisBase <dynamic> > matDict,
                                          CompilerDictionaryMode mode = CompilerDictionaryMode.Matrix)
        {
            if (_expo.tknType != TokenType.NUMBER)
            {
                _expo.val = !CheckMatrixAndUpdateVal(_expo, matDict, mode, true)
                                  ? throw new Exception(CompilerMessage.EXPO_NOT_SCALAR)
                                  : !((MatrisBase <dynamic>)_expo.val).IsScalar()
                                      ? throw new Exception(CompilerMessage.MAT_SHOULD_BE_SCALAR)
                                      : float.Parse(((MatrisBase <object>)_expo.val)[0, 0].ToString());

                if (!(_expo.val is int) && !(_expo.val is float) && !(_expo.val is double))
                {
                    throw new Exception(CompilerMessage.EXPO_NOT_SCALAR);
                }
            }

            if (_expo.val < 0)
            {
                throw new Exception(CompilerMessage.SPECOP_MATPOWER_EXPO);
            }
            else if (_expo.val == 0)
            {
                _expo.val     = 1;
                _expo.tknType = TokenType.NUMBER;
                return;
            }

            if (CheckMatrixAndUpdateVal(_base, matDict, mode, true))
            {
                if (!_base.val.IsSquare())
                {
                    throw new Exception(CompilerMessage.SPECOP_MATPOWER_SQUARE);
                }

                MatrisBase <dynamic> res       = _base.val.Copy();
                using MatrisBase <dynamic> mat = res is Dataframe ? ((Dataframe)res.Copy()) : res.Copy();

                AssertNotNull(_expo);

                for (int i = 1; i < _expo.val; i++)
                {
                    res = MatrisMul(res, mat);
                }

                _expo.val     = res;
                _expo.tknType = TokenType.MATRIS;
            }
            else
            {
                throw new Exception(CompilerMessage.SPECOP_MATPOWER_BASE);
            }

            Validations.CheckModeAndMatrixReference(mode, _expo.val);
        }
コード例 #6
0
        public MatrisBase <object> Sample(MatrisBase <object> df,
                                          int n = 5)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            int nr = df.Row;

            n = Math.Min(n, nr);
            if (n <= 0 || nr < n)
            {
                throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("satır", 1, nr));
            }

            if (nr == n)
            {
                return(df is Dataframe ? ((Dataframe)df.Copy()) : df.Copy());
            }
            else
            {
                List <List <object> > newList = new List <List <object> >();

                List <List <object> > shuffled = new MatrisArithmeticService().Shuffle(df, 0).GetValues();

                for (int i = 0; i < n; i++)
                {
                    newList.Add(shuffled[i]);
                }

                return(df is Dataframe dataframe
                    ? new Dataframe(newList,
                                    dataframe.Delimiter,
                                    dataframe.NewLine,
                                    null,
                                    Dataframe.GetCopyOfLabels(dataframe.GetColLabels()),
                                    dataframe.GetRowSettings().Copy(),
                                    dataframe.GetColSettings().Copy()
                                    )
                    : new MatrisBase <object>(newList));
            }
        }
コード例 #7
0
 public Dataframe ToDf(MatrisBase <object> matrix)
 {
     if (matrix.IsValid())
     {
         return(new Dataframe(matrix.Copy().GetValues()));
     }
     else
     {
         throw new Exception(CompilerMessage.INVALID_CONVERSION_TO_DF);
     }
 }
コード例 #8
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);
        }
コード例 #9
0
        public MatrisBase <object> Shuffle(MatrisBase <object> A,
                                           int axis = 2)
        {
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            int m = A.Row;
            int n = A.Col;

            if (m == 1 && n == 1)
            {
                return(A is Dataframe ? ((Dataframe)A.Copy()) : A.Copy());
            }

            if (axis == 0)
            {
                if (m == 1)
                {
                    return(A is Dataframe ? ((Dataframe)A.Copy()) : A.Copy());
                }

                List <int> indices = new List <int>();
                for (int c = 0; c < m; c++)
                {
                    indices.Add(c);
                }

                indices = indices.OrderBy(x => Guid.NewGuid()).ToList();
                List <List <object> > newvals = new List <List <object> >();
                List <List <object> > vals    = A.GetValues();

                int i = 0;
                foreach (int k in indices)
                {
                    newvals.Add(new List <object>());
                    for (int j = 0; j < n; j++)
                    {
                        newvals[i].Add(vals[k][j]);
                    }
                    i++;
                }

                return(A is Dataframe data
                    ? new Dataframe(newvals,
                                    data.Delimiter,
                                    data.NewLine,
                                    null,
                                    Dataframe.GetCopyOfLabels(data.GetColLabels()),
                                    null,
                                    data.GetColSettings().Copy(),
                                    true)
                    : new MatrisBase <object>(newvals));
            }
            else if (axis == 1)
            {
                if (n == 1)
                {
                    return(A is Dataframe ? ((Dataframe)A.Copy()) : A.Copy());
                }

                List <int> indices = new List <int>();
                for (int c = 0; c < n; c++)
                {
                    indices.Add(c);
                }

                indices = indices.OrderBy(x => Guid.NewGuid()).ToList();
                List <List <object> > newvals = new List <List <object> >();
                List <List <object> > vals    = A.GetValues();

                for (int i = 0; i < m; i++)
                {
                    newvals.Add(new List <object>());
                    foreach (int k in indices)
                    {
                        newvals[i].Add(vals[i][k]);
                    }
                }

                return(A is Dataframe data
                    ? new Dataframe(newvals,
                                    data.Delimiter,
                                    data.NewLine,
                                    Dataframe.GetCopyOfLabels(data.GetRowLabels()),
                                    null,
                                    data.GetRowSettings().Copy(),
                                    null,
                                    true)
                    : new MatrisBase <object>(newvals));
            }
            else if (axis == 2)
            {
                if (m == 1)
                {
                    return(Shuffle(A, 1));
                }
                else if (n == 1)
                {
                    return(Shuffle(A, 0));
                }

                List <int> indices = new List <int>();
                for (int k = 0; k < n * m; k++)
                {
                    indices.Add(k);
                }

                indices = indices.OrderBy(x => Guid.NewGuid()).ToList();
                List <List <object> > newvals = new List <List <object> >();
                List <List <object> > vals    = A.GetValues();

                int c = 0;
                int r = -1;
                foreach (int k in indices)
                {
                    if (c % n == 0)
                    {
                        newvals.Add(new List <object>());
                        r++;
                    }

                    newvals[r].Add(vals[k / n][k % n]);
                    c++;
                }

                return(A is Dataframe data
                    ? new Dataframe(newvals,
                                    data.Delimiter,
                                    data.NewLine,
                                    forceLabelsWhenNull: true)
                    : new MatrisBase <object>(newvals));
            }
            else
            {
                throw new Exception(CompilerMessage.ARG_INVALID_VALUE("axis", "Satır: 0, Sütun: 1, Rastgele:2 olmalı"));
            }
        }
コード例 #10
0
        private void InnerEchelon(MatrisBase <object> A, MatrisBase <object> result)
        {
            int                   nr             = A.Row;
            int                   nc             = A.Col;
            List <int>            zeroCols       = new List <int>();
            List <List <object> > filteredResult = A.Copy().GetValues();

            for (int j = 0; j < nc; j++)
            {
                if (A.IsZeroCol(j, 0, (float)0.0))
                {
                    for (int i = 0; i < nr; i++)
                    {
                        filteredResult[i].RemoveAt(j - zeroCols.Count);
                    }

                    zeroCols.Add(j);
                    nc--;
                }
            }

            result.SetValues(filteredResult);

            for (int r = 0; r < nr; r++)
            {
                if (result.IsZeroRow(r, 0, (float)0.0))
                {
                    result.SwapToEnd(r, 0);
                    nr--;
                }
            }

            int  p = 0;
            bool next;
            int  swapCount = 0;

            while (p < nr && p < nc)
            {
                next = false;
                int r = 1;
                while (float.Parse(result.GetValues()[p][p].ToString()) == (float)0.0)
                {
                    if (p + 1 < nr)
                    {
                        if (result.IsZeroRow(p, 0, (float)0.0))
                        {
                            nr--;
                        }

                        if (!Sub(result, p, nr, p, p + 1, 0).IsZeroCol(0, 0))
                        {
                            for (int ri = p + 1; ri < nr; ri++)
                            {
                                if (Math.Abs(float.Parse(result.GetValues()[ri][p].ToString())) > 1e-6)
                                {
                                    result.Swap(p, ri, based: 0);
                                    swapCount++;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            p++;
                        }
                        next = true;
                        break;
                    }
                    else
                    {
                        // Zeros to bottom
                        for (int i = 0; i < A.Row; i++)
                        {
                            if (result.IsZeroRow(i, 0, (float)0.0))
                            {
                                result.SwapToEnd(i, 0);
                            }
                        }
                        // Restore zero columns
                        if (zeroCols.Count > 0)
                        {
                            foreach (int j in zeroCols)
                            {
                                for (int i = 0; i < result.Row; i++)
                                {
                                    result.GetValues()[i].Insert(j, (dynamic)(float)0.0);
                                }
                            }
                            result.SetCol(A.Col);
                        }
                        result.FixMinusZero();
                        result.SwapCount = swapCount;
                        return;
                    }
                }

                if (next)
                {
                    continue;
                }

                for (; r >= 1 && r < (nr - p); r++)
                {
                    if (float.Parse(result.GetValues()[p + r][p].ToString()) != (float)0.0)
                    {
                        float x = -(float.Parse(result.GetValues()[p + r][p].ToString()) / float.Parse(result.GetValues()[p][p].ToString()));
                        for (int c = p; c < nc; c++)
                        {
                            result.GetValues()[p + r][c] = (dynamic)((float.Parse(result.GetValues()[p][c].ToString()) * x) + float.Parse(result.GetValues()[p + r][c].ToString()));
                        }
                    }
                }
                p++;
            }

            // Zeros to bottom
            for (int i = 0; i < A.Row; i++)
            {
                if (result.IsZeroRow(i, 0, (float)0.0))
                {
                    result.SwapToEnd(i, 0);
                }
            }
            // Restore zero columns
            if (zeroCols.Count > 0)
            {
                foreach (int j in zeroCols)
                {
                    for (int i = 0; i < result.Row; i++)
                    {
                        result.GetValues()[i].Insert(j, (dynamic)(float)0.0);
                    }
                }
                result.SetCol(A.Col);
            }

            result.FixMinusZero();
            result.SwapCount = swapCount;
        }
コード例 #11
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))]));
        }