Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Perform replacements and also give positions at which they occur
ResourceFunction["MultiReplace"][expr, rule] returns all possible subexpressions of expr after application of rule with information about each replacement. | |
ResourceFunction["MultiReplace"][expr, {rule1,rule2,…}] returns result after applying a list of rules. | |
ResourceFunction["MultiReplace"][expr,{x1,x2,…}→{y1,y2,…}] use a list of patterns to replace subexpression tuples/subsets. | |
ResourceFunction["MultiReplace"][expr,rules,level] specify a level spec. | |
ResourceFunction["MultiReplace"][expr,rules,level, head] uses a custom head to specify subexpression tuples/subsets. | |
ResourceFunction["MultiReplace"][expr,subexpr,…] return subexpression matches without performing substitution. |
| "Mode" | "Tuples" | how to match multiple subexpressions: "Tuples", "OrderedTuples", "Subsets" or "OrderlessSubsets" |
| "PatternSubstitutions" | False | whether to include pattern substitutions |
| "ReturnMatches" | False | return subexpression matches by ignoring right-hand-side of rules if present |
| Method | "Substitution" | substitution strategy to use: "Susbtitution", "Cosubstitution" or "Bisubstitution" |
| Heads | True | whether to match heads of expressions and their parts |
Apply a rule at each possible position of an expression:
| In[1]:= |
| Out[1]= |
Apply multiple rules:
| In[2]:= |
| Out[2]= | ![]() |
Apply tuple replacement rule:
| In[3]:= |
| Out[3]= |
Subexpressions are being spliced in the order of matched elements:
| In[4]:= |
| Out[4]= |
Splicing continues after the last element:
| In[5]:= |
| Out[5]= |
Single element can be matched more than once:
| In[6]:= |
| Out[6]= |
Specify a level:
| In[7]:= |
| Out[7]= |
Specify an explicit list of positions to replace at:
| In[8]:= |
| Out[8]= |
Use a custom head for designating tuples:
| In[9]:= |
| Out[9]= |
| In[10]:= |
| Out[10]= |
| In[11]:= |
| Out[11]= |
Specify a matching condition:
| In[12]:= |
| Out[12]= |
Ignore heads:
| In[13]:= |
| Out[13]= |
Change the mode of matching multiple subexpressions:
| In[14]:= |
| Out[14]= |
| In[15]:= |
| Out[15]= |
| In[16]:= |
| Out[16]= |
| In[17]:= |
| Out[17]= |
Three substitution strategies are supported, based on the direction of pattern unification:
| In[18]:= |
| Out[18]= |
| In[19]:= |
| Out[19]= |
| In[20]:= |
| Out[20]= |
Get all tuple bisubstitutions:
| In[21]:= |
| Out[21]= |
Return corresponding matches only without performing a substitution (note HoldForm wrapping of individual matched subexpressions to prevent evaluation):
| In[22]:= |
| Out[22]= | ![]() |
This is also equivalent to just dropping the right-hand-side of a rule:
| In[23]:= |
| Out[23]= | ![]() |
Include all corresponding pattern substitutions:
| In[24]:= |
| Out[24]= |
Cosubstitution and Bisubstitution include substitutions of patterns from the expression to subexpressions from left-hand-side of the rule:
| In[25]:= |
| Out[25]= |
| In[26]:= |
| Out[26]= |
Replacement is done in all possible ways similar to ReplaceList but at multiple positions:
| In[27]:= |
| Out[27]= |
| In[28]:= |
| Out[28]= |
SequenceReplace and SubsetReplace replace all the matches and destroy subexpressions, while MultiReplace does all possible single replacements and preserves subexpressions:
| In[29]:= |
| Out[29]= |
| In[30]:= |
| Out[30]= |
| In[31]:= |
| Out[31]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License