protected override Expression VisitTrigonometry(MethodCallExpression methodCall, TrigonometryFunction function) { Expression argument = methodCall.Arguments[0]; switch (function) { case TrigonometryFunction.Sine: { Expression innerDerivative = Visit(argument); return(Arithmeric.Multiply( innerDerivative, Trigonometry.Cosine(argument))); } case TrigonometryFunction.Cosine: { Expression innerDerivative = Visit(argument); return(Arithmeric.Negate( Arithmeric.Multiply( innerDerivative, Trigonometry.Sine(argument)))); } default: throw new NotSupportedException(String.Format("Trigonometric function {0} is not supported.", function.ToString())); } }
protected override Expression VisitMultiply(BinaryExpression binaryExpression) { Expression leftDerivative = Visit(binaryExpression.Left); Expression rightDerivative = Visit(binaryExpression.Right); return(Arithmeric.Add( Arithmeric.Multiply(leftDerivative, binaryExpression.Right), Arithmeric.Multiply(rightDerivative, binaryExpression.Left))); }
protected override Expression VisitParameter(ParameterExpression p) { if (p.Name == _variableName) { return(Arithmeric.One()); } return(Arithmeric.Zero()); }
protected override Expression VisitDivide(BinaryExpression binaryExpression) { Expression leftDerivative = Visit(binaryExpression.Left); Expression rightDerivative = Visit(binaryExpression.Right); return(Arithmeric.Subtract( Arithmeric.Divide(leftDerivative, binaryExpression.Right), Arithmeric.Divide( Arithmeric.Multiply(binaryExpression.Left, rightDerivative), Arithmeric.Multiply(binaryExpression.Right, binaryExpression.Right)))); }
public static Expression Negate(Expression term) { if (Elementary.IsConstantZero(term)) { return(Arithmeric.Zero()); } if (term.NodeType == ExpressionType.Negate) { UnaryExpression unary = (UnaryExpression)term; return(unary.Operand); } return(Expression.Negate(term)); }
protected override Expression VisitPower(BinaryExpression binaryExpression) { Expression leftDerivative = Visit(binaryExpression.Left); Expression rightDerivative = Visit(binaryExpression.Right); return(Arithmeric.Multiply( binaryExpression, Arithmeric.Add( Arithmeric.Multiply( rightDerivative, Exponential.Ln(binaryExpression.Left)), Arithmeric.Divide( Arithmeric.Multiply(binaryExpression.Right, leftDerivative), binaryExpression.Left)))); }
public static Expression Divide(Expression a, Expression b) { if (Elementary.IsConstantZero(a)) { return(Arithmeric.Zero()); } if (Elementary.IsConstantOne(b)) { return(a); } TypeInference type = new TypeInference(a, b); List <Expression> expressions = type.CastToMaxNumericList(); return(Expression.Divide(expressions[0], expressions[1])); }
protected override Expression VisitConstant(ConstantExpression c) { return(Arithmeric.Zero()); }