Re: Crossreference, code documentation
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1633] Re: Crossreference, code documentation
- From: Count Dracula <lk3a at kelvin.seas.virginia.edu>
- Date: Fri, 7 Jul 1995 01:33:09 -0400
- Organization: University of Virginia
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 ]]
------------------------------------------------------------------------------------
For example:
In[2]:= findsym[1/0]
Out[2]= {Power, Times}
In[3]:= FindSymbols[1/0]
Out[3]= {Power, Times}
In[4]:= {a, b, c, f, g} = {1, 2, 3, func1, func2};
In[5]:= Timing[FindSymbols[f[a][b, g[c]]]]
Out[5]= {0.01 Second, {a, b, c, f, g}}
In[6]:= Timing[findsym[f[a][b, g[c]]]]
Out[6]= {0. Second, {a, b, c, f, g}}
In[7]:= 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}])]
In[8]:= test2 := FindSymbols[P[{x, f[f[g[a], b], b, h[c], f]}] +
Sin[x + Pi y] Log[c] Exp[I x + u]/(9 Integrate[x Pi, {x, 0, 2}])]
In[9]:= Timing[test2]
Out[9]= {0.05 Second, {a, b, c, Exp, f, g, h, I, Integrate, List, Log, P, Pi, Plus, Power, Sin, Times, u, x, y}}
In[10]:= Timing[test]
Out[10]= {0.01 Second, {a, b, c, Exp, f, g, h, I, Integrate, List, Log, P, Pi, Plus, Power, Sin, Times, u, x, y}}
In[11]:= FindSymbols[Infinity/0 + "string"/0 + (3 + I u)/Infinity]
Out[11]= {I, Infinity, Plus, Power, Times, u}
In[12]:= findsym[Infinity/0 + "string"/0 + (3 + I u)/Infinity]
Out[12]= {I, Infinity, Plus, Power, Times, u}
In[13]:= findsym[Exit]
Out[13]= {Exit}
In[14]:= FindSymbols[Exit]
Out[14]= {Exit}
--
___________________________________________________________________________________
Levent Kitis lk3a at cars.mech.virginia.edu lk3a at kelvin.seas.virginia.edu
University of Virginia Department of Mechanical, Aerospace, and Nuclear Engineering
___________________________________________________________________________________