Re: Pattern Matching

*To*: mathgroup at smc.vnet.net*Subject*: [mg49020] Re: [mg49003] Pattern Matching*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Tue, 29 Jun 2004 04:49:46 -0400 (EDT)*References*: <200406280813.EAA04275@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On 28 Jun 2004, at 17:13, Bruce W. Colletti wrote: > This command (built from recent traffic) extracts the quadratic's > coefficients and exponents: > > Cases[4x + x^2, c_.*x^n_. -> {c,n}] > > {{4,1}, {1,2}} > > As I understand it, the symbol "_." uses the global default value for > the given variable, a value that can be set using Default. > > Question: For the specific (illustrative) problem above, how would I > set (using Default[ ]) different default values for c and n? Or must I > do something like: > > Cases[4x + x^2, (c_:5)*x^(n_:7) -> {c,n}] > > Thanks. > > Bruce > > Deafults are assigned to functions, not to variables. So you could do this: Unprotect[Times]; Default[Times] = 5 Cases[4*x + x^2, (c_.)*x^(n_:7) -> {c, n}] {{4, 7}, {5, 2}} But obviously it would not be a good idea to leave it like this! This is a much more subtle way to achieve this without unprotecting and modifying Times: Block[{Times}, Default[Times] = 5; SetAttributes[Times, {OneIdentity}]; Cases[4*x + x^2, (c_.)*x^(n_:7) -> {c, n}]] {{4, 7}, {5, 2}} Note that it will not work without assigning the Attribute OneIdentity to Times inside Block! The explanation why this is so is quite complicated so all I will say is I learnt some years back from Michael Trott's marvellous "Mathematica Guidebooks". Andrzej Kozlowski Chiba, Japan http://www.mimuw.edu.pl/~akoz/

**References**:**Pattern Matching***From:*"Bruce W. Colletti" <bcolletti@compuserve.com>