Re: Re: Crossreference, code documentation
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1673] Re: [mg1633] Re: Crossreference, code documentation
- From: Allan Hayes <hay%haystack at christensen.cybernetics.net>
- Date: Tue, 11 Jul 1995 05:37:19 -0400
Levent Kitis <lk3a at kelvin.seas.virginia.edu> wrote in [mg1633] Re: Crossreference, code documentation >In Message-ID: <3tahhp$6bb at news0.cybernetics.net> >wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner) writes: >> This function finds all symbols in an >> expression without allowing anything to evaluate: >> >> SetAttributes[FindSymbols, HoldFirst] >> FindSymbols[expr_] := >> Switch[Head[Unevaluated[expr]], >> String | Integer | Real | Complex | Rational, {}, >> Symbol, {HoldForm[expr]}, >> _, Union[Flatten[ReleaseHold[ >> Map[FindSymbols, >> Apply[List, Hold[expr], {1}], >> {2}] ]], >> FindSymbols @@ HeldPart[Hold[expr], 1, 0] >> ] >> ] >> > >This functionality is more simply achieved, without resorting to >recursion, by the function findsym : > >---------------------------------------------------------------------> > SetAttributes[{findsym, WrapAll}, HoldAll] > > findsym[expr_] := > Union[ > Cases[ > Level[ WrapAll[expr], {0, Infinity}, Heads -> True], > Hold[x_Symbol] :> HoldForm[x] > ] > ] > > WrapAll[expr_] := First[Map[ Hold, Hold[expr], Infinity, Heads -> > True ]] --------------------------------------------------------------------- Some further simplification and speed up seems possible: SetAttributes[findsym2, HoldAll] findsym2[expr_] := Union[ Cases[ Unevaluated[expr], x_Symbol -> HoldForm[x], {-1}, Heads->True ] ] EXAMPLES: { findsym[1/0] == findsym2[1/0], Do[ findsym[1/0],{100}]//Timing//First, Do[findsym2[1/0],{100}]//Timing//First } {True, 0.466667 Second, 0.15 Second} {a, b, c, f, g} = {1, 2, 3, func1, func2}; { findsym[f[a][ b, g[c]]] === findsym[f[a][ b, g[c]]], Do[ findsym[f[a][ b, g[c]]],{100}]//Timing//First, Do[findsym2[f[a][ b, g[c]]],{100}]//Timing//First } {True, 0.533333 Second, 0.216667 Second} test := findsym[P[{x, f[f[g[a], b], b, h[c], f]}] + Sin[x + Pi y] Log[c] Exp[I x + u]/(9 Integrate[Pi x, {x, 0, 2}])] test2 := findsym2[P[{x, f[f[g[a], b], b, h[c], f]}] + Sin[x + Pi y] Log[c] Exp[I x + u]/(9 Integrate[Pi x, {x, 0, 2}])] { test === test2, Do[test,{100}]//Timing//First, Do[test2,{100}]//Timing//First } {True, 2.21667 Second, 1. Second} findsym[Infinity/0 + "string"/0 + (3 + I u)/Infinity] === findsym2[Infinity/0 + "string"/0 + (3 + I u)/Infinity] True Allan Hayes De Monfort University, Leicester, UK hay at haystack.demon.co.uk