コード例 #1
0
ファイル: test_groebnertools.py プロジェクト: pernici/sympy
def test_benchmark_coloring():
    skip('takes too much time')

    V = range(1, 12+1)
    E = [(1,2),(2,3),(1,4),(1,6),(1,12),(2,5),(2,7),(3,8),(3,10),
         (4,11),(4,9),(5,6),(6,7),(7,8),(8,9),(9,10),(10,11),
         (11,12),(5,12),(5,9),(6,10),(7,11),(8,12),(3,4)]

    V = [Symbol('x' + str(i)) for i in V]
    E = [(V[i-1], V[j-1]) for i, j in E]

    x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12 = V

    I3 = [x**3 - 1 for x in V]
    Ig = [x**2 + x*y + y**2 for x, y in E]

    I = I3 + Ig

    assert groebner(I[:-1], V, order='lex') == [
        x1 + x11 + x12,
        x2 - x11,
        x3 - x12,
        x4 - x12,
        x5 + x11 + x12,
        x6 - x11,
        x7 - x12,
        x8 + x11 + x12,
        x9 - x11,
        x10 + x11 + x12,
        x11**2 + x11*x12 + x12**2,
        x12**3 - 1,
    ]

    assert groebner(I, V, order='lex') == [1]
コード例 #2
0
ファイル: test_groebnertools.py プロジェクト: hastebrot/sympy
def test_benchmark_coloring():
    skip('takes too much time')

    V = range(1, 12+1)
    E = [(1,2),(2,3),(1,4),(1,6),(1,12),(2,5),(2,7),(3,8),(3,10),
         (4,11),(4,9),(5,6),(6,7),(7,8),(8,9),(9,10),(10,11),
         (11,12),(5,12),(5,9),(6,10),(7,11),(8,12),(3,4)]

    V = [Symbol('x' + str(i)) for i in V]
    E = [(V[i-1], V[j-1]) for i, j in E]

    x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12 = V

    I3 = [x**3 - 1 for x in V]
    Ig = [x**2 + x*y + y**2 for x, y in E]

    I = I3 + Ig

    assert groebner(I[:-1], V, order='lex') == [
        x1 + x11 + x12,
        x2 - x11,
        x3 - x12,
        x4 - x12,
        x5 + x11 + x12,
        x6 - x11,
        x7 - x12,
        x8 + x11 + x12,
        x9 - x11,
        x10 + x11 + x12,
        x11**2 + x11*x12 + x12**2,
        x12**3 - 1,
    ]

    assert groebner(I, V, order='lex') == [1]
コード例 #3
0
ファイル: test_groebnertools.py プロジェクト: hastebrot/sympy
def helper_test_benchmark_katsura_4():
    x0, x1, x2, x3 = symbols('x:4')

    I = [x0 + 2*x1 + 2*x2 + 2*x3 - 1,
         x0**2 + 2*x1**2 + 2*x2**2 + 2*x3**2 - x0,
         2*x0*x1 + 2*x1*x2 + 2*x2*x3 - x1,
         x1**2 + 2*x0*x2 + 2*x1*x3 - x2]

    assert groebner(I, x0, x1, x2, x3, order='lex') == [
        5913075*x0 - 159690237696*x3**7 + 31246269696*x3**6 + 27439610544*x3**5 - 6475723368*x3**4 - 838935856*x3**3 + 275119624*x3**2 + 4884038*x3 - 5913075,
        1971025*x1 - 97197721632*x3**7 + 73975630752*x3**6 - 12121915032*x3**5 - 2760941496*x3**4 + 814792828*x3**3 - 1678512*x3**2 - 9158924*x3,
        5913075*x2 + 371438283744*x3**7 - 237550027104*x3**6 + 22645939824*x3**5 + 11520686172*x3**4 - 2024910556*x3**3 - 132524276*x3**2 + 30947828*x3,
        128304*x3**8 - 93312*x3**7 + 15552*x3**6 + 3144*x3**5 - 1120*x3**4 + 36*x3**3 + 15*x3**2 - x3,
    ]

    assert groebner(I, x0, x1, x2, x3, order='grlex') == [
        393*x1 - 4662*x2**2 + 4462*x2*x3 - 59*x2 + 224532*x3**4 - 91224*x3**3 - 678*x3**2 + 2046*x3,
        -x1 + 196*x2**3 - 21*x2**2 + 60*x2*x3 - 18*x2 - 168*x3**3 + 83*x3**2 - 9*x3,
        -6*x1 + 1134*x2**2*x3 - 189*x2**2 - 466*x2*x3 + 32*x2 - 630*x3**3 + 57*x3**2 + 51*x3,
        33*x1 + 63*x2**2 + 2268*x2*x3**2 - 188*x2*x3 + 34*x2 + 2520*x3**3 - 849*x3**2 + 3*x3,
        7*x1**2 - x1 - 7*x2**2 - 24*x2*x3 + 3*x2 - 15*x3**2 + 5*x3,
        14*x1*x2 - x1 + 14*x2**2 + 18*x2*x3 - 4*x2 + 6*x3**2 - 2*x3,
        14*x1*x3 - x1 + 7*x2**2 + 32*x2*x3 - 4*x2 + 27*x3**2 - 9*x3,
        x0 + 2*x1 + 2*x2 + 2*x3 - 1,
    ]
コード例 #4
0
ファイル: test_groebnertools.py プロジェクト: pernici/sympy
def test_benchmark_katsura_4():
    x0, x1, x2, x3 = symbols('x:4')

    I = [x0 + 2*x1 + 2*x2 + 2*x3 - 1,
         x0**2 + 2*x1**2 + 2*x2**2 + 2*x3**2 - x0,
         2*x0*x1 + 2*x1*x2 + 2*x2*x3 - x1,
         x1**2 + 2*x0*x2 + 2*x1*x3 - x2]

    gr = groebner(I, x0, x1, x2, x3, order='lex')
    gr.sort()
    assert gr == [
        5913075*x2 + 371438283744*x3**7 - 237550027104*x3**6 + 22645939824*x3**5 + 11520686172*x3**4 - 2024910556*x3**3 - 132524276*x3**2 + 30947828*x3,
        1971025*x1 - 97197721632*x3**7 + 73975630752*x3**6 - 12121915032*x3**5 - 2760941496*x3**4 + 814792828*x3**3 - 1678512*x3**2 - 9158924*x3,
        128304*x3**8 - 93312*x3**7 + 15552*x3**6 + 3144*x3**5 - 1120*x3**4 + 36*x3**3 + 15*x3**2 - x3,
        5913075*x0 - 159690237696*x3**7 + 31246269696*x3**6 + 27439610544*x3**5 - 6475723368*x3**4 - 838935856*x3**3 + 275119624*x3**2 + 4884038*x3 - 5913075,
    ]

    gr = groebner(I, x0, x1, x2, x3, order='grlex')
    gr.sort()
    assert gr == [
        x0 + 2*x1 + 2*x2 + 2*x3 - 1,
        7*x1**2 - x1 - 7*x2**2 - 24*x2*x3 + 3*x2 - 15*x3**2 + 5*x3,
        14*x1*x2 - x1 + 14*x2**2 + 18*x2*x3 - 4*x2 + 6*x3**2 - 2*x3,
        14*x1*x3 - x1 + 7*x2**2 + 32*x2*x3 - 4*x2 + 27*x3**2 - 9*x3,
        393*x1 - 4662*x2**2 + 4462*x2*x3 - 59*x2 + 224532*x3**4 - 91224*x3**3 - 678*x3**2 + 2046*x3,
        -x1 + 196*x2**3 - 21*x2**2 + 60*x2*x3 - 18*x2 - 168*x3**3 + 83*x3**2 - 9*x3,
        -6*x1 + 1134*x2**2*x3 - 189*x2**2 - 466*x2*x3 + 32*x2 - 630*x3**3 + 57*x3**2 + 51*x3,
        33*x1 + 63*x2**2 + 2268*x2*x3**2 - 188*x2*x3 + 34*x2 + 2520*x3**3 - 849*x3**2 + 3*x3,
    ]
コード例 #5
0
ファイル: test_groebnertools.py プロジェクト: pernici/sympy
def test_benchmark_minpoly():
    x, y, z = symbols('x,y,z')

    I = [x**3+x+1, y**2+y+1, (x+y)*z-(x**2+y)]

    assert groebner(I, x, y, z, order='lex') == [
        -975 + 2067*x + 6878*z - 11061*z**2 + 6062*z**3 - 1065*z**4 + 155*z**5,
        -308 + 159*y + 1043*z - 1161*z**2 + 523*z**3 - 91*z**4 + 12*z**5,
        13 - 46*z + 89*z**2 - 82*z**3 + 41*z**4 - 7*z**5 + z**6,
    ]

    assert groebner(I, x, y, z, order='lex', field=True) == [
        -S(25)/53 + x + 6878*z/2067 - 3687*z**2/689 + 6062*z**3/2067 - 355*z**4/689 + 155*z**5/2067,
        -S(308)/159 + y + 1043*z/159 - 387*z**2/53 + 523*z**3/159 - 91*z**4/159 + 4*z**5/53,
        13 - 46*z + 89*z**2 - 82*z**3 + 41*z**4 - 7*z**5 + z**6,
    ]
コード例 #6
0
    def search(H, n, m):
        """ H: array of polys
            n: number of Y variates already assigned
            m: number of Y variates
        """

        # the groebner_routine *reduced* computes the remainder
        # compute the remainder wrt to a Groebner basis (GB)
        # GB uses lex monomial ordering here, with y's ordered higher than x's
        # this will cause the y's to be eliminated first
        _, rem = groebner_routine(
            function, groebner(H, *range_var('y', range(1, m+1, 1))))

        if rem == 0:
            # return this
            yield [n, m] + [str_poly(h) for h in H] + ["rem: "+str_poly(rem)]

        # if extensions are no more possible
        if n == m:
            return

        # idea: next expression should be chosen to move the groebner basis
        # remainder to zero
        # TODO: of course would be more efficient to update the groebner basis
        # rather than recomputing from scratch each time
        try:
            for h, nn, mm in extensions(H, n, m, rem=rem):
                # h  : new constraint to be enforced
                # nn : update of number of Y variates assigned
                # mm : update of number of Y variates in total
                for g in search(H + [h], nn, mm):  # recurse into search
                    yield g
        except TypeError as e:
            print e
            pass  # for loop will barf if extensions return None
コード例 #7
0
def is_computible(polynomials, steps):
    conditions = get_algorithm_conditions(polynomials, steps)
    coefficients = set()
    for condition in conditions:
        coefficients.update(condition.free_symbols)
    basis = groebner(conditions, coefficients, order="grevlex")
    return all(element != 1 for element in basis)
コード例 #8
0
ファイル: test_groebnertools.py プロジェクト: hastebrot/sympy
def helper_test_benchmark_czichowski():
    x, t = symbols('x t')

    I = [9*x**8 + 36*x**7 - 32*x**6 - 252*x**5 - 78*x**4 + 468*x**3 + 288*x**2 - 108*x + 9, (-72 - 72*t)*x**7 + (-256 - 252*t)*x**6 + (192 + 192*t)*x**5 + (1280 + 1260*t)*x**4 + (312 + 312*t)*x**3 + (-404*t)*x**2 + (-576 - 576*t)*x + 96 + 108*t]

    assert groebner(I, x, t, order='lex') == [
        -160420835591776763325581422211936558925462474417709511019228211783493866564923546661604487873*t**7 - 1406108495478033395547109582678806497509499966197028487131115097902188374051595011248311352864*t**6 - 5241326875850889518164640374668786338033653548841427557880599579174438246266263602956254030352*t**5 - 10758917262823299139373269714910672770004760114329943852726887632013485035262879510837043892416*t**4 - 13119383576444715672578819534846747735372132018341964647712009275306635391456880068261130581248*t**3 - 9491412317016197146080450036267011389660653495578680036574753839055748080962214787557853941760*t**2 - 3767520915562795326943800040277726397326609797172964377014046018280260848046603967211258368000*t + 3725588592068034903797967297424801242396746870413359539263038139343329273586196480000*x - 632314652371226552085897259159210286886724229880266931574701654721512325555116066073245696000,
        610733380717522355121*t**8 + 6243748742141230639968*t**7 + 27761407182086143225024*t**6 + 70066148869420956398592*t**5 + 109701225644313784229376*t**4 + 109009005495588442152960*t**3 + 67072101084384786432000*t**2 + 23339979742629593088000*t + 3513592776846090240000
    ]

    assert groebner(I, x, t, order='grlex') == [
        16996618586000601590732959134095643086442*t**3*x - 32936701459297092865176560282688198064839*t**3 + 78592411049800639484139414821529525782364*t**2*x - 120753953358671750165454009478961405619916*t**2 + 120988399875140799712152158915653654637280*t*x - 144576390266626470824138354942076045758736*t + 60017634054270480831259316163620768960*x**2 + 61976058033571109604821862786675242894400*x - 56266268491293858791834120380427754600960,
        576689018321912327136790519059646508441672750656050290242749*t**4 + 2326673103677477425562248201573604572527893938459296513327336*t**3 + 110743790416688497407826310048520299245819959064297990236000*t**2*x + 3308669114229100853338245486174247752683277925010505284338016*t**2 + 323150205645687941261103426627818874426097912639158572428800*t*x + 1914335199925152083917206349978534224695445819017286960055680*t + 861662882561803377986838989464278045397192862768588480000*x**2 + 235296483281783440197069672204341465480107019878814196672000*x + 361850798943225141738895123621685122544503614946436727532800,
        -117584925286448670474763406733005510014188341867*t**3 + 68566565876066068463853874568722190223721653044*t**2*x - 435970731348366266878180788833437896139920683940*t**2 + 196297602447033751918195568051376792491869233408*t*x - 525011527660010557871349062870980202067479780112*t + 517905853447200553360289634770487684447317120*x**3 + 569119014870778921949288951688799397569321920*x**2 + 138877356748142786670127389526667463202210102080*x - 205109210539096046121625447192779783475018619520,
        -3725142681462373002731339445216700112264527*t**3 + 583711207282060457652784180668273817487940*t**2*x - 12381382393074485225164741437227437062814908*t**2 + 151081054097783125250959636747516827435040*t*x**2 + 1814103857455163948531448580501928933873280*t*x - 13353115629395094645843682074271212731433648*t + 236415091385250007660606958022544983766080*x**2 + 1390443278862804663728298060085399578417600*x - 4716885828494075789338754454248931750698880
    ]
コード例 #9
0
ファイル: test_groebnertools.py プロジェクト: hastebrot/sympy
def helper_test_benchmark_minpoly():
    x, y, z = symbols('x,y,z')

    I = [x**3 + x + 1, y**2 + y + 1, (x + y) * z - (x**2 + y)]

    assert groebner(I, x, y, z, order='lex') == [
        -975 + 2067*x + 6878*z - 11061*z**2 + 6062*z**3 - 1065*z**4 + 155*z**5,
        -308 + 159*y + 1043*z - 1161*z**2 + 523*z**3 - 91*z**4 + 12*z**5,
        13 - 46*z + 89*z**2 - 82*z**3 + 41*z**4 - 7*z**5 + z**6,
    ]

    assert groebner(I, x, y, z, order='lex', field=True) == [
        -S(25)/53 + x + 6878*z/2067 - 3687*z**2/689 + 6062*z**3/2067 - 355*z**4/689 + 155*z**5/2067,
        -S(308)/159 + y + 1043*z/159 - 387*z**2/53 + 523*z**3/159 - 91*z**4/159 + 4*z**5/53,
        13 - 46*z + 89*z**2 - 82*z**3 + 41*z**4 - 7*z**5 + z**6,
    ]
コード例 #10
0
ファイル: snippet.py プロジェクト: szabo92/gistable
def solve_as_groebner(fixed, boxsize):
    """Use groebner bases algorithm to solve Sudoku puzzle of dimension 
    'boxsize' with 'fixed' cells."""
    g = puzzle_as_polynomial_system(fixed, boxsize)
    h = sympy.groebner(g, cell_symbols(boxsize), order='lex')
    s = sympy.solve(h, cell_symbols(boxsize))
    keys = [int(symbol.name.replace('x', '')) for symbol in s.keys()]
    values = [i.__int__() for i in s.values()]
    return dict(zip(keys, values))
コード例 #11
0
ファイル: test_groebnertools.py プロジェクト: hastebrot/sympy
def helper_test_benchmark_katsura_3():
    x0, x1, x2 = symbols('x:3')

    I = [x0 + 2*x1 + 2*x2 - 1,
         x0**2 + 2*x1**2 + 2*x2**2 - x0,
         2*x0*x1 + 2*x1*x2 - x1]

    assert groebner(I, x0, x1, x2, order='lex') == [
        -7 + 7*x0 + 8*x2 + 158*x2**2 - 420*x2**3,
        7*x1 + 3*x2 - 79*x2**2 + 210*x2**3,
        x2 + x2**2 - 40*x2**3 + 84*x2**4,
    ]

    assert groebner(I, x0, x1, x2, order='grlex') == [
        7*x1 + 3*x2 - 79*x2**2 + 210*x2**3,
        -x1 + x2 - 3*x2**2 + 5*x1**2,
        -x1 - 4*x2 + 10*x1*x2 + 12*x2**2,
        -1 + x0 + 2*x1 + 2*x2,
    ]
コード例 #12
0
def is_computible(polynomials, steps):
    for operations, conditions in get_algorithm_conditions(polynomials, steps):
        coefficients = set()
        for condition in conditions:
            #print condition
            coefficients.update(condition.free_symbols)
        basis = groebner(conditions, coefficients, order="grevlex")
        if all(element != 1 for element in basis):
            print("Succeded with following operations order: %s" %
                  " ".join(operations))
            return True
    return False
コード例 #13
0
ファイル: test_groebnertools.py プロジェクト: hastebrot/sympy
def helper_test_benchmark_cyclic_4():
    a, b, c, d = symbols('a b c d')

    I = [a + b + c + d, a*b + a*d + b*c + b*d, a*b*c + a*b*d + a*c*d + b*c*d, a*b*c*d - 1]

    assert groebner(I, a, b, c, d, order='lex') == [
        4*a + 3*d**9 - 4*d**5 - 3*d,
        4*b + 4*c - 3*d**9 + 4*d**5 + 7*d,
        4*c**2 + 3*d**10 - 4*d**6 - 3*d**2,
        4*c*d**4 + 4*c - d**9 + 4*d**5 + 5*d, d**12 - d**8 - d**4 + 1
    ]

    assert groebner(I, a, b, c, d, order='grlex') == [
        3*b*c - c**2 + d**6 - 3*d**2,
        -b + 3*c**2*d**3 - c - d**5 - 4*d,
        -b + 3*c*d**4 + 2*c + 2*d**5 + 2*d,
        c**4 + 2*c**2*d**2 - d**4 - 2,
        c**3*d + c*d**3 + d**4 + 1,
        b*c**2 - c**3 - c**2*d - 2*c*d**2 - d**3,
        b**2 - c**2, b*d + c**2 + c*d + d**2,
        a + b + c + d
    ]
コード例 #14
0
ファイル: test_groebnertools.py プロジェクト: pernici/sympy
def test_benchmark_katsura_3():
    x0, x1, x2 = symbols('x:3')

    I = [x0 + 2*x1 + 2*x2 - 1,
         x0**2 + 2*x1**2 + 2*x2**2 - x0,
         2*x0*x1 + 2*x1*x2 - x1]

    gr = groebner(I, x0, x1, x2, order='lex')
    gr.sort()
    assert gr == [
        x2 + x2**2 - 40*x2**3 + 84*x2**4,
        7*x1 + 3*x2 - 79*x2**2 + 210*x2**3,
        -7 + 7*x0 + 8*x2 + 158*x2**2 - 420*x2**3,
    ]

    gr = groebner(I, x0, x1, x2, order='grlex')
    gr.sort()
    assert gr == [
        -1 + x0 + 2*x1 + 2*x2,
        -x1 + x2 - 3*x2**2 + 5*x1**2,
        -x1 - 4*x2 + 10*x1*x2 + 12*x2**2,
        7*x1 + 3*x2 - 79*x2**2 + 210*x2**3,
    ]
コード例 #15
0
def test_benchmark_katsura_3():
    x0, x1, x2 = symbols('x:3')

    I = [
        x0 + 2 * x1 + 2 * x2 - 1, x0**2 + 2 * x1**2 + 2 * x2**2 - x0,
        2 * x0 * x1 + 2 * x1 * x2 - x1
    ]

    gr = groebner(I, x0, x1, x2, order='lex')
    gr.sort()
    assert gr == [
        x2 + x2**2 - 40 * x2**3 + 84 * x2**4,
        7 * x1 + 3 * x2 - 79 * x2**2 + 210 * x2**3,
        -7 + 7 * x0 + 8 * x2 + 158 * x2**2 - 420 * x2**3,
    ]

    gr = groebner(I, x0, x1, x2, order='grlex')
    gr.sort()
    assert gr == [
        -1 + x0 + 2 * x1 + 2 * x2,
        -x1 + x2 - 3 * x2**2 + 5 * x1**2,
        -x1 - 4 * x2 + 10 * x1 * x2 + 12 * x2**2,
        7 * x1 + 3 * x2 - 79 * x2**2 + 210 * x2**3,
    ]
コード例 #16
0
    def reduce_groebner(self):
        # Special cases
        if self.eqs == [ 0 ]:
            self.eqs = []
            return
        
        if len(self.eqs) <= 1:
            return
        
        if not self.X_vars:
            self.eqs = [ to_poly(1, self.X_vars) ] if any(eq != 0 for eq in self.eqs) else []
            return

        # Compute Gröbner basis
        self.eqs = list(sp.groebner(self.eqs, self.X_vars, order = 'grevlex'))
コード例 #17
0
def eliminate(R, I, L=-1):
    """
    Inputs:
    $I$ - list[polynomial]: generators of ideal
    $L$ - integer: number of elimination ideals to obtain
    Output:
    $(I_L, [W_1, ..., W_L])$ - tuple[polynomial, list[polynomial]]: approximation of $elim_L(I)$
    * Implicitly construct "chordal graph" G
    """
    G = ideal_to_graph(I)
    order = G.get_mcs()
    syms = R.symbols

    if L < 0:
        L = len(syms) - 1

    I = list(I)  # copy
    W = []
    for l in xrange(L):
        # get clique X_l of "G"
        X_l = get_clique(G, l, order)
        print "X_l", X_l
        # split the I_l in to two sets of generators J_l, K_l
        J, K = split_gens(I, X_l)
        # Get lex grobner basis for J_l and append to J_l
        J = as_ring_exprs(
            R,
            groebner(
                as_polys(J), to_syms(R,
                                     X_l), domain=R.domain, order=R.order)) + J
        if 1 in J:
            J = [R(1)]
            W_ = []
        else:
            # Eliminate x_l from J_l: This is done by intersecting with K[X_l\x_l]
            J, _ = split_gens(set(J), X_l.difference([l]))
            print "J", J
            # Construct W_l from the coeff ring.
            W_ = [get_coefficient_term(R, f) for f in J] + K
            if 1 in W_: W_ = 1
        # Re construct the next I
        I = J + K
        W.append(W_)
        print "I_l, W_l", I, W_
    return I, W
コード例 #18
0
def chord_eliminate(R, I, G, order, L=-1):
    """
    Inputs:
    $I$ - list[polynomial]: generators of ideal
    $L$ - integer: number of elimination ideals to obtain
    Output:
    $[J_0, ..., J_{L-1}], [W_1, ..., W_L])$ - list[list[polynomial], list[polynomial]]: approximation of $elim_L(I)$
    * Implicitly construct "chordal graph" G
    """
    I = list(I)  # copy

    if L < 0: L = len(R.symbols)

    Js, Ws = [], []
    for l in order[:L]:
        # get clique X_l of "G"
        X_l = get_clique(G, l, order)
        print "X_l", X_l
        # split the I_l in to two sets of generators J_l, K_l
        J, K = split_gens(I, X_l)
        # Get lex grobner basis for J_l and append to J_l
        J = as_ring_exprs(
            R,
            groebner(
                as_polys(J), to_syms(R,
                                     X_l), domain=R.domain, order=R.order)) + J
        J = list(set(J))
        if 1 in J:
            J, J_elim = [R(1)], [R(1)]
            W = []
        else:
            # Eliminate x_l from J_l: This is done by intersecting with K[X_l\x_l]
            J_elim, _ = split_gens(set(J), X_l.difference([l]))
            # Construct W_l from the coeff ring.
            W = [get_coefficient_term(R, f) for f in J_elim] + K
            if 1 in W: W = [R(1)]
            else: W = list(set(W))
        Js.append(J)
        Ws.append(W)
        # Re construct the next I
        I = J_elim + K
        print "I_l, W_l", I, W
    return I, Js, Ws
コード例 #19
0
ファイル: chordal_elim.py プロジェクト: sidaw/polymom
def eliminate(R, I, L=-1):
    """
    Inputs:
    $I$ - list[polynomial]: generators of ideal
    $L$ - integer: number of elimination ideals to obtain
    Output:
    $(I_L, [W_1, ..., W_L])$ - tuple[polynomial, list[polynomial]]: approximation of $elim_L(I)$
    * Implicitly construct "chordal graph" G
    """
    G = ideal_to_graph(I)
    order = G.get_mcs()
    syms = R.symbols

    if L < 0:
        L = len(syms) - 1

    I = list(I) # copy
    W = []
    for l in xrange(L):
        # get clique X_l of "G"
        X_l = get_clique(G, l, order)
        print "X_l", X_l
        # split the I_l in to two sets of generators J_l, K_l
        J, K = split_gens(I, X_l)
        # Get lex grobner basis for J_l and append to J_l
        J = as_ring_exprs(R, groebner(as_polys(J), to_syms(R, X_l), domain=R.domain, order=R.order)) + J
        if 1 in J:
            J = [R(1)]
            W_ = []
        else:
            # Eliminate x_l from J_l: This is done by intersecting with K[X_l\x_l]
            J, _ = split_gens(set(J), X_l.difference([l]))
            print "J", J
            # Construct W_l from the coeff ring.
            W_ = [get_coefficient_term(R, f) for f in J] + K
            if 1 in W_: W_ = 1
        # Re construct the next I
        I = J + K
        W.append(W_)
        print "I_l, W_l", I, W_
    return I, W
コード例 #20
0
ファイル: chordal_elim.py プロジェクト: sidaw/polymom
def chord_eliminate(R, I, G, order, L = -1):
    """
    Inputs:
    $I$ - list[polynomial]: generators of ideal
    $L$ - integer: number of elimination ideals to obtain
    Output:
    $[J_0, ..., J_{L-1}], [W_1, ..., W_L])$ - list[list[polynomial], list[polynomial]]: approximation of $elim_L(I)$
    * Implicitly construct "chordal graph" G
    """
    I = list(I) # copy

    if L < 0: L = len(R.symbols)

    Js, Ws = [], []
    for l in order[:L]:
        # get clique X_l of "G"
        X_l = get_clique(G, l, order)
        print "X_l", X_l
        # split the I_l in to two sets of generators J_l, K_l
        J, K = split_gens(I, X_l)
        # Get lex grobner basis for J_l and append to J_l
        J = as_ring_exprs(R, groebner(as_polys(J), to_syms(R, X_l), domain=R.domain, order=R.order)) + J
        J = list(set(J))
        if 1 in J:
            J, J_elim = [R(1)], [R(1)]
            W = []
        else:
            # Eliminate x_l from J_l: This is done by intersecting with K[X_l\x_l]
            J_elim, _ = split_gens(set(J), X_l.difference([l]))
            # Construct W_l from the coeff ring.
            W = [get_coefficient_term(R, f) for f in J_elim] + K
            if 1 in W: W = [R(1)]
            else: W = list(set(W))
        Js.append(J)
        Ws.append(W)
        # Re construct the next I
        I = J_elim + K
        print "I_l, W_l", I, W
    return I, Js, Ws
コード例 #21
0
    Eq(w1 + w2 + b - g - k0**2, 0),
    Eq(w1 + w2 - b - g - k1**2, 0),
    Eq(u * w1 + v * w2 + b - g - k2**2, 0),
    Eq(w1**2 + w2**2 - 1, 0),
    Eq(a0 * (w1 + w2 + b - g), 0),
    Eq(a1 * (w1 + w2 - b - g), 0),
    Eq(a2 * (u * w1 + v * w2 + b - g), 0)
], [g, w1, w2, b, a0, a1, a2, beta, k0, k1, k2, u, v])

gb = groebner([
    Eq(a0**2 + a1**2 + a2**2, 1),
    Eq(a0**2 + a1**2 + u * a2**2 + 2 * beta * w1, 0),
    Eq(a0**2 + a1**2 + v * a2**2 + 2 * beta * w2, 0),
    Eq(a0**2 - a1**2 + a2**2, 0),
    Eq(w1 + w2 + b - g - k0**2, 0),
    Eq(w1 + w2 - b - g - k1**2, 0),
    Eq(u * w1 + v * w2 + b - g - k2**2, 0),
    Eq(w1**2 + w2**2 - 1, 0),
    Eq(a0 * (w1 + w2 + b - g), 0),
    Eq(a1 * (w1 + w2 - b - g), 0),
    Eq(a2 * (u * w1 + v * w2 + b - g), 0),
    Eq(u - v, 0)
], [u, v, g, a0, a1, a2, beta, k0, k1, k2, b, w1, w2])

w0, w1, b, a0, a1, a2, k0, k1, k2, u, v = symbols(
    'w0 w1 b a0 a1 a2 k0 k1 k2 u v')

## Three points, [[1,1],[-1,-1],[u,v]]; y: [1,-1,1]
soln = solve([
    Eq(a0**2 + a1**2 + u * a2**2, w0),
    Eq(a0**2 + a1**2 + v * a2**2, w1),
    Eq(a0**2 + a2**2, a1**2),
コード例 #22
0
from sympy import groebner, Poly
from sympy.parsing.sympy_parser import parse_expr

f = open("task.txt", "r")
s = f.readlines()
f.close()

k = s[1].strip()[6:-1].split(', ')

key = [Poly(i) for i in k]

F = groebner(key)

res = ""

for i in range(2, len(s)):
    p = parse_expr(s[i].strip())
    r = F.contains(p)
    if r:
        res += "1"
    else:
        res += "0"

print(''.join(chr(int(res[i:i+8], 2)) for i in range(0, len(res), 8)))
コード例 #23
0
# In[14]:


#Αλέξανδρος Κόντος 1526, Σπύρος Βασιλείου 1522, Παναγιώτης Κλωνής 1309, Αντώνης Μάτζος 1293

#PART A

from sympy import var, expand, lcm, LM, LT, reduced, monic
from sympy import groebner, solve_poly_system, solve
from sympy.polys.groebnertools import s_poly

#pairnoume tis eksiswseis [A**2 - p*(p - a)*(p - b)*(p - c)] kai [2*p - (a + b + c)]
A, a, b, c, p, x = var('A, a, b, c, p, x')
F = [A**2 - p*(p - a)*(p - b)*(p - c), 2*p - (a + b + c)]

basis = groebner(F, *[p, a, b, c, A], order='lex')
print(len(basis), '\n')
print(basis), '\n
print('triangle', basis, '\n')
area = basis[1]
print('last member of basis = ', area, '\n')
formula = area.subs({A**2 : x})
print('poly in x**2 :: ', formula, '\n')
print('Area of the triangle :: ', expand( solve(formula, x)[0]))


# In[18]:


#PART B
#prosthetoume tis eksiswseis ma=sqrt((2*b**2+2*c**2-a**2)/4), mb=sqrt((2*a**2+2*c**2-b**2)/4), mc=sqrt((2*a**2+2*b**2-c**2)/4)
コード例 #24
0
     gr.Line(A, B, colors[i])
     u = u + du
 M = pyBezier(pts_head, s, 0.5)
 pa = pyBezier(pts_head, s, t1)
 pb = pyBezier(pts_head, s + ds, t2)
 #print s,pa
 #print s+ds,pb
 #print
 curve1 = lambda t: [c(t)(pa[0]), c(t)(pa[1])]
 curve2 = lambda t: [c(t)(pb[0]), c(t)(pb[1])]
 p1 = lambda X: curve1(X[0])[0] - curve2(X[1])[0]
 p2 = lambda X: curve1(X[0])[1] - curve2(X[1])[1]
 X = [t1, t2]
 flag = True
 try:
     q1, q2 = list(groebner([p1(X), p2(X)], domain='ZZ'))
     #print "q1 = ",q1
     #print "q2 = ",q2
 except:
     q1, q2 = p1, p2
     flag = False
 if flag:
     try:
         T2 = list(np.roots(q2.as_poly(t2).all_coeffs()))
         T2 = filter(viz, T2)
         T2 = map(lambda z: z.real, T2)
         r = lambda u: q1.subs(t2, u).as_poly(t1).all_coeffs()
         T1 = map(r, T2)
         T1 = map(lambda C: list(np.roots(C))[0], T1)
         Z = zip(T1, T2)
         pts_Z = []
コード例 #25
0
ファイル: bench_groebnertools.py プロジェクト: vchekan/sympy
def time_vertex_color_12_vertices_23_edges():
    assert groebner(F_1, Vx) != [1]
コード例 #26
0
from sympy.polys.orderings import monomial_key
from sympy import var, expand, lcm, LM, LT, reduced, monic
from sympy import groebner, solve_poly_system, solve
from sympy.polys.groebnertools import s_poly
from math import sqrt
from sympy.polys.subresultants_qq_zz import *

A, a, b, c, x, y, z, ha, hb, hc, p, ma, mb, mc = symbols(
    'A a b c x y z ha hb hc p ma mb mc')

F = [
    2 * A - a * ha, 2 * A - b * hb, 2 * A - c * hc,
    A**2 - p * (p - a) * (p - b) * (p - c), 2 * p - (a + b + c)
]

basis = groebner(F, *[a, b, c, p, ha, hb, hc, A], order='lex')
print(len(basis), '\n')
print('triangle', basis, '\n')
area = basis[1]
print('last member of basis = ', area, '\n')

formula = area.subs({p: ((a + b + c) / 2)})
Area = solve(formula, A**2)
Area = simplify(Area)

print("A^2 = ", Area, '\n')
print("A is square root of ", Area, '\n')
#O typos pou prokyptei einai o typos tou Hrwna se ennalaktikh morfh A=\frac{1}{4}\sqrt{2(a^2 b^2+a^2c^2+b^2c^2)-(a^4+b^4+c^4)}

# An theloume na ektypwnei apeytheias to A tote:
# Apo tis opoies 2 rizes kratame thn thetikh giati milame gia emvado
コード例 #27
0
ファイル: bench_groebnertools.py プロジェクト: vchekan/sympy
def time_vertex_color_12_vertices_24_edges():
    assert groebner(F_2, Vx) == [1]
コード例 #28
0
ファイル: bench_groebnertools.py プロジェクト: Acebulf/sympy
def time_vertex_color_12_vertices_23_edges():
    assert groebner(F_1, Vx) != [1]
コード例 #29
0
ファイル: bench_groebnertools.py プロジェクト: Acebulf/sympy
def time_vertex_color_12_vertices_24_edges():
    assert groebner(F_2, Vx) == [1]
コード例 #30
0
# Program 10e: Computing Groebner bases. See Example 7.
# Reducing the first five Lyapunov quantities of a Lienard system.

from sympy import groebner, symbols

a1, a2, a3, a4, b2, b3 = symbols('a1 a2 a3 a4 b2 b3')

g = groebner([
    -a1, 2 * a2 * b2 - 3 * a3, 5 * b2 * (2 * a4 - b3 * a2), -5 * b2 *
    (92 * b2**2 * a4 - 99 * b3**2 * a2 + 1520 * a2**2 * a4 - 760 * a2**3 * b3 -
     46 * b2**2 * b3 * a2 + 198 * b3 * a4), -b2 *
    (14546 * b2**4 * a4 + 105639 * a2**3 * b3**2 + 96664 * a2**3 * b2**2 * b3 -
     193328 * a2**2 * b2**2 * a4 - 891034 * a2**4 * a4 + 445517 * a2**5 * b3 +
     211632 * a2 * a4**2 - 317094 * a2**2 * b3 * a4 - 44190 * b2**2 * b3 * a4 +
     22095 * b2**2 * b3**2 * a2 - 7273 * b2**4 * b3 * a2 + 5319 * b3**3 * a2 -
     10638 * b3**2 * a4)
],
             order='lex')

print(g)
コード例 #31
0
from sympy import prod, symbols, Poly, Matrix, pprint, IndexedBase, groebner, simplify
from sympy import LC, LM, LT, reduced, ZZ, degree_list
from sympy.polys.monomials import monomial_deg
from sympy.polys.orderings import monomial_key
from sympy import var, expand, lcm, LM, LT, reduced, monic
from sympy import groebner, solve_poly_system, solve
from sympy.polys.groebnertools import s_poly
from math import sqrt
from sympy.polys.subresultants_qq_zz import *

A, a, b, c, x, y, z, ha, hb, hc, p = symbols('A a b c x y z ha hb hc p')

F = [2*A-a*ha, 2*A-b*hb,2*A-c*hc,
         A**2 - p*(p - a)*(p - b)*(p - c), 2*p - (a + b + c)]

basis = groebner(F, *[a, b, c, p, ha, hb, hc, A], order='lex')
print(len(basis), '\n')
print('triangle', basis, '\n')
area = basis[1]
print('last member of basis = ', area, '\n')

formula = area.subs({p : ((a + b + c) / 2)})
Area = solve(formula, A**2)
Area= simplify(Area)

print("A^2 = ", Area, '\n')
print("A is square root of ", Area, '\n')
#O typos pou prokyptei einai o typos tou Hrwna se ennalaktikh morfh A=\frac{1}{4}\sqrt{2(a^2 b^2+a^2c^2+b^2c^2)-(a^4+b^4+c^4)}


# An theloume na ektypwnei apeytheias to A tote:
コード例 #32
0
# Programs 10d: Computing Groebner bases. See Example 6.
from sympy import groebner
from sympy.abc import x, y
G = groebner([
    x + y**2 - x**3, 4 * x**3 - 12 * x * y**2 + x**4 + 2 * x**2 * y**2 + y**4
],
             order='lex')
print(G)
コード例 #33
0
# Programs 10e: Computing Groebner bases. See Example 7.
# Reducing the first five Lyapunov quantities of a Lienard system.
from sympy import groebner
from sympy.abc import w, x, y, z, u, v

#Let a1=w,a2=x,a3=y,a4=z,b2=u,b3=v.
G=groebner([w,2*u*x-3*y, 5*u*(2*z-x*v),\
  -5*u*(92*z*u**2-99*x*v**2+1520*x**2*z-760*x**3*v-46*x*u**2*v+198*z*v),\
  -u*(14546*u**4*z+105639*x**3*v**2+96664*x**3*u**2*v-193328*x**2*u**2*z- \
   891034*x**4*z+445517*x**5*v+211632*x*z**2-317094*x**2*v*z- \
   44190*u**2*v*z+22095*u**2*v**2*x-7273*u**4*v*x+5319*v**3*x- \
   10638*v**2*z)],order='lex')

print(G)