Class ExpressionDerivation<T>
- Namespace
- Utils.Mathematics.Expressions
- Assembly
- Utils.Mathematics.dll
Provides single-variable symbolic differentiation for LINQ expression trees.
public class ExpressionDerivation<T> : ExpressionTransformer where T : IFloatingPoint<T>
Type Parameters
T
- Inheritance
-
ExpressionDerivation<T>
- Derived
- Inherited Members
- Extension Methods
Constructors
ExpressionDerivation(ParameterExpression, bool)
Initializes a new instance of the ExpressionDerivation<T> class that differentiates
with respect to a specific ParameterExpression instance rather than a name. Unlike
the name-based constructor, this identifies the differentiation variable unambiguously even when
parameter's Name is null or
shared by another, unrelated parameter in the same lambda (see TODO-2026-07-11-pass4.md item #47).
public ExpressionDerivation(ParameterExpression parameter, bool allowNumericalFallback = false)
Parameters
parameterParameterExpressionThe exact parameter instance to differentiate against.
allowNumericalFallbackbool
ExpressionDerivation(string, bool)
Initializes a new instance of the ExpressionDerivation<T> class for the specified parameter.
public ExpressionDerivation(string parameterName, bool allowNumericalFallback = false)
Parameters
parameterNamestringName of the variable with respect to which derivatives are computed.
allowNumericalFallbackboolSee AllowNumericalFallback. Defaults to false: unknown methods fail explicitly instead of silently falling back to a numerical approximation.
Properties
AllowNumericalFallback
Gets whether unknown single-argument methods (with no registered symbolic derivative rule) may be differentiated through a centered finite-difference approximation (see DeriveUnknownMethodCall(MethodCallExpression, Expression[])) instead of failing with NotSupportedException. This is opt-in and defaults to false, since the fallback turns an exact symbolic derivation into a numerical runtime approximation that evaluates the source method twice per derivative evaluation — only enable it for methods known to be pure (no side effects) and reasonably smooth near the evaluation point.
public bool AllowNumericalFallback { get; }
Property Value
ParameterName
Gets the name of the parameter that will be considered the differentiation variable.
public string ParameterName { get; }
Property Value
Methods
Add(BinaryExpression, Expression, Expression)
Applies the sum rule to differentiate the addition of two expressions.
[ExpressionSignature(ExpressionType.Add)]
protected Expression Add(BinaryExpression e, Expression left, Expression right)
Parameters
eBinaryExpressionAddition expression to transform.
leftExpressionLeft operand of the addition.
rightExpressionRight operand of the addition.
Returns
- Expression
The sum of the derivatives of the operands.
Constant(ConstantExpression, object)
Computes the derivative of a constant expression, which always yields zero.
[ExpressionSignature(ExpressionType.Constant)]
protected Expression Constant(ConstantExpression e, object value)
Parameters
eConstantExpressionSource constant expression.
valueobjectValue carried by the constant.
Returns
- Expression
A constant expression representing zero.
Convert(UnaryExpression, Expression)
Differentiates the wrapped operand and re-applies the conversion's declared result type when the derivative's type does not already match it, so the derivative expression stays type-consistent with the original conversion node.
[ExpressionSignature(ExpressionType.Convert)]
protected Expression Convert(UnaryExpression e, Expression operand)
Parameters
eUnaryExpressionConversion expression to transform.
operandExpressionWrapped expression operand.
Returns
- Expression
The derivative of the wrapped operand, converted back to
e.Typeif needed.
ConvertChecked(UnaryExpression, Expression)
Differentiates the wrapped operand through a checked conversion. Checked conversions exist specifically to guard against narrowing/overflow, which has no well-defined symbolic derivative; only a trivial same-type checked conversion is passed through, anything else is rejected instead of silently stripped.
[ExpressionSignature(ExpressionType.ConvertChecked)]
protected Expression ConvertChecked(UnaryExpression e, Expression operand)
Parameters
eUnaryExpressionChecked conversion expression to transform.
operandExpressionWrapped expression operand.
Returns
- Expression
The derivative of the wrapped operand when the checked conversion is a same-type no-op.
Exceptions
- NotSupportedException
Thrown when the checked conversion actually changes the type (a narrowing or otherwise non-trivial conversion), since differentiating through it is not well-defined.
Cos(MethodCallExpression, Expression)
Differentiates a cosine call expression by applying the chain rule and negating the sine.
[ExpressionCallSignature(typeof(double), "Cos")]
protected Expression Cos(MethodCallExpression e, Expression operand)
Parameters
eMethodCallExpressionCall expression describing the cosine invocation.
operandExpressionOperand of the cosine call.
Returns
- Expression
The derivative of the cosine expression.
Derivate(LambdaExpression)
Builds the derivative of the provided lambda expression with respect to the configured parameter.
public Expression Derivate(LambdaExpression e)
Parameters
eLambdaExpressionLambda expression to differentiate.
Returns
- Expression
The simplified derivative expression.
Exceptions
- InvalidOperationException
Thrown when no parameter named ParameterName is found in
e, or when more than one distinct parameter shares that name (an ambiguous match that cannot be resolved by name alone).
Derivate(LambdaExpression, out bool)
Builds the derivative of the provided lambda expression with respect to the configured parameter, additionally reporting whether the result is an exact symbolic derivative or includes at least one finite-difference approximation.
public Expression Derivate(LambdaExpression e, out bool isExact)
Parameters
eLambdaExpressionLambda expression to differentiate.
isExactbooltrue when every part of the result was produced by an exact symbolic rule; false when at least one sub-expression fell back to the numerical finite-difference approximation (only possible when AllowNumericalFallback is true, since otherwise an unknown method throws instead of approximating).
Returns
- Expression
The simplified derivative expression.
Exceptions
- InvalidOperationException
Thrown when this instance was constructed with a parameter name and no parameter named ParameterName is found in
e, or more than one distinct parameter shares that name (an ambiguous match that cannot be resolved by name alone). When this instance was instead constructed with an exact ParameterExpression instance, thrown when that instance is not one ofe's declared parameters.
Divide(BinaryExpression, Expression, Expression)
Applies the quotient rule to differentiate the division of two expressions.
[ExpressionSignature(ExpressionType.Divide)]
protected Expression Divide(BinaryExpression e, Expression left, Expression right)
Parameters
eBinaryExpressionDivision expression to transform.
leftExpressionDividend expression.
rightExpressionDivisor expression.
Returns
- Expression
The derivative computed using the quotient rule.
Exp(MethodCallExpression, Expression)
Differentiates an exponential call expression by applying the chain rule.
[ExpressionCallSignature(typeof(double), "Exp")]
protected Expression Exp(MethodCallExpression e, Expression operand)
Parameters
eMethodCallExpressionCall expression describing the exponential invocation.
operandExpressionOperand of the exponential call.
Returns
- Expression
The derivative of the exponential expression.
FinalizeExpression(Expression, Expression[])
Handles method-call derivatives that are not covered by specific derivative rules. For single-argument double functions, this applies a centered finite-difference approximation and multiplies by the inner derivative.
protected override Expression FinalizeExpression(Expression e, Expression[] parameters)
Parameters
eExpressionExpression currently being finalized.
parametersExpression[]Prepared child expressions.
Returns
- Expression
A derivative expression when supported.
Log10(MethodCallExpression, Expression)
Differentiates a base-10 logarithm call expression.
[ExpressionCallSignature(typeof(double), "Log10")]
protected Expression Log10(MethodCallExpression e, Expression operand)
Parameters
eMethodCallExpressionCall expression describing the logarithm invocation.
operandExpressionOperand of the logarithm call.
Returns
- Expression
The derivative of the base-10 logarithm expression.
LogMath(MethodCallExpression, Expression)
Differentiates a natural logarithm call expression.
[ExpressionCallSignature(typeof(double), "Log")]
protected Expression LogMath(MethodCallExpression e, Expression operand)
Parameters
eMethodCallExpressionCall expression describing the logarithm invocation.
operandExpressionOperand of the logarithm call.
Returns
- Expression
The derivative of the logarithm expression.
Multiply(BinaryExpression, Expression, Expression)
Applies the product rule to differentiate the multiplication of two expressions.
[ExpressionSignature(ExpressionType.Multiply)]
protected Expression Multiply(BinaryExpression e, Expression left, Expression right)
Parameters
eBinaryExpressionMultiplication expression to transform.
leftExpressionLeft operand of the multiplication.
rightExpressionRight operand of the multiplication.
Returns
- Expression
The derivative computed using the product rule.
Negate(UnaryExpression, Expression)
Applies the derivative to a negated expression following the chain rule.
[ExpressionSignature(ExpressionType.Negate)]
protected Expression Negate(UnaryExpression e, Expression operand)
Parameters
eUnaryExpressionNegation expression to transform.
operandExpressionOperand of the negation.
Returns
- Expression
The derivative of the negated operand.
Parameter(ParameterExpression)
Computes the derivative of a parameter expression by comparing it to the differentiation variable.
[ExpressionSignature(ExpressionType.Parameter)]
protected Expression Parameter(ParameterExpression e)
Parameters
eParameterExpressionSource parameter expression.
Returns
- Expression
One when the parameter matches the configured variable; otherwise zero.
Power(BinaryExpression, Expression, ConstantExpression)
Differentiates a power expression with a constant exponent using the standard power rule.
[ExpressionSignature(ExpressionType.Power)]
protected Expression Power(BinaryExpression e, Expression left, ConstantExpression right)
Parameters
eBinaryExpressionPower expression being transformed.
leftExpressionBase expression.
rightConstantExpressionConstant exponent.
Returns
- Expression
The derivative computed by the power rule.
Power(BinaryExpression, Expression, Expression)
Differentiates a power expression that has an expression exponent by applying logarithmic differentiation.
[ExpressionSignature(ExpressionType.Power)]
protected Expression Power(BinaryExpression e, Expression left, Expression right)
Parameters
eBinaryExpressionPower expression being transformed.
leftExpressionBase expression.
rightExpressionExponent expression.
Returns
- Expression
The derivative computed through logarithmic differentiation.
Remarks
Logarithmic differentiation of f(x)^g(x) requires f(x) > 0: this is not an
artificial narrowing introduced by using Log(f) here, since the original expression itself
is already only real-valued (rather than NaN) for a negative base when the
exponent is non-integer, and the exponent is a general (non-constant) expression here. Unlike the
integration rules for 1/x or tan(x) (which use Log(Abs(x)) because the
original expressions they replace remain well-defined for negative inputs), replacing
Log(f) with Log(Abs(f)) here would not extend the domain of a valid symbolic
derivative — it would just produce a different, unrelated formula.
Sin(MethodCallExpression, Expression)
Differentiates a sine call expression by applying the chain rule.
[ExpressionCallSignature(typeof(double), "Sin")]
protected Expression Sin(MethodCallExpression e, Expression operand)
Parameters
eMethodCallExpressionCall expression describing the sine invocation.
operandExpressionOperand of the sine call.
Returns
- Expression
The derivative of the sine expression.
Subtract(BinaryExpression, Expression, Expression)
Applies the difference rule to differentiate the subtraction of two expressions.
[ExpressionSignature(ExpressionType.Subtract)]
protected Expression Subtract(BinaryExpression e, Expression left, Expression right)
Parameters
eBinaryExpressionSubtraction expression to transform.
leftExpressionLeft operand of the subtraction.
rightExpressionRight operand of the subtraction.
Returns
- Expression
The difference of the derivatives of the operands.
Tan(MethodCallExpression, Expression)
Differentiates a tangent call expression using the identity d/dx tan(f) = f'(x) / cos²(f(x)).
[ExpressionCallSignature(typeof(double), "Tan")]
protected Expression Tan(MethodCallExpression e, Expression operand)
Parameters
eMethodCallExpressionCall expression describing the tangent invocation.
operandExpressionOperand of the tangent call.
Returns
- Expression
The derivative of the tangent expression.