## 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`_{1}, x_{2}, ..., x_{n})

. Suppose there is also a function that makes one of its arguments x_{j} dependent on a set of other arguments: `x`_{j} = G(t_{1}, ..., t_{m})

. The functions `F `

and `G `

can be combined into the function `(F*G)`

operating on the same arguments as function `F`

, except that argument x_{j} will be replaced by `t`_{1}, ..., t_{m}

:

`y = (F*G)(x`_{1}, ..., x_{j-1}, t_{1}, ..., t_{m}, x_{j+1}, ... ,x_{n})

.

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:

Expression<Func<double, double>> polynomialExpression =
x => x*x*x*x + 3*x*x*x + 5*x*x + 10*x + 20;

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`

:

Expression<Func<double, double>> polynomialExpressionWithShifts =
(x, shift) =>
(x-shift)*(x-shift)*(x-shift)*(x-shift) +
3*(x-shift)*(x-shift)*(x-shift) +
5*(x-shift)*(x-shift) +
10*(x-shift) +
20;

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`

:

Expression<Func<double, double>> shiftExpression = (x, shift) => x - shift
Expression<Func<double, double>> polynomialExpressionWithShifts =
(Expression<Func<double, double>>)polynomialExpression.Substitute
("x", shiftExpression);

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:

Expression<Func<double, double, double>> dilationAndShiftExpression =
(x, dilationFactor, shift) = x * dilationFactor - shift
Expression<Func<double, double, double>> polynomialExpressionWithShifts =
(Expression<Func<double, double, double>>)polynomialExpression.Substitute
("x", dilationAndShiftExpression);

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.

## Using the Code

The attached code consists of the following four projects:

`ExpressionModifier `

- Contains the core functionality - it produces .NET 4.0 DLL
`ExpressionModifierTester `

- Contains Microsoft tests for `ExpressionModifier `

functionality
`SilverlightExpressionModifier `

- The same as `ExpressionModifier`

, but it is a Silverlight 4.0 project
`SimplePlots `

- Silverlight 4.0 project that plots different expressions - its purpose is to give some visual examples of parameter substitutions.

The usage examples can be taken from `SimplePlot `

and `ExpressionModifierTester `

projects.

One should set `SimplePlot `

as the startup project of the solution.

The substitution within `SimplePlot `

project takes place within the function `MainPage.PlotMainAndModified`

:

public void PlotMainAndModified
(
Expression<Func<double, double>> mainExpression,
Expression<Func<double, double>> substExpression,
double delta = 0.1,
double pointSize = POINT_SIZE
)
{
AddPlot(Colors.Red, delta, mainExpression.Compile(), pointSize);
LambdaExpression modifiedExpression =
mainExpression.Substitute("x", substExpression);
AddPlot
(
Colors.Blue,
delta,
modifiedExpression.Compile() as Func<double, double>,
pointSize
);
}

This function plots the original function in red and the resulting function in blue.

Make sure that the sinusoidal example lines are uncommented at the end of `MainPage.MainPage `

constructor while the parabola example lines are commented out as below:

PlotMainAndModified(x => Math.Sin(-x), x => x * 2 + 3, 0.01, 10d);
AddPlot(Colors.Yellow, 0.01, x =>Math.Sin(-(x * 2 + 3)), 3d);

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:

PlotMainAndModified(x => x * x, x => x * x - 2, 0.01, 10d);
AddPlot(Colors.Yellow, 0.01, x => (x * x - 2) * (x * x - 2), 3d);

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 x^{2} function. Thick blue line is a plot of the result of substituting `x`^{2} - 2

for `x `

into the first function. Thin yellow is a plot of the `(x`^{2} - 2)^{2}

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:

[TestMethod]
public void TestSimpleFunction()
{
Expression<Func<double, double>>
originalExpression = t => t * t * t + 20;
Expression<Func<double, double>> substituteExpression = t => t * t;
LambdaExpression modifiedExpression =
originalExpression.Substitute("t", substituteExpression);
Func<double, double> modifiedFunction =
(Func<double, double>) modifiedExpression.Compile();
Assert.AreEqual((int) modifiedFunction(1), 21);
Assert.AreEqual((int) modifiedFunction(2), 2 * 2 * 2 * 2 * 2 * 2 + 20);
}

One can see that our original expression is `t`^{3} + 20

. We substitute `t`^{2}

for `t`

. The resulting expression corresponds to `t`^{6} + 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.

## History

- 6
^{th} January, 2011: Initial post