10

Inspired by this and this question (and how I handle this in practice), what is the best way to default a function value when a certain condition is met?

For example, if a function is defined as:

func[x_] := (x - 1)^2 + 5

and for values of x less than 0, x should equal zero. I also always want the function to evaluate even when x is less than 0. Normally, I would handle as such:

func2[x_] := Module[{xx = x},
   If[xx < 0, xx = 0];
   (xx - 1)^2 + 5]

If I try to use patterns to define the conditions, I have to define the function twice for each region:

func3[x_?Positive] := (x - 1)^2 + 5
func3[x_] := 6

I've had no luck using other patterns to define the function.

While this is an admittedly easy example that can be easily handled using Max[x,0], etc., patterns can be much more complex such as "integers divisible by a prime number".

So, can a pattern married with a default argument handle this situation? And what is the most efficient on a computation and memory basis?

UPDATE:

Here is a more complicated version of a function to which I referred to in the comments:

func[qi_, dei_, b_, dmin_, rt_, pt_, t_] := 
   Module[{ptt = Max[pt, 0], rtt = Max[rt, 0], di, diexp, xotime, xorate},
    di = 1/b*((1 - dei)^-b - 1)/365;
    diexp = -Log[1 - dmin]/365;
    xotime = (di - diexp)/(di*b*diexp) + rtt + ptt;
    xorate = qi*(1 + b*di*(xotime - ptt - rtt))^(-1/b); 
    Piecewise[{{qi/2 (1 + t/rtt), t <= rtt}, {qi, rtt < t <= (rtt + ptt)},
        {qi*(1 + b*di*(t - rtt - ptt))^(-1/b), (rtt + ptt) < t <= xotime},
        {xorate*Exp[-diexp*(t - xotime)], t > xotime}}]
    ]

I'll plug in random variates (which can sometimes be zero based on the distribution), and evaluate this function multiple times. However, rt and pt should never be negative and should default to zero. In other words, have minimum value of zero. While the way I have it works well, the original question stemmed from the idea that this could be done more efficient with a pattern. jVincent's vanishing patterns solution works only if one argument has this pattern. If both rt and pt have this pattern, the vanishing pattern doesn't seem to work.

kale
  • 10,922
  • 1
  • 32
  • 69
  • Can't you use Min[x,0] as func[x_] := (Min[x, 0] - 1)^2 + 5 ? – b.gates.you.know.what Feb 12 '13 at 14:14
  • @b.gatessucks Sure, in this situation, but I was looking for a more comprehensive solution for other conditionals (between values, integers, divisible by 71, etc...), that I can expand on later. And it would be Max in this case. – kale Feb 12 '13 at 14:16
  • You're right, thanks. – b.gates.you.know.what Feb 12 '13 at 14:19
  • func[x_]=(x - 1)^2 + 5; f[x_] = Piecewise[{{func[x], x >= 0}}, func[0]] – ssch Feb 12 '13 at 14:35
  • This doesn't work, since optional doesn't work like that, but if I understand you, you are looking for something like func[(_?Negative | x_: 0)] := (x - 1)^2 + 5 where x is matched as zero whenever it's negative and as it's actual match if positive? – jVincent Feb 12 '13 at 14:37
  • @jVincent YES. I've tried every combination of colons and parenthesis that I could, but couldn't get it to work. If you this can be expanded to other more advanced patterns (see edit) than it's exactly what I'm looking for. – kale Feb 12 '13 at 14:39
  • I've been down that road once before without luck. I think you'll always need a pattern to match something which it will then contain (potentially Sequence[]), and have to handle potential conversion on the right hand side. Though I'm waiting eagerly for someone to prove this wrong. :) – jVincent Feb 12 '13 at 14:42
  • @rm-rf Agreed, except in the answer linked, the function doesn't evaluate when the condition is not met (where I got hung up before). I still need it to evaluate with the default value when the condition is not met. – kale Feb 12 '13 at 14:51
  • So why don't you like defining a separate pattern for each region? Maybe we can help you find an alternative you do like...? – sebhofer Feb 12 '13 at 14:59
  • @kale Ah, I see. I edited my comment – rm -rf Feb 12 '13 at 15:01
  • @sebhofer I'm fine with using that method if it is the best way to do it, just need the say-so from the experts. In my mind it would seem easier to not have to define a function multiple times though. – kale Feb 12 '13 at 15:02

4 Answers4

8

I would use

f[x_?Negative] := f[0]
f[x_] := (x - 1)^2 + 5

or just Piecewise.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Or equivalently f[x_] := f[0];f[x_?NonNegative] := (x - 1)^2 + 5 but I think that's what the OP wants to avoid. – sebhofer Feb 12 '13 at 15:16
  • @sebhofer Maybe I didn't understand the question well, I thought he wanted to avoid writing out the definition (x - 1)^2 + 5 (or a special case of it) several times. I avoided that by "redirecting" to f[0]. I know I cheated by using that fact that 0 is not negative ... – Szabolcs Feb 12 '13 at 15:19
  • Maybe you're right... I was wondering the same. I thought he tried to avoid defining two distinct DownValues. Anyway +1 because that would have been my answer too. – sebhofer Feb 12 '13 at 15:23
  • Hi, inspired by your suggestion I ve tried something simple of the type: g[q_,p_?p_<q_]:=0, and g[q_,p_]:=4; but when i test it for inputs with p smaller than q, it still returns 4. Am I expressing the condition wrongly here? –  Jul 14 '17 at 13:50
  • @user929304 Yes, lots of mistakes in there ... Try g[q_, p_] /; p < q := 0. 1. Look up the difference between ? (PatternTest) and /; (Condition). 2. p_ (with underscore) is a pattern, and p (without underscore) refers to the expression that pattern matched 3. If you refer to both q and p on the RHS of /; then both q_ and p_ must be present on the LHS. Thus the condition needed to be moved outside of g. – Szabolcs Jul 15 '17 at 06:37
8

I'll add this as a different answer since it's quite different from my first alternative method. It seems that you can in special cases at least get by with:

func[x : (_?Positive | 0) : 0, dummy___?Negative] := (x - 1)^2 + 5

The working principle is that a dummy argument pattern is added, which matches the negative case. When supplied with a negative input, the first pattern fails, but the second will expand and match the input, letting x be set to the default.

jVincent
  • 14,766
  • 1
  • 42
  • 74
  • Very sneaky! I like it :) – sebhofer Feb 12 '13 at 16:25
  • nice!! (+1) also works without Alternatives: func[x : _?NonNegative : 0, dummy___?Negative] := (x - 1)^2 + 5? – kglr Feb 12 '13 at 19:18
  • Wow. My head is spinning on this one. Doesn't seem to work for functions that have multiple arguments which should match these patterns though. Impressive, nonetheless. – kale Feb 12 '13 at 19:27
3

I tried my hand at this problem before reading any of the answers and I arrived as jVincent's second solution too. You stated in a comment:

Doesn't seem to work for functions that have multiple arguments which should match these patterns though.

You should add an example of your more complicated function so that this might be addressed. Also, clarify if you mean multiple default arguments or simply multiple arguments. Here is an example of this method applied to a function with multiple arguments:

ClearAll[func]

func[x : (_?Positive | 0) : 0, ___, y_] := {x, y}

func[#, 2] & /@ {-3, 0, 5}
{{0, 2}, {0, 2}, {5, 2}}

Incidentally your specific example lends itself to a nice little trick using what I call "vanishing patterns" and the Head surrounding x:

ClearAll[func]

func[x_ | _] /; x > 0 := (x - 1)^2 + 5

func /@ {-3, 0, 5}
{6, 6, 21}

When the pattern x_ (under the Condition) does not match anything but an alternative pattern does, x in the RHS is replaced with an empty sequence. If we look at the FullForm of your RHS expression we see:

Plus[5,Power[Plus[-1,x],2]]

Therefore if x is removed it behaves the same as if x were zero.


Responding to your update here is a definition for your many-parameter function:

ClearAll[func]

func[qi_, dei_, b_, dmin_, rt : (_?Positive | 0) : 0,
   pt : (_?Positive | 0) : 0, ___, t_] := {qi, dei, b, dmin, rt, pt, t}

func[1, 2, 3, 4, -3, -7, 5]
{1, 2, 3, 4, 0, 0, 5}
func[1, 2, 3, 4, 3, 7, 5]
{1, 2, 3, 4, 3, 7, 5}

Let me be clear that this is all just for fun to me and in any serious application I would likely use some variation of Szabolcs's method, e.g.:

func[qi_, dei_, b_, dmin_, rt_, pt_, t_] := 
 func[{qi, dei, b, dmin, Max[0, rt], Max[0, pt], t}]

func[{qi_, dei_, b_, dmin_, rt_, pt_, t_}] := (* main def *)
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • See update for the more complicated function. rt and pt are the two values that should default to zero if the specified values are less than 0. – kale Feb 21 '13 at 16:26
  • Oh, and rt and pt are used later for the Piecewise function. If the vanishing pattern is used, I don't think these conditionals work. – kale Feb 21 '13 at 16:29
  • @kale I added an example to my answer, and I note of my intention. – Mr.Wizard Feb 21 '13 at 17:13
  • So your last paragraph is what I was really going for: "Yes you can do this but you should do this. As far as the variation of Szabolcs's vs my Module[rt=Max[rt,0] is there any benefit to one over the other? – kale Feb 21 '13 at 19:27
  • @kale Well, why didn't you say that in the first place? ;-) No, I am not aware of any method within single-definition pattern matching that is appropriate for production code. I saw this question as in interesting exploration of possible exploits. A bit later I'll add some thoughts about additional DownValues versus scoping. – Mr.Wizard Feb 22 '13 at 00:14
  • I know, I'm terrible at asking questions... As I said in one of my comments below, I was hoping to expand this to other patterns (not just a value less than zero). I was hoping for a "Hey dummy! Don't use a pattern to do this. Use this instead."-answer. I figured a pattern solution did exist out there (and my pattern-knowledge in Mathematica is a little weak), and an answer would suffice, but a little more color would be added regarding "best practices" in Mathematica-land. – kale Feb 22 '13 at 03:15
2

It seems like this sort of matching isn't possible. Personally I have, at times, used a method of using Alternatives to circumvent the pattern, such that it's bound to Sequence[], and then providing the default values on the right hand side:

def[a_, ___] := a
func[(x_?Positive | _)] := (def[x, 0] - 1)^2 + 5
jVincent
  • 14,766
  • 1
  • 42
  • 74