|
| 1 | +mutable struct Atomic{T} |
| 2 | + @atomic x::T |
| 3 | +end |
| 4 | + |
| 5 | +struct CuytLeeError <: Exception |
| 6 | + msg::String |
| 7 | +end |
| 8 | + |
| 9 | +Base.show(io::IO, e::CuytLeeError) = print(io, "CuytLeeError: ", e.msg) |
| 10 | + |
| 11 | +function _random_point(n::Int)::Vector{Int} |
| 12 | + map(a -> 1 + abs(a) % 99, rand(Int, n)) |
| 13 | +end |
| 14 | + |
| 15 | +function _estimate_total_degree( |
| 16 | + R::QQMPolyRing, |
| 17 | + bb::Function; |
| 18 | + samples::Int=5, |
| 19 | + show_progress::Bool=false |
| 20 | +)::Tuple{Int,Int} |
| 21 | + t = length(gens(R)) |
| 22 | + @assert t > 0 |
| 23 | + R_z, _ = polynomial_ring(QQ, :z) |
| 24 | + total_degree_counts = Dict{Tuple{Int,Int},Int}() |
| 25 | + total = 0 |
| 26 | + while total < samples |
| 27 | + x = _random_point(t) |
| 28 | + try |
| 29 | + f = thiele(R_z, k -> bb(k .* x); show_progress=show_progress) |
| 30 | + total_degree = (degree(denominator(f)), degree(numerator(f))) |
| 31 | + total_degree_counts[total_degree] = get(total_degree_counts, total_degree, 0) + 1 |
| 32 | + total += 1 |
| 33 | + if findmax(total_degree_counts)[1] > samples ÷ 2 |
| 34 | + break |
| 35 | + end |
| 36 | + catch e |
| 37 | + if isa(e, ThieleError) |
| 38 | + continue |
| 39 | + else |
| 40 | + rethrow(e) |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + return findmax(total_degree_counts)[2] |
| 45 | +end |
| 46 | + |
| 47 | +function _homogenize( |
| 48 | + f::QQMPolyRingElem, |
| 49 | + d::Int |
| 50 | +)::QQMPolyRingElem |
| 51 | + R = parent(f) |
| 52 | + C = MPolyBuildCtx(R) |
| 53 | + for i in 1:f.length |
| 54 | + exp = exponent_vector(f, i) |
| 55 | + @assert exp[1] == 0 |
| 56 | + total_deg = sum(exp) |
| 57 | + exp[1] += d - total_deg |
| 58 | + @assert exp[1] >= 0 |
| 59 | + push_term!(C, coeff(f, i), exp) |
| 60 | + end |
| 61 | + return finish(C) |
| 62 | +end |
| 63 | + |
| 64 | +function cuyt_lee_shifted( |
| 65 | + R::QQMPolyRing, |
| 66 | + bb::Function; |
| 67 | + retry::Int=10, |
| 68 | + nr_thrds::Int=1, |
| 69 | + show_progress::Bool=false, |
| 70 | + desc::String="Multivariate rational interpolation" |
| 71 | +)::FracFieldElem{QQMPolyRingElem} |
| 72 | + # https://arxiv.org/pdf/1608.01902 |
| 73 | + t = length(gens(R)) |
| 74 | + if t == 0 |
| 75 | + return R(bb(Vector{QQFieldElem}())) // one(R) |
| 76 | + end |
| 77 | + R_z, _ = polynomial_ring(QQ, :z) |
| 78 | + d_den, d_num = _estimate_total_degree(R, bb; show_progress=show_progress) |
| 79 | + d = [0; fill(max(d_den, d_num), t - 1)...] |
| 80 | + x = Vector{Vector{ZZRingElem}}() |
| 81 | + coeffs_den = [] |
| 82 | + coeffs_num = [] |
| 83 | + data_lock = ReentrantLock() |
| 84 | + prog = ProgressBar(total=prod(d .+ 1); desc=desc, enabled=show_progress) |
| 85 | + update!(prog, 0) |
| 86 | + function populate(cur::Vector{ZZRingElem}, dim::Int; num_threads::Int=1, offset=1)::Bool |
| 87 | + if dim > t |
| 88 | + f = nothing |
| 89 | + try |
| 90 | + f = thiele(R_z, k -> bb(k .* cur); retry=retry, show_progress=show_progress, offset=offset) |
| 91 | + catch e |
| 92 | + if isa(e, BoundsError) || isa(e, ThieleError) |
| 93 | + return false |
| 94 | + else |
| 95 | + rethrow(e) |
| 96 | + end |
| 97 | + end |
| 98 | + if degree(denominator(f)) != d_den || degree(numerator(f)) != d_num |
| 99 | + return false |
| 100 | + end |
| 101 | + c = constant_coefficient(denominator(f)) |
| 102 | + if c == 0 |
| 103 | + return false |
| 104 | + end |
| 105 | + lock(data_lock) do |
| 106 | + push!(x, copy(cur)) |
| 107 | + push!(coeffs_den, collect(coefficients(denominator(f))) ./ c) |
| 108 | + push!(coeffs_num, collect(coefficients(numerator(f))) ./ c) |
| 109 | + update!(prog, length(x)) |
| 110 | + end |
| 111 | + return true |
| 112 | + end |
| 113 | + total = Atomic(0) |
| 114 | + failures = Atomic(0) |
| 115 | + i = 1 |
| 116 | + while total.x < d[dim] + 1 |
| 117 | + num_threads_chunk = min(num_threads, d[dim] + 1 - total.x) |
| 118 | + Threads.@threads for j in 0:num_threads_chunk-1 |
| 119 | + if populate([cur; ZZ(i + j)], dim + 1; num_threads=1, offset=max(offset, j + 1)) |
| 120 | + @atomic total.x += 1 |
| 121 | + @atomic failures.x = 0 |
| 122 | + else |
| 123 | + @atomic failures.x += 1 |
| 124 | + end |
| 125 | + end |
| 126 | + i += num_threads_chunk |
| 127 | + if failures.x >= retry |
| 128 | + return false |
| 129 | + end |
| 130 | + end |
| 131 | + return true |
| 132 | + end |
| 133 | + res = populate([ZZ(1)], 2; num_threads=nr_thrds) |
| 134 | + if !res |
| 135 | + finish!(prog) |
| 136 | + throw(CuytLeeError("Failed to collect enough data points for interpolation. This could happen if the black box function is singular at zero, or if the expected total degree is incorrect.")) |
| 137 | + end |
| 138 | + perm = sortperm(x) |
| 139 | + x = x[perm] |
| 140 | + coeffs_den = coeffs_den[perm] |
| 141 | + coeffs_num = coeffs_num[perm] |
| 142 | + # We interpolate the denominator and numerator separately |
| 143 | + den = zero(R) |
| 144 | + num = zero(R) |
| 145 | + for i in 0:d_den |
| 146 | + y = [coeffs_den[j][i+1] for j in 1:length(x)] |
| 147 | + den += _homogenize(newton(R, x, y, d), i) |
| 148 | + end |
| 149 | + for i in 0:d_num |
| 150 | + y = [coeffs_num[j][i+1] for j in 1:length(x)] |
| 151 | + num += _homogenize(newton(R, x, y, d), i) |
| 152 | + end |
| 153 | + finish!(prog) |
| 154 | + return num // den |
| 155 | +end |
| 156 | + |
| 157 | +function cuyt_lee_with_shift( |
| 158 | + R::QQMPolyRing, |
| 159 | + bb::Function, |
| 160 | + shift::Vector{Int}; |
| 161 | + nr_thrds::Int=1, |
| 162 | + show_progress::Bool=false, |
| 163 | + desc::String="Multivariate rational interpolation" |
| 164 | +)::FracFieldElem{QQMPolyRingElem} |
| 165 | + t = length(gens(R)) |
| 166 | + if t == 0 |
| 167 | + return R(bb(Vector{QQFieldElem}())) // one(R) |
| 168 | + end |
| 169 | + f_shifted = cuyt_lee_shifted(R, z -> bb(z .+ shift); nr_thrds=nr_thrds, show_progress=show_progress, desc=desc) |
| 170 | + x = gens(R) .- shift |
| 171 | + num = evaluate(numerator(f_shifted), x) |
| 172 | + den = evaluate(denominator(f_shifted), x) |
| 173 | + return num // den |
| 174 | +end |
| 175 | + |
| 176 | +function cuyt_lee( |
| 177 | + R::QQMPolyRing, |
| 178 | + bb::Function; |
| 179 | + initial_shift=_random_point(length(gens(R))), |
| 180 | + retry::Int=10, |
| 181 | + nr_thrds::Int=1, |
| 182 | + show_progress::Bool=false, |
| 183 | + desc::String="Multivariate rational interpolation" |
| 184 | +)::FracFieldElem{QQMPolyRingElem} |
| 185 | + t = length(gens(R)) |
| 186 | + if t == 0 |
| 187 | + return R(bb(Vector{QQFieldElem}())) // one(R) |
| 188 | + end |
| 189 | + shift = initial_shift |
| 190 | + for i in 1:retry |
| 191 | + try |
| 192 | + return cuyt_lee_with_shift(R, bb, shift; nr_thrds=nr_thrds, show_progress=show_progress, desc=desc) |
| 193 | + catch e |
| 194 | + if isa(e, CuytLeeError) |
| 195 | + if show_progress |
| 196 | + @warn "Interpolation failed, retrying with a different shift... Retries left: $(retry - i)" |
| 197 | + end |
| 198 | + shift = _random_point(t) |
| 199 | + else |
| 200 | + rethrow(e) |
| 201 | + end |
| 202 | + end |
| 203 | + end |
| 204 | + throw(CuytLeeError("Interpolation failed after maximum number of retries.")) |
| 205 | +end |
0 commit comments