Prev: Newbie question: Using expanded Boolean Criteria w the Select funtion
Next: > classroom combinatorics
From: NP on 9 Aug 2010 05:15 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
From: Leonid Shifrin on 10 Aug 2010 03:58 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(a)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
First
|
Prev
|
Pages: 1 2 Prev: Newbie question: Using expanded Boolean Criteria w the Select funtion Next: > classroom combinatorics |