Re: Re: listing user defined, independent variables

*To*: mathgroup at smc.vnet.net*Subject*: [mg96876] Re: [mg96819] Re: listing user defined, independent variables*From*: "Paul Ellsmore" <paul.ellsmore at nanion.co.uk>*Date*: Thu, 26 Feb 2009 07:59:25 -0500 (EST)*References*: <go0j0p$mta$1@smc.vnet.net> <200902250907.EAA15875@smc.vnet.net>

Hi Albert, Thanks for this. I had other replies which mentioned OwnValues. I had never heard of this function, but it is clearly the kind of thing I need. Unfortunately, your code doesn't quite do what i need. As an example, here are two "independent" variables, indVarA and indVarB, and a "dependent" variable, depVarC. I actually want to get at indVarA and indVarB, but your code seems to be aimed at getting depVarC: In[In[26]:= indVarA=1 indVarB=2 depVarC=indVarA*indVarB Out[26]= 1 Out[27]= 2 Out[28]= 2 Using your code: In[29]:= (*ensure that we can see the symbols, not only the values:*) In[30]:= SetAttributes[dependentQ, HoldFirst] In[31]:= (* if we get a symbol which has an OwnValue, extract the unevaluated RHS of its OwnValue and see whether it contains symbols from the Global context: *) In[32]:= dependentQ[var_Symbol] := If[TrueQ[Length[OwnValues[var]] > 0], Not[FreeQ[Extract[OwnValues[var], {1, 2}, Hold], s_Symbol /; Context[s] == "Global`"]], False ] In[33]:= (* as a filter for variable names, it is handy if we accept strings as arguments, too: *) In[34]:= dependentQ[name_String] := dependentQ @@ ToExpression[name, InputForm, Hold] In[35]:= (* use as filter: *) In[36]:= Select[Names["Global`*"], dependentQ] We get a zero length list as output: Out[36]= {} I want to get indVarA and indVarB as output, but if I remove the "Not" from your code, I get: In[37]:= SetAttributes[dependentQ, HoldFirst] dependentQ[var_Symbol] := If[TrueQ[Length[OwnValues[var]] > 0], FreeQ[Extract[OwnValues[var], {1, 2}, Hold], s_Symbol /; Context[s] == "Global`"], False ] dependentQ[name_String] := dependentQ @@ ToExpression[name, InputForm, Hold] Select[Names["Global`*"], dependentQ] Out[40]= {depVarC,indVarA,indVarB} I am not entirely sure I understand your syntax, but I assume that we are trying to get OwnValues[var_Symbol], without the rhs being evaluated. I would hope to get OwnValues[depVarC] to be {HoldPattern[depVarC] :> indVarA + indVarB}, or something like that, but everything i try gives the evaluated rhs: In[41]:= SetAttributes[myExtract,HoldFirst] myExtract[var_Symbol]:=Extract[OwnValues[var], {1, 2}, Hold] In[43]:= myExtract[depVarC] Out[43]= Hold[2] If I can get the unevaluated rules for each synbol, using OwnValues or any other function, I will be able to do what I need. Any idea how to do this? Cheers, Paul -----Original Message----- From: Albert Retey [mailto:awnl at gmx-topmail.de] Sent: 25 February 2009 09:07 To: mathgroup at smc.vnet.net Subject: [mg96876] [mg96819] Re: listing user defined, independent variables Paul Ellsmore wrote: > Hi, > > > > I have a notebook in which there are over 1300 variables (symbols) defined. > The vast majority of these are dependent variables. I can get a list of them > all with Names["Global`*"], but what I really want is a list of only the > independent variables. Is there any obvious way to do this? Mathematica must > "know" which variables are functions of other symbols, so I was hoping that > there would be an "attribute" of a dependent variable that I could use as a > filter on Names[]. Any thoughts? I am using V5.1. I don't think that there is an attribute for that information. The internals of mathematica are mostly rule based, and OwnValues[] will give you the information which can be used to determine the dependencies that you are seeking. I could only think about a rather involved way to extract that information, since you need to be careful to not evaluate too early. The following is an approach to determine the dependencies: (*ensure that we can see the symbols, not only the values:*) SetAttributes[dependentQ, HoldFirst] (* if we get a symbol which has an OwnValue, extract the unevaluated RHS of its OwnValue and see whether it contains symbols from the Global context: *) dependentQ[var_Symbol] := If[TrueQ[Length[OwnValues[var]] > 0], Not[FreeQ[Extract[OwnValues[var], {1, 2}, Hold], s_Symbol /; Context[s] == "Global`"]], False ] (* as a filter for variable names, it is handy if we accept strings as arguments, too: *) dependentQ[name_String] := dependentQ @@ ToExpression[name, InputForm, Hold] (* use as filter: *) Select[Names["Global`*"], dependentQ] It might well be that there are simpler and/or more efficient ways to do this, but I can't think of one now and it should be a starting point... hth, albert

**References**:**Re: listing user defined, independent variables***From:*Albert Retey <awnl@gmx-topmail.de>