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> 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> 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)); }
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); } }
/// <summary> /// Given <paramref name="_base"/> value raised to power <paramref name="_expo"/> /// </summary> /// <param name="_base">Base value</param> /// <param name="_expo">Exponential value</param> /// <param name="matDict">Matrix dictionary to refer to if needed</param> public static void OPExpo(Token _base, Token _expo, Dictionary <string, MatrisBase <dynamic> > matDict, CompilerDictionaryMode mode = CompilerDictionaryMode.Matrix) { if (_expo.tknType != TokenType.NUMBER) { _expo.val = !CheckMatrixAndUpdateVal(_expo, matDict, mode, true) ? throw new Exception(CompilerMessage.EXPO_NOT_SCALAR) : !((MatrisBase <dynamic>)_expo.val).IsScalar() ? throw new Exception(CompilerMessage.MAT_SHOULD_BE_SCALAR) : ((MatrisBase <dynamic>)_expo.val)[0, 0]; } if (CheckMatrixAndUpdateVal(_base, matDict, mode, true)) // base matrix { AssertNotNull(_base); _expo.val = ((MatrisBase <object>)_base.val).Power((dynamic)_expo.val); _expo.tknType = TokenType.MATRIS; Validations.CheckModeAndMatrixReference(mode, _expo.val); } else // base is number { AssertNotNull(_base); AssertNotNull(_expo); _expo.val = CompilerUtils.PowerMethod(double.Parse(_base.val.ToString()), double.Parse(_expo.val.ToString())); _expo.tknType = TokenType.NUMBER; } }
public MatrisBase <object> MatrisMul(MatrisBase <object> A, MatrisBase <object> B) { CompilerUtils.AssertMatrixValsAreNumbers(A); CompilerUtils.AssertMatrixValsAreNumbers(B); return(A is Dataframe df ? new Dataframe(CompilerUtils.MatrisMul(A, B).GetValues(), df.Delimiter, df.NewLine, null, null, df.GetRowSettings().Copy(), df.GetColSettings().Copy()) : CompilerUtils.MatrisMul(A, B)); }
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); }
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> 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> 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); } } }