// a * b * c / (b * c * d) // => // a * (b / b) * (c / c) * 1/d private static IEnumerable <Entity> PairwiseGrouping(Entity num, Entity den, TreeAnalyzer.SortLevel level) { var numFactors = Mulf.LinearChildren(num); var denFactors = Mulf.LinearChildren(den); var factors = new Dictionary <string, Entity>(); foreach (var numFactor in numFactors) { var sorted = numFactor.SortHash(level); if (!factors.ContainsKey(sorted)) { factors[sorted] = 1; } factors[sorted] = (factors[sorted] * numFactor).InnerSimplified; } foreach (var denFactor in denFactors) { var sorted = denFactor.SortHash(level); if (!factors.ContainsKey(sorted)) { factors[sorted] = 1; } factors[sorted] = (factors[sorted] / denFactor).InnerSimplified; } return(factors.Values); }
internal static bool TrySolve(Entity expr, Variable x, out Set dst) { dst = Set.Empty; var children = TreeAnalyzer.GatherLinearChildrenOverSumAndExpand( expr, entity => entity.ContainsNode(x) ); if (children is null) { return(false); } Entity normalPolynom = 0; var fractioned = new List <(Entity multiplier, List <(Entity main, Integer pow)> fracs)>(); // Use PowerRules to replace sqrt(f(x))^2 => f(x) foreach (var child in children.Select(c => c.InnerSimplified.Replace(Patterns.PowerRules))) { (Entity multiplier, List <(Entity main, Integer pow)> fracs)potentialFraction; potentialFraction.multiplier = 1; potentialFraction.fracs = new List <(Entity main, Integer pow)>(); foreach (var mpChild in Mulf.LinearChildren(child)) { if (!(mpChild is Powf(var @base, Number num and not Integer))) // (x + 3) ^ 3 { potentialFraction.multiplier *= mpChild; continue; } if (num is not Rational fracNum) { return(false); // (x + 1)^0.2348728 } var newChild = MathS.Pow(@base, fracNum.ERational.Numerator).InnerSimplified; var den = fracNum.ERational.Denominator; potentialFraction.fracs.Add((newChild, den)); MultithreadingFunctional.ExitIfCancelled(); } if (potentialFraction.fracs.Count > 0) { fractioned.Add(potentialFraction); } else { normalPolynom += child; } } if (fractioned.Count == 0) { return(false); // means that one can either be solved polynomially or unsolvable at all }