private void SolveQuadratic(double a, double b) { var sqrD = ShitMath.Sqrt(Discriminant); var rootVal = (-b + sqrD) / (2 * a); var rootStr = _shouldNotReduceFraction ? "" + (-b + sqrD + "/" + 2 * a) : "" + rootVal; if (Regex.Match(rootStr, @"\-.+\/\-.+").Success) { rootStr = rootStr.Replace("-", ""); } Steps.Add( $"[Calculating first root]\tx0 = (-b + sqrt(D)) / 2a = ({-b} + {sqrD}) / {2 * a} = {rootStr}"); Roots.Add(rootStr); if (Discriminant > 0) { rootVal = (-b - sqrD) / (2 * a); rootStr = _shouldNotReduceFraction ? "" + (-b - sqrD + "/" + 2 * a) : "" + rootVal; if (Regex.Match(rootStr, @"\-.+\/\-.+").Success) { rootStr = rootStr.Replace("-", ""); } Steps.Add( $"[Calculating second root]\tx0 = (-b - sqrt(D)) / 2a = ({-b} - {sqrD}) / {2 * a} = {rootStr}"); Roots.Add(rootStr); } }
private static string SumExes(string lX, string rX) { double lVal, rVal; var power = GetExPower(ref lX); if (power != GetExPower(ref rX)) { return(""); } if (lX[0] != '-' && lX[0] != '+') { lX = lX.Insert(0, "+"); } if (lX.IndexOf('x') <= 1) { lVal = 1.0; lVal = lX[0] == '-' ? lVal * -1.0 : 1.0; } else { if (!double.TryParse(lX.Substring(0, lX.IndexOf('x')), out lVal)) { throw new Exception($"can't parse value of [{lX}] as number."); } } if (rX.IndexOf('x') <= 1) { rVal = 1.0; rVal = rX[0] == '-' ? rVal * -1.0 : 1.0; } else { if (!double.TryParse(rX.Substring(0, rX.IndexOf('x')), out rVal)) { throw new Exception($"can't parse value of [{rX}] as number."); } } var sum = lVal + rVal; if (sum == 0.0) { return("+0"); } if (ShitMath.Abs(sum) > int.MaxValue) { throw new Exception($"unable to work with numbers larger than INT_MAX: {lVal} + {rVal} = {sum}."); } var result = sum > 0.0 ? "+" : "-"; result += ShitMath.Abs(sum - 1.0) < 0.000000001 ? "" : "" + ShitMath.Abs(sum); return(result + 'x' + (power == 1 ? "" : "^" + power)); }
private static string CalculatePowers(ref string trimmedEqu) { var str = trimmedEqu.Substring(0); var match = Regex.Match(str, @"[\+\-]?\d+(\.\d+)?\^[\+\-]?\d+"); if (!match.Success) { return(trimmedEqu); } do { var splitPow = match.Value.Split('^'); var sign = splitPow[0][0] == '-' ? "-" : splitPow[0][0] == '+' ? "+" : ""; string result; splitPow[0] = Regex.Replace(splitPow[0], @"^[\+\-]", ""); if (!int.TryParse(splitPow[1], out var rPiece)) { throw new Exception($"can't parse power [{splitPow[1]}] as integer in [{match.Value}]."); } if (double.TryParse(splitPow[0], out var ldPiece)) { var value = ShitMath.Pow(ldPiece, rPiece); result = "" + value; if (value != 0 && (!double.IsNormal(value) || result.Contains('e', StringComparison.OrdinalIgnoreCase) || ShitMath.Abs(value) > int.MaxValue)) { throw new Exception($"value {ldPiece} powered by {rPiece} is too big or too small: {value}"); } } else { throw new Exception($"can't parse value [{splitPow[0]}] as number in [{match.Value}]."); } str = str.Remove(match.Index, match.Length).Insert(match.Index, sign + result); } while ((match = Regex.Match(str, @"[\+\-]?\d+(\.\d+)?\^[\+\-]?\d+")).Success); return(str); }
private void SolveComplex(double a, double b) { var sqrD = ShitMath.Sqrt(-Discriminant); var real = -b / (2 * a); var imaginary = ShitMath.Abs(sqrD / (2 * a)); var rStr = _shouldNotReduceFraction ? -b + "/" + 2 * a : "" + real; var iStr = _shouldNotReduceFraction ? sqrD + "/" + 2 * a : "" + imaginary; Steps.Add($"[Real part of roots]\t\tr = -b / 2a = {-b} / {2 * a} = {rStr}"); Steps.Add($"[Imaginary part of roots]\ti = sqrt(D) / 2a = {sqrD} / {2 * a} = {iStr}"); Steps.Add($"[Calculating first root]\tx0 = r - i = {rStr} - {iStr}i"); Steps.Add($"[Calculating second root]\tx1 = r + i = {rStr} + {iStr}i"); if (Regex.Match(rStr, @"\-.+\/\-.+").Success) { rStr = rStr.Replace("-", ""); } if (Regex.Match(iStr, @"\-.+\/\-.+").Success) { iStr = iStr.Replace("-", ""); } Roots.Add(rStr + " - " + iStr + "i"); Roots.Add(rStr + " + " + iStr + "i"); }
private static string CalculateSums(ref string trimmedEqu) { var result = ""; var parts = SplitEquationByParts(ref trimmedEqu); var exlessSum = 0.0; for (var curI = 0; curI < parts.Count - 1; curI++) { if (!parts[curI].Contains('x')) { continue; } var currentEx = parts[curI]; var currentPower = GetExPower(ref currentEx); for (var nextI = curI + 1; nextI < parts.Count; nextI++) { if (!parts[nextI].Contains('x')) { continue; } var nextEx = parts[nextI]; if (currentPower != GetExPower(ref nextEx)) { continue; } parts[curI] = SumExes(parts[curI], nextEx); parts.RemoveAt(nextI); if (parts[curI] == "+0") { break; } nextI--; } } for (var i = parts.Count - 1; i >= 0; i--) { if (parts[i].Contains('x')) { continue; } if (!double.TryParse(parts[i], out var part)) { throw new Exception($"unable to parse {parts[i]} as number."); } exlessSum += part; if (ShitMath.Abs(exlessSum) > int.MaxValue) { throw new Exception($"unable to work with numbers larger than INT_MAX: {parts[i]}."); } parts.RemoveAt(i); } var newParts = parts.ToArray(); Array.Sort(newParts, (s, s1) => GetExPower(ref s1) - GetExPower(ref s)); foreach (var part in newParts) { result += part; } if (result.Length > 0) { result = result[0] == '+' ? result.Remove(0, 1) : result; if (exlessSum != 0) { result += (exlessSum > 0 ? "+" : "") + exlessSum; } } else { result += exlessSum; } return(result + "=0"); }