Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Function with optional default argument cannot cache results

  • To: mathgroup at smc.vnet.net
  • Subject: [mg130686] Re: Function with optional default argument cannot cache results
  • From: Sseziwa Mukasa <mukasa at gmail.com>
  • Date: Wed, 1 May 2013 21:42:51 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net
  • References: <20130501073754.8ADA36A27@smc.vnet.net> <E2002B9A-B548-4C4B-9EE9-D9479239DA0D@gmail.com> <518118E6.40501@gmail.com>

On May 1, 2013, at 9:30 AM, Dan O'Brien <danobrie at gmail.com> wrote:

> Thanks for your response.  What you say was clear to start with, I should have asked a better question.  The question is why wouldn't mathematica assign downvalues to function evaluations when the default arguments (y_:2) are used?

I can't answer why, but one reasone why may be that it would conflict with memoized functions.  For example in your case you want f[x_,y_:2] to create a downvalue f[x_,2]:>{Pause[1],1,2} but your function definition creates a downvalue f[1,2]:>{Null,1,2} so now it's not clear what value you should return for f[1,2].

>
> The reason I ask this is because I was playing with a complicated function I defined where I would cache the values.  It occurred to me that one of the parameters typically didn't change and so I gave it a default value so that I didn't need to specify it every time. Then I quickly realized the function evalutations where not being cached (downvalues were not being assigned).  That is when I specify f[1], mathematica should know that that actually means f[1,2] and give it a downvalue upon  evaluation.  So my question is, why wouldn't mathematica do this?  Would it break some other intended functionality, or, to implement that sort of recognition is just not worth it?

My guess as to why defaults don't create downvalues is above.  But you can work around this by defining values for the single argument version of your function f[x_]:=f[x,default]=...

(Debug) In[18]:= f[x_, y_] := f[x, y] = {Pause[1], 1, 2}
f[x_] := f[x, 2]
(Debug) In[20]:= f[1, 2] // AbsoluteTiming
f[1, 2] // AbsoluteTiming
f[1] // AbsoluteTiming
f[1] // AbsoluteTiming
(Debug) Out[20]= {1.000464, {Null, 1, 2}}
(Debug) Out[21]= {3.*10^-6, {Null, 1, 2}}
(Debug) Out[22]= {6.*10^-6, {Null, 1, 2}}
(Debug) Out[23]= {4.*10^-6, {Null, 1, 2}}

>
> -Dan
>
>
> On 5/1/2013 7:46 AM, Sseziwa Mukasa wrote:
>> On May 1, 2013, at 3:37 AM, Dan O'Brien <danobrie at gmail.com> wrote:
>>
>>> Is there a discussion somewhere on why this is?
>>>
>>> In[1]:= $Version
>>> f[x_, y_: 2] := f[x, y] = {Pause[1], x, y}
>>> f[1, 2] // AbsoluteTiming
>>> f[1, 2] // AbsoluteTiming
>>> f[1] // AbsoluteTiming
>>> f[1] // AbsoluteTiming
>>>
>>> Out[1]= "9.0 for Microsoft Windows (64-bit) (January 25, 2013)"
>>>
>>> Out[3]= {1.014002, {Null, 1, 2}}
>>>
>>> Out[4]= {0., {Null, 1, 2}}
>>>
>>> Out[5]= {1.029602, {Null, 1, 2}}
>>>
>>> Out[6]= {1.014002, {Null, 1, 2}}
>>>
>> The value that's cached for f[1,2] is {Null,1,2} not {Pause[1],1,2}.  Out[4] returns the cached value, f[1] doesn't match the cached pattern so f[x_,y_:2] is evaluated again in the case of Out[5] and Out[6].  You can see this by looking at the DownValues of f:
>>
>> (Debug) In[7]:= DownValues[f]
>> (Debug) Out[7]= {HoldPattern[f[1, 2]] :> {Null, 1, 2},
>>  HoldPattern[f[x_, y_ : 2]] :> (f[x, y] = {Pause[1], x, y})}
>>
>> It's not clear what you are trying to accomplish, perhaps you want:
>>
>> (Debug) In[39]:= f[x_, y_: 2] := f[x, y] = {Hold[Pause[1]], x, y}
>> ReleaseHold[f[1, 2]] // AbsoluteTiming
>> ReleaseHold[f[1, 2]] // AbsoluteTiming
>> ReleaseHold[f[1]] // AbsoluteTiming
>> ReleaseHold[f[1]] // AbsoluteTiming
>> (Debug) Out[40]= {1.000602, {Null, 1, 2}}
>> (Debug) Out[41]= {1.000028, {Null, 1, 2}}
>> (Debug) Out[42]= {1.000572, {Null, 1, 2}}
>> (Debug) Out[43]= {1.000699, {Null, 1, 2}}
>




  • Prev by Date: Re: Intersection points of two contour plots
  • Next by Date: Formula Stirlinga
  • Previous by thread: Re: Function with optional default argument cannot cache
  • Next by thread: Re: Function with optional default argument cannot cache results