Group Abstract Group Abstract

Message Boards Message Boards

1
|
8.1K Views
|
5 Replies
|
10 Total Likes
View groups...
Share
Share this post:

[?] Provide a default value for x in f[x_?NumericQ]:=...?

If the restriction for x is expressed by specifying the Head, there is no problem: e.g. f[x_Complex : 1+ I] := .... . If the restriction is expressed by a predicate I did not find a way to set a default value for the argument. Who can help?

POSTED BY: Ulrich Mutze
5 Replies
Posted 8 years ago

In this case, you will want the expanded form of Pattern[] to account for both PatternTest[] and Optional[]. Thus:

f[x : (_?NumericQ) : Pi] := Sin[x]

{f[], f[Pi/2], f[y]}
   {0, 1, f[y]}
POSTED BY: J. M.

One thing to pay attention to when doing this is to make sure that the default value matches the pattern.

What happens if it doesn't match has even changed between versions (rather arbitrarily, and without documentation or warning):

https://mathematica.stackexchange.com/questions/108636/version-inconsistency-with-optional-arguments-what-if-the-default-value-doesnt

POSTED BY: Szabolcs Horvát
POSTED BY: Murray Eisenberg
POSTED BY: Ulrich Mutze
POSTED BY: Ulrich Mutze
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard