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> 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> 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> 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> 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); } }
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 <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)); }
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> 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)])); }
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 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); }
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); }
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 int RowDim(MatrisBase <object> A) { return(A.IsValid() ? A.Row : 0); }
public int ColDim(MatrisBase <object> A) { return(A.IsValid() ? A.Col : 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 )); }
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ı")); } }