File size: 4,127 Bytes
aff9293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Different exact learning/control flows sharing only an arithmetic contract."""
from __future__ import annotations
import numpy as np
from arithmetic import P, inv_scalar


def padded(a, n):
    out = np.zeros(n, dtype=np.int64)
    out[:min(n, len(a))] = a[:n]
    return out


def reciprocal(f, n, k):
    """Learn the causal inverse filter g: f*g = 1 modulo x**n."""
    if not 1 <= n <= 4096 or len(f) < n:
        raise ValueError('Invalid precision/input')
    g = np.array([inv_scalar(int(f[0]), k)], dtype=np.int64)
    while len(g) < n:
        m = min(2*len(g), n)
        fg = padded(k.mul(f[:m], g)[:m], m)
        correction = (-fg) % P
        correction[0] = (correction[0]+2) % P
        k.meter.add += m+1
        g = k.mul(g, correction)[:m]
    return g


def derivative(f, k):
    if len(f) < 2:
        return np.zeros(1, dtype=np.int64)
    k.meter.mul += len(f)-1
    return f[1:]*np.arange(1, len(f), dtype=np.int64) % P


def integral(f, n, k):
    if n >= P:
        raise ValueError('Integration needs invertible coefficient indices')
    out = np.zeros(n, dtype=np.int64)
    for i in range(1, min(n, len(f)+1)):
        out[i] = int(f[i-1])*inv_scalar(i, k) % P
        k.meter.mul += 1
    return out


def logarithm(g, n, k):
    if int(g[0]) != 1:
        raise ValueError('Formal logarithm requires constant one')
    gg = padded(g, n)
    ratio = k.mul(derivative(gg, k), reciprocal(gg, n, k))[:n-1]
    return integral(ratio, n, k)


def exponential(f, n, k):
    """Learn a solution of g'=f'g, g(0)=1, by Newton lifting."""
    if int(f[0]) != 0 or n >= P:
        raise ValueError('Invalid formal-exponential contract')
    g = np.ones(1, dtype=np.int64)
    while len(g) < n:
        m = min(2*len(g), n)
        correction = (f[:m]-logarithm(g, m, k)) % P
        correction[0] = (correction[0]+1) % P
        k.meter.add += m+1
        g = k.mul(g, correction)[:m]
    return g


def composition(f, g, n, k):
    if int(g[0]) != 0:
        raise ValueError('Truncated composition requires inner constant zero')
    z = np.zeros(n, dtype=np.int64)
    for a in reversed(f):
        z = padded(k.mul(z, g)[:n], n)
        z[0] = (z[0]+int(a)) % P
        k.meter.add += 1
    return z


def reversion(f, n, k):
    """Learn a local inverse map: f(g(x))=x modulo x**n."""
    if len(f) < 2 or int(f[0]) != 0 or int(f[1]) != 1:
        raise ValueError('Reversion requires f(0)=0 and f\'(0)=1')
    if n < 2:
        return np.zeros(n, dtype=np.int64)
    g = np.array([0, 1], dtype=np.int64)
    df = derivative(f, k)
    while len(g) < n:
        m = min(2*len(g)-1, n)
        gg = padded(g, m)
        r = composition(f, gg, m, k)
        r[1] = (r[1]-1) % P
        k.meter.add += 1
        jac = composition(df, gg, m, k)
        delta = k.mul(r, reciprocal(jac, m, k))[:m]
        g = (gg-delta) % P
        k.meter.add += m
    return g


def catalan(n, k):
    """Learn the implicit solution y=1+x*y**2; a nonlinear task family."""
    g = np.ones(1, dtype=np.int64)
    while len(g) < n:
        m = min(2*len(g), n)
        gg = padded(g, m)
        square = k.mul(gg, gg)[:m-1]
        r = gg.copy()
        r[0] = (r[0]-1) % P
        r[1:] = (r[1:]-square) % P
        jac = np.zeros(m, dtype=np.int64)
        jac[0] = 1
        jac[1:] = (-2*gg[:-1]) % P
        k.meter.add += m
        k.meter.mul += m-1
        delta = k.mul(r, reciprocal(jac, m, k))[:m]
        g = (gg-delta) % P
        k.meter.add += m
    return g


def reference_reciprocal(f, n):
    """Independent O(n^2) coefficient recurrence; never calls Kernel."""
    g = [pow(int(f[0]), P-2, P)]
    for t in range(1, n):
        g.append(-g[0]*sum(int(f[j])*g[t-j] for j in range(1, t+1)) % P)
    return np.array(g, dtype=np.int64)


def reference_exp(f, n):
    g = [1]
    for t in range(1, n):
        g.append(sum(j*int(f[j])*g[t-j] for j in range(1, t+1)) *
                 pow(t, P-2, P) % P)
    return np.array(g, dtype=np.int64)


def reference_catalan(n):
    g = [1]
    for t in range(1, n):
        g.append(sum(g[j]*g[t-1-j] for j in range(t)) % P)
    return np.array(g, dtype=np.int64)