RE: Re: Checking the form of an option
- To: mathgroup at smc.vnet.net
- Subject: [mg21239] RE: [mg21219] Re: [mg20982] Checking the form of an option
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Mon, 20 Dec 1999 02:27:59 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
John Ross wrote:
----------------------------
>I want to check whether option "K" to a function is an integer, a list of
>single numbers, or neither of these, and then to execute one of three
>different statements.
>
>There are many ways to approach this.
>I write a toy example that does what you want.
>
Thank you. This helps a lot. May I add an additional question related both
to this one and to one I asked previously and to which Andrzej Kozlowski
responded? If I have a function NonComplexQ[x_] which returns True if x is
a non-complex numeric, then how can I use *this* in your suggestions about
checking options. They appear to rely on a particular Head rather than a
predicate. Is there a neat way of moving over to the predicate form and
checking whether I have an option which is a single NonComplex numeric or a
list of NonComplex numerics?
------------------------------
REPLY:
I modify my previous example to do what you want. I throw in an extra case
to see if K is greater than one. In that case I use the pure function
(#>1&). If this notation is foreign to you look up Slot, SlotSequence at my
website.
As before the last pattern in Switch is (_) which matches anything. You only
get the that pattern if the other two fail.
In[1]:=
Options[f] = {K -> 2};
f[x_, opts___] :=
With[
{k1 = K /. Flatten[{opts, Options[f]}]},
Switch[k1,
_?(#>1&), (k1, "K is greater than one.")
_?NonComplexQ, {k1, "K is numeric and not complex."},
{__?NonComplexQ}, {k1, "K is a list of numeric, non-complex values."},
_, {k1, "K is not a non-complex numeric or list of non-complex
numerics."}
]
]
For anyone who missed the earlier discussion, NonComplexQ is a function that
several of us gave definitions for earlier.
--------------------
Regards,
Ted Ersek
For Mathematica tips, tricks see
http://www.dot.net.au/~elisha/ersek/Tricks.html