import itertools import random from fractions import Fractio...
Prompt
import itertools import random from fractions import Fraction import sympy as sp x, y = sp.symbols('x y') def monomials(d): return [(i, s - i) for s in range(1, d + 1) for i in range(s + 1)] def jacobian_determinant(P, Q): return sp.expand(sp.diff(P, x) * sp.diff(Q, y) - sp.diff(P, y) * sp.diff(Q, x)) def total_degree(f): f = sp.expand(f) if f == 0: return -1 return sp.Poly(f, x, y).total_degree() def diff_x(P): res = {} for (i, j), c in P.items(): if i > 0: val = c * Fraction(i) if val != Fraction(0): res[(i - 1, j)] = val return res def diff_y(P): res = {} for (i, j), c in P.items(): if j > 0: val = c * Fraction(j) if val != Fraction(0): res[(i, j - 1)] = val return res def poly_mul(A, B): res = {} for (i1, j1), c1 in A.items(): for (i2, j2), c2 in B.items(): k = (i1 + i2, j1 + j2) res[k] = res.get(k, Fraction(0)) + c1 * c2 return {k: v for k, v in res.items() if v != Fraction(0)} def poly_sub(A, B): res = dict(A) for k, v in B.items(): res[k] = res.get(k, Fraction(0)) - v return {k: v for k, v in res.items() if v != Fraction(0)} def jacobian_dict(P, Q): Px = diff_x(P) Py = diff_y(P) Qx = diff_x(Q) Qy = diff_y(Q) return poly_sub(poly_mul(Px, Qy), poly_mul(Py, Qx)) def solve_linear_system(A, b): n_rows = len(A) if n_rows == 0: return None n_cols = len(A[0]) M = [A[i] + [b[i]] for i in range(n_rows)] row = 0 col = 0 pivot_cols = [] while row < n_rows and col < n_cols: pivot_row = row while pivot_row < n_rows and M[pivot_row][col] == Fraction(0): pivot_row += 1 if pivot_row == n_rows: col += 1 continue M[row], M[pivot_row] = M[pivot_row], M[row] pivot_val = M[row][col] for j in range(col, n_cols + 1): M[row][j] /= pivot_val for i in range(row + 1, n_rows): factor = M[i][col] if factor != Fraction(0): for j in range(col, n_cols + 1): M[i][j] -= factor * M[row][j] pivot_cols.append(col) row += 1 col += 1 for i in range(row, n_rows): if M[i][n_cols] != Fraction(0): return None solution = [Fraction(0)] * n_cols curr_row = len(pivot_cols) - 1 for p_col in reversed(pivot_cols): val = M[curr_row][n_cols] for j in range(p_col + 1, n_cols): val -= M[curr_row][j] * solution[j] solution[p_col] = val curr_row -= 1 return solution def sympy_to_dict(expr): expr = sp.expand(expr) if expr == 0: return {} poly = sp.Poly(expr, x, y) res = {} for monom, coeff in zip(poly.monoms(), poly.coeffs()): if coeff != 0: res[(monom[0], monom[1])] = Fraction(str(coeff)) return res def dict_to_sympy(poly_dict): if not poly_dict: return sp.S.Zero return sp.expand(sp.Add(*[sp.Rational(c.numerator, c.denominator) * x**i * y**j for (i, j), c in poly_dict.items()])) def keller_partner_fast(P_dict, dQ): mons = monomials(dQ) J_cols = [] all_monos = set() for (i, j) in mons: m = {(i, j): Fraction(1)} Jm = jacobian_dict(P_dict, m) J_cols.append(Jm) all_monos.update(Jm.keys()) all_monos = list(all_monos) if (0, 0) not in all_monos: all_monos.append((0, 0)) n_rows = len(all_monos) n_cols = len(mons) A = [[Fraction(0)] * n_cols for _ in range(n_rows)] b = [Fraction(0)] * n_rows mono_to_row = {m: r for r, m in enumerate(all_monos)} for c, Jm in enumerate(J_cols): for m, coeff in Jm.items(): A[mono_to_row[m]][c] = coeff b[mono_to_row[(0, 0)]] = Fraction(1) sol = solve_linear_system(A, b) if sol is None: return None Q_dict = {} for c_val, (i, j) in zip(sol, mons): if c_val != Fraction(0): Q_dict[(i, j)] = c_val if not Q_dict: return None J_res = jacobian_dict(P_dict, Q_dict) J_res = {k: v for k, v in J_res.items() if v != Fraction(0)} if J_res == {(0, 0): Fraction(1)}: return Q_dict return None def is_counterexample_fast(P_dict, Q_dict): a = max([i + j for i, j in P_dict.keys()] + [-1]) b = max([i + j for i, j in Q_dict.keys()] + [-1]) if a < 1 or b < 1: return False return (b % a != 0) and (a % b != 0) def random_poly_dict(d, R): mons = monomials(d) while True: poly_dict = {} for (i, j) in mons: c = random.randint(-R, R) if c != 0: poly_dict[(i, j)] = Fraction(c) if not poly_dict: continue if max(i + j for i, j in poly_dict.keys()) == d: return poly_dict def keller_partner(P, dQ): P_dict = sympy_to_dict(P) Q_dict = keller_partner_fast(P_dict, dQ) if Q_dict is None: return None return dict_to_sympy(Q_dict) def is_counterexample(P, Q): J = jacobian_determinant(P, Q) if not (J.is_number and J != 0): return False a = total_degree(P) b = total_degree(Q) if a < 1 or b < 1: return False return (b % a != 0) and (a % b != 0) def random_poly(d, R): return dict_to_sympy(random_poly_dict(d, R)) def self_test(): Q = keller_partner(x + y**3, 1) print("Ănteszt Q =", Q, flush=True) if Q is None or jacobian_determinant(x + y**3, Q) != 1: raise RuntimeError("Az önteszt sikertelen") if is_counterexample(x + y**3, Q): raise RuntimeError("Az önteszt hamis cĂĄfolatot jelzett") print("Ănteszt rendben", flush=True) def save_result(P, Q, count): with open("cafolat.txt", "w", encoding="utf-8") as handle: handle.write(f"probalkozas = {count}\n") handle.write(f"P = {P}\n") handle.write(f"Q = {Q}\n") handle.write(f"J = {jacobian_determinant(P, Q)}\n") handle.write(f"deg P = {total_degree(P)}\n") handle.write(f"deg Q = {total_degree(Q)}\n") def search(R=2, tries_per_pair=2000, seed=None): if seed is not None: random.seed(seed) count = 0 for dP in itertools.count(2): for dQ in range(2, dP): if dP % dQ == 0: continue for _ in range(tries_per_pair): count += 1 P_dict = random_poly_dict(dP, R) Q_dict = keller_partner_fast(P_dict, dQ) if Q_dict is not None and is_counterexample_fast(P_dict, Q_dict): P = dict_to_sympy(P_dict) Q = dict_to_sympy(Q_dict) print("\n!!! CĂFOLAT !!!", flush=True) print("P =", P, flush=True) print("Q =", Q, flush=True) print("J =", jacobian_determinant(P, Q), flush=True) save_result(P, Q, count) return P, Q print(f"deg P={dP}, deg Q<={dQ} kĂ©sz, eddig {count} prĂłbĂĄlkozĂĄs", flush=True) def main(): self_test() try: result = search(R=2, tries_per_pair=2000) if result is not None: print("EredmĂ©ny elmentve: cafolat.txt", flush=True) except KeyboardInterrupt: print("\nKeresĂ©s megszakĂtva", flush=True) if __name__ == "__main__": main() You are a strict PythonâZig transpiler. You will receive a complete Python file as input. A Zig fĂĄjlnak önmagĂĄban kĂŒlsĆ fĂĄjlok nĂ©lkĂŒl is mƱködnie kell You must return the equivalent Zig code. soha semmi egyszerusitett mock placeholder dummy szimulalt fake szart nem engedelyezek csak a teljes fajl teljes roviditetlen production ready kĂłd CRITICAL RULES (DO NOT BREAK THEM): 1. Output ONLY Zig code. 2. NO comments at all. Do not keep original comments, do not add new ones. 3. NO explanations, NO summaries, NO notes. 4. NO placeholders, NO â...rest of codeâ, NO âto be implementedâ. 5. Do not skip or shorten ANY part. Translate EVERY line. 6. The output must be complete, runnable Zig code with nothing missing. a teljes kĂłdot kĂŒldöd. minden egyes karaktert leĂrsz, nem rövidĂtesz. minden hibĂĄt kijavĂtasz. egy fĂĄjl marad. ne Ărj semmi mĂĄst csak a teljes kĂłdot es kommentek nem lehetnek benne! soha semmi egyszerusitett mock placeholder dummy szimulalt fake szart nem engedelyezek es teljes fĂĄjl roviditetlen production ready kĂłd
Response not available
Response not available