Wolfram Research

Function Repository Resource:

FromRecursiveRewrite

Source Notebook

Recover expressions using RecursiveRewrite generated rules

Contributed by: Enrique Zeleny

ResourceFunction["FromRecursiveRewrite"][expr,rules,f]

recovers expression expr using rules generated by RecursiveRewrite and applies the function f to subexpressions.

Details and Options

Any string can be used in RewriteSymbol option.

Examples

Basic Examples

Choose an expression:

In[1]:=
expr = (#1[[1, 2]] &) /@ Solve[8 x^4 + 42 x^3 + 64 x^2 + 71 x + 27 == 0, x, Quartics -> True];

Get a list of replacements to rewrite the expression:

In[2]:=
optexpr = ResourceFunction["RecursiveRewrite"][expr]
Out[2]=

Choose a subexpression:

In[3]:=
expr2 = Nest[# /. optexpr &, "c58", 6][[1]]
Out[3]=

Hover the mouse over gray boxes to see the values of subexpressions:

In[4]:=
ResourceFunction["FromRecursiveRewrite"][expr2, optexpr, Tooltip[Framed[#1, Background -> GrayLevel[.95]], #2] &]
Out[4]=

Hover the mouse over gray boxes to see the byte count of expressions:

In[5]:=
ResourceFunction["FromRecursiveRewrite"][expr2, optexpr, Mouseover[#1, Framed[Style[ByteCount[#2], Red], FrameStyle -> Red]] &]
Out[5]=

Get information of expressions:

In[6]:=
Dynamic[Column[{ResourceFunction["FromRecursiveRewrite"][expr2, optexpr, Annotation[#1, Row[{"Length:", Length[#2], "LeafCount:", LeafCount[#2], "Depth:", Depth[#2]}, Spacer[4]], "Mouse"] &], MouseAnnotation[]}, Dividers -> All]]
Out[6]=

See the tree structure of an expression:

In[7]:=
TreeForm[ResourceFunction["FromRecursiveRewrite"][expr2, optexpr, #1 &]]
Out[7]=

Replace expressions with buttons that replace the values of the subexpressions when clicked:

In[8]:=
newexpr = Nest[# /. optexpr &, "c58", 5][[1]]
Out[8]=
In[9]:=
DynamicModule[{r = {}}, Dynamic[ResourceFunction["FromRecursiveRewrite"][
   newexpr //. Flatten[Cases[optexpr, _[#, _]] & /@ r], optexpr, Button[#1, (r = Flatten@Union@AppendTo[r, StringDelete[First[#1], "\""]]) &, ImageSize -> Medium] &]]]
Out[9]=

Options

RewriteSymbol

Rename the replacements to k instead of c using RewriteSymbol:

In[10]:=
ResourceFunction[
 "FromRecursiveRewrite"]["k28", {"k1" :> -9032, "k2" :> -1, "k3" :> -(1/2), "k4" :> -(1/3), "k5" :> 1/16, "k6" :> 1/3, "k7" :> 1/2, "k8" :> 2/3, "k9" :> 2, "k10" :> 3, "k11" :> 8, "k12" :> 299, "k13" :> 46535, "k14" :> 560405067, "k15" :> 
\!\(\*SuperscriptBox[\("\<k14\>"\), \("\<k7\>"\)]\), "k16" :> 
\!\(\*SuperscriptBox[\("\<k9\>"\), \("\<k8\>"\)]\), "k17" :> "k10" "k15", "k18" :> "k13" + "k17", "k19" :> 
\!\(\*SuperscriptBox[\("\<k18\>"\), \("\<k4\>"\)]\), "k20" :> "k18" "k9", "k21" :> "k1" "k16" "k19", "k22" :> 
\!\(\*SuperscriptBox[\("\<k20\>"\), \("\<k6\>"\)]\), "k23" :> "k11" "k22", "k24" :> "k12" + "k21" + "k23", "k25" :> 
\!\(\*SuperscriptBox[\("\<k24\>"\), \("\<k2\>"\)]\), "k26" :> "k10" "k25", "k27" :> 
\!\(\*SuperscriptBox[\("\<k26\>"\), \("\<k3\>"\)]\), "k28" :> "k27" "k5"}, #2 &, RewriteSymbol -> "k"]
Out[10]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information