Wolfram Research

Function Repository Resource:

MultiReplace (2.0.0) current version: 2.2.3 »

Source Notebook

Perform replacements and also give positions at which they occur

Contributed by: Nikolay Murzin

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.

Details and Options

ResourceFunction["MultiReplace"] returns an association with keys containing a rule index and list of positions of a tuple/subset of subexpressions where the corresponding rule was applied.
If one rule is given, keys contain positions only.
Association values are results of applying ReplaceList at corresponding positions, so for a given position multiple results of substitution are possible.
The left-hand side of a rule can be a list or a custom head to replace tuples/subsets of subexpressions, similar to SequenceReplace and SubsetReplace but for all positions.
When a tuple is specified as a left-hand side, replacement applies at multiple positions where all elements of a tuple match, meaning each position can be replaced multiple times.
In contrast, with "Subsets"True option, multiple positions are considered where elements of a subset match, i.e. only disjoint positions are considered.
If a rule returns a list (or a head) of expressions, they are substituted for the corresponding tuple/subset positions on the left-hand side.
ResourceFunction["MultiReplace"] takes the same options as the resource function SubexpressionPositions for subexpression selection.
ResourceFunction["MultiReplace"] takes the following options:
"Subsets"Falsewhether to look for tuples or subsets
Method"Substitution"substitution strategy to use: "Susbtitution", "Cosubstitution" or "Bisubstitution"

Examples

Basic Examples (3) 

Apply a rule at each possible position of an expression:

In[1]:=
ResourceFunction["MultiReplace"][f[x, g[y]], a_ :> h[a]]
Out[1]=

Apply multiple rules:

In[2]:=
ResourceFunction["MultiReplace"][f[x, g[y]], {a_ :> h[a], b_ -> 1}]
Out[2]=

Apply tuple replacement rule:

In[3]:=
ResourceFunction["MultiReplace"][{x, y, x, y}, {x, y} -> {A, B}]
Out[3]=

Scope (4) 

Tuples replacement is being spliced in the order of matched elements:

In[4]:=
ResourceFunction["MultiReplace"][{a, b}, {b, a} :> {XXXX, YYYY}]
Out[4]=

Splicing continues after the last element:

In[5]:=
ResourceFunction["MultiReplace"][{a, b}, {b, a} :> {XXXX, YYYY}]
Out[5]=
In[6]:=
ResourceFunction["MultiReplace"][{a, b}, {b, a} :> {XXXX, YYYY, ZZZZ}]
Out[6]=

A single element can be matched more than once:

In[7]:=
ResourceFunction["MultiReplace"][{a, b}, {a, a, a} :> XXXX]
Out[7]=

Specify a level:

In[8]:=
ResourceFunction["MultiReplace"][{a, b, f[c]}, x_ :> g[x], {1}]
Out[8]=

Use a custom head to replace tuples within it:

In[9]:=
ResourceFunction["MultiReplace"][f[a, b], f[b, a] :> f[XXX, YYY, ZZZZ], All, f]
Out[9]=

Options (4) 

Heads (1) 

Ignore heads:

In[10]:=
ResourceFunction["MultiReplace"][f[x], a_ :> g[a], Heads -> False]
Out[10]=

Subsets (1) 

Use subsets instead of tuples:

In[11]:=
ResourceFunction["MultiReplace"][{a, b}, {a, a} :> {XXXX, YYYY}, "Subsets" -> True]
Out[11]=
In[12]:=
ResourceFunction["MultiReplace"][{a, b}, {a, a} :> {XXXX, YYYY}, "Subsets" -> False]
Out[12]=

Method (2) 

Three substitution strategies are supported, based on the direction of pattern unification:

In[13]:=
ResourceFunction["MultiReplace"][{f[a, b], f[y_, x_], f[a, x_]}, f[y_, b] -> c, 1, Method -> "Substitution"]
Out[13]=
In[14]:=
ResourceFunction["MultiReplace"][{f[a, b], f[y_, x_], f[a, x_]}, f[y_, b] -> c, 1, Method -> "Cosubstitution"]
Out[14]=
In[15]:=
ResourceFunction["MultiReplace"][{f[a, b], f[y_, x_], f[a, x_]}, f[y_, b] -> c, 1, Method -> "Bisubstitution"]
Out[15]=

Get all tuple bisubstitutions:

In[16]:=
ResourceFunction["MultiReplace", ResourceVersion->"2.0.0"][{f[a, x_], f[b, y_], f[c, z_], g[x_, y_, z_]}, {f[y_, 1], f[z_, 2]} -> {y, z}, 1, Method -> "Bisubstitution"]
Out[16]=

Properties and Relations (2) 

Replacement is done in all possible ways similar to ReplaceList but at multiple positions:

In[17]:=
ReplaceList[f[a, f[b, c]], f[___, x_, ___] :> f[x]]
Out[17]=
In[18]:=
ResourceFunction["MultiReplace"][f[a, f[b, c]], f[___, x_, ___] :> f[x]]
Out[18]=

SequenceReplace and SubsetReplace replace all the matches and destroy subexpressions, while MultiReplace does all possible single replacements and preserves subexpressions:

In[19]:=
SequenceReplace[{a, b, x, x, a, c}, {a, e_} :> f[e]]
Out[19]=
In[20]:=
SubsetReplace[{a, b, x, x, a, c}, {a, e_} :> f[e]]
Out[20]=

Version History

  • 2.2.3 – 05 January 2024
  • 2.2.2 – 27 September 2023
  • 2.2.1 – 22 February 2023
  • 2.2.0 – 03 February 2023
  • 2.1.0 – 15 June 2022
  • 2.0.0 – 01 February 2022
  • 1.0.0 – 08 November 2021

License Information