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 for the "RewriteSymbol" option.

Examples

Basic Examples (8) 

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[# /. Last[optexpr] &, "c58", 6][[1]]
Out[3]=

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

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

Hover the cursor over the subexpressions to see the byte count of the expressions:

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

Get the information for the expressions:

In[6]:=
Dynamic[Column[{ResourceFunction["FromRecursiveRewrite"][expr2, Last[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, Last[optexpr], #1 &]]
Out[8]=

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

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

Options (1) 

RewriteSymbol (1) 

The "RewriteSymbol" option specifies which string to use when rewriting:

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

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.1 – 26 October 2020
  • 1.0.0 – 06 March 2019

Related Resources

License Information