Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Replace repeated subexpressions in an expression with new symbols
ResourceFunction["SimplifyRepeatedSubexpressions"][expr] returns a list of the expr with repeated subexpressions removed, along with the replacement rules used to rewrite them in simpler form. |
Find the repeated term in the expression (a+b+c)3+(a+b+c)2:
In[1]:= |
![]() |
Out[1]= |
![]() |
Reconstruct the original expression from the decomposed form:
In[2]:= |
![]() |
Out[2]= |
![]() |
Finding many small common subexpressions may not be helpful:
In[3]:= |
![]() |
Out[3]= |
![]() |
Find only the largest with "MinLeafCount":
In[4]:= |
![]() |
Out[4]= |
![]() |
Remove the discriminant from the quadratic solution equations:
In[5]:= |
![]() |
Out[5]= |
![]() |
Reconstruct the original expression from the decomposed form by applying ReplaceAll:
In[6]:= |
![]() |
Out[6]= |
![]() |
In[7]:= |
![]() |
Out[7]= |
![]() |
The FullForm of subexpressions must match; in this case no match is found:
In[8]:= |
![]() |
Out[8]= |
![]() |
This is because the outermost Plus has four arguments and not just the a, b and c:
In[9]:= |
![]() |
Out[9]= |
![]() |
If the input expression contains elements with HoldAll, HoldRest or HoldFirst, the contents will evaluate during the search:
In[10]:= |
![]() |
Out[10]= |
![]() |
Simplify the order three polynomial solution:
In[11]:= |
![]() |
Out[11]= |
![]() |
And the order four polynomial solution:
In[12]:= |
![]() |
Out[12]= |
![]() |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License