Exemple #1
0
        /// <summary>
        /// We have pattern and we have keys. That is the function
        /// to get an expression from the pattern and keys.
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        internal Entity BuildTree(Dictionary <int, Entity> keys)
        {
            if (this.entType != Entity.EntType.PATTERN)
            {
                return(this);
            }
            if (keys.ContainsKey(PatternNumber))
            {
                return(keys[PatternNumber]);
            }
            var PatternType = (this as Pattern).patType;

            if (PatternType == PatType.NUMBER)
            {
                return(new NumberEntity(ComplexNumber.Parse(Name)));
            }
            if (PatternType == PatType.VARIABLE)
            {
                return(new VariableEntity(Name));
            }
            var newChildren = new List <Entity>();

            foreach (var child in Children)
            {
                newChildren.Add(child.BuildTree(keys));
            }
            if (PatternType == PatType.FUNCTION)
            {
                var res = new FunctionEntity(Name)
                {
                    Children = newChildren
                };
                return(res);
            }
            else if (PatternType == PatType.OPERATOR)
            {
                switch (Name)
                {
                case "sumf": return(newChildren[0] + newChildren[1]);

                case "minusf": return(newChildren[0] - newChildren[1]);

                case "mulf": return(newChildren[0] * newChildren[1]);

                case "divf": return(newChildren[0] / newChildren[1]);

                case "powf": return(MathS.Pow(newChildren[0], newChildren[1]));

                case "logf": return(MathS.Log(newChildren[0], newChildren[1]));

                case "sinf": return(MathS.Sin(newChildren[0]));

                case "cosf": return(MathS.Cos(newChildren[0]));

                case "tanf": return(MathS.Tan(newChildren[0]));

                case "cotanf": return(MathS.Cotan(newChildren[0]));

                case "arcsinf": return(MathS.Arcsin(newChildren[0]));

                case "arccosf": return(MathS.Arccos(newChildren[0]));

                case "arctanf": return(MathS.Arctan(newChildren[0]));

                case "arccotanf": return(MathS.Arccotan(newChildren[0]));

                default: return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
 /// <summary>
 /// Substitute a variable with an expression
 /// </summary>
 /// <param name="x">
 /// A name of variable to substitute
 /// </param>
 /// <param name="value">
 /// The value we replace variable with
 /// </param>
 /// <returns></returns>
 public Entity Substitute(string x, Entity value)
 => Substitute(MathS.Var(x), value, false);
        public static Entity Simplify(List <Entity> args)
        {
            MathFunctions.AssertArgs(args.Count, 2);
            var r1 = args[0].InnerSimplify();
            var r2 = args[1].InnerSimplify();

            if (r1.entType == Entity.EntType.NUMBER && r2.entType == Entity.EntType.NUMBER)
            {
                // TODO: Consider cases like sqrt(12) which could be simplified to 2 sqrt(3)
                var(n1, n2) = ((r1 as NumberEntity).Value, (r2 as NumberEntity).Value);
                return(InnerSimplifyAdditionalFunctional.KeepIfBad(Number.Pow(n1, n2), MathS.Pow(r1, r2), n1, n2));
            }
            else if (r1 == 0 || r1 == 1)
            {
                return(r1);
            }
            else if (r2 == 1)
            {
                return(r1);
            }
            else if (r2 == 0)
            {
                return(1);
            }
            else if (r2 == -1)
            {
                return(1 / r1);
            }
            else
            {
                return(r1.Pow(r2));
            }
        }
Exemple #4
0
 internal static Entity EvalIfCan(Entity a)
 => MathS.CanBeEvaluated(a) ? a.Eval() : a;