} // The function deals with the Order/power operation. bool Unit_Intake_Command() // For the command like sin, cos, tan { bool done = true; if (Expressions.Contains("`")) { for (int c = Expressions.Count - 1; c >= 0; c--) { string Element = Expressions [c]; if (Element == "`") { MathParserExpression RHS = theData [Expressions [c + 1].Trim("-".ToCharArray())]; if (Expressions [c + 1].Contains("-")) { if (RHS.Type.Contains("Number")) { RHS = new MathParserExpression((Number)RHS.Data * new Number(-1)); Expressions [c + 1].Replace("-", ""); } if (RHS.Type.Contains("Matrix")) { RHS = new MathParserExpression((Matrix)RHS.Data * -1); Expressions [c + 1].Replace("-", ""); } } UnitFunctionSolver ufs = new UnitFunctionSolver(Expressions [c - 1].Trim('-'), RHS); if (ufs.isProcessed()) { string name = autoNamer(); MathParserExpression ans = ufs.getSolution(); if (Expressions [c - 1].Contains("-")) { if (ans.Type.Contains("Number")) { ans = new MathParserExpression((Number)ans.Data * new Number(-1)); Expressions [c + 1].Replace("-", ""); } if (ans.Type.Contains("Matrix")) { ans = new MathParserExpression((Matrix)ans.Data * -1); Expressions [c + 1].Replace("-", ""); } } theData.Add(name, ans); Expressions [c - 1] = name; } else { Processed = false; done = false; break; } Expressions.RemoveRange(c, 2); c = Expressions.Count; } } } return(done); }
public void Solve() { MathParser.StringObserver sol = new StringObserver(theExpression, Checker.History); if (sol.isProcessed()) { solution = sol.getSolution(); } else { Processed = false; throw new Exception("Expression Unsolvable."); } solution.Statement = theExpression; if (saveHistory) { if (string.IsNullOrEmpty(solution.Tag) || string.IsNullOrWhiteSpace(solution.Tag)) { if (solution.Type.Contains("Number")) { string name = autoNumberNamer(); solution.Tag = name; solution.setEntireTag(name); Checker.History.Add(name, solution); } else if (solution.Type.Contains("Matrix")) { string name = autoMatrixNamer(); solution.Tag = name; solution.setEntireTag(name); Checker.History.Add(name, solution); } } else { string name = solution.Tag; if (Checker.History.ContainsKey(name)) { // solution.setEntireTag (name); Checker.History[name] = solution; } else { // solution.setEntireTag (name); Checker.History.Add(name, solution); } } } } // end method for solve
public void Solve() { if (Expressions.Count == 0) { Processed = false; throw new MathParserException("No Data to processes"); } else if (Expressions.Count == 1) { if (Expressions[0].Contains("-")) { MathParserExpression hold = theData[Expressions[0].Trim("-".ToCharArray())]; if (hold.Type.Contains("Number")) { Number ans = new Number((double)-1) * hold.Data; solution = new MathParserExpression(ans); } else if (hold.Type.Contains("Matrix")) { Matrix ans = -1 * hold.Data; solution = new MathParserExpression(ans); } } else { solution = theData[Expressions[0].Trim("-".ToCharArray())]; } } else { if (Unit_Intake_Command()) { if (Order()) { if (Division()) { if (Multiplication()) { if (Addition()) { solution = new MathParserExpression(theData [Expressions [0].Trim("-".ToCharArray())]); } } } } } } }
} // The function deals with the multiplication operations. bool Division() { bool done = true; if (Expressions.Contains("÷") || Expressions.Contains("/")) { for (int c = Expressions.Count - 1; c >= 0; c--) { string Element = Expressions [c]; if (Element == "/" || Element == "÷") { MathParserExpression lhs = theData [Expressions [c - 1].Trim("-".ToCharArray())]; MathParserExpression rhs = theData [Expressions [c + 1].Trim("-".ToCharArray())]; if (lhs.Type.Contains("Number") && rhs.Type.Contains("Number")) { Number l = lhs.Data; Number r = rhs.Data; Number ans = new Number((double)l.Data / (double)r.Data); string name = autoNamer(); theData.Add(name, new MathParserExpression(ans)); Expressions [c - 1] = name; } else if (lhs.Type.Contains("Matrix") && rhs.Type.Contains("Number")) { Matrix l = lhs.Data; Number r = rhs.Data; Matrix ans = l / ((double)r.Data); string name = autoNamer(); theData.Add(name, new MathParserExpression(ans)); Expressions [c - 1] = name; } else { Processed = false; done = false; throw new Exception("Invalid Division."); } Expressions.RemoveRange(c, 2); c = Expressions.Count - 1; } } } return(done); }
private void Interpret() { if (isOverdrive(givenString)) { MathParserOverdrive mpo = new MathParserOverdrive(givenString.TrimStart(new char[] { '@' })); mpo.Solve(); if (isProcessed()) { Solution = mpo.getSolution(); Processed = true; return; } } else if (!givenString.Contains("=")) { NonEquation solve = new NonEquation(givenString); solve.History = History; solve.Solve(); if (solve.isProcessed()) { Solution = solve.getSolution(); } else { Processed = false; } } else { Equation sol = new Equation(givenString); sol.theHistory = History; sol.Solve(); if (sol.isProcessed()) { Solution = sol.getSolution(); } else { Processed = false; } } }
void Process(string command, string[] args, ref MathParserExpression theSolution) { MathParser.Solver sol = makeMathParserSolverObject(); sol.SaveHistory(); if (command == "eval") { foreach (var x in args) { sol.setMathExpression(x); sol.Solve(); } theSolution = sol.getSolution(); return; } throw new MathParserException("Keyword does not exist"); }
public void Solve() { if (Expressions.Count == 1) { if (Expressions[0].Contains("-")) { MathParserExpression hold = theData[Expressions[0].Trim("-".ToCharArray())]; if (hold.Type.Contains("Number")) { Number ans = new Number((double)-1) * hold.Data; solution = new MathParserExpression(ans); } else if (hold.Type.Contains("Matrix")) { Matrix ans = -1 * hold.Data; solution = new MathParserExpression(ans); } } else { solution = theData[Expressions[0].Trim("-".ToCharArray())]; } } else { if (Division()) { if (Multiplication()) { if (Addition()) { solution = new MathParserExpression(theData [Expressions [0].Trim("-".ToCharArray())]); } } } } }
public void Solve() { string testString = givenExpression.Replace("=", ""); int testLenght = testString.Length; if (givenExpression.Length - testLenght <= 1) // Declaration statement. a = 2+2 { string Name, Exp; { string[] parts = givenExpression.Split('='); Name = parts [0].Trim(); Exp = parts [1].Trim(); } NonEquation sol = new NonEquation(Exp); sol.History = theHistory; sol.Solve(); BasicOperators = sol.theBasicOperatorsString; string numbers = "0123456789."; if (sol.ConstantName.Contains(Name) || sol.theKeyWordsList.Contains(Name) || Checker.ifContainOperation(Name, BasicOperators) || numbers.Contains(new string(Name[0], 1)) || string.IsNullOrEmpty(Name) || string.IsNullOrWhiteSpace(Name)) { Processed = false; throw new MathParserException($"Invalid declaration. '{Name}'."); } if (sol.isProcessed()) { solution = new MathParserExpression(sol.getSolution()); solution.Tag = Name.Trim(); //if (string.IsNullOrEmpty (solution.Data.Tag) || string.IsNullOrWhiteSpace (solution.Data.Tag)) { solution.setEntireTag(Name.Trim()); } else { Processed = false; throw new MathParserException("Unsolveable Input."); } } } // end solve().
} // end Operator syncor functions. public void Solve() // the method deals with the prelimanry making of the Expression Understading. { OperatorSync(); // Syncs the operators to be considered. SyncKeyWord(); ConstantSync(); if (givenExpression.Contains("(") || givenExpression.Contains(")")) { Only_Number_And_Operator_List_Maker(); if (!(ExpressionElements[ExpressionElements.Count - 1] == ")") && theBasicOperatorsString.Contains(ExpressionElements[ExpressionElements.Count - 1])) { Processed = false; throw new MathParserException($"" + "Invalid operator entry. No basic operator ({theBasicOperatorList.ToString()}) can be at the end of the expression."); } the_Data_List_Maker(); OperatorHandler oh = new OperatorHandler(ref ExpressionElements); oh.BasicOperatorsString = theBasicOperatorsString; oh.Process(); if (!oh.isCorrectOperatorSequence()) { Processed = false; throw new MathParserException("Bad operator sequence."); } // Balancing brakkets. if (ExpressionElements.Contains("(") || ExpressionElements.Contains("-(") || ExpressionElements.Contains(")")) { int nsb = 0; int neb = 0; foreach (string s in ExpressionElements) { if (s.Contains("(")) { nsb++; } if (s.Contains(")")) { neb++; } } if (nsb > neb) { for (int c = 0; c < (nsb - neb); c++) { ExpressionElements.Add(")"); } } if (neb > nsb) { List <string> dumy = new List <string>(); for (int c = 0; c < (-nsb + neb); c++) { dumy.Add("("); } foreach (string x in ExpressionElements) { dumy.Add(x); } ExpressionElements = dumy; } } // end balancing brakkets MathParser.BraketSolver sol = new BraketSolver(ExpressionElements, theData); if (sol.isProcessed()) { Solution = sol.getSolution(); } else { Processed = false; throw new MathParserException("Error"); } } else if ((givenExpression.Contains("[") && !givenExpression.Contains("]")) || (!givenExpression.Contains("[") && givenExpression.Contains(("]")))) // if the expession string contans an invalid matrix formate. { throw (new MathParserException("Invalid Matrix Formate. Entered.")); } else // if the expression string contain only operators and numbers. { Only_Number_And_Operator_List_Maker(); if (theBasicOperatorsString.Contains(ExpressionElements[ExpressionElements.Count - 1])) { Processed = false; throw new MathParserException($"Invalid operator entry. No basic operator ({theBasicOperatorList.ToString()}) can be at the end of the expression."); } else { the_Data_List_Maker(); OperatorHandler oh = new OperatorHandler(ref ExpressionElements); oh.BasicOperatorsString = theBasicOperatorsString; oh.Process(); if (!oh.isCorrectOperatorSequence()) { Processed = false; throw new MathParserException("Bad operator sequence."); } else { DMASSolver DS = new DMASSolver(ExpressionElements, theData); DS.Solve(); if (DS.isProcessed()) { Solution = new MathParserExpression(DS.getSolution()); } else { Processed = false; } } } // end } // end } // end solve
public MathParserExpression getSolution() { MathParserExpression result = new MathParserExpression(theMatrix); return(result); }
// End matrix syntax checker. public void Parse() // Parse method. { bool proceed = MatrixSyntaxChecker(); if (!proceed) { Processed = false; } else { string matExpression = givenExpression.TrimStart(FlagStart.ToCharArray()); matExpression = matExpression.TrimEnd(FlagEnd.ToCharArray()); matExpression = matExpression.Trim(); List <string> theStringRows = new List <string>(matExpression.Split(FlagRowSeperation.ToCharArray())); theStringRows.Remove(""); int rows = theStringRows.Count; int cols = 0; List <List <string> > theMatrixElements = new List <List <string> >(); foreach (string row in theStringRows) { List <string> dumyList = new List <string>(row.Trim().Split(FlagElementSeperation.ToCharArray())); dumyList.Remove(""); if (dumyList.Count > cols) { cols = dumyList.Count; } theMatrixElements.Add(dumyList); } theMatrix = new Matrix(rows, cols, ""); int irow = 0, icol = 0; foreach (List <string> row in theMatrixElements) { icol = 0; foreach (string element in row) { if (OnParse != null) { dynamic mElement = OnParse(element.Trim(), ref Processed); theMatrix[irow, icol] = mElement; } else if (staticOnParse != null) { dynamic mElement = staticOnParse(element.Trim(), ref Processed); theMatrix[irow, icol] = mElement; } else { NonEquation sol = new NonEquation(element.Trim()); sol.History = Checker.History; sol.Solve(); Number a; if (sol.isProcessed()) { MathParserExpression ans = sol.getSolution(); a = ans.Data; } else { Processed = false; throw new MathParserException("Invalid Entry."); } theMatrix[irow, icol] = (double)a.Data; Processed = true; } if (!Processed) { Processed = false; break; } icol++; } if (!Processed) { Processed = false; break; } irow++; } if (!Processed) { theMatrix = new Matrix(1, 1); } } }
bool Addition() { bool done = true; if (Expressions.Contains("+")) { for (int c = Expressions.Count - 1; c >= 0; c--) { if (Expressions[c] == "+") { MathParserExpression rhs = theData[Expressions[c - 1].Trim("-".ToCharArray())]; MathParserExpression lhs = theData[Expressions[c + 1].Trim("-".ToCharArray())]; if (rhs.Type == lhs.Type) { if (rhs.Type.Contains("Number")) { if (Expressions [c - 1].Contains("-")) { rhs = new MathParserExpression(rhs.Data * new Number(-1)); Expressions [c - 1].Replace("-", ""); } if (Expressions [c + 1].Contains("-")) { lhs = new MathParserExpression(lhs.Data * new Number(-1)); Expressions [c + 1].Replace("-", ""); } Number ans = rhs.Data + lhs.Data; string name = autoNamer(); Expressions [c - 1] = name; theData.Add(name, new MathParserExpression(ans)); } else if (rhs.Type.Contains("Matrix")) { if (Expressions [c - 1].Contains("-")) { rhs = new MathParserExpression((Matrix)rhs.Data * -1); Expressions [c - 1].Replace("-", ""); } if (Expressions [c + 1].Contains("-")) { lhs = new MathParserExpression((Matrix)lhs.Data * -1); Expressions [c + 1].Replace("-", ""); } Matrix mrhs = rhs.Data; Matrix mlhs = lhs.Data; if (mrhs.Rows == mlhs.Rows && mrhs.Columns == mlhs.Columns) { Matrix ans = rhs.Data + lhs.Data; string name = autoNamer(); Expressions [c - 1] = name; theData.Add(name, new MathParserExpression(ans)); } else { Processed = false; done = false; throw new Exception("Invalid row and column matrix addition."); } } } else { Processed = false; done = false; throw new Exception("Invalid data types addition."); } Expressions.RemoveRange(c, 2); c = Expressions.Count - 1; } // end if } // end for loop. } return(done); } // The function deals with the addition operations.
} // The function deals with the addition operations. bool Multiplication() { bool done = true; string multiply_cross = new string (((char)215), 1); if (Expressions.Contains("*") || Expressions.Contains(multiply_cross)) { for (int c = Expressions.Count - 1; c >= 0; c--) { if (Expressions[c] == "*" || Expressions[c] == multiply_cross) { MathParserExpression rhs = theData[Expressions[c - 1]]; MathParserExpression lhs = theData[Expressions[c + 1]]; if (rhs.Type == lhs.Type) { if (rhs.Type.Contains("Number")) { if (Expressions [c - 1].Contains("-")) { rhs = new MathParserExpression(rhs.Data * new Number(-1)); Expressions [c - 1].Replace("-", ""); } if (Expressions [c + 1].Contains("-")) { lhs = new MathParserExpression(lhs.Data * new Number(-1)); Expressions [c + 1].Replace("-", ""); } Number ans = rhs.Data * lhs.Data; string name = autoNamer(); Expressions [c - 1] = name; theData.Add(name, new MathParserExpression(ans)); } else if (rhs.Type.Contains("Matrix")) { if (Expressions [c - 1].Contains("-")) { rhs = new MathParserExpression(rhs.Data * -1); Expressions [c - 1].Replace("-", ""); } if (Expressions [c + 1].Contains("-")) { lhs = new MathParserExpression(lhs.Data * -1); Expressions [c + 1].Replace("-", ""); } Matrix mrhs = rhs.Data; Matrix mlhs = lhs.Data; if (mrhs.Rows == mlhs.Columns) { Matrix ans = rhs.Data * lhs.Data; string name = autoNamer(); Expressions [c - 1] = name; theData.Add(name, new MathParserExpression(ans)); } else { Processed = false; done = false; throw new Exception("Invalid matrix multiplication."); } } } else if (lhs.Type.Contains("Number") && rhs.Type.Contains("Matrix")) { Number l = lhs.Data; Matrix r = rhs.Data; Matrix ans = (double)l.Data * r; string name = autoNamer(); Expressions [c - 1] = name; theData.Add(name, new MathParserExpression(ans)); } else if (rhs.Type.Contains("Number") && lhs.Type.Contains("Matrix")) { Number r = rhs.Data; Matrix l = lhs.Data; Matrix ans = l * (double)r.Data; string name = autoNamer(); Expressions [c - 1] = name; theData.Add(name, new MathParserExpression(ans)); } else { Processed = false; done = false; throw new Exception("Not a valid multiplication."); } Expressions.RemoveRange(c, 2); c = Expressions.Count - 1; } // end if } // end for loop. } return(done); } // The function deals with the multiplication operations.
bool Order() { bool done = true; if (Expressions.Contains("^")) { for (int c = Expressions.Count - 1; c >= 0; c--) { string Element = Expressions [c]; if (Element == "^") { MathParserExpression lhs = theData [Expressions [c - 1].Trim("-".ToCharArray())]; MathParserExpression rhs = theData [Expressions [c + 1].Trim("-".ToCharArray())]; if (lhs.Type.Contains("Number") && rhs.Type.Contains("Number")) { if (lhs.Data.Data == (double)Math.E) { if (Expressions [c + 1].Contains("-")) { rhs = new MathParserExpression(rhs.Data * new Number(-1)); Expressions [c + 1].Replace("-", ""); } Number ans = new Number(Math.Exp((double)((Number)rhs.Data).Data)); if (Expressions [c - 1].Contains("-")) { ans = ans * new Number(-1); } string name = autoNamer(); Expressions [c - 1] = name; theData.Add(name, new MathParserExpression(ans)); } else { if (Expressions [c + 1].Contains("-")) { rhs = new MathParserExpression(rhs.Data * new Number(-1)); Expressions [c + 1].Replace("-", ""); } Number ans = new Number(Math.Pow((double)((Number)lhs.Data).Data, (double)((Number)rhs.Data).Data)); if (Expressions [c - 1].Contains("-")) { ans = ans * new Number(-1); } string name = autoNamer(); Expressions [c - 1] = name; theData.Add(name, new MathParserExpression(ans)); } } else { if (onOrder != null && onOrder(lhs, rhs, theData, Expressions, autoNamer(), c)) { } else { Processed = false; done = false; throw new MathParserException("Invalid base and exponent."); } } Expressions.RemoveRange(c, 2); c = Expressions.Count; } } } return(done); } // The function deals with the Order/power operation.
bool Division() { bool done = true; if (Expressions.Contains("÷") || Expressions.Contains("/")) { for (int c = 0; c < Expressions.Count; c++) { string Element = Expressions [c]; if (Element == "/" || Element == "÷") { MathParserExpression lhs = theData [Expressions [c - 1].Trim("-".ToCharArray())]; MathParserExpression rhs = theData [Expressions [c + 1].Trim("-".ToCharArray())]; if (lhs.Type.Contains("Number") && rhs.Type.Contains("Number")) { if (Expressions [c + 1].Contains("-")) { rhs = new MathParserExpression(rhs.Data * new Number(-1)); Expressions [c + 1].Replace("-", ""); } if (Expressions [c - 1].Contains("-")) { lhs = new MathParserExpression(lhs.Data * new Number(-1)); Expressions [c - 1].Replace("-", ""); } Number l = lhs.Data; Number r = rhs.Data; Number ans = new Number((double)l.Data / (double)r.Data); string name = autoNamer(); theData.Add(name, new MathParserExpression(ans)); Expressions [c - 1] = name; } else if (lhs.Type.Contains("Matrix") && rhs.Type.Contains("Number")) { if (Expressions [c + 1].Contains("-")) { rhs = new MathParserExpression(rhs.Data * new Number(-1)); Expressions [c + 1].Replace("-", ""); } if (Expressions [c - 1].Contains("-")) { lhs = new MathParserExpression(lhs.Data * -1); Expressions [c - 1].Replace("-", ""); } Matrix l = lhs.Data; Number r = rhs.Data; Matrix ans = l / ((double)r.Data); string name = autoNamer(); theData.Add(name, new MathParserExpression(ans)); Expressions [c - 1] = name; } else { if (onDivision != null && onDivision(lhs, rhs, theData, Expressions, autoNamer(), c)) { } else { Processed = false; done = false; throw new MathParserException("Invalid Division."); } } Expressions.RemoveRange(c, 2); c = 0; } } } return(done); } // The function deals with the division operation.
void Solve() { if (!(ExpressionElements.Contains("(") || ExpressionElements.Contains(")"))) { DMASSolver sol = new DMASSolver(ExpressionElements, theData); sol.Solve(); if (sol.isProcessed()) { solution = sol.getSolution(); } else { Processed = false; throw new MathParserException("The expression could not be solved"); } } else { bool intake = false; int bstart = 0; int belements = 1; List <string> theBraketList = new List <string>(); for (int c = 0; c < ExpressionElements.Count; c++) { string Element = ExpressionElements [c]; if (!ExpressionElements.Contains("(") && !ExpressionElements.Contains("-(")) { break; } if (Element == ")") { intake = false; DMASSolver sol = new DMASSolver(theBraketList, theData); sol.Solve(); if (sol.isProcessed()) { MathParserExpression ANS = sol.getSolution(); if (ExpressionElements [bstart].Contains("-")) { if (ANS.Type.Contains("Number")) { Number g = ANS.Data; g = g * new Number((double)-1); ANS = new MathParserExpression(g); } else if (ANS.Type.Contains("Matrix")) { Matrix g = ANS.Data; g = g * -1; ANS = new MathParserExpression(g); } } string name = autoNamer(); theData.Add(name, ANS); ExpressionElements [bstart] = name; ExpressionElements.RemoveRange(bstart + 1, belements + 1); c = -1; } else { Processed = false; throw new MathParserException("Error"); } belements = 0; } if (intake) { theBraketList.Add(Element); belements++; } if (Element.Contains("(")) { theBraketList = new List <string> (); intake = true; bstart = c; belements = 0; } } BraketSolver bsol = new BraketSolver(ExpressionElements, theData); if (bsol.isProcessed()) { solution = bsol.getSolution(); } else { Processed = false; throw new MathParserException("Error"); } } }