4
$\begingroup$

I cannot find the method how to apply some pattern specification to the values of options in a function. For example, it is necessary to define the power (exponentiation) with both positive base and power

strangepower[x_ /; x > 0, OptionsPattern["power" -> 1]] := (x^OptionValue["power"])

How to specify that option "power" can be only positive?

$\endgroup$
1
  • $\begingroup$ What would you like to happen when the condition is not satisfied? $\endgroup$
    – MarcoB
    Commented Mar 19 at 20:36

2 Answers 2

2
$\begingroup$

I'm not 100% sure, but I don't think that's possible. The typical thing to do if you have extra constraints on options is to check them explicitly in the function definition:

strangepower[x_ /; x > 0, OptionsPattern["power" -> 3]] :=
  If[Positive[OptionValue["power"]], (x^OptionValue["power"]), x^3, x^3]
$\endgroup$
3
  • $\begingroup$ Thank you for your suggestion, but I didn't mean this. In the body of a function, it is possible to verify any conditions regarding the variables. However I would like to specify conditions regarding options while defining OptionsPattern (similar to usual patterns like x_). I'm sorry that didn't mention this in the question directly :( $\endgroup$
    – Konstantin
    Commented Mar 19 at 16:24
  • $\begingroup$ Yes, I understood the question. As I said, I think the answer to your question is "you can't". That's why I offered an alternative. There is also the question of what you expect to happen even if this were possible. Options typically need to be checked in the body anyway, as they affect how the function works and usually fallback to a default value when not provided by the caller. So, there really isn't a point to specifying a constraint as a pattern in the formal arguments. Maybe your example isn't really illustrative of what you're actually trying to achieve. $\endgroup$
    – lericr
    Commented Mar 19 at 17:46
  • $\begingroup$ It doesn't matter. There is a number of workarounds. My question was rather theoretical $\endgroup$
    – Konstantin
    Commented Mar 19 at 18:09
2
$\begingroup$

I am not sure if it is possible directly within OptionsPattern, but perhaps you could do away with the named option and use an optional argument instead.

Here is an example:

ClearAll[power]
power[x_?NonNegative, Optional[power_?NonNegative, 2]] := x^power

With that definition:

power[3]       (*  9 *)
power[3, 3]    (* 27 *)
power[3, -1]   (* power[3, -1] *)

As a note, the Optional[power_?NonNegative, 2] can also be written as power : _?NonNegative : 2 in the function definition.

$\endgroup$
1
  • $\begingroup$ Thank you. Indeed, one can use "optional arguments" in such a way. Unfortunately, it is not possible to use patterns with options themself $\endgroup$
    – Konstantin
    Commented Mar 19 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.