5
$\begingroup$

Regarding my recent question on using a default value for a function argument when a pattern was not met yielded some interesting answers, but the general consensus was "Yes this can be done, but there are better options."

Assuming the same conditions that certain function arguments (rt and pt in this case) should default to zero when less than zero, the three main options are as follows:

Downvalues:

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_}] := (*do stuff*)

Or...

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

func[qi_, dei_, b_, dmin_, rt_, pt_, t_] :=(*do stuff*)

Scoping:

func[qi_, dei_, b_, dmin_, rt_, pt_, t_] := Module[{rtt = Max[0,rt], ptt = Max[0,pt]},
   (*do stuff with rtt and ptt instead*) ]

What are the pros and cons of each function definition? Which is the most efficient?

$\endgroup$
6
  • $\begingroup$ Sorry I forgot to update my old answer but you'll get more diverse opinions this way anyhow. $\endgroup$
    – Mr.Wizard
    Feb 25, 2013 at 15:57
  • $\begingroup$ @Mr.Wizard No big deal. $\endgroup$
    – kale
    Feb 25, 2013 at 16:00
  • $\begingroup$ Methink you've lost the braces around some functions' arguments. I'm not editing your question because you already rolled back one edit. But please recheck $\endgroup$ Feb 25, 2013 at 16:41
  • $\begingroup$ Your function with _?Negative doesn't do what the other does, for instance if you pass rt=-3; pt=1 it will be evaluated as rt=0; pt=0 instead of rt=0;pt=1 $\endgroup$
    – ssch
    Feb 25, 2013 at 17:16
  • $\begingroup$ @belisarius Yep, sorry. Some extra braces made it in there. Edited... $\endgroup$
    – kale
    Feb 25, 2013 at 18:47

1 Answer 1

5
$\begingroup$

To start the discussion I'll look at timings. A simplistic test indicates that the DownValues method is somewhat faster, and this agrees with my past experience as well.

f1[a_, b_, c_, d_] := f1[{a, b, Max[c, 0], Max[d, 0]}]
f1[{a_, b_, c_, d_}] := a + b + c + d

f2[a_, b_, c0_, d0_] := With[{c = Max[c0, 0], d = Max[d0, 0]}, a + b + c + d]

a = RandomInteger[{-5, 5}, {1*^6, 4}];

f1 @@@ a // Timing // First
f2 @@@ a // Timing // First

1.872

2.012

Note that I changed Module to With. This should always be faster when it can be used.
For comparison:

f2m[a_, b_, c0_, d0_] := Module[{c = Max[c0, 0], d = Max[d0, 0]}, a + b + c + d]

f2m @@@ a // Timing // First

3.791

$\endgroup$
2
  • $\begingroup$ Wow... I wasn't aware that Module is so much slower! $\endgroup$
    – sebhofer
    Feb 25, 2013 at 18:20
  • $\begingroup$ @sebhofer Yes, for simple (fast) functions the overhead of Module is significant. With does not let you modify variables. Block is faster than Module but you have to be very careful to avoid unintended consequences. You can often combine e.g. Module outside the definition of the function and Block inside for something reasonably fast. $\endgroup$
    – Mr.Wizard
    Feb 26, 2013 at 1:55

Your Answer

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

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