Function Repository Resource:

AlgebraicReplace (1.0.0) current version: 1.0.1 »

Source Notebook

Replace with a new symbol all occurrences of an algebraic subexpression in a given mathematical expression

Contributed by: Daniel Lichtblau

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

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

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

Rewrite 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 (1) 

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]=

Properties and Relations (2) 

ReplaceAll only replaces literal matches:

In[3]:=
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[3]=

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

In[4]:=
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[4]=

Possible Issues (2) 

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

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

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

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

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