Function Repository Resource:

AlgebraicReplace

Source Notebook

Replace all occurrences of an algebraic subexpression with a new symbol

Contributed by: Daniel Lichtblau (Wolfram Research)

ResourceFunction["AlgebraicReplace"][expr,reps,repvars]

rewrites expr, replacing each occurrence of an element of reps with the corresponding element of repvars.

ResourceFunction["AlgebraicReplace"][expr,reps,repvars,vars]

rewrites expr, burrowing inside any subexpression that is not a polynomial in vars.

Details

As the name implies, ResourceFunction["AlgebraicReplace"] provides an algebraic form of polynomial replacement as opposed to requiring matching of patterns as used by ReplaceAll and related functions. For example, ordinary replacement of x y by z in the monomial x2y3 will have no effect, whereas AlgebraicReplace will return y z2.
ResourceFunction["AlgebraicReplace"] uses PolynomialQ to determine whether to burrow into subexpressions. If vars is not specified, it will use Variables to determine them.

Examples

Basic Examples (1) 

Algebraically replace xy by a new variable z in a bivariate polynomial:

In[1]:=
ResourceFunction["AlgebraicReplace"][3 x^3 - 7 x^2 y + 4 x y^2 + y^3, x y, z]
Out[1]=

Scope (2) 

AlgebraicReplace works with non-polynomial expressions:

In[2]:=
ResourceFunction["AlgebraicReplace"][
 Exp[3 x^3 - 7 x^2 y + 4 x y^2 + y^3] BesselJ[2, x^2 y^4]/
   Sin[Pi x^4 y - 3], x y, z, {x, y}]
Out[2]=

Use several replacements at once:

In[3]:=
ResourceFunction["AlgebraicReplace"][
 Exp[3 w^4 x^3 - 7 w x^2 y + 4 x y^2 + w^2 y^3] BesselJ[2, w^3 x^2 y + w x^2 y^4]/Sin[Pi w x^4 y - 3 E w x y z], {x w - y, x y}, {z, t}, {w, x, y}]
Out[3]=

Properties and Relations (2) 

ReplaceAll only replaces literal matches:

In[4]:=
ReplaceAll[3 x^3 - 7 x^2 y + 4 x y^2 + y^3 - 2 x^2 + 5 x y + y^2 - 3, x y -> z]
Out[4]=

AlgebraicReplace rewrites all monomials containing powers of xy to have powers of z:

In[5]:=
ResourceFunction["AlgebraicReplace"][
 3 x^3 - 7 x^2 y + 4 x y^2 + y^3 - 2 x^2 + 5 x y + y^2 - 3, x y, z]
Out[5]=

AlgebraicReplace with a set of reducing polynomials polys and replacement variables vars has behavior similar to PolynomialReduce with polys-vars:

In[6]:=
poly = 3 w^4 x^3 - 7 w x^2 y + 4 x y^2 + w^2 y^3;
reducers = {x y, w x^2 - y};
redvars = {z, t};
{ResourceFunction["AlgebraicReplace"][poly, reducers, redvars, {w, x, y}],
 PolynomialReduce[poly, reducers - redvars, {w, x, y, z, t}][[2]]}
Out[7]=

This is not canonical insofar as the reduction is not done with a Gröbner basis, so results can depend on ordering of inputs:

In[8]:=
{ResourceFunction["AlgebraicReplace"][poly, reducers, redvars, {w, x, y}], ResourceFunction["AlgebraicReplace"][poly, Reverse@reducers, Reverse@redvars, {w, x, y}]}
Out[8]=

If we compute a Gröbner basis then the result is canonical for the given variable ordering:

In[9]:=
PolynomialReduce[poly, GroebnerBasis[reducers - redvars, {w, x, y, z, t}], {w, x, y, z, t}][[2]]
Out[9]=

Reversing the reduction polynomials does not change the result of PolynomialReduce when a Gröbner basis is used:

In[10]:=
PolynomialReduce[poly, GroebnerBasis[Reverse[reducers - redvars], {w, x, y, z, t}], {w, x, y, z, t}][[2]]
Out[10]=

Possible Issues (2) 

If the underlying variables are omitted, AlgebraicReplace might not recognize what are the correct ones:

In[11]:=
ResourceFunction["AlgebraicReplace"][Sin[Pi x^4 y - 3], x y, z]
Out[11]=

Specify x and y as variables to get the desired replacement:

In[12]:=
ResourceFunction["AlgebraicReplace"][Sin[Pi x^4 y - 3], x y, z, {x, y}]
Out[12]=

As seen in the "Properties and Relations" examples, AlgebraicReplace does not compute and use a Gröbner basis. Changing the order of the reducer inputs can affect the result.

Version History

  • 1.0.1 – 26 May 2023
  • 1.0.0 – 08 May 2023

Related Resources

Author Notes

This functionality is often requested on MSE, Wolfram Community and older forums as well. It could benefit from a better get-all-variables function. Another task for another day…

License Information