private static string[] CalculateRange(string rawEquation, string variables) { int success = Consolidate.ConvertAndCheckInputs(rawEquation, variables, Solver.GetValidOperators(), Solver.GetValidTerminators(), Input.GetLineDelimiter(), Input.GetFieldDelimiter()); string[] results = null; if (success == 0) { EquationStruct eq = Consolidate.GetEquationStruct(); IntervalStruct[] intervals = Consolidate.GetIntervalStructList(); IntervalStruct range = Solver.FindRange(eq, intervals); if (range != null) { results = new string[] { Output.PrintInterval(range, false), Output.PrintEquationTree(eq) }; frm_Main.UpdateLog("Range calculated successfully." + System.Environment.NewLine); hasRun = true; } } else { successCode = success; } return(results); }
/* CONVERSION FUNCTION */ private static IntervalStruct MakeInterval(string varName, string min, string max) { IntervalStruct iv = null; bool proceed = true; double cMin = 0; double cMax = 0; // Check that a non-empty variable name was provided if (CheckVarName(varName) && CheckBoundExistence(min, max)) { // Try to convert the min to a string try { if (min == "") { frm_Main.UpdateLog("Warning: No minimum interval bound given. Setting it to the same value as the maximum bound." + System.Environment.NewLine); min = max; } cMin = System.Convert.ToDouble(min); } catch (System.FormatException) { frm_Main.UpdateLog("Error: The string provided for the minimum bound cannot be converted to a real number." + System.Environment.NewLine); proceed = false; } // Try to convert the max to a string try { if (max == "") { frm_Main.UpdateLog("Warning: No maximum interval bound given. Setting it to the same value as the minimum bound." + System.Environment.NewLine); max = min; } cMax = System.Convert.ToDouble(max); } catch (System.FormatException) { frm_Main.UpdateLog("Error: The string provided for the maximum bound cannot be converted to a real number." + System.Environment.NewLine); proceed = false; } // If you have reached this point, all the inputs are available to create an IntervalStruct object if (proceed) { iv = new IntervalStruct(varName, cMin, cMax, true, true); } } return(iv); }
private static IntervalStruct IntervalAsExponent(double b, IntervalStruct x) { IntervalStruct exp = null; if (b > 1) { exp = new IntervalStruct("", System.Math.Pow(b, x.GetMinBound()), System.Math.Pow(b, x.GetMaxBound()), true, true); } else { frm_Main.UpdateLog("Error: An unsupported operation was encountered while solving for the range of the equation (Exponent base <= 1)." + System.Environment.NewLine); } return(exp); }
/* CALCULATION */ public static IntervalStruct FindRange(EquationStruct eqRoot, IntervalStruct[] intervals) { string[] varNames = GetVariableNamesFromIntervals(intervals); IntervalStruct range = null; if (eqRoot != null) { range = CalculateRange(eqRoot, intervals, varNames); } else { throw new System.ArgumentException("Error: No information was provided for the equation."); } return(range); }
/* CALCULATION -- MULTIPLICATION */ private static IntervalStruct IntervalMultiplication(IntervalStruct x, IntervalStruct y) { // Comparing a1 * a2 and a1 * b2 double min = System.Math.Min(x.GetMinBound() * y.GetMinBound(), x.GetMinBound() * y.GetMaxBound()); double max = System.Math.Max(x.GetMinBound() * y.GetMinBound(), x.GetMinBound() * y.GetMaxBound()); // Comparing the previous results to b1 * a2 min = System.Math.Min(min, x.GetMaxBound() * y.GetMinBound()); max = System.Math.Max(max, x.GetMaxBound() * y.GetMinBound()); // Comparing the previous results to b1 * b2 min = System.Math.Min(min, x.GetMaxBound() * y.GetMaxBound()); max = System.Math.Max(max, x.GetMaxBound() * y.GetMaxBound()); return(new IntervalStruct("", min, max, true, true)); }
private static IntervalStruct[] RemoveInterval(IntervalStruct[] nameList, int index) { IntervalStruct[] newIvList = new IntervalStruct[nameList.Length - 1]; int i = 0; int j = 0; while (i < nameList.Length) { if (i != index) { newIvList[j] = nameList[i]; j++; } i++; } return(newIvList); }
/* CALCULATION -- EXPONENTS */ private static IntervalStruct IntervalExponents(IntervalStruct x, IntervalStruct y) { IntervalStruct exp = null; if (x.GetMinBound() == x.GetMaxBound()) { exp = IntervalAsExponent(x.GetMinBound(), y); } else if (y.GetMinBound() == y.GetMaxBound()) { exp = IntervalAsBase(x, y.GetMinBound()); } else { frm_Main.UpdateLog("Error: An unsupported operation was encountered while solving for the range of the equation (Exponents)." + System.Environment.NewLine); } return(exp); }
/* CALCULATION -- DIVISION */ private static IntervalStruct IntervalDivision(IntervalStruct x, IntervalStruct y) { IntervalStruct divInterval = null; // 0 < a2 <= b2 if (y.GetMinBound() > 0) { divInterval = IntervalDivisionPositiveDivisor(x, y); } // a2 <= b2 < 0 else if (y.GetMaxBound() < 0) { divInterval = IntervalDivisionNegativeDivisor(x, y); } // a2 = 0 v b2 = 0 else { frm_Main.UpdateLog("Error: An unsupported operation was encountered while solving for the range of the equation (Mixed interval division)." + System.Environment.NewLine); } return(divInterval); }
private static IntervalStruct IntervalDivisionNegativeDivisor(IntervalStruct x, IntervalStruct y) { double min = 0; double max = 0; // 0 < a1 <= b1 if (x.GetMinBound() > 0) { min = x.GetMaxBound() / y.GetMaxBound(); max = x.GetMinBound() / y.GetMinBound(); } // a1 = 0, a1 <= b1 else if (x.GetMinBound() == 0) { min = x.GetMaxBound() / y.GetMaxBound(); max = 0; } // a1 < 0 < b1 else if (x.GetMaxBound() > 0) { min = x.GetMaxBound() / y.GetMaxBound(); max = x.GetMinBound() / y.GetMaxBound(); } // b1 = 0, a1 <= b1 else if (x.GetMaxBound() == 0) { min = 0; max = x.GetMinBound() / y.GetMaxBound(); } //a1 <= b1 < 0 else if (x.GetMaxBound() < 0) { min = x.GetMaxBound() / y.GetMinBound(); max = x.GetMinBound() / y.GetMaxBound(); } return(new IntervalStruct("", min, max, true, true)); }
private static IntervalStruct IntervalAsBase(IntervalStruct x, double n) { IntervalStruct exp = null; double roundedN = System.Math.Round(n); if (n >= 0) { if (n != roundedN) { frm_Main.UpdateLog("Warning: The value provided for the exponent" + System.Convert.ToString(n) + "is not a natural number. It has been rounded to " + System.Convert.ToString(roundedN) + System.Environment.NewLine); } if (roundedN % 2 != 0) { exp = new IntervalStruct("", System.Math.Pow(x.GetMinBound(), roundedN), System.Math.Pow(x.GetMaxBound(), roundedN), true, true); } else if (x.GetMinBound() >= 0) { exp = new IntervalStruct("", System.Math.Pow(x.GetMinBound(), roundedN), System.Math.Pow(x.GetMaxBound(), roundedN), true, true); } else if (x.GetMaxBound() < 0) { exp = new IntervalStruct("", System.Math.Pow(x.GetMaxBound(), roundedN), System.Math.Pow(x.GetMinBound(), roundedN), true, true); } else { exp = new IntervalStruct("", 0, System.Math.Max(System.Math.Pow(x.GetMinBound(), roundedN), System.Math.Pow(x.GetMaxBound(), roundedN)), true, true); } } else { frm_Main.UpdateLog("Error: An unsupported operation was encountered while solving for the range of the equation (Exponent < 0)." + System.Environment.NewLine); } return(exp); }
private static IntervalStruct CalculateRange(EquationStruct eqTree, IntervalStruct[] intervals, string[] intervalNames) { IntervalStruct range = null; IntervalStruct leftResult = null; IntervalStruct rightResult = null; double constant = 0; int varIndex = -1; // This is either a variable or a constant if (eqTree.GetLeftOperand() == null && eqTree.GetRightOperand() == null) { try { constant = System.Convert.ToDouble(eqTree.GetVariableName()); range = new IntervalStruct("", constant, constant, true, true); } catch (System.FormatException) { varIndex = System.Array.IndexOf(intervalNames, eqTree.GetVariableName()); if (varIndex > -1) { range = intervals[varIndex]; } else { frm_Main.UpdateLog("Error: Could not find an associated interval for variable " + eqTree.GetVariableName() + System.Environment.NewLine); } } } // This is an equation, so calculate the result else if (eqTree.GetOperator() == "+") { leftResult = CalculateRange(eqTree.GetLeftOperand(), intervals, intervalNames); rightResult = CalculateRange(eqTree.GetRightOperand(), intervals, intervalNames); if (leftResult != null && rightResult != null) { range = IntervalAddition(leftResult, rightResult); } } else if (eqTree.GetOperator() == "-") { leftResult = CalculateRange(eqTree.GetLeftOperand(), intervals, intervalNames); rightResult = CalculateRange(eqTree.GetRightOperand(), intervals, intervalNames); if (leftResult != null && rightResult != null) { range = IntervalSubtraction(leftResult, rightResult); } } else if (eqTree.GetOperator() == "*") { leftResult = CalculateRange(eqTree.GetLeftOperand(), intervals, intervalNames); rightResult = CalculateRange(eqTree.GetRightOperand(), intervals, intervalNames); if (leftResult != null && rightResult != null) { range = IntervalMultiplication(leftResult, rightResult); } } else if (eqTree.GetOperator() == "/") { leftResult = CalculateRange(eqTree.GetLeftOperand(), intervals, intervalNames); rightResult = CalculateRange(eqTree.GetRightOperand(), intervals, intervalNames); if (leftResult != null && rightResult != null) { range = IntervalDivision(leftResult, rightResult); } } else if (eqTree.GetOperator() == "^") { leftResult = CalculateRange(eqTree.GetLeftOperand(), intervals, intervalNames); rightResult = CalculateRange(eqTree.GetRightOperand(), intervals, intervalNames); if (leftResult != null && rightResult != null) { range = IntervalExponents(leftResult, rightResult); } } else if (eqTree.GetOperator() == "()") { range = CalculateRange(eqTree.GetLeftOperand(), intervals, intervalNames); } else { frm_Main.UpdateLog("Error: An unsupported operation was encountered while solving for the range of the equation (Unknown operator)." + System.Environment.NewLine); } return(range); }
/* CALCULATION -- SUBTRACTION */ private static IntervalStruct IntervalSubtraction(IntervalStruct x, IntervalStruct y) { return(new IntervalStruct("", x.GetMinBound() - y.GetMinBound(), x.GetMaxBound() - y.GetMaxBound(), true, true)); }