public void ToTaylorExpansion_1() { var cosine = new Cosine { Aparam = 2, Bparam = 5 }; var expansion = cosine.ToTaylorExpansion(5).ToList(); expansion.Should().HaveCount(7); expansion.Select(s => s.PolynomialDegree).Should().ContainInOrder(0, 1, 2, 3, 4, 5, 0); expansion.Select(s => s.LittleODegree).Should().ContainInOrder(0, 0, 0, 0, 0, 0, 5); expansion.Select(s => s.Coefficient) .Should() .ContainInOrder( Math.Cos(5), -2 * Math.Sin(5), -2 * Math.Cos(5), 8.0 / 6 * Math.Sin(5), 16.0 / 24 * Math.Cos(5), -32.0 / 120 * Math.Sin(5), 1.0 ); }
public static IElementaryFunction ConvertToElementaryFunction(string str) { IElementaryFunction function = null; double[] arr = null; if (str.Contains("sin")) { function = new Sine(); arr = FindParameters(str.Substring(4, str.Count() - 4)); function.Aparam = arr[0]; function.Bparam = arr[1]; } else if (str.Contains("cos")) { function = new Cosine(); arr = FindParameters(str.Substring(4, str.Count() - 4)); function.Aparam = arr[0]; function.Bparam = arr[1]; } else if (str.Contains("ln")) { function = new LogarithmicFunction(); arr = FindParameters(str.Substring(3, str.Count() - 3)); function.Aparam = arr[0]; function.Bparam = arr[1]; } else if (str.Contains("e^")) { function = new ExponentialFunction(); arr = FindParameters(str.Substring(3, str.Count() - 3)); function.Aparam = arr[0]; function.Bparam = arr[1]; } else if (str.Contains(")^(") || str.Contains("x^(") || str.Contains(")^")) { function = new PowerFunction(); int powerPosition = 0; for (int i = 0; i < str.Count(); i++) { if (str[i] == '^') { powerPosition = i; StringBuilder num = new StringBuilder(); StringBuilder den = new StringBuilder(); bool slash = false; for (int j = i + 1; j < str.Count(); j++) { if (str[j] != '/' && str[j] != '(' && str[j] != ')') { if (slash == false) { num.Append(str[j]); } else den.Append(str[j]); } else if (str[j] == '/') { slash = true; } } if (slash == false) { ((PowerFunction)function).PowerDenominator = 1; } else { ((PowerFunction)function).PowerDenominator = int.Parse(den.ToString()); } ((PowerFunction)function).PowerNumerator = int.Parse(num.ToString()); } } int beginPosition = 0; if (str[0] == '(') { beginPosition = 1; } arr = FindParameters(str.Substring(beginPosition, powerPosition - beginPosition)); function.Aparam = arr[0]; function.Bparam = arr[1]; } return function; }