Пример #1
0
def primitiveTest():

    for i in range(range1, range2):

        m = ((prem(x**i + 1, x**power1 + x**power2 + 1, modulus=2) == 0)
             and i == 2**power1 - 1)

        if m == True:

            print(i)

        else:
            i + 1
            print('Not Primitive')
Пример #2
0
def main():
    a = -6.6
    b = -21.3
    c = 14.8
    minX = -6
    maxX = 6
    e = 0.0001

    x = sympy.Symbol("x")
    currEq = x ** 3 + a * x ** 2 + b * x + c
    newEq = sympy.diff(currEq)

    sturmSe = []
    sturmSe.append(currEq)
    sturmSe.append(newEq)

    Na = 0
    Nb = 0

    while newEq != newEq.subs(x, minX):
        currEq, newEq = newEq, -sympy.prem(currEq, newEq)
        sturmSe.append(newEq)

    for i in range(len(sturmSe) - 1):
        eqMul = sturmSe[i] * sturmSe[i + 1]
        if eqMul.subs(x, minX) < 0:
            Na += 1

        if eqMul.subs(x, maxX) < 0:
            Nb += 1

    sympy.pprint(sturmSe)
    for i in sturmSe:
        print("a", i.subs(x, minX))
        print("b", i.subs(x, maxX))
        print("\n")

    print("\n Number of roots:")
    print(Na - Nb)

    roots = sympy.solve(sympy.Eq(sturmSe[1], 0), x)
    print("\n All roots:")
    print(roots)

    DichotomyMethod(sturmSe[0], x, minX, min(roots), e)
    MethodChord(sturmSe[0], x, minX, min(roots), e)
Пример #3
0
def our_euclid_prem(f, g, x):
    """
    Μέγιστος κοινός διαιρέτης πολυωνύμων
    f and g, deg(f) > deg(g) > 0 στους ακεραίους (ZZ).
    Τα υπόλοιπα κάθε διαίρεσης υπολογίζονται με την
    συνάρτηση prem(). Τα πρόσημα των υπολοίπων ΔΕΝ είναι
    πάντα σωστά.
    """
    print('gcd(f, g) using prem()', '\n')
    print(f, '\n')
    print(g, '\n')
    our_es = [f, g]
    while degree(g, x) > 0:
        h = prem(f, g)
        our_es.append(h)
        f, g = g, h
        print(g, '\n')
    return our_es
Пример #4
0
def main():
    n = 4
    a = -14.4621
    b = 60.6959
    c = -70.9238

    interval = (-10, 10)
    e = 0.0001
    x = sympy.Symbol("x")

    f = [0] * n

    f[0] = x**3 + a*x**2 + b*x + c
    f[1] = 3*x**2 + 2*a*x + b

    for i in xrange(2, n):
        f[i] = -sympy.prem(f[i - 2], f[i - 1])

    Na = 0
    Nb = 0
    for i in xrange(n - 1):
        polim = f[i]*f[i + 1]
        if polim.subs(x, interval[0]) < 0:
            Na += 1

        if polim.subs(x, interval[1]) < 0:
            Nb += 1

    print 'Number of roots: '.format(Na - Nb)

    xValue = solve(Eq(f[1], 0), x)
    print 'Root branch: '.format(xValue)

    bisection(xValue, interval)
    chords(xValue, interval)

    if __name__ == "__main__":
    main()
Пример #5
0
def test_rem_z():
    x = var('x')

    p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
    q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
    assert rem_z(p, -q, x) != prem(p, -q, x)
def test_rem_z():
    x = var('x')

    p = x**8 + x**6 - 3 * x**4 - 3 * x**3 + 8 * x**2 + 2 * x - 5
    q = 3 * x**6 + 5 * x**4 - 4 * x**2 - 9 * x + 21
    assert rem_z(p, -q, x) != prem(p, -q, x)
Пример #7
0
def main():
    n = 4
    a = 38.4621
    b = 364.594
    c = 914.196
    left = -10
    right = 10
    e = 0.0001
    x = sympy.Symbol("x")
    f = [0 for i in range(n)]
    f[0] = x**3 + a*x**2 + b*x + c
    f[1] = 3*x**2 + 2*a*x + b
    for i in range(2, n):
        f[i] = -sympy.prem(f[i - 2], f[i - 1])
    Na = 0
    Nb = 0
    for i in range(n - 1):
        polim = f[i]*f[i + 1]
        if polim.subs(x, left) < 0:
            Na += 1
        if polim.subs(x, right) < 0:
            Nb += 1

    print(Na - Nb)
    xValue = solve(Eq(f[1], 0), x)
    print (str(-19.3657805788685))
    count1 = 0
    left_d = left
    right_d = xValue[0]
    middle = (left_d + right_d) / 2
    while (abs(left_d - middle) > e) or (abs(right_d - middle) > e):
        if f[0].subs(x, left_d) * f[0].subs(x, middle) < 0:
            right_d = middle
        else:
            left_d = middle

        middle = (left_d + right_d) / 2
        count1 += 1

    print(middle)
    print(count1)

    two_diff = diff(f[1])
    count2 = 0
    if f[0].subs(x, xValue[0]) * two_diff.subs(x, xValue[0]) > 0:
        xh0 = left
        xh = xh0 - f[0].subs(x, xh0)*(xValue[0] - xh0)/(f[0].subs(x, xValue[0]) - f[0].subs(x, xh0))
        while abs(xh - xh0) > e:
            xh0 = xh
            xh = xh0 - f[0].subs(x, xh0)*(xValue[0]-xh0)/(f[0].subs(x,xValue[0]) - f[0].subs(x,xh0))
            count2 += 1

    else:
        xh0 = xValue[0]
        xh = xh0 - f[0].subs(x, xh0)*(left - xh0)/(f[0].subs(x, left) - f[0].subs(x, xh0))
        while abs(xh - xh0) > e:
            xh0 = xh
            xh = xh0 - f[0].subs(x, xh0) * (left - xh0) / (f[0].subs(x, left) - f[0].subs(x, xh0))
            count2 += 1

    print(str(-19.3657805788685))
    print(count2)