/// <summary>
 /// Considers expr > 0
 /// </summary>
 internal static Set Solve(Entity expr, Variable x)
 {
     {
         if (MathS.Utils.TryGetPolyLinear(expr, x, out var a, out var b))
         {
             a = a.InnerSimplified;
             b = b.InnerSimplified;
             var root = PolynomialSolver.SolveLinear(a, b).First();
             if (root is Complex and not Real)
             {
                 return(Empty);
             }
             if (a is Real {
                 IsNegative : true
             })
예제 #2
0
        internal static void Solve(Entity expr, VariableEntity x, Set dst, bool compensateSolving)
        {
            if (expr == x)
            {
                dst.Add(0);
                return;
            }

            // Applies an attempt to downcast roots
            void DestinationAddRange(Set toAdd)
            {
                toAdd.FiniteApply(ent => TryDowncast(expr, x, ent));
                dst.AddRange(toAdd);
            }

            var polyexpr = expr.DeepCopy();
            Set res      = PolynomialSolver.SolveAsPolynomial(polyexpr, x);

            if (res != null)
            {
                res.FiniteApply(e => e.InnerSimplify());
                DestinationAddRange(res);
                return;
            }

            if (expr.entType == Entity.EntType.OPERATOR)
            {
                switch (expr.Name)
                {
                case "mulf":
                    Solve(expr.Children[0], x, dst);
                    Solve(expr.Children[1], x, dst);
                    return;

                case "divf":

                    bool IsSetNumeric(Set a)
                    => a.Select(piece => piece.LowerBound().Item1).All(MathS.CanBeEvaluated);

                    var zeroNumerators = new Set();
                    Solve(expr.Children[0], x, zeroNumerators);
                    if (!IsSetNumeric(zeroNumerators))
                    {
                        dst.AddRange(zeroNumerators);
                        return;
                    }
                    var zeroDenominators = new Set();
                    Solve(expr.Children[1], x, zeroDenominators);
                    if (!IsSetNumeric(zeroDenominators))
                    {
                        dst.AddRange(zeroNumerators);
                        return;
                    }
                    dst.AddRange((zeroNumerators & !zeroDenominators) as Set);
                    return;

                case "powf":
                    Solve(expr.Children[0], x, dst);
                    return;

                case "minusf":
                    if (expr.Children[1].FindSubtree(x) == null && compensateSolving)
                    {
                        if (expr.Children[0] == x)
                        {
                            dst.Add(expr.Children[1]);
                            return;
                        }
                        var    subs      = 0;
                        Entity lastChild = null;
                        foreach (var child in expr.Children[0].Children)
                        {
                            if (child.FindSubtree(x) != null)
                            {
                                subs     += 1;
                                lastChild = child;
                            }
                        }
                        if (subs != 1)
                        {
                            break;
                        }
                        var resInverted = TreeAnalyzer.FindInvertExpression(expr.Children[0], expr.Children[1], lastChild);
                        foreach (var result in resInverted.FiniteSet())
                        {
                            Solve(lastChild - result, x, dst, compensateSolving: true);
                        }
                        return;
                    }
                    break;
                }
            }
            else if (expr.entType == Entity.EntType.FUNCTION)
            {
                DestinationAddRange(TreeAnalyzer.InvertFunctionEntity(expr as FunctionEntity, 0, x));
                return;
            }


            // Here we generate a unique variable name
            var uniqVars = MathS.Utils.GetUniqueVariables(expr);

            uniqVars.Pieces.Sort((a, b) => ((Entity)b).Name.Length.CompareTo(((Entity)a).Name.Length));
            VariableEntity newVar = ((Entity)uniqVars.Pieces[0]).Name + "quack";
            // // //


            // Here we find all possible replacements
            var replacements = new List <Tuple <Entity, Entity> >();

            replacements.Add(new Tuple <Entity, Entity>(TreeAnalyzer.GetMinimumSubtree(expr, x), expr));
            foreach (var alt in expr.Alternate(4).FiniteSet())
            {
                if ((alt).FindSubtree(x) == null)
                {
                    return; // in this case there is either 0 or +oo solutions
                }
                replacements.Add(new Tuple <Entity, Entity>(TreeAnalyzer.GetMinimumSubtree(alt, x), alt));
            }
            // // //

            // Here we find one that has at least one solution

            foreach (var replacement in replacements)
            {
                Set solutions = null;
                if (replacement.Item1 == x)
                {
                    continue;
                }
                var newExpr = replacement.Item2.DeepCopy();
                TreeAnalyzer.FindAndReplace(ref newExpr, replacement.Item1, newVar);
                solutions = newExpr.SolveEquation(newVar);
                if (!solutions.IsEmpty())
                {
                    var bestReplacement = replacement.Item1;

                    // Here we are trying to solve for this replacement
                    Set newDst = new Set();
                    foreach (var solution in solutions.FiniteSet())
                    {
                        var str = bestReplacement.ToString();
                        // TODO: make a smarter comparison than just comparison of complexities of two expressions
                        // The idea is
                        // similarToPrevious = ((bestReplacement - solution) - expr).Simplify() == 0
                        // But Simplify costs us too much time
                        var similarToPrevious = (bestReplacement - solution).Complexity() >= expr.Complexity();
                        if (!compensateSolving || !similarToPrevious)
                        {
                            Solve(bestReplacement - solution, x, newDst, compensateSolving: true);
                        }
                    }
                    DestinationAddRange(newDst);
                    if (!dst.IsEmpty())
                    {
                        break;
                    }
                    // // //
                }
            }
            // // //

            // if no replacement worked, try trigonometry solver
            if (dst.IsEmpty())
            {
                var trigexpr = expr.DeepCopy();
                res = TrigonometricSolver.SolveLinear(trigexpr, x);
                if (res != null)
                {
                    DestinationAddRange(res);
                    return;
                }
            }
            // // //


            // if nothing has been found so far
            if (dst.IsEmpty() && MathS.Settings.AllowNewton)
            {
                Set allVars = new Set();
                TreeAnalyzer._GetUniqueVariables(expr, allVars);
                if (allVars.Count == 1)
                {
                    DestinationAddRange(expr.SolveNt(x));
                }
            }
        }