#8342: Efficient Arbitrary Sequence Generator
---------------------------+------------------------------------------------
   Reporter:  colah        |       Owner:  jkantor            
       Type:  enhancement  |      Status:  new                
   Priority:  major        |   Milestone:  sage-4.3.4         
  Component:  numerical    |    Keywords:  sequence, generator
     Author:  colah        |    Upstream:  N/A                
   Reviewer:               |      Merged:                     
Work_issues:               |  
---------------------------+------------------------------------------------
 {{{
 def construct_sequence(base,f):
     """
     Returns a function that gives sequence terms, building off past calls.

     Input:
        - `base` -- the base case(s) of the sequence.
        - `f`    -- the recursive description of the sequence; it takes a
 sequence referencing function and returns a function that will, given n,
 return the nth term.

     Output:
        A function that, given n, returns the nth term in the sequence.

     Examples:
        sage: fib = construct_sequence([1,1], lambda t: lambda n : t(n-1) +
 t(n-2)) #The Fibonacci sequence

        sage: hof = construct_sequence([1,1], lambda t: lambda n :
 t(n-t(n-1)) + t(n-t(n-2))) #The Hofstadter Q sequence
        sage: [hof(n) for n in range(100)] # List of 100 terms
     """
     b = {}
     for i in range(len(base)):
         b[i] = base[i]
     def get_term(n):
         if n in b.keys(): return b[n]
         else:
             b[n] = f(get_term)(n)
             return b[n]
     return get_term
 }}}

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/8342>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to