Example #1
0
    def multi_divide_list(self, f, F):
        """

        This function divides given f by the set of polynomials called F = { f1, f2, ... , fN}
        This function returns the u1, u2, ..., uN for the polynomials where F = u1.f1 + u2.f2 +..... + uN.fN

        """
        preff = []
        m = Poly()
        preff = m.preferred()
        h = Poly()
        r = Poly()
        U = []
        h.poly = f.poly
        lt_h = mono()
        lt_h = h.leading_term(preff)
        Flagr = 0

        while h.poly != 0:

            for i in F:

                lt_Fi = mono()
                lt_Fi = i.leading_term(preff)
                Flagr = 0
                if lt_h.is_divisible_by(lt_Fi):

                    mulP = Poly()
                    d = Poly()
                    div = mono()
                    div.monoDiv(lt_h, lt_Fi)
                    u = Poly()
                    u.poly.append(div)
                    U.append(u)
                    mulP = mulP.monoMultPoly(div, i)
                    s = Poly()
                    r = Poly()
                    r = s.sub(h, mulP)
                    d = s.simplified()
                    h.poly = d.poly
                    lt_h = h.leading_term(preff)
                    Flagr = 1

                if Flagr == 0 and h != 0:

                    r.poly = h.poly
                    h.poly = 0
                    return False

        return True
Example #2
0
    def sub(self, first, second):
        """

        This function returns the subtraction of two polynomials



        """
        i = 0
        j = 0
        self.poly = []
        l1 = len(first.poly)
        l2 = len(second.poly)

        while i < l1 or j < l2:
            if (i < l1 and j < l2) and first.poly[i].x == second.poly[
                    j].x and first.poly[i].y == second.poly[
                        j].y and first.poly[i].z == second.poly[j].z:
                z = mono()
                #first.poly[i].c = first.poly[i].c - second.poly[j].c
                z.monoSub(first.poly[i], second.poly[j])
                self.poly.append(z)
                i = i + 1
                j = j + 1

            elif i == l1 and j < l2:
                second.poly[j].c = -1 * second.poly[j].c
                self.poly.append(second.poly[j])
                j = j + 1

            else:
                self.poly.append(first.poly[i])
                i = i + 1
        return self
Example #3
0
    def leader_in_modules(self, lead):

        m = mono()
        if lead == 1:
            m, index = self.lt_in_module([3, 2, 1], 1)
        elif lead == 2:
            m, index = self.lt_in_module([2, 3, 1], 1)
        elif lead == 3:
            m, index = self.lt_in_module([1, 2, 3], 1)
        return m, index
Example #4
0
    def leader_in_poly(self, lead):

        m = mono()
        if lead == 1:
            m = self.leading_term([3, 2, 1])
        elif lead == 2:
            m = self.leading_term([2, 3, 1])
        elif lead == 3:
            m = self.leading_term([1, 2, 3])
        return m
Example #5
0
    def getValuesPoly(self):
        """

        To input degrees of monomials

        """
        self.poly = []
        n = int(input('How many terms?'))
        for i in range(n):
            x = mono()
            x.getValues()
            self.poly.append(x)
Example #6
0
    def s_polynomial_for_modules(self, f, g, preff, choice):
        lt_f, index = f.lt_in_module(preff, choice)
        lt_g, index = g.lt_in_module(preff, choice)
        l = mono()
        l = l.lcm(lt_f, lt_g)
        t1 = mono()
        t1.monoDiv(l, lt_f)
        t2 = mono()
        t2.monoDiv(l, lt_g)

        T1 = Module()
        T2 = Module()
        for i in f.module:
            p = Poly()
            p.monoMultPoly(t1, i)
            T1.module.append(p)
        for i in g.module:
            p = Poly()
            p.monoMultPoly(t2, i)
            T2.module.append(p)

        return self.sub_module(T1, T2)
Example #7
0
    def leading_term(self, pref):
        """

        This function returns the leading term of the polynomial


        """

        x = pref[0]
        y = pref[1]
        z = pref[2]

        a = Poly()
        a.poly = []
        for i in range(len(self.poly)):
            lo = mono()
            lo.x = x * self.poly[i].x
            lo.y = y * self.poly[i].y
            lo.z = z * self.poly[i].z
            a.poly.append(lo)

        if self.poly[0].c == 0:
            sum0 = 0
        else:
            sum0 = (a.poly[0].x + a.poly[0].y + a.poly[0].z)

        big = mono()
        big = self.poly[0]

        for i in range(len(a.poly)):
            sum = (a.poly[i].x + a.poly[i].y + a.poly[i].z)

            if sum > sum0 and self.poly[i].c != 0:
                sum0 = sum
                big = self.poly[i]
            else:
                pass
        return big
Example #8
0
    def mult(self, poly1, poly2):
        """

        This function returns the result of multiplication of two polynomials


        """
        self.poly = []
        for i in poly1.poly:
            for j in poly2.poly:
                z = mono()
                z.monoMult(i, j)
                self.poly.append(z)
                print(i.repr(), j.repr(), z.repr())
        self.repr_poly(self)
        return self
Example #9
0
    def monoMultPoly(self, first, other):
        """

        :param first: monomial
        :param other: polynomial
        ;return self: polynomial


        This function returns the multiplication of a monomial with a polynomial

        """
        self.poly = []
        for i in other.poly:
            mul = mono()
            mul = mul.monoMult(first, i)
            self.poly.append(mul)
        return self
Example #10
0
    def s_polynomial(self, f, g):
        """

        This function returns the S polynomial of two ploynomials.
        formula used:
        S(f,g) = LCM(lt(f), lt(g))         LCM(lt(f), lt(g))
                ----------------- . f  -   ----------------- . g
                    lt(f)                         lt(g)



        """
        preff = []
        m = Poly()
        preff = m.preferred()
        lt_f = mono()
        lt_f = f.leading_term(preff)
        lt_g = mono()
        lt_g = g.leading_term(preff)
        L = mono()
        L.lcm(lt_f, lt_g)
        t1 = mono()
        t1.monoDiv(L, lt_f)
        t2 = mono()
        t2.monoDiv(L, lt_g)
        T1 = Poly()
        T2 = Poly()
        for i in f.poly:
            v = mono()
            v.monoMult(i, t1)
            T1.poly.append(v)
        for i in g.poly:
            v = mono()
            v.monoMult(i, t2)
            T2.poly.append(v)
        return self.sub(T1, T2)
Example #11
0
    def reduced_grobner_sev_terms(self, G, n):
        q = []
        g = Module()
        g.module = self.module
        print('In part 1')
        print("g ", g.repr_module(g))
        i = 0
        while i < n and g.is_div_by_mod(G):
            i = i + 1
            print('In Part 2')
            t11 = mono()
            t21 = mono()
            t31 = mono()
            index11 = 0
            index21 = 0
            index31 = 0
            t11, index11 = g.leader_in_modules(1)
            t21, index21 = g.leader_in_modules(2)
            t31, index31 = g.leader_in_modules(3)
            print('t11 ', t11.repr())
            print('t21 ', t21.repr())
            print('t31 ', t31.repr())
            for j in G:
                t12 = mono()
                t22 = mono()
                t32 = mono()
                index12 = 0
                index22 = 0
                index32 = 0
                t12, index12 = j.leader_in_modules(1)
                t22, index22 = j.leader_in_modules(2)
                t32, index32 = j.leader_in_modules(3)
                print('t12 ', t12.repr())
                print('t22 ', t22.repr())
                print('t32 ', t32.repr())
                if t11.is_divisible_by(t12) and t21.is_divisible_by(
                        t22) and t31.is_divisible_by(
                            t32) and index11 == index12:
                    print('In part 3')

                    m = Module()
                    m.module = []
                    ind = Poly()
                    div = mono()
                    mulP = Poly()
                    k = Module()
                    s = Module()
                    d = Module()
                    for a in j.module:
                        m.module.append(a)
                    ind = j.module[index11]
                    div.monoDiv(t11, t12)
                    mulP = mulP.monoMultPoly(div, ind)
                    index23 = 0
                    index33 = 0
                    t23, index23 = mulP.leader_in_poly(2)
                    t33, index33 = mulP.leader_in_poly(3)
                    if t21.is_divisible_by(t23) and t31.is_divisible_by(t33):
                        m.module[index11] = mulP
                        k = s.sub_module(g, m)
                        d = k.simplified_module()
                        g.module = d.module
                        print('g = ', g.repr_module(g))
                    else:
                        pass
        return g
Example #12
0
    def division_in_modules(self, f, G, preff, choice):
        """
        This algorithm returns boolean True if f is reducible by G, otherwise it returns boolean False
        :param choice:
        :param preff:
        :param f: module containing polynomials
        :param G: set of Modules.
        :return: reduced remainder
        """

        a = Poly()
        b = Poly()
        c = Poly()
        z = mono()
        a.poly.append(z)
        b.poly.append(z)
        c.poly.append(z)
        r = Module()
        r.module = []
        r.module.append(a)
        r.module.append(b)
        r.module.append(c)

        h = Module()
        h.module = f.module
        lt_h = mono()
        lt_h, index1 = h.lt_in_module(preff, choice)
        Flag_r = 0
        o = 0
        while o < 10 and h.isNotEmpty():
            o = o + 1
            for i in G:
                lt_Gi = mono()
                lt_Gi, index2 = i.lt_in_module(preff, choice)
                Flag_r = 0

                if lt_h.is_divisible_by(lt_Gi) and index1 == index2:

                    m = Module()
                    e = Poly()
                    ind = Poly()
                    mulP = Poly()
                    div = mono()
                    d = Module()
                    s = Module()
                    k = Module()
                    m.module = []
                    e.poly = []
                    for j in i.module:
                        m.module.append(j)
                    ind = i.module[index1]
                    div.monoDiv(lt_h, lt_Gi)
                    mulP = mulP.monoMultPoly(div, ind)
                    m.module[index1] = mulP
                    k = s.sub_module(h, m)
                    d = k.simplified_module()
                    h = d
                    lt_h, index1 = h.lt_in_module(preff, choice)
                    Flag_r = 1

            if Flag_r == 0 and h != 0:

                lt_h, index1 = h.lt_in_module(preff, choice)
                # ---------------------------------------------
                # r = r + lt(h)
                x = mono()
                x = lt_h
                if index1 == 0:
                    a.poly.append(x)

                elif index1 == 1:
                    b.poly.append(x)

                elif index1 == 2:
                    c.poly.append(x)
                # ------------------------------------------------
                # h = h - lt(h)
                w = Poly()
                u = Poly()
                l = Poly()
                w.poly.append(x)
                l = h.module[index1]
                u.sub(l, w)
                u.simplified()
                h.module[index1] = u
                lt_h, index1 = h.lt_in_module(preff, choice)

        return r
Example #13
0
    def lt_in_module(self, preff, choice):
        """
        This function returns the leading term and index according to deglex ordering
        :param preff: between x > y > z and more cases
        :param choice: between Term over position or Position over term
        :return: monomial

        """

        ch = 0
        x = preff[0]
        y = preff[1]
        z = float(preff[2])
        while ch == 0:

            if choice == 1:
                ch = ch + 1
                p = Poly()
                p.poly = []

                for i in range(len(self.module)):
                    e = mono()
                    e = self.module[i].leading_term(preff)
                    p.poly.append(e)
                # -----------------------------------------------------------
                m = mono()
                a = Poly()
                a.poly = []
                for i in range(len(p.poly)):
                    lo = mono()
                    lo.x = x * p.poly[i].x
                    lo.y = y * p.poly[i].y
                    lo.z = z * p.poly[i].z
                    a.poly.append(lo)

                if p.poly[0].c == 0:
                    sum0 = 0
                else:
                    sum0 = (a.poly[0].x + a.poly[0].y + a.poly[0].z)
                    index = 0
                big = mono()
                big = p.poly[0]
                index = 0
                for i in range(len(a.poly)):
                    sum = (a.poly[i].x + a.poly[i].y + a.poly[i].z)

                    if sum > sum0 and p.poly[i].c != 0:
                        sum0 = sum
                        big = p.poly[i]
                        index = i
                    else:
                        pass
                m = big
                #  -----------------------------------------------------------
                # m = p.leading_term(preff)
                return m, index

            elif choice == 2:
                ch = ch + 1
                while char == 0:

                    char = int(
                        input(
                            'Choose any one: \nMenu \n1. First Base at Highest \n2. Last base at Highest'
                        ))
                    if char == 1:
                        m = mono()
                        m = self.module[0].leading_term(preff)
                        return m

                    elif char == 2:
                        l = len(self.module)
                        n = l - 1
                        m = mono()
                        m = self.module[n].leading_term(preff)
                        return m

                    else:
                        print('Wrong Input!! Try Again ')
                        char = 0
            else:
                print('Wrong Input!! Try Again ')
                ch = 0
Example #14
0
    )
    while 1:
        time = str(input())
        a = time.split(':')
        if int(a[0]) < 11 and int(a[1]) < 61:
            print('\nTime: ', time)
            cut(time)
            break
        else:
            print('Invalid option, choose another time')

elif '2)' in exercice:
    histogram()

elif '3)' in exercice:
    print(
        'Define the width and height as (360x240) or the resolution as (720p)')
    while 1:
        size = str(input())
        if 'p' in size or 'x' in size:
            print('\nSize: ', size)
            resize(size)
            break
        else:
            print('Invalid option, choose another size')

elif '4)' in exercice:
    print('Which codec do you want to use?')
    codec = str(input())
    mono(codec)