public MatrisBase <dynamic> Range(float start, float end, float interval = 1, int axis = 0, int digits = 6) { if (start > end) { throw new Exception(CompilerMessage.MAT_START_END_ORDER); } if (interval > end - start) { throw new Exception(CompilerMessage.MAT_INTERVAL_INVALID); } if (axis != 0 && axis != 1) { throw new Exception(CompilerMessage.ARG_INVALID_VALUE("axis", "Satır vektör için 0, sütun vekör için 1 olmalı.")); } int amount = Math.Min((int)((end - start) / interval), axis == 0 ? (int)MatrisLimits.forRows : (int)MatrisLimits.forCols); if (Math.Abs(end - (start + (amount * interval))) > 1e-7) { throw new Exception(CompilerMessage.MAT_INTERVAL_EXCESS); } List <List <dynamic> > vals = new List <List <dynamic> >(); if (axis == 0) { vals.Add(new List <dynamic>()); for (int i = 0; i <= amount; i++) { vals[0].Add(Math.Round(start + (interval * i), digits)); } } else { for (int i = 0; i <= amount; i++) { vals.Add(new List <dynamic>() { Math.Round(start + (interval * i), digits) }); } } return(new MatrisBase <dynamic>(vals)); }
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> 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> 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> 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ı")); } }