Esempio n. 1
0
 /// <summary>
 /// Constructor that asks for the declaration to bind
 /// </summary>
 /// <param name="value"></param>
 public Input(string name, Entity.Variable value, bool reference)
 {
     this.name       = name;
     this.definition = value;
     this.reference  = reference;
     IsValueSet      = false;
 }
Esempio n. 2
0
        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);
            }
Esempio n. 3
0
 public Formula(Dictionary <string, Entity.Variable> inputs, Entity.Variable output) : base()
 {
     foreach (KeyValuePair <string, Entity.Variable> input in inputs)
     {
         AddInput(input.Key, input.Value);
     }
     AddOutput("result", output);
 }
Esempio n. 4
0
        public RemoveValueAtKey()
        {
            var refVal = new Entity.Variable(Entity.Type.DictType.Instance);

            AddInput("reference", refVal, true);
            AddInput("key", new Entity.Variable(Entity.Type.Scalar.String));
            AddOutput("reference", refVal, true);
        }
Esempio n. 5
0
 public SetValueAt(Entity.DataType stored) : base()
 {
     Entity.Variable val = new Entity.Variable(stored);
     AddInput("array", new Entity.Variable(new Entity.Type.ListType(stored)), true);
     AddInput("value", val);
     AddInput("index", new Entity.Variable(Entity.Type.Scalar.Integer));
     AddOutput("value", val, true);
 }
Esempio n. 6
0
        public SetValueAtKey()
        {
            var refVar = new Entity.Variable(Entity.Type.DictType.Instance);

            AddInput("reference", refVar, true);
            AddInput("key", new Entity.Variable(Entity.Type.Scalar.String));
            AddInput("value", new Entity.Variable(Entity.Type.AnyType.Instance), true);
            AddOutput("reference", refVar);
        }
Esempio n. 7
0
        public Resize()
        {
            var matrix = new Entity.Variable(Entity.Type.Matrix.Instance);

            AddInput("reference", matrix, true);
            AddInput("rowCount", new Entity.Variable(Entity.Type.Scalar.Integer));
            AddInput("columnCount", new Entity.Variable(Entity.Type.Scalar.Integer));
            AddInput("copy", new Entity.Variable(Entity.Type.Scalar.Boolean));
            AddOutput("result", matrix, true);
        }
Esempio n. 8
0
        public static IEnumerable <double> FindVerticalAsymptotes(string funcString, Entity.Variable vari)
        {
            Entity equation = MathS.FromString($"1 / ({funcString})").Simplify();

            Entity.Set set = equation.SolveEquation(vari);
            return(set.DirectChildren.Select(s =>
            {
                var val = s.EvalNumerical().ToNumerics().Real;
                return val;
            }));
        }
Esempio n. 9
0
        public static IEnumerable <double> FindVerticalAsymptotes(Entity func, Entity.Variable vari)
        {
            Entity equation = (1 / func).Expand().Simplify();

            Entity.Set set = equation.SolveEquation(vari);
            return(set.DirectChildren.SelectMany(e => e.DirectChildren.Append(e)).Where(e => e.EvaluableNumerical).Select(s =>
            {
                var val = s.EvalNumerical().ToNumerics().Real;
                return val;
            }));
        }
Esempio n. 10
0
 public SetAttribute(Entity.Type.ObjectType type) : base()
 {
     stored = type;
     AddInput("this", new Entity.Variable(type), true);
     foreach (KeyValuePair <string, Global.IDefinition> curr in type.GetAttributes())
     {
         Entity.Variable definition = new Entity.Variable((Entity.DataType)curr.Value);
         AddInput(curr.Key, definition);        //each time the node is executed, input are refreshed
         AddOutput(curr.Key, definition, true); //if the definition of output is the same as the input they will be refreshed too
         attributes.Add(curr.Key);
     }
 }
Esempio n. 11
0
        public void TestComplicated()
        {
            Entity subExpr = "(a * x2 + b x) / (c x2 - 3)";
            Entity expr    = MathS.Sqrt(subExpr * 3 / MathS.Sin(subExpr) + MathS.Sin("d"));

            Entity.Variable x     = nameof(x);
            Entity          dest  = Real.PositiveInfinity;
            var             limit = expr.Limit(x, dest, ApproachFrom.Left);

            Assert.NotNull(limit);
            Assert.Equal("sqrt(a / c * 3 / sin(a / c) + sin(d))", limit?.Stringize());
        }
        internal static Entity?SolveBySplittingSum(Entity expr, Entity.Variable x)
        {
            var splitted = TreeAnalyzer.GatherLinearChildrenOverSumAndExpand(expr, e => e.ContainsNode(x));

            if (splitted is null || splitted.Count < 2)
            {
                return(null);                                                    // nothing to do, let other solvers do the work
            }
            splitted[0] = Integration.ComputeIndefiniteIntegral(splitted[0], x); // base case for aggregate
            var result = splitted.Aggregate((e1, e2) => e1 + Integration.ComputeIndefiniteIntegral(e2, x));

            return(result);
        }
Esempio n. 13
0
        public override void Execute()
        {
            Entity.Variable objReference = GetInput("this").Definition;

            foreach (String curr in attributes)
            {
                Input inp = GetInput(curr);

                if (inp.IsValueSet)
                {
                    stored.SetAttributeValue(objReference.Value, curr, inp.Value);
                }
            }
        }
Esempio n. 14
0
        public override void Execute()
        {
            List <dynamic> value = new List <dynamic>();

            Entity.Variable array   = GetInput("array").Definition;
            dynamic         element = GetInputValue("element");
            dynamic         count   = GetInputValue("count");

            for (int i = 0; i < count; i++)
            {
                value.Add(element);
            }
            array.Value = value;
        }
            static Entity?IntegrateByParts(Entity v, Entity u, Entity.Variable x, int currentRecursion = 0)
            {
                if (v == 0)
                {
                    return(0);
                }
                if (currentRecursion == MathS.Settings.MaxExpansionTermCount)
                {
                    return(null);
                }

                var integral     = Integration.ComputeIndefiniteIntegral(u, x);
                var differential = v.Differentiate(x);
                var result       = IntegrateByParts(differential, integral, x, currentRecursion + 1);

                return((result is null) ? null : v *integral - result);
            }
Esempio n. 16
0
        public async Task <Entity> FindLimit(Entity expr, Entity.Variable x, Entity dest, AngouriMath.Core.ApproachFrom from)
        {
            if (lastTokenSource is not null)
            {
                lastTokenSource.Cancel();
            }
            var tokenSource = new CancellationTokenSource();

            tokenSource.CancelAfter(Timeout);
            lastTokenSource = tokenSource;
            return(await Task.Run(
                       () =>
            {
                MathS.Multithreading.SetLocalCancellationToken(tokenSource.Token);
                return expr.Limit(x, dest, from);
            }, tokenSource.Token
                       ).ContinueWith(t => t.IsCanceled ? "Timeout" : t.Result));
        }
Esempio n. 17
0
        /// <summary>Numerically checks if a root fits an equation</summary>
        internal static void AssertRoots(Entity equation, Entity.Variable toSub, Entity varValue, Integer?subValue = null)
        {
            subValue ??= 3;
            string eqNormal = equation.Stringize();

            var rootSimplified = varValue.Complexity > 100 ? varValue : varValue.Simplify();

            equation = equation.Substitute(toSub, rootSimplified);
            // MUST be integer to correspond to integer coefficient of periodic roots
            var substitutions = new Dictionary <Entity.Variable, Integer>();

            foreach (var vr in equation.Vars)
            {
                substitutions.Add(vr, subValue + substitutions.Count);
            }
            equation = equation.Substitute(substitutions);
            var err = equation.EvalNumerical().Abs();

            Assert.True(err < 0.001m, $"\nError = {err.Stringize()}\n{eqNormal}\nWrong root: {toSub.Stringize()} = {rootSimplified.Stringize()}");
        }
Esempio n. 18
0
        public async Task <Entity> Solve(Entity expr, Entity.Variable @var)
        {
            if (lastTokenSource is not null)
            {
                lastTokenSource.Cancel();
            }
            var tokenSource = new CancellationTokenSource();

            tokenSource.CancelAfter(Timeout);
            lastTokenSource = tokenSource;
            return(await Task.Run(
                       () =>
            {
                MathS.Multithreading.SetLocalCancellationToken(tokenSource.Token);
                if (expr is Entity.Statement)
                {
                    return expr.Solve(@var).Simplify();
                }
                return MathS.Equality(expr, 0).Solve(@var).Simplify();
            }, tokenSource.Token
                       ).ContinueWith(t => t.IsCanceled ? "Timeout" : t.Result));
        }
Esempio n. 19
0
 /// <summary>
 /// Allow to add an output to the instruction
 /// </summary>
 /// <param name="name">Name of the output</param>
 /// <param name="definition">Variable definition of the output</param>
 public void AddOutput(string name, Entity.Variable definition, bool reference = false)
 {
     this.outputs[name] = new Output(definition, reference);
 }
Esempio n. 20
0
 internal static Entity?TryStandardIntegrals(Entity expr, Entity.Variable x) => expr switch
 {
Esempio n. 21
0
 /// <summary>Plots from an expression over variable x.
 /// It's better to run this function of already compiled function</summary>
 /// <param name="expr">Expression to build</param>
 /// <param name="from">Low bound</param>
 /// <param name="to">High bound</param>
 public void PlotScatter(Entity expr, Entity.Variable x, Entity.Number.Complex from, Entity.Number.Complex to)
 => PlotScatter(expr.Compile(x), from, to);
Esempio n. 22
0
 /// <summary>
 /// Allow to add a value to the enumeration
 /// </summary>
 /// <param name="name">Represents the name of the value</param>
 /// <param name="definition">Represents the variable definition of the value</param>
 public void SetValue(string name, Entity.Variable definition)
 {
     //check given definition validity
     this.values[name] = definition;
 }
Esempio n. 23
0
 /// <summary>
 /// Constructor that need the declaration to bind
 /// </summary>
 /// <param name="value">Variable declaration to bind</param>
 public Output(Entity.Variable definition, bool reference = false)
 {
     this.definition = definition;
     this.reference  = reference;
 }
Esempio n. 24
0
 /// <summary>
 /// Constructor to print only one variable
 /// </summary>
 /// <param name="toprint">Variable to watch</param>
 public Debug(Entity.Variable toprint) : base()
 {
     AddInput("to_print", toprint, true);
     GetInput("to_print").IsValueSet = true;
 }
Esempio n. 25
0
 /// <summary>
 /// Constructor that asks for the variable definition to get
 /// </summary>
 /// <param name="toget">Variable definition to get as output</param>
 public Getter(Entity.Variable toget) : base()
 {
     AddOutput("reference", toget, true);
 }
 internal static Entity?SolveIntegratingByParts(Entity expr, Entity.Variable x)
 {
 internal static Entity?SolveAsPolynomialTerm(Entity expr, Entity.Variable x) => expr switch
 {
Esempio n. 28
0
 /// <summary>
 /// Constructor that need the definition of the variable to set
 /// </summary>
 /// <param name="toset">Definition of the variable to set</param>
 public Setter(Entity.Variable toset) : base()
 {
     AddInput("value", new Entity.Variable(toset.Type)); //creates a copy of the variable in input
     AddOutput("reference", toset, true);                //set the variable to set in output
 }
Esempio n. 29
0
 /// <summary>Performs a grid search with each iteration done by NewtonIter</summary>
 /// <param name="expr">The equation with one variable to be solved</param>
 /// <param name="v">The variable to solve over</param>
 /// <param name="settings">
 /// Some settings regarding how we should perform the Newton solver process
 /// A complex number, thus, if stepCount.Im == 0, no operations will be performed at all. If you
 /// need to iterate over real numbers only, set it to 1, i. e. new Number(your_number, 1)
 /// How many approximations we need to do before we reach the most precise result.
 /// </param>
 internal static HashSet <Complex> SolveNt(Entity expr, Entity.Variable v, MathS.Settings.NewtonSetting settings)
 {
Esempio n. 30
0
 /// <summary>
 /// Allow to add input to the instruction
 /// </summary>
 /// <param name="name">Name of the input</param>
 /// <param name="definition">Variable definition of the input</param>
 public void AddInput(string name, Entity.Variable definition, bool reference = false)
 {
     this.inputs[name] = new Input(name, definition, reference);
 }