RE: looking for a "test[ 1, ALL ]
- To: mathgroup at smc.vnet.net
- Subject: [mg38414] RE: [mg38373] looking for a "test[ 1, ALL ]
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Sat, 14 Dec 2002 03:19:47 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Oliver Ruebenkoenig [mailto:ruebenko at donne.imtek.uni-freiburg.de] To: mathgroup at smc.vnet.net >Sent: Friday, December 13, 2002 10:09 AM >To: mathgroup at smc.vnet.net >Subject: [mg38414] [mg38373] looking for a "test[ 1, ALL ] > > >Hi Mathgroup, > >consider the following: > >In[1]:= test[1, 1] = a; > test[1, 3] = b; > >In[2]:= ?test >Global`test > >test[1, 1] = a > >test[1, 3] = b > >In[3]:= from := 1; > to := 3; > test[ 1, #1 ] & /@ Range[ from, to ] > >Out[5]= {a, test[1, 2], b} > >Of course the test[ 1, 2 ] is not evaluated; it does not >exist. If I want >to delete the existing ones I could use: > >In[9]:= deletableValue := 1; > ( HoldForm[ test[ deletableValue, #1 ] =. & ] /@ > Range[ from, to ] ) // ReleaseHold > >Unset::norep: Assignment on test for test[1, 2] not found. > >Out[10]= {Null, $Failed, Null} > >Which makes sense. > >In[14]:= ?test >Global`test > >No "test" in the global context. My question now: is there a mechanism >e.g. something like test[ 1, All ] that does only access the existing >test? > >I am aware of the possibility to use a conditional (If, Case, ..) and >DownValues to avoid the error message, but is there a more direct >(faster?) way? > >Thanks a lot for you comments, > >Oliver > > >Oliver Ruebenkoenig, <ruebenko at imtek.de> > Phone: ++49 +761 203 7293 > > Oliver, look at: In[84]:= Clear[test] In[85]:= test[1, 1] = 11; test[1, 3] = 13; test[2, 0] = 20; test[a] := test[1, 4] + 14 test[_] := Print["what test?"] In[90]:= DownValues[test] Out[90]= {HoldPattern[test[a]] :> test[1, 4] + 14, HoldPattern[test[1, 1]] :> 11, HoldPattern[test[1, 3]] :> 13, HoldPattern[test[2, 0]] :> 20, HoldPattern[test[_]] :> Print["what test?"]} Selectively extract definitions (rules) In[91]:= Cases[DownValues[test], def : (Verbatim[HoldPattern][test[1, _]] :> _) :> def, {1}] Out[91]= {HoldPattern[test[1, 1]] :> 11, HoldPattern[test[1, 3]] :> 13} Try to selectively extract patterns defined... In[92]:= Cases[DownValues[test], def : HoldPattern[test[1, _]] :> HoldPattern[def], {3}] Out[92]= {HoldPattern[test[1, 4]], HoldPattern[test[1, 1]], HoldPattern[test[1, 3]]} ...as you see, this is not good enough to clear selected definitions: In[93]:= Cases[DownValues[test], def : HoldPattern[test[1, _]] :> Unset[def], {3}] >From In[93]:= Unset::"norep": "Assignment on \!\(test\) for \!\(test[\(\(1, 4\)\)]\) not \ found." Out[93]= {$Failed, Null, Null} In[94]:= DownValues[test] Out[94]= {HoldPattern[test[a]] :> test[1, 4] + 14, HoldPattern[test[2, 0]] :> 20, HoldPattern[test[_]] :> Print["what test?"]} But match full definitions to only clear those definitions with matches appearing at lhs: In[95]:= Clear[test] In[96]:= test[1, 1] = 11; test[1, 3] = 13; test[2, 0] = 20; test[a] := test[1, 4] + 14 test[_] := Print["what test?"] In[101]:= DownValues[test] Out[101]= .... In[102]:= Cases[DownValues[test], (Verbatim[HoldPattern][def : test[1, _]] :> _) :> Unset[def], {1}]; In[103]:= DownValues[test] Out[103]= {HoldPattern[test[a]] :> test[1, 4] + 14, HoldPattern[test[2, 0]] :> 20, HoldPattern[test[_]] :> Print["what test?"]} What, do you think, should or could be made faster? -- Hartmut Wolf