Esempio n. 1
0
        public override E Evaluate(EvalSettings evalSettings = null, bool isRootNode = true)
        {
            evalSettings = evalSettings ?? E.DefaultEvalSettings;

            if (evalSettings.SubstituteVariables)
            {
                if (Type == VariableType.Numeric)
                {
                    object val = null;

                    if (Value != null)
                    {
                        val = Value;
                    }

                    object settingsVal;

                    if (evalSettings.Values.TryGetValue(Name, out settingsVal))
                    {
                        val = settingsVal;
                    }

                    if (val != null && val.IsNumeric())
                    {
                        return(SimplifyIfRoot(E.NumConst(Convert.ToDouble(val)), evalSettings, isRootNode));
                    }
                }
            }

            return(SimplifyIfRoot(this, evalSettings, isRootNode));
        }
        public override E Evaluate(EvalSettings evalSettings = null, bool isRootNode = true)
        {
            evalSettings = evalSettings ?? E.DefaultEvalSettings;
            E evaluatedObject = Object.Evaluate(evalSettings, false);

            if (evaluatedObject is Variable)
            {
                Variable var = evaluatedObject as Variable;

                if (var.IsValueNode)
                {
                    double4 v = ( double4 )var.Value;

                    if (Property == "x")
                    {
                        return(SimplifyIfRoot(E.NumConst(v.x), evalSettings, isRootNode));
                    }
                    else if (Property == "y")
                    {
                        return(SimplifyIfRoot(E.NumConst(v.y), evalSettings, isRootNode));
                    }
                    else if (Property == "z")
                    {
                        return(SimplifyIfRoot(E.NumConst(v.z), evalSettings, isRootNode));
                    }
                    else if (Property == "w")
                    {
                        return(SimplifyIfRoot(E.NumConst(v.w), evalSettings, isRootNode));
                    }
                }
            }

            return(SimplifyIfRoot(evaluatedObject.Prop(Property), evalSettings, isRootNode));
        }
Esempio n. 3
0
        public override E Evaluate(EvalSettings evalSettings = null, bool isRootNode = true)
        {
            evalSettings = evalSettings ?? E.DefaultEvalSettings;
            E evaluatedExpression = Expression.Evaluate(evalSettings, false);

            if (evaluatedExpression is Literal && OpKind == UnaryOpKind.Negate)
            {
                if (evaluatedExpression is NumericConstant)
                {
                    NumericConstant num = evaluatedExpression as NumericConstant;

                    return(SimplifyIfRoot(E.NumConst(-num.Value), evalSettings, isRootNode));
                }
                else if (evaluatedExpression.IsValueNode)
                {
                    Variable var = evaluatedExpression as Variable;
                    double4  v   = ( double4 )var.Value;

                    return(SimplifyIfRoot(E.Vec(var.Name, -v), evalSettings, isRootNode));
                }
            }

            if (OpKind == UnaryOpKind.Negate)
            {
                return(SimplifyIfRoot(E.Negate(evaluatedExpression), evalSettings, isRootNode));
            }
            else
            {
                return(SimplifyIfRoot(evaluatedExpression, evalSettings, isRootNode));
            }
        }
Esempio n. 4
0
        protected static E SimplifyIfRoot(E expr, EvalSettings evalSettings, bool isRootNode)
        {
            if (isRootNode)
            {
                if (evalSettings.MaximumSimplicity)
                {
                    int i = 0;
                    E   ePrev;

                    do
                    {
                        ePrev = expr;
                        expr  = expr.Simplify(evalSettings);
                        i++;
                    } while (ePrev != expr && i <= EvalSettings.INFINITE_LOOP_CYCLE_NUM);

                    if (i == EvalSettings.INFINITE_LOOP_CYCLE_NUM)
                    {
                        Debug.Fail("Simplify Error", "It seems that simplification will never stop. Aborting.");
                    }

                    return(expr);
                }
                else
                {
                    return(expr.Simplify(evalSettings));
                }
            }
            else
            {
                return(expr);
            }
        }
Esempio n. 5
0
        public Vec4 Evaluate(EvalSettings evalSettings = null)
        {
            evalSettings = evalSettings ?? E.DefaultEvalSettings;
            Vec4 v = new Vec4(x.Evaluate(evalSettings), y.Evaluate(evalSettings),
                              z.Evaluate(evalSettings), w.Evaluate(evalSettings));

            return(v);
        }
Esempio n. 6
0
        public override E Simplify(EvalSettings evalSettings = null)
        {
            evalSettings = evalSettings ?? E.DefaultEvalSettings;

            if (FuncKind == Analyze.FuncKind.Pow ||
                FuncKind == Analyze.FuncKind.Sqrt)
            {
                return(global::Math3.Analyze.MultipleOp.SimplifyNestedPowers(this));
            }

            return(base.Simplify(evalSettings));
        }
Esempio n. 7
0
        public E EvaluateStepByStep(out string htmlLog, params object [] varValues)
        {
            List <Tuple <string, string> > htmlSteps = new List <Tuple <string, string> > ();

            EvalFlags [] stepFlags = new [] {
                EvalFlags.ReduceZeroMultiplier | EvalFlags.ReduceUnitMultiplier | EvalFlags.ReduceUnitDivider,
                EvalFlags.GroupByCommonFactor | EvalFlags.GroupMultipliersToFractions,
                EvalFlags.SumFractions,
                EvalFlags.SimplifyTrigonometricFunctions,
                EvalFlags.EvalFuncs,
                EvalFlags.SubstituteVariables
            };

            E e = this;

            htmlSteps.Add(Tuple.Create("Initial expression", e.AsHtml));

            for (int i = 0; i < stepFlags.Length; i++)
            {
                E   prevE = e;
                int c     = 0;

                do
                {
                    for (int j = 0; j <= i; j++)
                    {
                        prevE = e;

                        EvalSettings evalSettings = new EvalSettings(stepFlags [j], varValues);
                        evalSettings.MaximumSimplicity = false;
                        e = e.Evaluate(evalSettings);

                        if (e != prevE)
                        {
                            htmlSteps.Add(Tuple.Create(stepFlags [j].ToString(), e.AsHtml));
                        }
                    }

                    c++;
                } while (e != prevE && c <= EvalSettings.INFINITE_LOOP_CYCLE_NUM);

                if (c == EvalSettings.INFINITE_LOOP_CYCLE_NUM)
                {
                    Debug.Fail("Simplify Error", "It seems that simplification will never stop. Aborting.");
                }
            }

            htmlLog = string.Join("<br />", htmlSteps.Select(step => string.Format("<h4>{0}:</h4>{1}", step.Item1, step.Item2)));

            return(e);
        }
Esempio n. 8
0
        public override E Simplify(EvalSettings evalSettings = null)
        {
            evalSettings = evalSettings ?? E.DefaultEvalSettings;
            E simplifiedExpression = Expression.Simplify(evalSettings);

            if (OpKind == UnaryOpKind.Negate && simplifiedExpression.IsNegative)
            {
                return(simplifiedExpression.SignFree);
            }
            else
            {
                return(new UnaryOp(OpKind, simplifiedExpression));
            }
        }
Esempio n. 9
0
        public Mat4 Simplify(EvalSettings evalSettings = null)
        {
            evalSettings = evalSettings ?? E.DefaultEvalSettings;
            Mat4 mat = new Mat4();

            for (int m = 0; m < 4; m++)
            {
                for (int n = 0; n < 4; n++)
                {
                    mat.M [m, n] = M [m, n].Simplify(evalSettings);
                }
            }

            return(mat);
        }
Esempio n. 10
0
        public Mat4 Evaluate(EvalSettings evalSettings = null)
        {
            evalSettings = evalSettings ?? E.DefaultEvalSettings;
            Mat4 mat = new Mat4();

            for (int m = 0; m < 4; m++)
            {
                for (int n = 0; n < 4; n++)
                {
                    mat.M [m, n] = M [m, n].Evaluate(evalSettings, true);
                }
            }

            return(mat);
        }
Esempio n. 11
0
        public virtual ExpressionCompareResult Compare(E e, EvalSettings evalSettings = null)
        {
            evalSettings = evalSettings ?? E.DefaultEvalSettings;

            if (this.Equals(e))
            {
                return(ExpressionCompareResult.Equal);
            }
            else if ((this.IsNegative && !e.IsNegative && this.SignFree.Equals(e)) ||
                     (!this.IsNegative && e.IsNegative && this.Equals(e.SignFree)))
            {
                return(ExpressionCompareResult.DiffersBySign);
            }
            else
            {
                return(ExpressionCompareResult.NotEqual);
            }
        }
Esempio n. 12
0
 public virtual E Evaluate(EvalSettings evalSettings = null, bool isRootNode = true)
 {
     return(this);
 }
Esempio n. 13
0
 public virtual E Simplify(EvalSettings evalSettings = null)
 {
     return(this);
 }
Esempio n. 14
0
        public override E Evaluate(EvalSettings evalSettings = null, bool isRootNode = true)
        {
            evalSettings = evalSettings ?? E.DefaultEvalSettings;
            List <E> evaluatedArgs = new List <E> (Args.Count);

            foreach (E arg in Args)
            {
                evaluatedArgs.Add(arg.Evaluate(evalSettings, false));
            }

            if (evalSettings.EvalFuncs)
            {
                if (FuncKind == FuncKind.Sin ||
                    FuncKind == FuncKind.Cos ||
                    FuncKind == FuncKind.Abs ||
                    FuncKind == FuncKind.Sqrt)
                {
                    E firstArg = evaluatedArgs [0];

                    if (firstArg is NumericConstant)
                    {
                        double inputValue  = (firstArg as NumericConstant).Value;
                        double returnValue = 0;

                        if (FuncKind == Analyze.FuncKind.Sin)
                        {
                            returnValue = Math.Sin(inputValue);
                        }
                        else if (FuncKind == Analyze.FuncKind.Cos)
                        {
                            returnValue = Math.Cos(inputValue);
                        }
                        else if (FuncKind == Analyze.FuncKind.Abs)
                        {
                            returnValue = Math.Abs(inputValue);
                        }
                        else if (FuncKind == Analyze.FuncKind.Sqrt)
                        {
                            returnValue = Math.Sqrt(inputValue);
                        }

                        return(SimplifyIfRoot(E.NumConst(returnValue), evalSettings, isRootNode));
                    }
                    else
                    {
                        if (FuncKind == Analyze.FuncKind.Sin && firstArg.IsNegative)
                        {
                            return(SimplifyIfRoot(E.Negate(E.Sin(firstArg.SignFree)), evalSettings, isRootNode));
                        }
                        else if (FuncKind == Analyze.FuncKind.Cos && firstArg.IsNegative)
                        {
                            return(SimplifyIfRoot(E.Cos(firstArg.SignFree), evalSettings, isRootNode));
                        }
                        else if (FuncKind == Analyze.FuncKind.Abs && firstArg.IsNegative)
                        {
                            return(SimplifyIfRoot(E.Abs(firstArg.SignFree), evalSettings, isRootNode));
                        }
                    }
                }
                else if (FuncKind == FuncKind.Pow)
                {
                    E firstArg  = evaluatedArgs [0];
                    E secondArg = evaluatedArgs [1];

                    if (firstArg is NumericConstant &&
                        secondArg is NumericConstant)
                    {
                        double a = (firstArg as NumericConstant).Value;
                        double b = (secondArg as NumericConstant).Value;

                        return(SimplifyIfRoot(E.NumConst(Math.Pow(a, b)), evalSettings, isRootNode));
                    }
                    else if (secondArg is NumericConstant)
                    {
                        double b = (secondArg as NumericConstant).Value;

                        if (firstArg.IsNegative)
                        {
                            if (b % 2 == 0)
                            {
                                return(SimplifyIfRoot(E.Pow(firstArg.SignFree, secondArg), evalSettings, isRootNode));
                            }
                            else
                            {
                                return(SimplifyIfRoot(E.Negate(E.Pow(firstArg.SignFree, secondArg)), evalSettings, isRootNode));
                            }
                        }
                    }
                }
            }

            return(SimplifyIfRoot(E.Func(FuncKind, evaluatedArgs.ToArray()), evalSettings, isRootNode));
        }