Ejemplo n.º 1
0
def main():
    """Main function to test Poly class.

    """
    coeff = Poly([2, 0, 4, -1, 0, 6])
    coeff2 = Poly([-1, -3, 0, 4.5])
    print('P_a: ', coeff, ' Order = ', Poly.order(coeff))
    print('P_b: ', coeff2, ' Order = ', Poly.order(coeff2))
    print('P_a + P_b: ', coeff + coeff2)
    print('Diff P_a: ', Poly.deriv(coeff))
    print('Diff P_b: ', Poly.deriv(coeff2))
    print('Integral P_a: ', Poly.anti_deriv(coeff))
    print('Integral P_b: ', Poly.anti_deriv(coeff2))
Ejemplo n.º 2
0
def poly_reduce(f, g, *symbols):
    """Removes common content from a pair of polynomials.

       >>> from sympy import *
       >>> x = Symbol('x')

       >>> f = Poly(2930944*x**6 + 2198208*x**4 + 549552*x**2 + 45796, x)
       >>> g = Poly(17585664*x**5 + 8792832*x**3 + 1099104*x, x)

       >>> F, G = poly_reduce(f, g)

       >>> F
       Poly(64*x**6 + 48*x**4 + 12*x**2 + 1, x)
       >>> G
       Poly(384*x**5 + 192*x**3 + 24*x, x)

    """
    if not isinstance(f, Poly):
        f = Poly(f, *symbols)
    elif symbols:
        raise SymbolsError("Redundant symbols were given")

    f, g = f.unify_with(g)

    fc = int(f.content)
    gc = int(g.content)

    cont = igcd(fc, gc)

    if cont != 1:
        f = f.div_term(cont)
        g = g.div_term(cont)

    return f, g
Ejemplo n.º 3
0
    def test_derivative(self):
        field = FF(p=3)
        polyring = Poly(field=field)

        f = [field.uni(1) for i in range(11)]
        self.assertEqual(polyring.derivative(f),
                         [field.uni(i + 1) for i in range(10)])
Ejemplo n.º 4
0
    def right_factor(f, s):
        n, lc = f.degree, f.LC

        f = f.as_uv_dict()
        q = {s: S.One}

        r = n // s

        for k in xrange(1, s):
            coeff = S.Zero

            for j in xrange(0, k):
                if not n + j - k in f:
                    continue

                if not s - j in q:
                    continue

                fc, qc = f[n + j - k], q[s - j]

                coeff += (k - r * j) * fc * qc

            if coeff is not S.Zero:
                q[s - k] = coeff / (k * r * lc)

        return Poly(q, *symbols, **flags)
Ejemplo n.º 5
0
    def test_add(self):
        field = FF(p=5)
        polyring = Poly(field=field)

        f = [[0], [1]]
        g = [[], [], [2]]

        self.assertEqual(polyring.add(f, g), [[], [1], [2]])
Ejemplo n.º 6
0
    def test_gcd(self):
        field = FF(p=3)
        polyring = Poly(field=field)

        f = [[0], [1]]
        g = [[], [], [2]]

        self.assertEqual(polyring.gcd(f, g), f)
Ejemplo n.º 7
0
    def test_div_mod(self):
        field = FF(p=3)
        polyring = Poly(field=field)

        f = [[0], [1]]
        g = [[], [], [2]]

        self.assertEqual(polyring.div_mod(g, f), ([[], [2]], []))
Ejemplo n.º 8
0
def poly_pdiv(f, g, *symbols):
    """Univariate polynomial pseudo-division with remainder.

       Given univariate polynomials f and g over an integral domain D[x]
       applying classical division algorithm to LC(g)**(d + 1) * f and g
       where  d = max(-1, deg(f) - deg(g)),  compute polynomials q and r
       such that LC(g)**(d + 1)*f = g*q + r and r = 0 or deg(r) < deg(g).
       Polynomials q and r are called the pseudo-quotient of f by g and
       the pseudo-remainder of f modulo g respectively.

       For more information on the implemented algorithm refer to:

       [1] M. Bronstein, Symbolic Integration I: Transcendental
           Functions, Second Edition, Springer-Verlang, 2005

    """
    if not isinstance(f, Poly):
        f = Poly(f, *symbols)
    elif symbols:
        raise SymbolsError("Redundant symbols were given")

    f, g = f.unify_with(g)

    if f.is_multivariate:
        raise MultivariatePolyError(f)

    symbols, flags = f.symbols, f.flags

    q, r = Poly((), *symbols, **flags), f
    coeff, N = g.LC, f.degree - g.degree + 1

    while not r.is_zero:
        M = r.degree - g.degree

        if M < 0:
            break
        else:
            T, N = (r.LC, (M, )), N - 1

            q = q.mul_term(coeff).add_term(*T)
            r = r.mul_term(coeff) - g.mul_term(*T)

    return (q.mul_term(coeff**N), r.mul_term(coeff**N))
Ejemplo n.º 9
0
def poly_half_gcdex(f, g, *symbols):
    """Half extended Euclidean algorithm.

       Efficiently computes gcd(f, g)  and one of the coefficients
       in extended Euclidean algorithm. Formally, given univariate
       polynomials f and g over an Euclidean domain, computes s
       and h, such that h = gcd(f, g) and s*f = h (mod g).

       For more information on the implemented algorithm refer to:

       [1] M. Bronstein, Symbolic Integration I: Transcendental
           Functions, Second Edition, Springer-Verlang, 2005

    """
    if not isinstance(f, Poly):
        f = Poly(f, *symbols)
    elif symbols:
        raise SymbolsError("Redundant symbols were given")

    f, g = f.unify_with(g)

    if f.is_multivariate:
        raise MultivariatePolyError(f)

    symbols, flags = f.symbols, f.flags

    a = Poly(S.One, *symbols, **flags)
    b = Poly((), *symbols, **flags)

    while not g.is_zero:
        q, r = poly_div(f, g)

        f, g = g, r
        c = a - q * b
        a, b = b, c

    return a.div_term(f.LC), f.as_monic()
Ejemplo n.º 10
0
    def left_factor(f, h):
        g, i = {}, 0

        while not f.is_zero:
            q, r = poly_div(f, h)

            if not r.is_constant:
                return None
            else:
                if r.LC is not S.Zero:
                    g[i] = r.LC

                f, i = q, i + 1

        return Poly(g, *symbols, **flags)
Ejemplo n.º 11
0
def main():
    p = Poly("-312*x^2 +13x^3")
    print("p =", p)
    print("p(10) =", p.application(10))
    print("2p =", p * 2)
    print()

    q = Poly("9x^20 + 5 - 5x^3 + 1x")
    print("q =", q)
    print("-q =", -q)
    print("q coefficients:", q.power_series())
    print()

    r = Poly("7 - 3*x^5 + 10*x - 5")
    print("r =", r)
    print()

    s = Poly("1 - 1x + 1x^2 - 1x^3 + 1x^4")
    print("s =", s)
    print()

    s_ = Poly(
        "1 - 1x + 1x^2 - 1x^3 + 1x^4 - 1x^5 + 1x^6 - 1x^7 + 1x^8 - 1x^9 + 1x^10"
    )
    print("s_ =", s_)
    print()

    t = Poly([1, -2, 3, 4, -5, 6])
    print("t =", t)
    print()

    u = Poly.optimal_fit([1, 8, 27, 64])
    print("u =", u)
    print()

    print("p == q:", p == q)
    print("p + q =", p + q)
    print("p - q =", p - q)
    print("p * q =", p * q)
    print()

    target = Poly(
        "1 - 1x + 1x^2 - 1x^3 + 1x^4 - 1x^5 + 1x^6 - 1x^7 + 1x^8 - 1x^9 + 1x^10"
    )
    print(target)
    print(target.derivative())
    print()
Ejemplo n.º 12
0
    def on_click(self):
        errormsg = ""  # Message to append to in case of bad behaviour.
        # Ensure poly_to_factor parses correctly.
        p = 1
        to_factor = None
        pivot = None

        try:
            str = self.polynomialInput.toPlainText()
            to_factor = parse_unbracketed(str)
        except:
            errormsg += "Error, cannot parse polynomial to factor.\n"

        # Ensure p pares correctly.
        try:
            p = int(self.charpInput.toPlainText())
        except:
            errormsg += "Error, p is not a valid integer.\n"

        # Ensure p is prime.
        if not (prime(p)):
            errormsg += "Error, p is not prime.\n"

        # Ensure pivot parses correctly.
        try:
            pivot = parse_unbracketed(self.minPolyEntry.toPlainText(),
                                      as_integer=True)
        except:
            errormsg += "Error, cannot parse minimal polynomial."

        # Ensure pivot is irreducible.

        if errormsg == "":
            try:
                field = FF(p, pivot=pivot)
                polyring = Poly(field)
                msg = factorise(to_factor, polyring)
                self.outputDisplay.setText(msg)
            except:
                self.outputDisplay.setText(
                    "An error occured during factoring, please try again.")
        else:
            self.outputDisplay.setText(errormsg)
Ejemplo n.º 13
0
	def test_method_iadd_different_coeffs_num_1(self):
		self.pm = Poly([1, 2, 3, 4])
		self.pm1 = Poly([1, 2, 3, 4, 5])
		self.pm += self.pm1
		self.assertEqual(self.pm.__str__(), "x^4+3x^3+5x^2+7x+9")
Ejemplo n.º 14
0
	def test_method_iadd_single_val(self):
		self.pm = Poly([1])
		self.pm1 = Poly("1")
		self.pm += self.pm1
		self.assertEqual(self.pm.__str__(), "2")
Ejemplo n.º 15
0
	def test_method_ne_list_str(self):
		self.pm1 = Poly("1, 2, 3, 5")
		self.assertTrue(self.pm1 != self.pm, "Error! Polynomials are equal")
Ejemplo n.º 16
0
	def test_method_ne_list_tupl(self):
		self.pm1 = Poly((1, 2, 3, 5))
		self.assertTrue(self.pm1 != self.pm, "Error! Polynomials are equal")
Ejemplo n.º 17
0
	def test_method_ne_long_true(self):
		self.pm = Poly(range(100))
		self.pm1 = Poly(range(100))
		self.assertFalse(self.pm != self.pm1, "Error! Polynomials aren't equal")
Ejemplo n.º 18
0
	def setUp(self):
		self.pm = Poly([1, 2, 3, 4])
		pass
Ejemplo n.º 19
0
	def test_method_mul_single_val(self):
		self.pm = Poly([1])
		self.pm1 = self.pm * Poly("1")
		self.assertEqual(self.pm1.__str__(), "1")
Ejemplo n.º 20
0
	def test_method_equal_list_range(self):
		self.pm1 = Poly(range(1,5))
		self.assertTrue(self.pm1 == self.pm, "Error! Polynomials are equal")
Ejemplo n.º 21
0
	def test_method_sub_different_coeffs_num_2(self):
		self.pm = Poly([1, 2, 3, 4, 5, 6])
		self.pm1 = Poly("1, 2, 3, 4")
		self.pm2 = self.pm - self.pm1
		self.assertEqual(self.pm2.__str__(), "x^5+2x^4+2x^3+2x^2+2x+2")
Ejemplo n.º 22
0
	def test_method_sub_pm_and_float(self):
		self.pm = Poly([1, 2, 3, 4, 5, 6])
		self.pm1 = self.pm - 2
		self.assertEqual(self.pm1.__str__(), "x^5+2x^4+3x^3+4x^2+5x+4")
Ejemplo n.º 23
0
	def test_method_str_list_float(self):
		self.pm = Poly([17.5, 1.3, -1.746, 8])
		self.assertEqual(self.pm.__str__(), "17.5x^3+1.3x^2-1.75x+8")
Ejemplo n.º 24
0
	def test_method_mul_pm_and_float(self):
		self.pm = Poly([1, 2, 3, 4, 5, 6])
		self.pm1 = self.pm * 2
		self.assertEqual(self.pm1.__str__(), "2x^5+4x^4+6x^3+8x^2+10x+12")
Ejemplo n.º 25
0
	def test_method_mul_different_coeffs_num_2(self):
		self.pm = Poly((3, 2, 0, -1))
		self.pm1 = self.pm * (-1, 0, 1)
		self.assertEqual(self.pm1.__str__(), "-3x^5-2x^4+3x^3+3x^2-1")
Ejemplo n.º 26
0
	def test_method_equal_single_val(self):
		self.pm = Poly([1])
		self.pm1 = Poly(1)
		self.assertTrue(self.pm == self.pm1, "Error! Polynomials aren't equal")
Ejemplo n.º 27
0
	def test_method_equal_long_false(self):
		self.pm = Poly(range(100))
		self.pm1 = Poly(range(101))
		self.assertFalse(self.pm1 == self.pm, "Error! Polynomials are equal")
Ejemplo n.º 28
0
	def test_method_str_single_val(self):
		self.pm = Poly(76543)
		self.assertEqual(self.pm.__str__(), "76543")
Ejemplo n.º 29
0
	def test_method_str_tuple(self):
		self.pm = Poly((1, 2, 3, 4))
		self.assertEqual(self.pm.__str__(), "x^3+2x^2+3x+4")
Ejemplo n.º 30
0
	def test_method_equal_list_list(self):
		self.pm1 = Poly([1, 2, 3, 4])
		self.assertTrue(self.pm1 == self.pm, "Error! Polynomials are equal")