|
[Date Index]
[Thread Index]
[Author Index]
RE: Pattern Matching
- To: mathgroup at smc.vnet.net
- Subject: [mg49017] RE: [mg49003] Pattern Matching
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 29 Jun 2004 04:49:44 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
- Thread-topic: [mg49003] Pattern Matching
>-----Original Message-----
>From: Bruce W. Colletti [mailto:bcolletti at compuserve.com]
To: mathgroup at smc.vnet.net
>Sent: Monday, June 28, 2004 10:14 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg49017] [mg49003] Pattern Matching
>
>
>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
>
>
Let's look at Help:
"The form s_:v is equivalent to Optional[s_, v]. This form is also equivalent to s:_:v. There is no syntactic ambiguity since s must be a symbol in this case."
"The special form s_. is equivalent to Optional[s_] and can be used to represent function arguments which, if omitted, should be replaced by default values globally specified for the functions in which they occur."
"Values for Default[f,...] specify default values to be used when _. appears as an argument of f. Any assignments for Default[f,...] must be made before _. first appears as an argument of f."
The Defaults for Times and Power are defined as:
In[53]:= DefaultValues[Times]
Out[53]= {HoldPattern[Default[Times]] :> 1}
In[54]:= DefaultValues[Power]
Out[54]= {HoldPattern[Default[Power, 2]] :> 1}
But I would strickly disadvice to change them. You 'could' do so however:
In[55]:= Unprotect[Times, Power]
Out[55]= {"Times", "Power"}
In[57]:= Default[Times] = 5;
Default[Power, 2] = 7;
In[59]:= Cases[4x + x^2, c_.*x^n_. -> {c, n}]
Out[59]= {{4, 7}, {5, 2}}
--
Hartmut Wolf
Prev by Date:
Re: PlotLabel with assigned variables *and* on several lines
Next by Date:
RE: options Transpose[] ?
Previous by thread:
Re: Pattern Matching
Next by thread:
matrix times a vector
|