Parameter Substitution within Expression Trees

This article describes how to substitute a parameter within an Expression tree by another expression, similar to how one can substitute an argument within a mathematical function by a different function

Introduction

This article describes a method to transform a System.Linq.Expression object by substituting some of its parameters by different expressions. This technique can be applied, e.g., when one uses lambda expressions to describe mathematical functions - it will allow generating new functions by argument substitution technique. Correspondingly, it can be useful in creating plots, 2-D and 3-D graphics.

This article will only show how to apply this technique to generating new functions and creating Silverlight plots of these functions. A future article will describe applying this technique to 2-D graphics and animations.

Background

There is a very common problem in mathematics. Suppose, we have a function y = f(x). There is also another function x = g(t); we can combine these two functions to obtain dependency y = (f*g)(t) where f*g is a combined function.

This problem can be generalized for functions with multiple arguments: suppose we have a function y = F(x<sub>1</sub>, x<sub>2</sub>, ..., x<sub>n</sub>). Suppose there is also a function that makes one of its arguments xj dependent on a set of other arguments: x<sub>j</sub> = G(t<sub>1</sub>, ..., t<sub>m</sub>). The functions F and G can be combined into the function (F*G) operating on the same arguments as function F, except that argument xj will be replaced by t<sub>1</sub>, ..., t<sub>m</sub>:y = (F*G)(x<sub>1</sub>, ..., x<sub>j-1</sub>, t<sub>1</sub>, ..., t<sub>m</sub>, x<sub>j+1</sub>, ... ,x<sub>n</sub>).

In .NET, mathematical functions can be described by lambda expressions or corresponding expression trees. The problem we are trying to solve is best shown by an example: suppose we have an expression corresponding to e.g. some polynomial:

Suppose we want to create and use other expressions that represent the arbitrary shifts of the expression above along variable x. In a straightforward way, we can simply create another expression dependent on 2 arguments: x and shift:

This however is, not a very pleasant expression to write. Furthermore, if more degrees of freedom are required, e.g., we need to have arbitrary dilations (making the function thinner or wider along axis x) in addition to arbitrary shifts, the expression will become increasingly more complex.

Using the approach described in this article, all one has to do is to write the following code in order to obtain the polynomialExpressionWithShifts:

The extension method Substitute will replace the ParameterExpression x in polynomialExpression with ParameterExpressions x and shift and modify the code of the expression replacing any occurrence of variable 'x' with 'x - shift' (the body of shiftExpression).

To add the dilation functionality, all we have to do is to write the following code:

The approach is generic enough to handle multiple variables also in the original expression. This is the reason for specifying the variable name in the extension function Substitute - we only replace a variable whose name is passed to the Expression as one of its parameters.

Rebuilding and running SimplePlot project will then result in the following plot:

The original function sin(-x) is shown in red. The function that was a result of substituting 2x + 3 in place of x into the original function is shown in blue. Finally, we plot the combined function sin(-(2x + 3)) in a thinner yellow to show that it does match the result of the substitution (in thicker blue).

Now, if we comment out the two lines related to the sinusoidal example and uncomment the lines related to a parabola example as shown below:

and then rebuild and re-run the project, we'll have the following plot displayed:

As in the first example, the original parabola in red corresponds to x2 function. Thick blue line is a plot of the result of substituting x<sup>2</sup> - 2 for x into the first function. Thin yellow is a plot of the (x<sup>2</sup> - 2)<sup>2</sup> function just to show that it matches the result of the substitution.

ExpressionModifierTester project can also be used to learn how to use the substitution functionality. Below the code for TestSimpleFunction is shown:

One can see that our original expression is t<sup>3</sup> + 20. We substitute t<sup>2</sup> for t. The resulting expression corresponds to t<sup>6</sup> + 20. Then we test that the resulting expression indeed produces correct values.

TestComplexPolynomial has similar functionality except that its original function is much more complex.

Review of the Core Substitution Functionality

The core functionality is located within two files: ExpressionVariableSubstituteHelper.cs and SubstExpressionVisitor.cs which contain classes that have the same names. The files are located under ExpressionModifier project. There are links to the same files under SilverlightExpressionModifier project.

Static class ExpressionVariableSubstituteHelper contains the Substitute extension function. It finds the ParameterExpression corresponding to the parameter to be substituted within the main function (it throws an exception if it cannot find such parameter by name). It also does some other checks, e.g. it checks that with the possibly expanded list of parameters after substitution, there will be no name clashes. Then it calls the Visit function of SubstExpressionVisitor class to actually create the resulting Expression tree.

SubstExpressionVisitor class is derived from ExpressionVisitor with three functions overridden: VisitUnary, VisitMethodCall and VisitBinary. Perhaps I overlooked some other Expressions that might have ParameterExpression objects as children and then, perhaps, the code might break on some complex Expression, but most of the Expressions seem to be covered by the above three functions. Each of the above three functions is implemented to check if one or more of its children is a ParameterExpression that we are substituting, and if it is, it returns a new Expression of the same type that replaces that ParameterExpression children with the body of the substituting Expression.

Points of Interest

This new way of manipulating the Expression trees can become very helpful in math intensive projects e.g. in 2-D and 3-D graphics and animations. This article described the application of this approach to generating plots for different functions. My next article will use this approach for 2-D animations along arbitrary paths.

Share

About the Author

I have 15 years of experience developing enterprise software, starting from C++ and Java on UNIX and moving towards C# on Windows platforms. I am fascinated by the new .NET technologies especially WPF, Silverlight and LINQ. Recently I decided to make a move and start my own contracting consulting and mentoring company AWebPros. I can be contacted via my web site awebpros.com or through my blog at nickssoftwareblog.com

The thing is that I am not very much into the theory of lambda calculus. I learned about lambdas from programming C#. Even though it is fascinating, but for me the article is more about how to solve a concrete problem (substitution of a parameter within an Expression object) by means of a concrete language (C# System.Linq.Expression functionality) rather then an incursion into a general theory of lambda calculus. I'll take a look at your references though, and see if I can understand them enough to mention this concept in the article.Thanks for bringing it up!