1from .interpolate import variety_lex_leading_terms, nf_lex_points
2from .PyPolyBoRi import easy_linear_factors
3
4
5def easy_linear_polynomials(p):
6    """ Get linear polynomials implied by given polynomial.
7
8    >>> from brial.frontend import *
9    >>> easy_linear_polynomials(x(1)*x(2) + 1)
10    [x(1) + 1, x(2) + 1]
11    >>> easy_linear_polynomials(x(1)*x(2) + 0)
12    []
13    >>> easy_linear_polynomials(x(0)*x(1) + x(0)*x(2) + 1)
14    [x(0) + 1, x(1) + x(2) + 1]
15    """
16    res = []
17    if p.deg() >= 2:
18        if p.vars_as_monomial().deg() > 8:
19            opp = p + 1
20            for q in easy_linear_factors(opp):
21                res.append(q + 1)
22        else:
23            res = easy_linear_polynomials_via_interpolation(p)
24    return res
25
26
27def easy_linear_polynomials_via_interpolation(p):
28    """ Get linear polynomials implied by given polynomial using interpolation
29    of the variety.
30
31    >>> from brial.frontend import *
32    >>> easy_linear_polynomials_via_interpolation(x(1)*x(2) + 1)
33    [x(1) + 1, x(2) + 1]
34    >>> easy_linear_polynomials_via_interpolation(x(1)*x(2) + 0)
35    []
36    >>> easy_linear_polynomials_via_interpolation(x(0)*x(1) + x(0)*x(2) + 1)
37    [x(0) + 1, x(1) + x(2) + 1]
38    """
39    res = []
40    p_vars = p.vars_as_monomial()
41    space = p_vars.divisors()
42    zeros = p.zeros_in(space)
43    lex_leads = variety_lex_leading_terms(zeros, p_vars)
44    for m in lex_leads:
45        if m.deg() == 1:
46            red = m + nf_lex_points(m, zeros)
47            if red.lead_deg() == 1:  # normal ordering
48                res.append(red)
49    return res
50