Beispiel #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));
        }
Beispiel #2
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 #3
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 #4
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));
        }
        /// <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));
        }
Beispiel #6
0
        public MatrisBase <object> Replace(MatrisBase <object> A,
                                           dynamic old  = null,
                                           dynamic with = null,
                                           float TOL    = (float)1e-6)
        {
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            if (TOL < 0)
            {
                throw new Exception(CompilerMessage.ARG_INVALID_VALUE("TOL", " Sıfırdan büyük-eşit olmalı."));
            }

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

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

            if (with is null)
            {
                with = new None();
            }

            if (old is null || old is None)
            {
                for (int i = 0; i < m; i++)
                {
                    newVals.Add(new List <object>());
                    for (int j = 0; j < n; j++)
                    {
                        if (vals[i][j] is null || vals[i][j] is None)
                        {
                            newVals[i].Add(with);
                        }
Beispiel #7
0
        public MatrisBase <object> SDev(MatrisBase <object> df,
                                        int usePopulation = 0,
                                        int numberOnly    = 1)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            if (usePopulation != 0 && usePopulation != 1)
            {
                throw new Exception(CompilerMessage.ARG_INVALID_VALUE("usePopulation", "Örneklem için 0, popülasyon için 1 olmalı!"));
            }

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

            if (nr == 1 && usePopulation == 1)
            {
                usePopulation = 0;
            }

            List <object>         means = Mean(df).RowList(1);
            List <object>         sdevs = new List <object>();
            List <List <object> > vals  = df.GetValues();
            int pop;

            for (int j = 0; j < nc; j++)
            {
                float sdev = 0;
                float mean = (float)means[j];
                if (float.IsNaN(mean))
                {
                    if (numberOnly == 1)
                    {
                        sdevs.Add(0);
                    }
                    else
                    {
                        sdevs.Add(float.NaN);
                    }
                    continue;
                }
                for (int i = 0; i < nr; i++)
                {
                    if (float.TryParse(vals[i][j].ToString(), out float res))
                    {
                        if (numberOnly == 1 && float.IsNaN(res))
                        {
                            continue;
                        }
                        sdev += (float)Math.Pow(res - mean, 2);
                    }
                    else
                    {
                        if (numberOnly == 1)
                        {
                            continue;
                        }
                        else
                        {
                            sdev = float.NaN;
                            break;
                        }
                    }
                }
                pop = nr - usePopulation - (numberOnly == 1 ? df.AmountOfNanInColumn(j) : 0);
                if (pop == 0)
                {
                    sdevs.Add(0);
                }
                else
                {
                    sdevs.Add((float)Math.Pow(sdev * (1.0 / pop), 0.5));
                }
            }

            return(df is Dataframe data
                ? new Dataframe(new List <List <object> >()
            {
                sdevs
            },
                                data.Delimiter,
                                data.NewLine,
                                null,
                                Dataframe.GetCopyOfLabels(data.GetColLabels()),
                                null,
                                data.GetColSettings().Copy())
                : new MatrisBase <object>(new List <List <object> >()
            {
                sdevs
            }));
        }
Beispiel #8
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ı"));
            }
        }
Beispiel #9
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;
        }