(*********************************************************************** This file was generated automatically by the Mathematica front end. It contains Initialization cells from a Notebook file, which typically will have the same name as this file except ending in ".nb" instead of ".m". This file is intended to be loaded into the Mathematica kernel using the package loading commands Get or Needs. Doing so is equivalent to using the Evaluate Initialiation Cells menu command in the front end. DO NOT EDIT THIS FILE. This entire file is regenerated automatically each time the parent Notebook file is saved in the Mathematica front end. Any changes you make to this file will be overwritten. ***********************************************************************) (* A package for extracting large repeated sub-expressions in order to display expressions in a more compact form Author: Jon McLoone, Marketing Manager, Wolfram Research Europe Ltd Created: Spring 97 Version 1 *) BeginPackage["Subexpressions`"]; Subexpressions::usage = "Subexpressions[expr] finds repeated subexpressions within expr. It \ returns a held object. The subexpressions are ordered according to size and number of \ repeated occurences. To return to the original expression, use RelaseHold."; MinLeafCount::usage = "This is an option for Subexpressions which dictates the smallest \ subexpression to be extracted, measured by its LeafCount"; MaxSubexpressions::usage = "This is an option for Subexpressions which dictates how many \ subexpressions to try to find. When set to automatic it bases this number on the size of the \ original expression."; Begin["Private`"]; FindBiggest[source_] := (If[Length[#1] > 0, First[First[#1]]] & )[Sort[Select[Split[Sort[source]], Length[#1] > 1 & ], LeafCount[Drop[#1, 1]] > LeafCount[Drop[#2, 1]] & ]]; ReplaceSubs[{expr_, {}}, 0, leaf_Integer] := expr; ReplaceSubs[{expr_, rules_}, 0, leaf_Integer] := (HoldForm[#1 //. #2] & )[expr, rules]; ReplaceSubs[{expr_, rules1_}, n_Integer, leaf_Integer] := Block[{rules, biggest, source = {}}, Scan[If[LeafCount[#1] >= leaf, AppendTo[source, #1]] & , {expr, rules1}, {2, Infinity}]; biggest = FindBiggest[source]; ReplaceSubs[{expr //. (rules = If[LeafCount[biggest] >= Max[leaf, 2], {biggest -> Unique[]}, {}]), Join[rules1 //. rules, Reverse /@ rules]}, If[biggest === Null, 0, n - 1], leaf]]; End[]; Subexpressions[expr_, opts___] := Private`ReplaceSubs[{expr, {}}, MaxSubexpressions /. {opts} /. Options[Subexpressions] /. Automatic -> Floor[0.75 + LeafCount[expr]/200], MinLeafCount /. {opts} /. Options[Subexpressions]]; Options[Subexpressions] = {MaxSubexpressions -> Automatic, MinLeafCount -> 10}; Protect[Subexpressions]; EndPackage[];