Ticket #7742: trac_7742-add-compose-etc_v2.1.patch

File trac_7742-add-compose-etc_v2.1.patch, 4.1 KB (added by flawrence, 10 years ago)

checks that n is a nonnegative integer

  • sage/misc/all.py

    # HG changeset patch
    # User Christopher Olah <christopherolah.co@gmail.com>
    # Date 1268530242 18000
    # Node ID 3058046e868797e22f1318b4f182414588823f09
    # Parent  8438b7c20d79c02a2ece3e1c3f7224a772ff8f07
    Trac 7742: add compose, self_compose and nest functions
    
    diff -r 8438b7c20d79 -r 3058046e8687 sage/misc/all.py
    a b  
    77                  repr_lincomb, tmp_dir, tmp_filename,
    88                  pad_zeros, attrcall,
    99                  DOT_SAGE, SAGE_ROOT, SAGE_URL, SAGE_DB, SAGE_TMP,
    10                   is_32_bit, is_64_bit, newton_method_sizes)
     10                  is_32_bit, is_64_bit, newton_method_sizes, compose,
     11                  self_compose, nest)
    1112
    1213from misc_c import prod, running_total, balanced_sum
    1314
  • sage/misc/misc.py

    diff -r 8438b7c20d79 -r 3058046e8687 sage/misc/misc.py
    a b  
    826826            z = z[0]
    827827        attr = "_" + z[len(x.__module__)+1:] + attr
    828828    x.__dict__[attr] = init
     829
     830
     831def compose(f, g):
     832    """
     833    Return the composition of one-variable functions: `f \circ g`
    829834   
     835    See also :func:`self_compose()` and :func:`nest()`
     836   
     837    INPUT:
     838        - `f` -- a function of one variable
     839        - `g` -- another function of one variable
     840   
     841    OUTPUT:
     842        A function, such that compose(f,g)(x) = f(g(x))
     843   
     844    EXAMPLES::
     845   
     846        sage: def g(x): return 3*x
     847        sage: def f(x): return x + 1
     848        sage: h1 = compose(f,g)
     849        sage: h2 = compose(g,f)
     850        sage: _ = var ('x')
     851        sage: h1(x)
     852        3*x + 1
     853        sage: h2(x)
     854        3*x + 3
     855   
     856    ::
     857        sage: _ = function('f g')
     858        sage: _ = var ('x')
     859        sage: compose(f,g)(x)
     860        f(g(x))
     861       
     862    """
     863    return lambda x: f(g(x))
     864
     865
     866def self_compose(f, n):
     867    """
     868    Return the function `f` composed with itself `n` times.
     869   
     870    See :func:`nest()` if you want `f(f(...(f(x))...))` for
     871    known `x`.
     872   
     873   
     874    INPUT:
     875        - `f` -- a function of one variable
     876        - `n` -- a nonnegative integer
     877       
     878    OUTPUT:
     879        A function, the result of composing `f` with itself `n` times
     880   
     881    EXAMPLES::
     882   
     883        sage: def f(x): return x^2 + 1
     884        sage: g = self_compose(f, 3)
     885        sage: x = var('x')
     886        sage: g(x)
     887        ((x^2 + 1)^2 + 1)^2 + 1
     888   
     889    ::
     890       
     891        sage: def f(x): return x + 1
     892        sage: g = self_compose(f, 10000)
     893        sage: g(0)
     894        10000
     895   
     896    ::
     897       
     898        sage: x = var('x')
     899        sage: self_compose(sin, 0)(x)
     900        x
     901       
     902    """
     903    from sage.rings.all import Integer
     904   
     905    typecheck(n, (int, long, Integer), 'n')
     906    if n < 0:
     907        raise ValueError, "n must be a nonnegative integer, not %s." % n
     908   
     909    return lambda x: nest(f, n, x)
     910
     911
     912def nest(f, n, x):
     913    """
     914    Return `f(f(...f(x)...))`, where the composition occurs n times.
     915   
     916    See also :func:`compose()` and :func:`self_compose()`
     917   
     918    INPUT:
     919        - `f` -- a function of one variable
     920        - `n` -- a nonnegative integer
     921        - `x` -- any input for `f`
     922   
     923    OUTPUT:
     924        `f(f(...f(x)...))`, where the composition occurs n times
     925   
     926    EXAMPLES::
     927   
     928        sage: def f(x): return x^2 + 1
     929        sage: x = var('x')
     930        sage: nest(f, 3, x)
     931        ((x^2 + 1)^2 + 1)^2 + 1
     932   
     933    ::
     934       
     935        sage: _ = function('f')
     936        sage: _ = var('x')
     937        sage: nest(f, 10, x)
     938        f(f(f(f(f(f(f(f(f(f(x))))))))))
     939   
     940    ::
     941       
     942        sage: _ = function('f')
     943        sage: _ = var('x')
     944        sage: nest(f, 0, x)
     945        x
     946   
     947    """
     948    from sage.rings.all import Integer
     949   
     950    typecheck(n, (int, long, Integer), 'n')
     951    if n < 0:
     952        raise ValueError, "n must be a nonnegative integer, not %s." % n
     953   
     954    for i in xrange(n):
     955        x = f(x)
     956    return x
     957
     958
    830959#################################################################
    831960# Ranges and [1,2,..,n] notation.
    832961#################################################################