| 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 | |
| 866 | def 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 | |
| 912 | def 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 | |