Пример #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> 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
            }));
        }
Пример #3
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));
        }
Пример #4
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));
        }
        internal static async Task <Dataframe?> CreateDataframe(TcpConnectionService tcpConnection, CancellationToken ct)
        {
            var dataframe = new Dataframe(tcpConnection, ct);

            var oneByteArray = await dataframe.GetNextBytes(1);

            Debug.WriteLine($"First byte: {oneByteArray?[0]}");

            if (oneByteArray is null)
            {
                return(null);
            }

            var bits = new BitArray(oneByteArray);

            return(dataframe with
            {
                FIN = bits[7],
                RSV1 = bits[6],
                RSV2 = bits[5],
                RSV3 = bits[4],
                Opcode = GetOpcode(),
                Fragment = oneByteArray[0] switch
                {
                    (byte)FragmentKind.First => FragmentKind.First,
                    (byte)FragmentKind.Last => FragmentKind.Last,
                    _ => FragmentKind.None
                }
            });
Пример #6
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)));
        }
Пример #7
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);
            }
        }
Пример #8
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))]));
        }
Пример #9
0
    /* Method: GenerateDataSetFromFile
     * Generates a float dataset from an input file.
     * The dataset is saved on the static class attribute to be used and accessed from another context.
     *
     * Parameters:
     *      - path: path to file on project structure.
     * Outputs:
     *      - N/A
     */
    public static void GenerateDataSetFromFile(string path)
    {
        // Reads lines from file
        var lines = ReadFile(path);

        var xArray = new int[lines.Length];
        var yArray = new int[lines.Length];
        var values = new float[lines.Length];

        // Iterates and separates each line
        for (int i = 0; i < lines.Length; i++)
        {
            var separatedValues = lines[i].Split(';');

            int xCoord = int.Parse(separatedValues[0]);
            int yCoord = int.Parse(separatedValues[1]);

            float.TryParse(separatedValues[2], out float value);


            xArray[i] = xCoord;
            yArray[i] = yCoord;
            values[i] = value;
        }

        dataset        = new Dataframe(xArray, yArray, values);
        visibleDataset = new Dataframe(xArray, yArray, values);

        maxFilter = Mathf.CeilToInt(dataset.maxValue);
        minFilter = Mathf.FloorToInt(dataset.minValue);
    }
Пример #10
0
 public static void ResetFilters()
 {
     maxFilter        = dataset.maxValue;
     minFilter        = dataset.minValue;
     visibleDataset   = dataset;
     newFilterApplied = true;
 }
Пример #11
0
    public static void FilterDataset()
    {
        Dataframe newDataframe = new Dataframe
        {
            width    = dataset.width,
            length   = dataset.length,
            depth    = dataset.depth,
            minValue = dataset.minValue,
            maxValue = dataset.maxValue,
            mean     = dataset.mean
        };

        float [,] newValues = new float[dataset.width, dataset.length];

        int xSize = dataset.values.GetLength(0);
        int ySize = dataset.values.GetLength(1);

        for (int i = 0; i < xSize; i++)
        {
            for (int j = 0; j < ySize; j++)
            {
                float value          = dataset.values[i, j];
                bool  aboveMinFilter = value > minFilter / dataset.depth;
                bool  belowMaxFilter = value < maxFilter / dataset.depth;
                newValues[i, j] = (aboveMinFilter && belowMaxFilter) ? value : 0.00f;
            }
        }

        newDataframe.values = newValues;

        visibleDataset   = newDataframe;
        newFilterApplied = true;
    }
Пример #12
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string dataFileName       = openFileDialog1.FileName;
                System.IO.StreamReader fs = System.IO.File.OpenText(openFileDialog1.FileName);

                string[] token = fs.ReadLine().Split(' ');

                int length   = int.Parse(token[0]);
                int numLabel = int.Parse(token[1]);

                dataframe = new Dataframe(numLabel, length);

                for (int i = 0; i < length; i++)
                {
                    token = fs.ReadLine().Split('@');

                    for (int j = 0; j < numLabel; j++)
                    {
                        dataframe.Features[j, i] = double.Parse(token[j]);
                    }
                }
            }
            else
            {
                return;
            }
        }
 internal async Task SendPong(
     Dataframe dataframe,
     CancellationToken ct) =>
 await ComposeFrameAndSendAsync(
     dataframe.Binary,
     OpcodeKind.Pong,
     FragmentKind.None,
     ct);
 public MatrisBase <object> ToMat(Dataframe dataframe)
 {
     if (dataframe.IsValid() && dataframe.IsAllNumbers())
     {
         return(new MatrisBase <object>(dataframe.Copy().GetValues()));
     }
     else
     {
         throw new Exception(CompilerMessage.INVALID_CONVERSION_TO_MAT);
     }
 }
Пример #15
0
 private void _read()
 {
     _dataframes = new List <Dataframe>();
     {
         var       i = 0;
         Dataframe M_;
         do
         {
             M_ = new Dataframe(m_io, this, m_root);
             _dataframes.Add(M_);
             i++;
         } while (!(M_.Finished));
     }
 }
Пример #16
0
 private void _read()
 {
     _initialFrame = new InitialFrame(m_io, this, m_root);
     if (InitialFrame.Header.Finished != true)
     {
         _trailingFrames = new List <Dataframe>();
         {
             var       i = 0;
             Dataframe M_;
             do
             {
                 M_ = new Dataframe(m_io, this, m_root);
                 _trailingFrames.Add(M_);
                 i++;
             } while (!(M_.Header.Finished));
         }
     }
 }
Пример #17
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
            }));
        }
Пример #18
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));
            }
        }
Пример #19
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));
        }
Пример #20
0
        public MatrisBase <object> Transpose(MatrisBase <object> A)
        {
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

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

            for (int j = 0; j < A.Col; j++)
            {
                result.Add(A.ColList(j, 0));
            }

            return(A is Dataframe df
                ? new Dataframe(result,
                                df.Delimiter,
                                df.NewLine,
                                Dataframe.GetCopyOfLabels(df.GetColLabels()),
                                Dataframe.GetCopyOfLabels(df.GetRowLabels()),
                                df.GetRowSettings().Copy(),
                                df.GetColSettings().Copy())
                : new MatrisBase <object>(result));
        }
Пример #21
0
    private TerrainData UpdateTerrainData(TerrainData td, Dataframe ds = null)
    {
        // updates the terrain object and sets all its properties
        float[,] heights;
        if (ds != null)
        {
            Debug.Log("Input generated map");
            width   = ds.width;
            length  = ds.length;
            depth   = ds.depth;
            heights = ds.values;
        }
        else
        {
            heights = GenerateHeights();
            // Debug.Log("Noise generated map");
        }

        // sets the heigthmap of the terrain
        td.size = new Vector3(width, depth, length);
        td.heightmapResolution = GetMapResolution(width, length);
        td.SetHeights(0, 0, heights);
        return(td);
    }
Пример #22
0
        /// <summary>
        /// Create and save a dataframe with given name and values from text
        /// </summary>
        public async Task OnPostAddDataframe()
        {
            if (_frontService.CheckCmdDate(HttpContext.Session.Get <DateTime>(SessionLastCommandDate)))
            {
                DateTime LastCmdDate = DateTime.Now;

                Dictionary <string, string> reqdict = new Dictionary <string, string>();
                await _utils.ReadAndDecodeRequest(Request.Body, Encoding.Default, IgnoredParams, reqdict);

                if (reqdict.ContainsKey(DfNameParam) &&
                    reqdict.ContainsKey(DfValsParam) &&
                    reqdict.ContainsKey(DfDelimParam) &&
                    reqdict.ContainsKey(DfNewLineParam)
                    )
                {
                    Dictionary <string, Dataframe> _dict = HttpContext.Session.GetDfDict(SessionDfDict, SessionDfLabels, SessionDfSettings);

                    if (!HttpContext.Session.GetMatrixDict(SessionMatrisDict, SessionSeedDict).ContainsKey(reqdict[DfNameParam]))
                    {
                        if (!_dict.ContainsKey(reqdict[DfNameParam]))
                        {
                            try
                            {
                                Validations.ValidMatrixName(reqdict[DfNameParam], throwOnBadName: true);

                                reqdict[DfDelimParam]   = _utils.FixLiterals(reqdict[DfDelimParam]);
                                reqdict[DfNewLineParam] = _utils.FixLiterals(reqdict[DfNewLineParam]);
                                reqdict[DfValsParam]    = _utils.FixLiterals(reqdict[DfValsParam]);

                                List <string> optlist = _utils.GetOptionList(reqdict);

                                using Dataframe df = new Dataframe
                                                     (
                                          _utils.StringTo2DList
                                          (
                                              reqdict[DfValsParam],
                                              reqdict[DfDelimParam],
                                              reqdict[DfNewLineParam],
                                              true,
                                              (int)DataframeLimits.forRows,
                                              (int)DataframeLimits.forCols,
                                              true,
                                              optlist
                                          ),
                                          optlist
                                                     );
                                _frontService.AddToDfDict
                                (
                                    reqdict[DfNameParam],
                                    df,
                                    _dict
                                );

                                Dictionary <string, List <List <object> > >                  vals     = HttpContext.Session.GetMatVals(SessionDfDict, true);
                                Dictionary <string, Dictionary <string, dynamic> >           settings = HttpContext.Session.GetDfSettings(SessionDfSettings);
                                Dictionary <string, Dictionary <string, List <LabelList> > > labels   = HttpContext.Session.GetDfLabels(SessionDfLabels);

                                _frontService.SetDfDicts(_dict, vals, labels, settings);

                                HttpContext.Session.Set(SessionDfDict, vals);
                                HttpContext.Session.SetDfLabels(SessionDfLabels, labels);
                                HttpContext.Session.SetDfSettings(SessionDfSettings, settings);

                                using CommandMessage msg = new CommandMessage(CompilerMessage.SAVED_DF(reqdict[DfNameParam]), CommandState.SUCCESS);
                                HttpContext.Session.SetLastMsg(
                                    SessionLastMessage,
                                    msg
                                    );

                                _utils.DisposeDfDicts(null, labels);
                                vals.Clear();
                                labels.Clear();
                                settings.Clear();
                            }
                            catch (Exception err)
                            {
                                using CommandMessage msg = err.InnerException != null
                                                    ? new CommandMessage(err.InnerException.Message, CommandState.ERROR)
                                                    : new CommandMessage(err.Message, CommandState.ERROR);

                                HttpContext.Session.SetLastMsg(
                                    SessionLastMessage,
                                    msg
                                    );
                            }
                        }
                        else
                        {
                            using CommandMessage msg = new CommandMessage(CompilerMessage.DF_NAME_ALREADY_EXISTS(reqdict[DfNameParam]), CommandState.WARNING);
                            HttpContext.Session.SetLastMsg(
                                SessionLastMessage,
                                msg
                                );
                        }
                    }
                    else
                    {
                        using CommandMessage msg = new CommandMessage(CompilerMessage.MAT_NAME_ALREADY_EXISTS(reqdict[DfNameParam]), CommandState.WARNING);
                        HttpContext.Session.SetLastMsg(
                            SessionLastMessage,
                            msg
                            );
                    }

                    _utils.DisposeDfDicts(_dict, null);
                    _dict.Clear();
                }
                else
                {
                    using CommandMessage msg = new CommandMessage(RequestMessage.REQUEST_MISSING_KEYS("AddDataframe", new string[2] { DfNameParam, DfValsParam }), CommandState.ERROR);
                    HttpContext.Session.SetLastMsg(
                        SessionLastMessage,
                        msg
                        );
                }

                HttpContext.Session.Set(SessionLastCommandDate, LastCmdDate);
            }
            else
            {
                using CommandMessage msg = new CommandMessage(RequestMessage.REQUEST_SPAM(HttpContext.Session.Get <DateTime>(SessionLastCommandDate)), CommandState.WARNING);
                HttpContext.Session.SetLastMsg(
                    SessionLastMessage,
                    msg
                    );
            }
        }
Пример #23
0
 private void button3_Click(object sender, EventArgs e)
 {
     dataframe = new Dataframe();
     dataframe.makeRandomData(9, 500);
 }
Пример #24
0
        public MatrisBase <object> PseudoInverse(MatrisBase <object> A,
                                                 int side = -1)
        {
            if (Rank(A) != Math.Min(A.Row, A.Col))
            {
                throw new Exception(CompilerMessage.MAT_PSEINV_NOT_FULL_RANK);
            }

            if (side != -1 && side != 1)
            {
                throw new Exception(CompilerMessage.MAT_PSEINV_BAD_SIDE);
            }

            CompilerUtils.AssertMatrixValsAreNumbers(A);

            string sidename = side == -1 ? "sol" : "sağ";

            // Left inverse
            if (side == -1)
            {
                try
                {
                    return(A is Dataframe df
                        ? new Dataframe(MatrisMul(Inverse(MatrisMul(Conjugate(A), A)), Conjugate(A)).GetValues(),
                                        df.Delimiter,
                                        df.NewLine,
                                        Dataframe.GetCopyOfLabels(df.GetRowLabels()),
                                        Dataframe.GetCopyOfLabels(df.GetColLabels()),
                                        df.GetRowSettings().Copy(),
                                        df.GetColSettings().Copy())
                        : MatrisMul(Inverse(MatrisMul(Conjugate(A), A)), Conjugate(A)));
                }
                catch (Exception err)
                {
                    if (err.Message == CompilerMessage.MAT_DET_ZERO_NO_INV)
                    {
                        throw new Exception(CompilerMessage.MAT_PSEINV_DET_ZERO(sidename));
                    }

                    throw new Exception("Genelleştirilmiş ters matris hatası:\n", err);
                }
            }
            else  // Right inverse
            {
                try
                {
                    return(A is Dataframe df
                        ? new Dataframe(MatrisMul(Conjugate(A), Inverse(MatrisMul(A, Conjugate(A)))).GetValues(),
                                        df.Delimiter,
                                        df.NewLine,
                                        Dataframe.GetCopyOfLabels(df.GetRowLabels()),
                                        Dataframe.GetCopyOfLabels(df.GetColLabels()),
                                        df.GetRowSettings().Copy(),
                                        df.GetColSettings().Copy())
                        : MatrisMul(Conjugate(A), Inverse(MatrisMul(A, Conjugate(A)))));
                }
                catch (Exception err)
                {
                    if (err.Message == CompilerMessage.MAT_DET_ZERO_NO_INV)
                    {
                        throw new Exception(CompilerMessage.MAT_PSEINV_DET_ZERO(sidename));
                    }

                    throw new Exception("Genelleştirilmiş ters matris hatası:\n", err);
                }
            }
        }
Пример #25
0
        public MatrisBase <object> Mode(MatrisBase <object> df,
                                        int numberOnly = 1)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            int           nc    = df.Col;
            List <object> modes = new List <object>();

            for (int j = 0; j < nc; j++)
            {
                List <object>            col     = df.ColList(j, 0);
                Dictionary <object, int> tracker = new Dictionary <object, int>();
                foreach (object val in col)
                {
                    if (tracker.ContainsKey(val))
                    {
                        tracker[val]++;
                    }
                    else
                    {
                        tracker.Add(val, 1);
                    }
                }

                object mode        = null;
                object placeholder = new None();
                placeholder = (df is Dataframe ? placeholder : float.NaN);

                int currentmax = 0;
                foreach (KeyValuePair <object, int> pair in tracker)
                {
                    if (numberOnly == 1 && (pair.Key is None ||
                                            pair.Key is null ||
                                            ((!float.TryParse(pair.Value.ToString(), out float res) ||
                                              float.IsNaN(res)))))
                    {
                        continue;
                    }

                    if (pair.Value > currentmax)
                    {
                        currentmax = pair.Value;
                        mode       = pair.Key;
                    }
                }
                modes.Add(mode ?? placeholder);
            }

            return(df is Dataframe dataframe
                ? new Dataframe(new List <List <object> >()
            {
                modes
            },
                                dataframe.Delimiter,
                                dataframe.NewLine,
                                null,
                                Dataframe.GetCopyOfLabels(dataframe.GetColLabels()),
                                null,
                                dataframe.GetColSettings().Copy())
                : new MatrisBase <object>(new List <List <object> >()
            {
                modes
            }));
        }
Пример #26
0
        public MatrisBase <object> Median(MatrisBase <object> df,
                                          int numberOnly = 1)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            int           pop;
            int           nr   = df.Row;
            int           nc   = df.Col;
            List <object> meds = new List <object>();

            if (df is Dataframe dataframe)
            {
                for (int j = 0; j < nc; j++)
                {
                    if (!dataframe.IsAllNumberColumn(j, 0))
                    {
                        if (numberOnly == 1)
                        {
                            List <object> col = new List <object>();
                            foreach (object o in dataframe.ColList(j, 0))
                            {
                                if (o is None ||
                                    o is null ||
                                    ((!float.TryParse(o.ToString(), out float res) ||
                                      float.IsNaN(res))))
                                {
                                    continue;
                                }
                                col.Add(o);
                            }

                            if (col.Count == 0)
                            {
                                meds.Add(float.NaN);
                                continue;
                            }

                            pop = nr - df.AmountOfNanInColumn(j);
                            col.Sort();
                            meds.Add((pop % 2 == 1)
                                ? (float)col[((pop + 1) / 2) - 1]
                                : ((float)col[(pop / 2) - 1] + (float)col[(int)Math.Round((decimal)(pop + 1) / 2, 0) - 1]) / 2);
                        }
                        else
                        {
                            meds.Add(float.NaN);
                        }
                    }
                    else
                    {
                        List <object> col = dataframe.ColList(j, 0);
                        col.Sort();
                        meds.Add((nr % 2 == 1)
                            ? (float)col[((nr + 1) / 2) - 1]
                            : ((float)col[(nr / 2) - 1] + (float)col[(int)Math.Round((decimal)(nr + 1) / 2, 0) - 1]) / 2);
                    }
                }

                return(new Dataframe(new List <List <object> >()
                {
                    meds
                },
                                     dataframe.Delimiter,
                                     dataframe.NewLine,
                                     null,
                                     Dataframe.GetCopyOfLabels(dataframe.GetColLabels()),
                                     null,
                                     dataframe.GetColSettings().Copy()));
            }
            else
            {
                for (int j = 0; j < nc; j++)
                {
                    if (numberOnly == 1)
                    {
                        List <object> col = new List <object>();
                        foreach (object o in df.ColList(j, 0))
                        {
                            if (o is None ||
                                o is null ||
                                ((!float.TryParse(o.ToString(), out float res) ||
                                  float.IsNaN(res))))
                            {
                                continue;
                            }
                            col.Add(o);
                        }

                        if (col.Count == 0)
                        {
                            meds.Add(float.NaN);
                            continue;
                        }

                        pop = nr - df.AmountOfNanInColumn(j);
                        col.Sort();
                        meds.Add((pop % 2 == 1)
                            ? (float)col[((pop + 1) / 2) - 1]
                            : ((float)col[(pop / 2) - 1] + (float)col[(int)Math.Round((decimal)(pop + 1) / 2, 0) - 1]) / 2);
                    }
                    else
                    {
                        List <object> col = df.ColList(j, 0);
                        col.Sort();
                        meds.Add((nr % 2 == 1)
                            ? (float)col[((nr + 1) / 2) - 1]
                            : (float)col[(nr / 2) - 1] + (float)col[(int)Math.Round((decimal)(nr + 1) / 2, 0) - 1]);
                    }
                }

                return(new MatrisBase <object>(new List <List <object> >()
                {
                    meds
                }));
            }
        }
Пример #27
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
            }));
        }
Пример #28
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ı"));
            }
        }
Пример #29
0
        public MatrisBase <object> Describe(MatrisBase <object> df,
                                            int usePopulation = 0,
                                            int numberOnly    = 1)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            List <object> mins = ArrayMethods.CopyList <float>(Min(df, numberOnly)[0]);
            List <object> meds = ArrayMethods.CopyList <float>(Median(df, numberOnly)[0]);
            List <object> maxs = ArrayMethods.CopyList <float>(Max(df, numberOnly)[0]);
            List <object> mods = ArrayMethods.CopyList <float>(Mode(df, numberOnly)[0]);
            List <object> meas = ArrayMethods.CopyList <float>(Mean(df, numberOnly)[0]);
            List <object> sdev = ArrayMethods.CopyList <float>(SDev(df, usePopulation, numberOnly)[0]);
            List <object> vars = ArrayMethods.CopyList <float>(Var(df, usePopulation, numberOnly)[0]);

            int nc = df.Col;
            List <List <object> > desc = new List <List <object> >();

            for (int j = 0; j < nc; j++)
            {
                desc.Add(new List <object>()
                {
                    mins[j],
                    meds[j],
                    maxs[j],
                    mods[j],
                    meas[j],
                    sdev[j],
                    vars[j]
                }
                         );
            }
            mins.Clear();
            meds.Clear();
            maxs.Clear();
            mods.Clear();
            meas.Clear();
            sdev.Clear();
            vars.Clear();

            List <LabelList> newcollabels = CompilerUtils.Create1DLabelListFromList("Min", "Median", "Max", "Mode", "Mean", "Sdev", "Var");

            return(df is Dataframe dataframe
                ? new Dataframe(desc,
                                dataframe.Delimiter,
                                dataframe.NewLine,
                                Dataframe.GetCopyOfLabels(dataframe.GetColLabels()) ?? new List <LabelList>()
            {
                new LabelList(df.Col, 1, "col_", 1)
            },
                                newcollabels,
                                dataframe.GetRowSettings().Copy(),
                                dataframe.GetColSettings().Copy(),
                                true
                                )
                : new Dataframe(desc,
                                rowLabels: new List <LabelList>()
            {
                new LabelList(df.Col, 1, "col_", 1)
            },
                                colLabels: newcollabels
                                ));
        }