Re: Default and head in function argument
- To: mathgroup at smc.vnet.net
- Subject: [mg111694] Re: Default and head in function argument
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Tue, 10 Aug 2010 03:57:38 -0400 (EDT)
Not that this is a complete answer, but I have a hunch that the value of
the constant part in the optional pattern must match the pattern. In the
previous case, we had say
x:(_Integer?Positive):3,
and 3 was matching the pattern (_Integer?Positive). Now, Automatic does not
match the pattern _?NumericQ, thus your result. The following hack
therefore
works:
Clear[f3];
f3[A_?MatrixQ, epsilon : (_?NumericQ | Automatic) : Automatic] :=
Module[{cv = epsilon},
If[cv === Automatic,
cv = IdentityMatrix[Dimensions[A][[1]]] + (1/Det[A]) A,
IdentityMatrix[Dimensions[A][[1]]] + epsilon A]]
In[9]:= f3[{{1, 2}, {3, 4}}]
Out[9]= {{1/2, -1}, {-(3/2), -1}}
Regards,
Leonid
On Mon, Aug 9, 2010 at 1:15 PM, NP <nomplume69 at gmail.com> wrote:
> Hi,
>
> Thank you all so much for the helpful solutions with explanations!
>
> On a similar theme, I got the following to work for the case where
> options are not constants
>
> Clear[f1];
> f1[A_?MatrixQ, epsilon_: Automatic] := Module[{cv = epsilon},
> If[cv === Automatic,
> cv = IdentityMatrix[Dimensions[A][[1]]] + (1/Det[A]) A,
> IdentityMatrix[Dimensions[A][[1]]] + epsilon A]]
> f1[{{1, 2}, {3, 4}}]
>
> but not the following (only change is in the second argument)
>
> Clear[f2];
> f2[A_?MatrixQ, epsilon:(_?NumericQ):Automatic] :=
> Module[{cv = epsilon},
> If[cv === Automatic,
> cv = IdentityMatrix[Dimensions[A][[1]]] + (1/Det[A]) A,
> IdentityMatrix[Dimensions[A][[1]]] + epsilon A]]
> f2[{{1, 2}, {3, 4}}]
>
>
> Thanks in advance!
>
> NP