internal static Set?SolveLinear(Entity expr, Entity.Variable x) { var replacement = Variable.CreateTemp(expr.Vars); Func <Entity, Entity> preparator = e => e switch { Powf(var @base, var arg) when TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out var b) => MathS.Pow(@base, b) * MathS.Pow(MathS.Pow(MathS.e, x), MathS.Ln(@base) * a), _ => e, }; Func <Entity, Entity> replacer = e => e switch { Powf(var @base, var arg) when @base == MathS.e && arg == x => replacement, _ => e, }; expr = expr.Replace(preparator); expr = expr.Replace(replacer); if (expr.ContainsNode(x)) { return(null); // cannot be solved, not a pure exponential } expr = expr.InnerSimplified; if (AnalyticalEquationSolver.Solve(expr, replacement) is FiniteSet els && els.Any()) { return((Set)els.Select(sol => MathS.Pow(MathS.e, x).Invert(sol, x).ToSet()).Unite().InnerSimplified); }
// solves equation f(sin(x), cos(x), tan(x), cot(x)) for x internal static bool TrySolveLinear(Entity expr, Variable variable, out Set res) { res = Empty; var replacement = Variable.CreateTemp(expr.Vars); expr = expr.Replace(Patterns.NormalTrigonometricForm); expr = expr.Replace(Patterns.TrigonometricToExponentialRules(variable, replacement)); MultithreadingFunctional.ExitIfCancelled(); // if there is still original variable after replacements, // equation is not in a form f(sin(x), cos(x), tan(x), cot(x)) if (expr.ContainsNode(variable)) { return(false); } if (AnalyticalEquationSolver.Solve(expr, replacement) is FiniteSet els) { MultithreadingFunctional.ExitIfCancelled(); res = (Set)els.Select(sol => MathS.Pow(MathS.e, MathS.i * variable).Invert(sol, variable).ToSet()).Unite().InnerSimplified; return(true); } else { return(false); } }
/// <summary>Solves one equation</summary> internal static Set Solve(Entity equation, Variable x) { using var _ = MathS.Settings.PrecisionErrorZeroRange.Set(1e-12m); using var __ = MathS.Settings.FloatToRationalIterCount.Set(0); using var ___ = MathS.Settings.MaxExpansionTermCount.Set(50); var solutions = AnalyticalEquationSolver.Solve(equation, x);
/// <summary>Solves one equation</summary> internal static Set Solve(Entity equation, Variable x) { var solutions = MathS.Settings.PrecisionErrorZeroRange.As(1e-12m, () => MathS.Settings.FloatToRationalIterCount.As(0, () => AnalyticalEquationSolver.Solve(equation, x) ));