# Substitution of products of symbolic variables

**URL:** <https://discourse.julialang.org/t/substitution-of-products-of-symbolic-variables/88733>\
**Category:** Modelling & Simulations\
**Tags:** symbolic, symbolics\
**Created:** [October 14, 2022, 2:17pm UTC](https://discourse.julialang.org/t/substitution-of-products-of-symbolic-variables/88733 "2022-10-14T14:17:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![afossa](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@afossa](https://discourse.julialang.org/u/afossa)\
**Post date:** [October 14, 2022, 2:17pm UTC](https://discourse.julialang.org/t/substitution-of-products-of-symbolic-variables/88733/1 "2022-10-14T14:17:45Z")

</div>

I am trying to simplify a symbolic expression by replacing products of symbolic variables, eg. `x*y`, with a single new variable, e.g. `z`, but I cannot succeed in doing it. Any suggestion?

MWE:

```julia
using Symbolics
using SymbolicUtils

@variables x[1:2] z

f = 3.0 + 2.0*(x[1]*x[2]);
g = substitute(f, Dict(x[1]*x[2] => z));

println(g);

```

Actual output: `3.0 + 2.0x[1]*x[2]`  
Expected output: `3.0 + 2.0z`

Thanks!

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [October 14, 2022, 3:36pm UTC](https://discourse.julialang.org/t/substitution-of-products-of-symbolic-variables/88733/2 "2022-10-14T15:36:30Z")

</div>

> <https://github.com/JuliaSymbolics/SymbolicUtils.jl/issues/443>
>
> I expect \`substitute\` to substitute the x\*z in x\*z\*k, but it doesn't? Is there …a workaround? I'm using version 0.19.7.
> Here's a minimal working example:
> 
> \`\`\`
> using SymbolicUtils
> 
> @syms x y z k
> 
> y = x\*z\*k
> 
> eq = 0.0 ~ y
> 
> println(eq.rhs) #got x\*z\*k
> println(substitute(eq.rhs,Dict(x\*z=\>0))) #expecting to get 0, but got x\*z\*k
> \`\`\`

```julia
using SymbolicUtils

@syms a b z

r = @acrule *(a, b) => z
     
f = 3.0 + 2.0b*a
simplify(f, RuleSet([r]))

```

---

<div class="post-metadata">

**Author:** ![afossa](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@afossa](https://discourse.julialang.org/u/afossa)\
**Post date:** [October 17, 2022, 7:07am UTC](https://discourse.julialang.org/t/substitution-of-products-of-symbolic-variables/88733/3 "2022-10-17T07:07:56Z")

</div>

Thank you.  
Is this extensible to arrays of symbolic variables someway?

Ideally, I have a complex expression (a polynomial in the `x[i]`, where `x` is an array of symbolic variables and the coefficients can be either floats or other symbolic variables) and a list of “monomials” that I want to replace, e.g. `c*x[1]*x[2] => c*y[1]`, `d*x[1]^2 => d*y[2]`, etc…

The goal is:

- set to zero all monomials of order greater than 2, e.g. `x[1]^3` or `x[1]*x[2]*x[3]`
- replace all second-order expressions with a simpler one, e.g. `c*x[1]^2 => c*y[1]` or `d*x[1]*x[2] => d*y[2]`

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [October 17, 2022, 11:42am UTC](https://discourse.julialang.org/t/substitution-of-products-of-symbolic-variables/88733/4 "2022-10-17T11:42:06Z")

</div>

Write down all the rules and interpolate when you refer to an array element.  
Example:  
`r = [@acrule(*($(x[1]), $(x[2]), $(x[3])) => 0)]`  
If you work with polynomials the `polynomial_coeffs` function may be useful.  
Example:  
`d, rem = polynomial_coeffs(f, [x...])`
