internal static Entity TaylorExpansion(Entity expr, Variable exprVariable, Variable polyVariable, Entity point, int termCount) { if (termCount < 0) { throw new InvalidNumberException($"{nameof(termCount)} is supposed to be at least 0"); } var terms = new List <Entity>(); foreach (var term in TaylorExpansionTerms(expr, exprVariable, polyVariable, point)) { if (terms.Count >= termCount) { return(TreeAnalyzer.MultiHangBinary(terms, (a, b) => a + b)); } terms.Add(term); } throw new AngouriBugException($"We cannot get here, as {nameof(TaylorExpansionTerms)} is supposed to be endless"); }
/// <summary> /// Sorts an expression into a polynomial. /// See more MathS.Utils.TryPolynomial /// </summary> /// <param name="expr"></param> /// <param name="variable"></param> /// <param name="dst"></param> /// <returns></returns> internal static bool TryPolynomial(Entity expr, VariableEntity variable, out Entity dst) { dst = null; var children = TreeAnalyzer.LinearChildren(expr.Expand(), "sumf", "minusf", Const.FuncIfSum); var monomialsByPower = PolynomialSolver.GatherMonomialInformation <long>(children, variable); if (monomialsByPower == null) { return(false); } var newMonomialsByPower = new Dictionary <int, Entity>(); var keys = monomialsByPower.Keys.ToList(); keys.Sort((i, i1) => (i < i1 ? 1 : (i > i1 ? -1 : 0))); var terms = new List <Entity>(); foreach (var index in keys) { var pair = new KeyValuePair <long, Entity>(index, monomialsByPower[index]); Entity px; if (pair.Key == 0) { terms.Add(pair.Value.Simplify()); continue; } if (pair.Key == 1) { px = variable; } else { px = MathS.Pow(variable, pair.Key); } if (pair.Value == 1) { terms.Add(px); continue; } else { terms.Add(pair.Value.Simplify() * px); } } if (terms.Count == 0) { return(false); } dst = terms[0]; for (int i = 1; i < terms.Count; i++) { if (terms[i].Name == "mulf" && terms[i].Children[0].entType == Entity.EntType.NUMBER && terms[i].Children[0].GetValue().IsReal() && terms[i].Children[0].GetValue().Real < 0) { dst -= ((-1) * terms[i].Children[0].GetValue()) * terms[i].Children[1]; } else { dst += terms[i]; } } dst = dst.InnerSimplify(); return(true); }