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: [mg114303] Re: Extract[expr, Position[expr, patt], h]
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Tue, 30 Nov 2010 04:05:46 -0500 (EST)

_. is a pattern for an expression with a globally defined default value

http://reference.wolfram.com/mathematica/tutorial/PatternsAndTransformationRules.html

http://reference.wolfram.com/mathematica/ref/Default.html

?? Default


Default /@ {Plus, Times}

{0, 1}


Bob Hanlon

---- Alexei Boulbitch <alexei.boulbitch at iee.lu> wrote: 

=============
Hi, Peter,

could you please explain the use of the construct "underscore, point", like a_. that you use here:
  
Extract[ex, Position[ex, (a_.)*x^(nx_.)*y^(ny_.)*z^(nz_.) /;
     nx == ny - 1 == nz - 3]]

Thank you, Alexei



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
>



-- 
Alexei Boulbitch, Dr. habil.
Senior Scientist
Material Development

IEE S.A.
ZAE Weiergewan
11, rue Edmond Reuter
L-5326 CONTERN
Luxembourg

Tel: +352 2454 2566
Fax: +352 2454 3566
Mobile: +49 (0) 151 52 40 66 44

e-mail: alexei.boulbitch at iee.lu

www.iee.lu

--




  • Prev by Date: Re: Problems with Mathematica 8.0 Solve
  • Next by Date: Re: Why are my 3D plots blue?
  • Previous by thread: Re: Extract[expr, Position[expr, patt], h]
  • Next by thread: Need help NIntegrating stepwise 3D probability density functions