Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:57:35+00:00 2026-05-25T14:57:35+00:00

Memoized functions are functions which remember values they have found. Look in the doc

  • 0

Memoized functions are functions which remember values they have found.
Look in the doc center for some background on this in Mathematica, if necessary.

Suppose you have the following definition

f[0] = f[1] = 1
f[x_] := f[x] = f[x - 1] + f[x - 2]

in one of your packages. A user may load the package and start asking right away f[1000].
This will trigger a $RecursionLimit::reclim error message and abort.
Even if the user then tries something smaller, say f[20], by now the definition of f is corrupt and the result is not good anymore.Of course the package developer might increase the recursion limit and warn the user, but my question is:

How can you improve the f definition so that if the user asks for f[1000] he/she gets the answer without any problem? I am interested in a way to trap the user input, analyze it and take whatever steps are necessary to evaluate f[1000].

I can easily imagine that one can change the recursion limit if the input is more than 255 (and then bring it back to the original level), but what I would really like to see is, if there is a way for the f to find out how many values it “knows” (fknownvalues) and accept any input <=fknownvalues+$RecursionLimit without problems or increase the $RecursionLimit if the input is higher.

Thank you for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:57:36+00:00Added an answer on May 25, 2026 at 2:57 pm

    Here is the code assuming that you can determine a value of $RecursionLimit from the value of the input argument:

    Clear[f];
    Module[{ff},
      ff[0] = ff[1] = 1;
      ff[x_] := ff[x] = ff[x - 1] + ff[x - 2];
    
      f[x_Integer] :=f[x] =
         Block[{$RecursionLimit = x + 5},
            ff[x]
      ]]
    

    I am using a local function ff to do the main work, while f just calls it wrapped in Block with a proper value for $RecursionLimit:

    In[1552]:= f[1000]
    Out[1552]=  7033036771142281582183525487718354977018126983635873274260490508715453711819693357974224
    9494562611733487750449241765991088186363265450223647106012053374121273867339111198139373125
    598767690091902245245323403501  
    

    EDIT

    If you want to be more precise with the setting of $RecursionLimit, you can modify the part of the code above as:

    f[x_Integer] :=
      f[x] =
        Block[{$RecursionLimit = x - Length[DownValues[ff]] + 10},
        Print["Current $RecursionLimit: ", $RecursionLimit];
        ff[x]]]
    

    The Print statement is here for illustration. The value 10 is rather arbitrary – to get a lower bound on it, one has to compute the necessary depth of recursion, and take into account that the number of known results is Length[DownValues[ff]] - 2 (since ff has 2 general definitions). Here is some usage:

    In[1567]:= f[500]//Short
    
    During evaluation of In[1567]:= Current $RecursionLimit: 507
    Out[1567]//Short= 22559151616193633087251269<<53>>83405015987052796968498626
    
    In[1568]:= f[800]//Short
    
    During evaluation of In[1568]:= Current $RecursionLimit: 308
    Out[1568]//Short= 11210238130165701975392213<<116>>44406006693244742562963426
    

    If you also want to limit the maximal $RecursionLimit possible, this is also easy to do, along the same lines. Here, for example, we will limit it to 10000 (again, this goes inside Module):

    f::tooLarge = 
    "The parameter value `1` is too large for single recursive step. \
    Try building the result incrementally";
    f[x_Integer] :=
       With[{reclim = x - Length[DownValues[ff]] + 10},
         (f[x] =
            Block[{$RecursionLimit = reclim },
            Print["Current $RecursionLimit: ", $RecursionLimit];
            ff[x]]) /; reclim < 10000];
    
    f[x_Integer] := "" /; Message[f::tooLarge, x]]
    

    For example:

    In[1581]:= f[11000]//Short
    
    During evaluation of In[1581]:= f::tooLarge: The parameter value 11000 is too 
    large for single recursive step. Try building the result incrementally
    Out[1581]//Short= f[11000]
    
    In[1582]:= 
    f[9000];
    f[11000]//Short
    
    During evaluation of In[1582]:= Current $RecursionLimit: 9007
    During evaluation of In[1582]:= Current $RecursionLimit: 2008
    Out[1583]//Short= 5291092912053548874786829<<2248>>91481844337702018068766626
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working memoize decorator which uses Django's cache backend to remember the
I wrote some code that looks like this: def get(x, y) @cachedResults.set(x,y, Math.hypot(x, y))
Well, I have this bit of code that is slowing down the program hugely
I would like to redefine some functions in my program on startup according to
I found this nice memoizing decorator: http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize The particular application is in artificial intelligence,
I have memoized the factorial function in C as follows: int fact(int n) {
I'm working with some government data published via Socrata's SODA api . This API
I have a decorated function (simplified version): class Memoize: def __init__(self, function): self.function =
I'm trying to create a memoization interface for functions with arbitrary number of arguments,
Python doesn't support complicated anonymous functions. What's a good alternative? For example: class Calculation:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.