recursion_usecases.py 4.04 KB
Newer Older
dugupeiwen's avatar
dugupeiwen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
Usecases of recursive functions.

Some functions are compiled at import time, hence a separate module.
"""

from numba import jit


@jit("i8(i8)", nopython=True)
def fib1(n):
    if n < 2:
        return n
    # Note the second call uses a named argument
    return fib1(n - 1) + fib1(n=n - 2)


def make_fib2():
    @jit("i8(i8)", nopython=True)
    def fib2(n):
        if n < 2:
            return n
        return fib2(n - 1) + fib2(n=n - 2)

    return fib2

fib2 = make_fib2()


def make_type_change_self(jit=lambda x: x):
    @jit
    def type_change_self(x, y):
        if x > 1 and y > 0:
            return x + type_change_self(x - y, y)
        else:
            return y
    return type_change_self


# Implicit signature
@jit(nopython=True)
def fib3(n):
    if n < 2:
        return n
    return fib3(n - 1) + fib3(n - 2)


# Run-away self recursion
@jit(nopython=True)
def runaway_self(x):
    return runaway_self(x)


@jit(nopython=True)
def raise_self(x):
    if x == 1:
        raise ValueError("raise_self")
    elif x > 0:
        return raise_self(x - 1)
    else:
        return 1


# Mutual recursion
@jit(nopython=True)
def outer_fac(n):
    if n < 1:
        return 1
    return n * inner_fac(n - 1)


@jit(nopython=True)
def inner_fac(n):
    if n < 1:
        return 1
    return n * outer_fac(n - 1)


# Mutual recursion with different arg names
def make_mutual2(jit=lambda x: x):
    @jit
    def foo(x):
        if x > 0:
            return 2 * bar(z=1, y=x)
        return 1 + x

    @jit
    def bar(y, z):
        return foo(x=y - z)

    return foo, bar


# Mutual runaway recursion

@jit(nopython=True)
def runaway_mutual(x):
    return runaway_mutual_inner(x)


@jit(nopython=True)
def runaway_mutual_inner(x):
    return runaway_mutual(x)


# Mutual type changing recursion

def make_type_change_mutual(jit=lambda x: x):
    @jit
    def foo(x, y):
        if x > 1 and y > 0:
            # call bar first to exercise partial type inference.
            # typeinferer suspended at the call to bar() and haven't determined
            # the potential return type from the else-branch
            return x + bar(x - y, y)
        else:
            return y

    @jit
    def bar(x, y):
        if x > 1 and y > 0:
            return x + foo(x - y, y)
        else:
            return y

    return foo


# Indirect mutual recursion
def make_four_level(jit=lambda x: x):
    @jit
    def first(x):
        # The recursing call must have a path that is non-recursing.
        if x > 0:
            return second(x) * 2
        else:
            return 1

    @jit
    def second(x):
        return third(x) * 3

    @jit
    def third(x):
        return fourth(x) * 4

    @jit
    def fourth(x):
        return first(x / 2 - 1)

    return first


def make_inner_error(jit=lambda x: x):
    @jit
    def outer(x):
        if x > 0:
            return inner(x)

        else:
            return 1

    @jit
    def inner(x):
        if x > 0:
            return outer(x - 1)
        else:
            # this branch is actually never executed
            return error_fun(x)

    @jit
    def error_fun(x):
        # to trigger an untyped attribute error
        return x.ndim

    return outer


def make_raise_mutual(jit=lambda x: x):
    @jit
    def outer(x):
        if x > 0:
            return inner(x)
        else:
            return 1

    @jit
    def inner(x):
        if x == 1:
            raise ValueError('raise_mutual')
        elif x > 0:
            return outer(x - 1)
        else:
            return 1

    return outer


def make_optional_return_case(jit=lambda x: x):
    @jit
    def foo(x):
        if x > 5:
            return x - 1
        else:
            return

    @jit
    def bar(x):
        out = foo(x)
        if out is None:
            return out
        elif out < 8:
            return out
        else:
            return x * bar(out)

    return bar


def make_growing_tuple_case(jit=lambda x: x):
    # From issue #4387
    @jit
    def make_list(n):
        if n <= 0:
            return None

        return (n, make_list(n - 1))
    return make_list