示例#1
0
        // 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);
        }
        private static Entity SortAndGroup(IEnumerable <Entity> children, TreeAnalyzer.SortLevel level, Func <Entity, Entity, Entity> ctor)
        {
            var groups = new Dictionary <string, List <Entity> >();

            foreach (var child in children)
            {
                var hash = child.SortHash(level);
                if (!groups.ContainsKey(hash))
                {
                    groups[hash] = new();
                }
                groups[hash].Add(child);
            }
            return(groups.OrderBy(pair => pair.Key).Select(pair => pair.Value.Aggregate(ctor)).Aggregate(ctor));
        }
示例#3
0
 internal static Entity FractionCommonDenominatorRules(Entity expr, TreeAnalyzer.SortLevel level)
 => expr switch
 {
 /// <summary>Actual sorting with <see cref="Entity.SortHash(TreeAnalyzer.SortLevel)"/></summary>
 internal static Func <Entity, Entity> SortRules(TreeAnalyzer.SortLevel level) => x => x switch
 {