Beispiel #1
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);
            }
        }
Beispiel #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));
        }
Beispiel #3
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));
        }
 /// <summary>
 /// Assert given matrix has values which are all parsable as numbers
 /// </summary>
 /// <param name="df">Matrix or dataframe to check</param>
 /// <exception cref="CommandMessage.DF_HAS_NON_NUMBER_VALS"><see cref=""/></exception>
 public static void AssertMatrixValsAreNumbers(MatrisBase <object> df)
 {
     if (df is Dataframe _df && !_df.IsAllNumbers())
     {
         throw new Exception(CompilerMessage.DF_HAS_NON_NUMBER_VALS);
     }
 }
Beispiel #5
0
 public MatrisBase <object> Get(MatrisBase <object> A,
                                int i,
                                int j,
                                int based = 0)
 {
     return(!A.IsValid()
         ? throw new Exception(CompilerMessage.MAT_INVALID_SIZE)
         : i - based < 0 || i - based >= A.Row
         ? throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("satır", based, A.Row - 1))
         : j - based < 0 || j - based >= A.Col
         ? throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("sütun", based, A.Col - 1))
         : A is Dataframe df
             ? new Dataframe(new List <List <object> >()
     {
         new List <object>()
         {
             A[r: i - based, c: j - based]
         }
     },
                             df.Delimiter,
                             df.NewLine,
                             null,
                             null,
                             df.GetRowSettings().Copy(),
                             df.GetColSettings().Copy())
             : new MatrisBase <object>(new List <List <object> >()
     {
         new List <object>()
         {
             A[r: i - based, c: j - based]
         }
     }));
 }
Beispiel #6
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))]));
        }
Beispiel #7
0
 public float Minor(MatrisBase <object> A,
                    int row,
                    int col,
                    int based = 0)
 {
     return(Determinant(MinorMatris(A, row, col, based)));
 }
Beispiel #8
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)));
        }
Beispiel #9
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);
            }
        }
Beispiel #10
0
        public MatrisBase <object> Abs(MatrisBase <object> A)
        {
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            CompilerUtils.AssertMatrixValsAreNumbers(A);

            int m = A.Row;
            int n = A.Col;
            List <List <object> > vals    = A.GetValues();
            List <List <object> > newvals = new List <List <object> >();

            for (int i = 0; i < m; i++)
            {
                newvals.Add(new List <object>());
                for (int j = 0; j < n; j++)
                {
                    newvals[i].Add((dynamic)Math.Abs(float.Parse(vals[i][j].ToString())));
                }
            }

            return(A is Dataframe df
                ? new Dataframe(newvals,
                                df.Delimiter,
                                df.NewLine,
                                Dataframe.GetCopyOfLabels(df.GetRowLabels()),
                                Dataframe.GetCopyOfLabels(df.GetColLabels()),
                                df.GetRowSettings().Copy(),
                                df.GetColSettings().Copy())
                : new MatrisBase <object>(newvals));
        }
Beispiel #11
0
        public MatrisBase <object> Mul(MatrisBase <object> df,
                                       int numberOnly = 1)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            int nc = df.Col;
            int nr = df.Row;

            List <object> muls = new List <object>();

            for (int c = 0; c < nc; c++)
            {
                muls.Add(ArrayMethods.ArrayMul(df.ColList(c, 0), 0, nr, numberOnly) ?? float.NaN);
            }

            return(df is Dataframe data
                ? new Dataframe(new List <List <object> >()
            {
                muls
            },
                                data.Delimiter,
                                data.NewLine,
                                null,
                                Dataframe.GetCopyOfLabels(data.GetColLabels()),
                                null,
                                data.GetColSettings().Copy())
                : new MatrisBase <object>(new List <List <object> >()
            {
                muls
            }));
        }
Beispiel #12
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)]));
        }
        public MatrisBase <dynamic> Fill(int row,
                                         int col,
                                         float fill)
        {
            if (row <= 0 || col <= 0)
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            row = Math.Min(row, (int)MatrisLimits.forRows);
            col = Math.Min(col, (int)MatrisLimits.forCols);

            List <List <dynamic> > vals = new List <List <dynamic> >();

            for (int i = 0; i < row; i++)
            {
                vals.Add(new List <dynamic>());
                for (int j = 0; j < col; j++)
                {
                    vals[i].Add(fill);
                }
            }

            MatrisBase <dynamic> res = new MatrisBase <dynamic>()
            {
                Row = row, Col = col
            };

            res.SetValues(vals);
            return(res);
        }
        /// <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);
        }
Beispiel #15
0
 public MatrisBase <object> Col(MatrisBase <object> A,
                                int j,
                                int based = 0)
 {
     return(!A.IsValid()
         ? throw new Exception(CompilerMessage.MAT_INVALID_SIZE)
         : j - based < 0 || j - based >= A.Col
         ? throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("sütun", based, A.Col - 1))
         : A.ColMat(j, based));
 }
Beispiel #16
0
 public MatrisBase <object> Row(MatrisBase <object> A,
                                int i,
                                int based = 0)
 {
     return(!A.IsValid()
         ? throw new Exception(CompilerMessage.MAT_INVALID_SIZE)
         : i - based < 0 || i - based >= A.Row
         ? throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("satır", based, A.Row - 1))
         : A.RowMat(i, based));
 }
 public Dataframe ToDf(MatrisBase <object> matrix)
 {
     if (matrix.IsValid())
     {
         return(new Dataframe(matrix.Copy().GetValues()));
     }
     else
     {
         throw new Exception(CompilerMessage.INVALID_CONVERSION_TO_DF);
     }
 }
        public MatrisBase <dynamic> RandFloat(int row,
                                              int col,
                                              float min    = (float)0.0,
                                              float max    = (float)1.0,
                                              dynamic seed = null)
        {
            if (row <= 0 || col <= 0)
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            row = Math.Min(row, (int)MatrisLimits.forRows);
            col = Math.Min(col, (int)MatrisLimits.forCols);

            Random random;
            int    s;

            if (seed != null)
            {
                random = new Random(seed);
                s      = seed;
            }
            else
            {
                s      = Environment.TickCount & int.MaxValue;
                random = new Random(s);
            }

            if (max < min)
            {
                throw new Exception(CompilerMessage.MAT_MINMAX_ORDER);
            }

            List <List <dynamic> > vals = new List <List <dynamic> >();
            float realmax = max - min;

            for (int i = 0; i < row; i++)
            {
                vals.Add(new List <dynamic>());
                for (int j = 0; j < col; j++)
                {
                    vals[i].Add((dynamic)(min + (((float)random.NextDouble()) * realmax)));
                }
            }

            MatrisBase <dynamic> res = new MatrisBase <dynamic>()
            {
                Row = row, Col = col, Seed = s, CreatedFromSeed = true
            };

            res.SetValues(vals);
            return(res);
        }
        public void MatrisMul()
        {
            MatrisBase <dynamic> matmul_A_B =
                new MatrisBase <dynamic>(new List <List <dynamic> >()
            {
                new List <dynamic>()
                {
                    -1, 1
                },
                new List <dynamic>()
                {
                    -1, 1
                }
            }
                                         );
            MatrisBase <dynamic> matmul_B_A =
                new MatrisBase <dynamic>(new List <List <dynamic> >()
            {
                new List <dynamic>()
                {
                    -2, -2
                },
                new List <dynamic>()
                {
                    2, 2
                }
            }
                                         );

            MatrisBase <dynamic> matmul_A_B_Result = MatrisArithmeticService.MatrisMul(A, B);

            Assert.AreEqual(matmul_A_B_Result.ToString(),
                            matmul_A_B.ToString(),
                            "\nMatris çarpımı hatalı! \nBeklenen:\n" + matmul_A_B.ToString() + "\nAlınan:\n" + matmul_A_B_Result.ToString());

            MatrisBase <dynamic> matmul_B_A_Result = MatrisArithmeticService.MatrisMul(B, A);

            Assert.AreEqual(matmul_B_A_Result.ToString(),
                            matmul_B_A.ToString(),
                            "\nMatris çarpımı hatalı! \nBeklenen:\n" + matmul_B_A.ToString() + "\nAlınan:\n" + matmul_B_A_Result.ToString());

            MatrisBase <dynamic> matmul_A_ID = MatrisArithmeticService.MatrisMul(A, SpecialMatricesService.Identity(2));

            Assert.AreEqual(A.ToString(),
                            matmul_A_ID.ToString(),
                            "\nMatris çarpımı hatalı! \nBeklenen:\n" + A.ToString() + "\nAlınan:\n" + matmul_A_ID.ToString());

            MatrisBase <dynamic> matmul_ID_A = MatrisArithmeticService.MatrisMul(SpecialMatricesService.Identity(2), A);

            Assert.AreEqual(A.ToString(),
                            matmul_ID_A.ToString(),
                            "\nMatris çarpımı hatalı! \nBeklenen:\n" + A.ToString() + "\nAlınan:\n" + matmul_ID_A.ToString());
        }
Beispiel #20
0
        private static void InnerRREchelon(MatrisBase <object> A, MatrisBase <object> result)
        {
            int lead = 0;
            int nr   = A.Row;
            int nc   = A.Col;

            for (int r = 0; r < nr; r++)
            {
                if (nc <= lead)
                {
                    break;
                }
                int i = r;
                while (float.Parse(result[i, lead].ToString()) == (float)0.0)
                {
                    i++;
                    if (nr == i)
                    {
                        i = r;
                        lead++;
                        if (nc == lead)
                        {
                            break;
                        }
                    }
                }

                if (nc == lead)
                {
                    break;
                }
                result.Swap(i, r, 0, 0);

                if (float.Parse(result[r, lead].ToString()) != (float)0.0)
                {
                    result.MulRow(r, (float)1.0 / float.Parse(result[r, lead].ToString()), 0);
                }
                for (int j = 0; j < nr; j++)
                {
                    if (j != r)
                    {
                        for (int k = 0; k < nc; k++)
                        {
                            result.MulThenSubFromOtherRow(r, float.Parse(result[j, lead].ToString()), j, 0);
                        }
                    }
                }
                lead++;
            }

            result.FixMinusZero();
        }
Beispiel #21
0
        public MatrisBase <object> MatrisMul(MatrisBase <object> A,
                                             MatrisBase <object> B)
        {
            CompilerUtils.AssertMatrixValsAreNumbers(A);
            CompilerUtils.AssertMatrixValsAreNumbers(B);

            return(A is Dataframe df
                ? new Dataframe(CompilerUtils.MatrisMul(A, B).GetValues(),
                                df.Delimiter,
                                df.NewLine,
                                null,
                                null,
                                df.GetRowSettings().Copy(),
                                df.GetColSettings().Copy())
                : CompilerUtils.MatrisMul(A, B));
        }
Beispiel #22
0
        public MatrisBase <object> Resize(MatrisBase <object> A,
                                          int row,
                                          int col)
        {
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            if (A.ElementCount != row * col)
            {
                throw new Exception(CompilerMessage.MAT_INVALID_RESIZE);
            }

            int m = A.Row;
            int n = A.Col;
            List <List <object> > vals = A.GetValues();

            List <List <object> > newlis = new List <List <object> >();

            dynamic val;

            for (int r = 0; r < row; r++)
            {
                newlis.Add(new List <object>());
            }

            for (int r = 0; r < m; r++)
            {
                for (int c = 0; c < n; c++)
                {
                    val = vals[r][c];
                    newlis[((r * n) + c) / col].Add(val);
                }
            }

            return(A is Dataframe df
                ? new Dataframe(newlis,
                                df.Delimiter,
                                df.NewLine,
                                null,
                                null,
                                df.GetRowSettings().Copy(),
                                df.GetColSettings().Copy())
                : new MatrisBase <object>(newlis));
        }
Beispiel #23
0
        public MatrisBase <object> Mean(MatrisBase <object> df,
                                        int numberOnly = 1)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            int nc = df.Col;
            int nr = df.Row;

            List <object> means = new List <object>();
            int           pop;

            for (int c = 0; c < nc; c++)
            {
                pop = nr - (numberOnly == 1 ? df.AmountOfNanInColumn(c) : 0);
                object res = ArrayMethods.ArraySum(df.ColList(c, 0), 0, nr, numberOnly) ?? float.NaN;
                if (pop == 0)
                {
                    means.Add(float.NaN);
                }
                else
                {
                    means.Add(float.IsNaN((float)res) ? res : (float)res / pop);
                }
            }

            return(df is Dataframe data
                ? new Dataframe(new List <List <object> >()
            {
                means
            },
                                data.Delimiter,
                                data.NewLine,
                                null,
                                Dataframe.GetCopyOfLabels(data.GetColLabels()),
                                null,
                                data.GetColSettings().Copy())
                : new MatrisBase <object>(new List <List <object> >()
            {
                means
            }));
        }
Beispiel #24
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);
        }
Beispiel #25
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));
            }
        }
        /// <summary>
        /// Gets the matrix multiplication of <paramref name="A"/> and <paramref name="B"/> in the given order
        /// </summary>
        /// <param name="A">Matrix on the left</param>
        /// <param name="B">Matrix on the right</param>
        /// <returns>Resulting matrix from the matrix multiplication of <paramref name="A"/> and <paramref name="B"/></returns>
        public static MatrisBase <T> MatrisMul <T>(MatrisBase <T> A, MatrisBase <T> B)
        {
            if (A.Col != B.Row)
            {
                throw new Exception(CompilerMessage.MAT_MUL_BAD_SIZE);
            }

            List <List <T> > result = new List <List <T> >();

            for (int i = 0; i < A.Row; i++)
            {
                result.Add(new List <T>());
                for (int j = 0; j < B.Col; j++)
                {
                    result[i].Add(DotProduct(A.GetValues()[i], B.ColList(j, 0)));
                }
            }

            return(new MatrisBase <T>(result));
        }
        public void Transpose()
        {
            MatrisBase <dynamic> A_T =
                new MatrisBase <dynamic>(new List <List <dynamic> >()
            {
                new List <dynamic>()
                {
                    1, 3
                },
                new List <dynamic>()
                {
                    2, 4
                }
            }
                                         );
            MatrisBase <dynamic> B_T =
                new MatrisBase <dynamic>(new List <List <dynamic> >()
            {
                new List <dynamic>()
                {
                    1, -1
                },
                new List <dynamic>()
                {
                    -1, 1
                }
            }
                                         );

            MatrisBase <dynamic> transpose_A = MatrisArithmeticService.Transpose(A);

            Assert.AreEqual(transpose_A.ToString(),
                            A_T.ToString(),
                            "\nMatris transpozu hatalı! \nBeklenen:\n" + A_T.ToString() + "\nAlınan:\n" + transpose_A.ToString());

            MatrisBase <dynamic> transpose_B = MatrisArithmeticService.Transpose(B);

            Assert.AreEqual(transpose_B.ToString(),
                            B_T.ToString(),
                            "\nMatris transpozu hatalı! \nBeklenen:\n" + B_T.ToString() + "\nAlınan:\n" + transpose_B.ToString());
        }
Beispiel #28
0
        public MatrisBase <object> Sub(MatrisBase <object> A,
                                       int r1,
                                       int r2,
                                       int c1,
                                       int c2,
                                       int based = 0)
        {
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            r1 -= based;
            r2 -= based;
            if (r2 <= r1 || r1 < 0)
            {
                throw new Exception(CompilerMessage.MAT_INVALID_ROW_INDICES);
            }

            c1 -= based;
            c2 -= based;
            if (c2 <= c1 || c1 < 0)
            {
                throw new Exception(CompilerMessage.MAT_INVALID_COL_INDICES);
            }

            return(r2 > A.Row
                ? throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("satır", based, A.Row - 1))
                : c2 > A.Col
                ? throw new Exception(CompilerMessage.MAT_OUT_OF_RANGE_INDEX("sütun", based, A.Col - 1))
                : A is Dataframe df
                ? new Dataframe(A[new Range(r1, r2), new Range(c1, c2)],
                                df.Delimiter,
                                df.NewLine,
                                null,
                                null,
                                df.GetRowSettings().Copy(),
                                df.GetColSettings().Copy())
                : new MatrisBase <object>(A[new Range(r1, r2), new Range(c1, c2)]));
        }
Beispiel #29
0
        public MatrisBase <object> Round(MatrisBase <object> A,
                                         int n = 0)
        {
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            CompilerUtils.AssertMatrixValsAreNumbers(A);

            return(n < 0
                ? throw new Exception(CompilerMessage.ARG_INVALID_VALUE("n", " Sıfırdan büyük olmalı."))
                : A is Dataframe df
                ? new Dataframe(A.Round(n).GetValues(),
                                df.Delimiter,
                                df.NewLine,
                                Dataframe.GetCopyOfLabels(df.GetRowLabels()),
                                Dataframe.GetCopyOfLabels(df.GetColLabels()),
                                df.GetRowSettings().Copy(),
                                df.GetColSettings().Copy())
                : A.Round(n));
        }
Beispiel #30
0
        public float Trace(MatrisBase <object> A)
        {
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID);
            }
            float trace = float.Parse(A[0, 0].ToString());
            int   m     = Math.Min(A.Row, A.Col);

            for (int i = 1; i < m; i++)
            {
                if (float.TryParse(A[i, i].ToString(), out float res))
                {
                    trace += res;
                }
                else
                {
                    return(float.NaN);
                }
            }

            return(trace);
        }