Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions problems/near-coincident-sinkhorn-divergence/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
from decimal import Context, Decimal
import numpy as np
from scipy.linalg import cho_factor, cho_solve, solve as linsolve
from scipy.spatial.distance import cdist

SOLVER = "@xiaoxiaoxiaotao"
HARNESS = "antigravity, devin"
MODEL = "gemini-4-neon, swe-2.1-max"
METHOD = "Vectorized double-double kernel and marginal refinement with Schur-complement Newton warm start"


def _build_pow2_table():
ctx = Context(prec=50)
two = Decimal(2)
d64 = Decimal(64)
hi = np.empty(64, dtype=np.float64)
lo = np.empty(64, dtype=np.float64)
for k in range(64):
val = ctx.power(two, ctx.divide(Decimal(k), d64))
h = float(val)
hi[k] = h
lo[k] = float(ctx.subtract(val, Decimal(h)))
ln2_64 = ctx.divide(ctx.ln(two), d64)
lsh = float(ln2_64)
lsl = float(ctx.subtract(ln2_64, Decimal(lsh)))
return hi, lo, np.float64(lsh), np.float64(lsl)


PH, PL, LSH, LSL = _build_pow2_table()


def ts(a, b):
s = a + b
z = s - a
return s, (a - (s - z)) + (b - z)


def tp(a, b):
p = a * b
ca = 134217729.0 * a
ah = ca - (ca - a)
al = a - ah
cb = 134217729.0 * b
bh = cb - (cb - b)
bl = b - bh
return p, ((ah * bh - p) + ah * bl + al * bh) + al * bl


def add(ah, al, bh, bl):
s, e = ts(ah, bh)
return ts(s, e + al + bl)


def sub(ah, al, bh, bl):
return add(ah, al, -bh, -bl)


def mul(ah, al, bh, bl):
p, e = tp(ah, bh)
return ts(p, e + ah * bl + al * bh)


def div(ah, al, bh, bl):
q = np.asarray(ah) / np.asarray(bh)
ph, pl = tp(q, bh)
return ts(q, ((ah - ph) - pl + al - q * bl) / bh)


def sum0(ph, pl):
h, l = ph, pl
while len(h) > 1:
m = len(h) // 2
hh, ll = add(h[:m], l[:m], h[m : 2 * m], l[m : 2 * m])
if len(h) % 2:
hh = np.r_[hh, h[-1:]]
ll = np.r_[ll, l[-1:]]
h, l = hh, ll
return h[0], l[0]


def matvec(Kh, Kl, vh, vl):
ph, pl = mul(Kh, Kl, vh[None, :], vl[None, :])
return sum0(np.transpose(ph), np.transpose(pl))


def expdd(xh, xl):
qf = np.rint(xh * (64.0 / np.log(2.0)))
ch, cl = tp(qf, LSH)
rh, rl = sub(xh, xl, ch, cl + qf * LSL)
sh, sl = add(np.ones_like(rh), np.zeros_like(rh), rh, rl)
r2h, r2l = mul(rh, rl, rh, rl)
t = ((((rh / 5040.0 + 1.0 / 720.0) * rh + 1.0 / 120.0) * rh + 1.0 / 24.0) * rh + 1.0 / 6.0)
th, tl = ts(0.5, t * rh)
th, tl = mul(r2h, r2l, th, tl)
sh, sl = add(sh, sl, th, tl)
q = qf.astype(np.int64)
n = np.floor_divide(q, 64)
j = (q - 64 * n).astype(int)
sh, sl = mul(sh, sl, PH[j], PL[j])
return np.ldexp(sh, n), np.ldexp(sl, n)


def logdd(xh, xl):
yh, yl = ts(np.log(xh), xl / xh)
eh, el = expdd(yh, yl)
qh, ql = div(eh, el, xh, xl)
rh, rl = sub(qh, ql, np.ones_like(qh), np.zeros_like(qh))
return sub(yh, yl, rh, rl)


def kernel(x, y, e):
K0 = -cdist(x, y, "sqeuclidean")
eh = np.float64(e)
rh, rl = div(1.0, 0.0, eh, np.zeros_like(eh))
Kh, Kl = mul(K0, np.zeros_like(K0), rh, rl)
if x is y:
iu = np.triu_indices(len(x))
eh2, el2 = expdd(Kh[iu], Kl[iu])
Kh = np.zeros_like(K0)
Kl = np.zeros_like(K0)
Kh[iu] = eh2
Kl[iu] = el2
Kh[(iu[1], iu[0])] = eh2
Kl[(iu[1], iu[0])] = el2
return Kh, Kl
return expdd(Kh, Kl)


def crossdd(a, b, Kh, Kl):
a = np.asarray(a, float)
b = np.asarray(b, float)
v = np.ones_like(b)
for _ in range(48):
v = b / (Kh.T @ (a / (Kh @ v)))
f = np.log(v)
f -= f[-1]
for _ in range(3):
v = np.exp(f)
u = a / (Kh @ v)
P = (u[:, None] * Kh) * v
c = P.sum(0)
J = np.diag(c) - (P.T / a[None, :]) @ P
f[:-1] += cho_solve(cho_factor(J[:-1, :-1]), (b - c)[:-1])
fh, fl = f.copy(), np.zeros_like(f)
vh, vl = expdd(fh, fl)
kvh, kvl = matvec(Kh, Kl, vh, vl)
uh, ul = div(a, np.zeros_like(a), kvh, kvl)
P = (uh[:, None] * Kh) * vh[None, :]
c = P.sum(0)
J = np.diag(c) - (P.T / a[None, :]) @ P
hh = np.zeros_like(fh)
hh[:-1] = cho_solve(cho_factor(J[:-1, :-1]), np.asarray((b - c)[:-1], float))
fh, fl = add(fh, fl, hh, np.zeros_like(hh))
luh, lul = logdd(uh, ul)
corr = (P / a[:, None]) @ hh
luh, lul = sub(luh, lul, corr, np.zeros_like(corr))
return luh, lul, fh, fl


def symdd(a, Kh, Kl):
a = np.asarray(a, float)
u = np.sqrt(a)
for _ in range(24):
u = np.sqrt(u * a / (Kh @ u))
fh = np.log(u)
fl = np.zeros_like(fh)
uh, ul = expdd(fh, fl)
kuh, kul = matvec(Kh, Kl, uh, ul)
qh, ql = mul(uh, ul, kuh, kul)
rh, rl = sub(a, np.zeros_like(a), qh, ql)
P = (uh[:, None] * Kh) * uh[None, :]
J = np.eye(len(a)) + P / a[:, None]
return add(fh, fl, linsolve(J, np.asarray(rh / a, float)), np.zeros_like(rh))


def valsum(w, lh, ll):
wf = np.asarray(w, float)
zh, zl = mul(wf, np.zeros_like(wf), lh, ll)
return sum0(zh[:, None], zl[:, None])


def solve(inp):
x, a, y, b, e = inp
Kabh, Kabl = kernel(x, y, e)
Kaah, Kaal = kernel(x, x, e)
Kbbh, Kbbl = kernel(y, y, e)
uah, ual, vah, val = crossdd(a, b, Kabh, Kabl)
xaah, xaal = symdd(a, Kaah, Kaal)
xbbh, xbbl = symdd(b, Kbbh, Kbbl)
lah, lal = sub(uah, ual, xaah, xaal)
lbh, lbl = sub(vah, val, xbbh, xbbl)
sah, sal = valsum(a, lah, lal)
sbh, sbl = valsum(b, lbh, lbl)
sh, sl = add(sah, sal, sbh, sbl)
return float(np.sum(mul(sh, sl, np.full_like(sh, float(e)), np.zeros_like(sh))))