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

foo[-2] = -2

foo[-1] = -1

foo = 0

foo = 1

foo = 2

foo = 3

In:= Cases[DownValues[foo], f:foo[_?Positive] :> (f =. ), Infinity]
Out= {Null, Null, Null}
In:= Information["foo", LongForm -> False]
Global`foo
foo[-3] = -3

foo[-2] = -2

foo[-1] = -1

foo = 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]