Skip to main content

Modifying expressions

Nancy.Expressions allows to apply equivalences to (sub-parts of) expressions that may reduce their computational complexity. Equivalences can be either user-defined ones or well-known ones (already reported in literature and implemented in the library). Furthermore, the user can also replace parts of expressions by specifying which sub-expression needs to be substituted (replacing all occurences) or using its position (replace only one occurence).

Applying a simplification​

The following code cell constructs the DNC expression (f‾⊗g)⊘f‾(\overline{f} \otimes g) \oslash \overline{f}.

var f = Expressions.FromCurve(
curve: new Curve(<args>),
name: "f");
var g = Expressions.FromCurve(
curve: new Curve(<args>),
name: "g");
var f_sac = f.SubAdditiveClosure();
var conv = Expressions.Convolution(f_sac, g);
var deconv = Expressions.Deconvolution(conv, f_sac);

The expression (f‾⊗g)⊘f‾(\overline{f} \otimes g) \oslash \overline{f} is equivalent to f‾⊗g\overline{f} \otimes g which allows to avoid completely the deconvolution operation. Therefore, it is possible to manually simplify the expression by using the method ApplyEquivalence and the class DeconvAndSubAdditiveClosure which represents the simplification:

var deconv_sac_equivalence = new DeconvAndSubAdditiveClosure();
var deconv_eq = deconv.ApplyEquivalence(deconv_sac_equivalence);

Replacing all occurences​

Substitute all occurences of ff with f+hf + h, and re-apply the previous equivalence:

Replace all occurrences, notebook cell and output.

Replacement at position​

Substitute the deconvolution with a convolution, and re-apply the previous equivalence (which does not hold in this case). Another equivalence can be applied in this case: f⊗f=ff \otimes f = f, when ff is subadditive, as shown in the following screen:

Replace at position, notebook cell and output.

Other example: replace the right operand of the deconvolution with hh from the initial expression. The right operand is specified as first argument of the method ReplaceByPosition writing deconv.RootPosition().RightOperand() (whose correctness is checked only at runtime, therefore the user should take care of its consistency during development).

Replace at position (with expression position), notebook cell and output.