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 <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)); }
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 })); }
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)); }
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))); }
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))])); }
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 })); }
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)); } }
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)); }
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)); }
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 })); }
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 })); } }
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 })); }
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 )); }
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ı")); } }
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); } } }