Beispiel #1
0
        public override Expression GetDerivation(string parameter)
        {
            if (this.Operands.Count == 0)
            {
                return(new ConstantExpression(0));
            }
            if (this.Operands.Count == 1)
            {
                return(this.Operands.Single().GetDerivation(parameter));
            }
            Expression exp1 = this.Operands.First();
            Expression exp2 = this.Operands.Count == 2 ? this.Operands[1] : new MultiplyExpression(
                this.Operands.Skip(1).ToList());
            var e = new AddExpression(new List <Expression>
            {
                new MultiplyExpression(new List <Expression> {
                    exp1.GetDerivation(parameter), exp2
                }),
                new MultiplyExpression(new List <Expression> {
                    exp1, exp2.GetDerivation(parameter)
                }),
            }).Normalize();

            return(e);
        }
Beispiel #2
0
        private Expression expr1(int priority)
        {
            Expression retVal;
            Expression right;

            if (priority >= 0)
            {
                retVal = expr1(priority - 1);
            }
            else
            {
                return(factor());
            }

            while (inPriority(lookAhead.TokenType, priority))
            {
                switch (lookAhead.TokenType)
                {
                case '+':
                    accept();
                    right  = expr1(priority - 1);
                    retVal = new AddExpression(new List <Expression> {
                        retVal, right
                    });
                    break;

                case '-':
                    accept();
                    right  = expr1(priority - 1);
                    retVal = new AddExpression(new List <Expression> {
                        retVal, new MultiplyExpression(
                            new List <Expression> {
                            new ConstantExpression(-1),
                            right
                        }
                            )
                    });
                    break;

                case '*':
                    accept();
                    right  = expr1(priority - 1);
                    retVal = new MultiplyExpression(new List <Expression> {
                        retVal, right
                    });
                    break;

                default:
                    return(retVal);
                }
            }
            return(retVal);
        }
Beispiel #3
0
        public override Expression Normalize()
        {
            if (Normalized)
            {
                return(this);
            }

            var middle = operands.Select(_ => _.Normalize()).Where(_ => {
                if (_ is ConstantExpression)
                {
                    if ((_ as ConstantExpression).Constant == 0)
                    {
                        return(false);
                    }
                }
                return(true);
            });

            double constant   = 0;
            var    normalized = new List <Expression>();

            foreach (var operand in middle)
            {
                if (operand is ConstantExpression)
                {
                    constant += operand.Calculate(null);
                }
                else if (operand is AddExpression)
                {
                    foreach (var c in (operand as AddExpression).operands.OfType <ConstantExpression>())
                    {
                        constant += c.Constant;
                    }
                    normalized.AddRange((operand as AddExpression).operands.Where(_ => !(_ is ConstantExpression)));
                }
                else
                {
                    normalized.Add(operand);
                }
            }

            if (constant != 0)
            {
                normalized.Add(new ConstantExpression(constant));
            }

            normalized = normalized.OrderBy(_ => _, CompareExpression.Instance).ToList();

            var        normalized2    = new List <Expression>();
            Expression lastExpression = null;

            foreach (var expression in normalized)
            {
                if (lastExpression != null && !(lastExpression is ConstantExpression) && !(expression is ConstantExpression))
                {
                    var operand1 = GetExpressionParametersMultiplied(lastExpression);
                    var operand2 = GetExpressionParametersMultiplied(expression);
                    if (operand1.Equals(operand2))
                    {
                        var constantd = GetExpressionConstant(lastExpression) + GetExpressionConstant(expression);
                        if (constantd == 0)
                        {
                            lastExpression = null;
                        }
                        else if (constantd == 1)
                        {
                            lastExpression = operand1;
                        }
                        else
                        {
                            var expressionOperands = new List <Expression>();
                            expressionOperands.Add(new ConstantExpression(constantd));
                            if (operand1 is ParameterExpression)
                            {
                                expressionOperands.Add(operand1);
                            }
                            else
                            {
                                expressionOperands.AddRange((operand1 as MultiplyExpression).Operands);
                            }
                            lastExpression            = new MultiplyExpression(expressionOperands);
                            lastExpression.Normalized = true;
                        }
                        continue;
                    }
                }
                if (lastExpression != null)
                {
                    normalized2.Add(lastExpression);
                }
                lastExpression = expression;
            }
            if (lastExpression != null)
            {
                normalized2.Add(lastExpression);
            }

            if (normalized2.Count() == 1)
            {
                return(normalized2.Single());
            }
            if (normalized2.Count() == 0)
            {
                return(new ConstantExpression(0));
            }
            var exp = new AddExpression(normalized2);

            exp.Normalized = true;
            return(exp);
        }