Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Function with optional default argument cannot cache
Replies: 2   Last Post: May 1, 2013 9:39 PM

 Search Thread: Advanced Search

 Messages: [ Previous | Next ]
 Sseziwa Mukasa Posts: 108 Registered: 8/26/07
Re: Function with optional default argument cannot cache results
Posted: May 1, 2013 9:39 PM
 Plain Text Reply

On May 1, 2013, at 3:37 AM, Dan O'Brien <danobrie@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}}=

Date Subject Author
5/1/13 waku
5/1/13 Sseziwa Mukasa

© The Math Forum at NCTM 1994-2017. All Rights Reserved.