public static double Tan(double x) { if ((2.0 * x / pi) % 2 == 1) { throw new ArgumentException("Can't compute!"); } else if (x % pi == 0) { return(0.0); } else if (x > 0) { return(Simple.Sqrt(Simple.Pow(1.0 / Cos(x), 2) - 1)); } return(0 - Simple.Sqrt(Simple.Pow(1.0 / Cos(x), 2) - 1)); }
public double[] Quadratic() { double[] pieces = new double[9]; pieces = GetCoefficients(left, pieces, 1); pieces = GetCoefficients(right, pieces, -1); double[] answer = { 0.0, 0.0 }; double d = Simple.Pow(pieces[pieces.Length - 2], 2) - 4.0 * pieces[pieces.Length - 3] * pieces[pieces.Length - 1]; if (d < 0) { throw new ArgumentException("Imaginary number!"); } else if (d == 0) { d = 0.0; } else { d = Simple.Sqrt(d); } answer[0] = (0 - pieces[pieces.Length - 2] + d) / (2.0 * pieces[pieces.Length - 3]); answer[1] = (0 - pieces[pieces.Length - 2] - d) / (2.0 * pieces[pieces.Length - 3]); return(answer); }
public double[] PolySolve() { double[] pieces = new double[15]; if (left.Length == 1 && right[0].Equals("0")) { int count = 0; for (int i = 0; i < left[0].Length; i++) { if (left[0].ElementAt(i) == '(') { count++; } } double[] aResults = new double[count]; string full = left[0]; int j = 0; while (j <= count) { string next = full.Substring(1, full.IndexOf(")") - 1); int max = 0; if (next.IndexOf("x") != -1) { max = 1; } for (int i = 0; i < next.Length; i++) { int min = next.Substring(i).IndexOf("+"); if (next.Substring(i).IndexOf("−") > -1 && next.Substring(i).IndexOf("−") < min) { min = next.Substring(i).IndexOf("−"); } else if (min == -1) { min = next.Substring(i).IndexOf("−"); } if (next[i] == '^' && max < Int32.Parse("" + next.Substring(i + 1, min - i))) { max = Int32.Parse("" + next.Substring(i + 1, min - i)); } } if (j == 0) { aResults = new double[max]; } else { double[] pResults = new double[aResults.Length + max]; for (int i = 0; i < aResults.Length; i++) { pResults[i] = aResults[i]; } aResults = pResults; } if (max == 1) { pieces = new double[15]; pieces = GetCoefficients(GetParts(next), pieces, 1); if (pieces[pieces.Length - 2] == 0.0) { double[] newResults = new double[count - 1]; for (int i = 0; i < aResults.Length - 1; i++) { if (i < j) { newResults[i] = aResults[i]; } else if (i >= j) { newResults[i] = aResults[i + 1]; } } aResults = newResults; } else { aResults[j] = (0 - pieces[pieces.Length - 1]) / pieces[pieces.Length - 2]; } } else if (max == 2) { pieces = new double[15]; pieces = GetCoefficients(GetParts(next), pieces, 1); double d = Simple.Sqrt(Simple.Pow(pieces[pieces.Length - 2], 2) - 4.0 * pieces[pieces.Length - 1] * pieces[pieces.Length - 3]); aResults[j++] = (0 - pieces[pieces.Length - 2] + d) / (2.0 * pieces[pieces.Length - 3]); aResults[j] = (0 - pieces[pieces.Length - 2] - d) / (2.0 * pieces[pieces.Length - 3]); } full = full.Substring(full.IndexOf(")^") + 3); j++; } return(aResults); } double[] results = { 0.0 }; return(results); }
private double[] TrigAnswers(MyFunction f, double[] pieces, int index, int count) { if (pieces[pieces.Length - 4] != 0 && count == 1 && pieces[pieces.Length - 5] % 2 == 0) { double a = Simple.Pow((0 - pieces[pieces.Length - 1]) / pieces[index], 1.0 / pieces[pieces.Length - 5]); double c1 = pieces[pieces.Length - 2] - f(a); double c2 = pieces[pieces.Length - 2] - f(-a); double d1 = -1.0; if (Simple.Pow(pieces[pieces.Length - 3], 2) - 4.0 * pieces[pieces.Length - 4] * c1 >= 0) { d1 = Simple.Sqrt(Simple.Pow(pieces[pieces.Length - 3], 2) - 4.0 * pieces[pieces.Length - 4] * c1); } double d2 = -1.0; if (Simple.Pow(pieces[pieces.Length - 3], 2) - 4.0 * pieces[pieces.Length - 4] * c2 >= 0) { d2 = Simple.Sqrt(Simple.Pow(pieces[pieces.Length - 3], 2) - 4.0 * pieces[pieces.Length - 4] * c2); } if (d1 >= 0 && d2 >= 0) { double[] results = { (0 - pieces[pieces.Length - 3] + d1) / (2.0 * pieces[pieces.Length - 4]), (0 - pieces[pieces.Length - 3] - d1) / (2.0 * pieces[pieces.Length - 4]), (0 - pieces[pieces.Length - 3] + d2) / (2.0 * pieces[pieces.Length - 4]), (0 - pieces[pieces.Length - 3] - d2) / (2.0 * pieces[pieces.Length - 4]) }; return(results); } else if (d1 >= 0) { double[] results = { (0 - pieces[pieces.Length - 3] + d1) / (2.0 * pieces[pieces.Length - 4]), (0 - pieces[pieces.Length - 3] - d1) / (2.0 * pieces[pieces.Length - 4]) }; return(results); } else if (d2 >= 0) { double[] results = { (0 - pieces[pieces.Length - 3] + d2) / (2.0 * pieces[pieces.Length - 4]), (0 - pieces[pieces.Length - 3] - d2) / (2.0 * pieces[pieces.Length - 4]) }; return(results); } throw new ArgumentException("Imaginary number!"); } else if (pieces[pieces.Length - 4] != 0) { if (pieces[pieces.Length - 5] == 0) { pieces[pieces.Length - 5] = 1.0; } double c = pieces[pieces.Length - 2] - f(Simple.Pow((0 - pieces[pieces.Length - 1]) / pieces[index], 1.0 / pieces[pieces.Length - 5])); double d = Simple.Sqrt(Simple.Pow(pieces[pieces.Length - 3], 2) - 4.0 * pieces[pieces.Length - 4] * c); double[] results = { (0 - pieces[pieces.Length - 3] + d) / (2.0 * pieces[pieces.Length - 4]), (0 - pieces[pieces.Length - 3] - d) / (2.0 * pieces[pieces.Length - 4]) }; return(results); } else if (pieces[pieces.Length - 4] == 0 && count == 1 && (1.0 / pieces[pieces.Length - 5]) % 2 == 0) { double a = Simple.Pow(((0 - pieces[pieces.Length - 1]) / pieces[index]), 1.0 / pieces[pieces.Length - 5]); double[] results = { (f(a) - pieces[pieces.Length - 2]) / pieces[pieces.Length - 3], (f(-a) - pieces[pieces.Length - 2]) / pieces[pieces.Length - 3] }; return(results); } else { if (pieces[pieces.Length - 5] == 0) { pieces[pieces.Length - 5] = 1.0; } double[] results = { (f(Simple.Pow((0 - pieces[pieces.Length - 1]) / pieces[index], 1.0 / pieces[pieces.Length - 5])) - pieces[pieces.Length - 2]) / pieces[pieces.Length - 3] }; return(results); } }