/// <summary> /// Returns a list of linear children over sum /// where at most one term doesn't contain x, e. g. /// x2 + x + a + b + x /// => /// [x2, x, a + b, x] /// </summary> internal static List <Entity>?GatherLinearChildrenOverSumAndExpand(Entity expr, Func <Entity, bool> conditionForUniqueTerms) { if (expr is not(Sumf or Minusf)) { return(SmartExpandOver(expr, conditionForUniqueTerms)); } var res = new List <Entity>(); Entity?freeTerm = null; foreach (var child in Sumf.LinearChildren(expr)) { if (conditionForUniqueTerms(child)) { var expanded = SmartExpandOver(child, conditionForUniqueTerms); if (expanded is null) { return(null); } res.AddRange(expanded); } else { freeTerm = freeTerm is { } } }
/// <summary> /// Finds all monomials with respect to the power of <paramref name="variable"/> /// </summary> internal static bool TryGetPolynomial(Entity expr, Variable variable, [NotNullWhen(true)] out Dictionary <EInteger, Entity>?dst) { var children = Sumf.LinearChildren(expr.Expand()); dst = Algebra.AnalyticalSolving.PolynomialSolver.GatherMonomialInformation <EInteger, PrimitiveInteger>(children, variable); return(dst is not null); }
/// <summary> /// Sorts an expression into a polynomial. /// See more at <see cref="MathS.TryPolynomial"/> /// </summary> internal static bool TryPolynomial(Entity expr, Variable variable, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Entity?dst) { dst = null; var children = Sumf.LinearChildren(expr.Expand()); var monomialsByPower = Algebra.AnalyticalSolving.PolynomialSolver.GatherMonomialInformation <EInteger, TreeAnalyzer.PrimitiveInteger>(children, variable); if (monomialsByPower == null) { return(false); } var res = BuildPoly(monomialsByPower, variable); if (res is null) { return(false); } dst = res; return(true); }
static (Entity poly, Entity remainder) ExtractPolynomialLocally(Entity expr, Variable x) { var(powInfo, rem) = PolynomialSolver.GatherMonomialInformationAllowingBad <EInteger, PrimitiveInteger>(Sumf.LinearChildren(expr), x); if ( powInfo.TryGetValue(EInteger.Zero, out var info) // if we have something line x^5 + a + a over x then we should get (x^5, a + a) so that we could continute working with the remainder ) { rem += info; powInfo.Remove(EInteger.Zero); } return(Simplificator.BuildPoly(powInfo, x) ?? 0, rem); }
Sinf(Sumf(var any1, var any2)) => new Sinf(any1) * new Cosf(any2) + new Sinf(any2) * new Cosf(any1),