Research Atlas

Главная / Пять размерностей одного геометрического фактора / Расчёт коядра

Программа · Pythoncompute_exterior_cokernel.py
from collections import defaultdict
from fractions import Fraction
from itertools import combinations
from math import comb


N = 7
ZERO = (0,) * N
MONOMIALS = tuple(combinations(range(8), 2))
POSITIONS = {pair: i for i, pair in enumerate(MONOMIALS)}


def key(term):
    exponents, position = term
    return sum(exponents), tuple(-x for x in reversed(exponents)), position


def plus(a, b):
    return tuple(x + y for x, y in zip(a, b))


def diff(a, b):
    return tuple(x - y for x, y in zip(a, b))


def divides(a, b):
    return all(x <= y for x, y in zip(a, b))


def power(i):
    return tuple(int(i == j) for j in range(N))


def subtract(target, scalar, shift, polynomial):
    for (exponents, position), coeff in polynomial.items():
        k = plus(exponents, shift), position
        new = target.get(k, 0) - scalar * coeff
        if new:
            target[k] = new
        else:
            target.pop(k, None)


def generators(kind, restriction):
    if kind == 6:
        skew = [((0, 2), (1, 4)), ((0, 3), (1, 5))]
    else:
        skew = [((0, 2), (1, 3)), ((0, 3), (1, 4))]
    linear = []
    for i in range(8):
        if i == restriction[0]:
            linear.append({power(j): c for j, c in restriction[1]})
        else:
            linear.append({power(len(linear) - int(i > restriction[0])): 1})
    for form in skew:
        rows = [{} for _ in range(8)]
        for a, b in form:
            rows[a] = {mon: -c for mon, c in linear[b].items()}
            rows[b] = linear[a]
        for triple in combinations(range(8), 3):
            polynomial = {}
            for i, output in enumerate(triple):
                position = POSITIONS[triple[:i] + triple[i + 1:]]
                for monomial, coeff in rows[output].items():
                    polynomial[monomial, position] = (-1) ** i * coeff
            if polynomial:
                yield polynomial
    for j in range(8):
        polynomial = {}
        for i in range(8):
            if i == j:
                continue
            position = POSITIONS[tuple(sorted((i, j)))]
            for monomial, coeff in linear[i].items():
                polynomial[monomial, position] = coeff * (1 if i < j else -1)
        if polynomial:
            yield polynomial


def groebner(columns):
    basis = []
    positions = defaultdict(list)
    pairs = []

    def reduce_and_add(polynomial):
        residual = {}
        while polynomial:
            monomial, slot = term = max(polynomial, key=key)
            coeff = polynomial[term]
            for leading, other_index in positions[slot]:
                if divides(leading, monomial):
                    subtract(polynomial, coeff, diff(monomial, leading), basis[other_index])
                    break
            else:
                residual[term] = polynomial.pop(term)
        if not residual:
            return
        monomial, slot = lead = max(residual, key=key)
        scalar = Fraction(1, residual[lead])
        index = len(basis)
        basis.append({t: coeff * scalar for t, coeff in residual.items()})
        for other, other_index in positions[slot]:
            lcm = tuple(max(a, b) for a, b in zip(monomial, other))
            pairs.append((index, other_index, diff(lcm, monomial), diff(lcm, other)))
        positions[slot].append((monomial, index))

    for column in columns:
        reduce_and_add(column)
    for first, second, shift_first, shift_second in pairs:
        difference = {}
        subtract(difference, -1, shift_first, basis[first])
        subtract(difference, 1, shift_second, basis[second])
        reduce_and_add(difference)
    return basis, positions, len(pairs)


def hilbert_numerator(initial):
    numerator = defaultdict(int)
    for slot in POSITIONS.values():
        coefficients = {ZERO: 1}
        for leading, _ in initial[slot]:
            old = list(coefficients.items())
            for monomial, scalar in old:
                lcm = tuple(max(a, b) for a, b in zip(monomial, leading))
                coefficients[lcm] = coefficients.get(lcm, 0) - scalar
                if not coefficients[lcm]:
                    coefficients.pop(lcm)
        for exponents, scalar in coefficients.items():
            numerator[sum(exponents)] += scalar
    return dict(sorted((d, c) for d, c in numerator.items() if c))


def dimension(hilbert, degree):
    return sum(coeff * comb(degree - d + 6, 6) for d, coeff in hilbert.items() if d <= degree)


if __name__ == '__main__':
    cases = (
        (6, (7, []), 118, 243, 8, 219265080),
        (5, (7, []), 106, 183, 6, 219270570),
        (6, (2, []), 102, 169, 6, 219272430),
        (6, (2, [(4, 1)]), 105, 185, 7, 219269935),
        (5, (3, []), 94, 136, 6, 219363657),
    )
    for kind, relation, expected_basis, expected_pairs, expected_lcm, expected_dimension in cases:
        basis, initials, pairs = groebner(generators(kind, relation))
        lcm_degree = max(sum(max(monomial[j] for monomial, _ in component)
                             for j in range(N)) for component in initials.values() if component)
        assert (len(basis), pairs, lcm_degree) == (expected_basis, expected_pairs, expected_lcm)
        h = hilbert_numerator(initials)
        total = sum(c * dimension(h, d) for c, d in ((21, 41), (-7, 40), (1, 39)))
        assert total == expected_dimension
        print(kind, relation, 'basis', len(basis), 'pairs', pairs, 'max_lcm', lcm_degree,
              'HF', h, 'degree46', total, flush=True)

Исходные технические записи отражают состояние работы на дату их составления. Уточнения и итоговый вывод приведены в статье.

Контрольная сумма оригинала · SHA-256981ba7d3b568c14a92497f20f6a660e29cd49f60b77a3049532925ff27be5979