MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Extract[expr, Position[expr, patt], h]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114190] Re: Extract[expr, Position[expr, patt], h]
  • From: Peter Pein <petsie at dordos.net>
  • Date: Fri, 26 Nov 2010 05:30:38 -0500 (EST)
  • References: <icdo2m$68e$1@smc.vnet.net>

Hi!

Try Cases:

In[1]:= ex = Expand[(x + y + z)^13]; 
In[2]:= Extract[ex, Position[ex, (a_.)*x^(nx_.)*y^(ny_.)*z^(nz_.) /; 
    nx == ny - 1 == nz - 3]]
Out[2]= {60060*x^3*y^4*z^6}
In[3]:= Cases[ex, (a_.)*x^(nx_.)*y^(ny_.)*z^(nz_.) /; 
   nx == ny - 1 == nz - 3]
Out[3]= {60060*x^3*y^4*z^6}
In[4]:= Extract[ex, Position[ex, (a_.)*x^(nx_.)*y^(ny_.)*z^(nz_.) /; 
    nx == ny - 1 == nz - 3], h]
Out[4]= {h[60060*x^3*y^4*z^6]}
In[5]:= Cases[ex, pat:(a_.)*x^(nx_.)*y^(ny_.)*z^(nz_.) /; 
    nx == ny - 1 == nz - 3 :> h[pat]]
Out[5]= {h[60060*x^3*y^4*z^6]}
In[6]:= Do[foo[i] = i, {i, -3, 3}]
In[7]:= Information["foo", LongForm -> False]
Global`foo
foo[-3] = -3
 
foo[-2] = -2
 
foo[-1] = -1
 
foo[0] = 0
 
foo[1] = 1
 
foo[2] = 2
 
foo[3] = 3

In[13]:= Cases[DownValues[foo], f:foo[_?Positive] :> (f =. ), Infinity]
Out[13]= {Null, Null, Null}
In[14]:= Information["foo", LongForm -> False]
Global`foo
foo[-3] = -3
 
foo[-2] = -2
 
foo[-1] = -1
 
foo[0] = 0
Am Mon, 22 Nov 2010 12:35:34 +0000 (UTC)
schrieb kj <no.email at please.post>:

> 
> 
> I am surprised that Mathematica does not have the equivalent of
> these functions already:
> 
> extractMatching[expr_, patt_] := Extract[expr, Position[expr, patt]]
> extractMatching[expr_, patt_, h_] := Extract[expr, Position[expr,
> patt], h]
> 
> The first one extracts all the subexpressions in expr that match
> the pattern patt (including possibly expr itself).  The second
> wraps the extracted patterns with the head h before evaluation.
> 
> I find myself needing one or the other of these functions all the
> time.  Is there a way to make them automatically available to all
> my Mathematica sessions?
> 
> But maybe they already exist in Mathematica, and I just missed
> them.  If so, please let me know.
> 
> Alternatively, maybe my reliance on extractMatching is diagnostic
> of my having some thought habits that are counterproductive when
> doing rules-based programming (analogous to the habit developed
> through the practice of procedural programming of using for-loops
> and while-loops)...
> 
> Just to be concrete, here's the latest task for which I needed this
> functionality: Unset all the defined expressions foo[i] where i is
> some positive integer.  The solution I found (after many, many
> failed attempts!) turned out to be
> 
> extractMatching[DownValues[foo], HoldPattern[foo[x_ /; x > 0]], Unset]
> 
> (The HoldPattern was necessitated by the fact that some of the
> DownValues of foo recursively refer to foo; without the HoldPattern
> one gets an infinite recursion.)
> 
> Is there a simpler solution to this problem in terms Mathematica
> built-in functions?
> 
> TIA!
> 
> ~kj
> 





  • Prev by Date: Re: Finding a function that makes it possible to
  • Next by Date: Re: MathLink - Linux - Calling the Kernel from External Program - undefined reference to `shm_open'
  • Previous by thread: Extract[expr, Position[expr, patt], h]
  • Next by thread: Re: Extract[expr, Position[expr, patt], h]