.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/abstract_algebra/polynomial_ring/plot_01_division_and_gcd.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_abstract_algebra_polynomial_ring_plot_01_division_and_gcd.py: Polynomial long division and the Euclidean algorithm ============================================================ Divides two polynomials with remainder, and finds their greatest common divisor via the polynomial Euclidean algorithm -- exactly the integer Euclidean algorithm with polynomial division in place of integer division. .. GENERATED FROM PYTHON SOURCE LINES 12-14 .. code-block:: Python from mathematicskit.abstract_algebra import Polynomial, poly_divmod, poly_gcd, poly_mul .. GENERATED FROM PYTHON SOURCE LINES 15-17 Divide x^3 - 1 by x - 1 ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 17-24 .. code-block:: Python p = Polynomial([-1, 0, 0, 1]) # x^3 - 1 q = Polynomial([-1, 1]) # x - 1 quotient, remainder = poly_divmod(p, q) print(f"(x^3 - 1) / (x - 1) = quotient {quotient.coeffs}, remainder {remainder.coeffs}") print(f"check: quotient * (x-1) + remainder = {(poly_mul(quotient, q) + remainder).coeffs}") .. rst-class:: sphx-glr-script-out .. code-block:: none (x^3 - 1) / (x - 1) = quotient [Fraction(1, 1), Fraction(1, 1), Fraction(1, 1)], remainder [Fraction(0, 1)] check: quotient * (x-1) + remainder = [Fraction(-1, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 1)] .. GENERATED FROM PYTHON SOURCE LINES 25-27 GCD of x^4 - 1 and x^6 - 1 ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-32 .. code-block:: Python a = Polynomial([-1, 0, 0, 0, 1]) # x^4 - 1 b = Polynomial([-1, 0, 0, 0, 0, 0, 1]) # x^6 - 1 g = poly_gcd(a, b) print(f"\ngcd(x^4 - 1, x^6 - 1) has degree {g.degree}, coefficients {g.coeffs}") .. rst-class:: sphx-glr-script-out .. code-block:: none gcd(x^4 - 1, x^6 - 1) has degree 2, coefficients [Fraction(-1, 1), Fraction(0, 1), Fraction(1, 1)] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.001 seconds) .. _sphx_glr_download_api_gallery_abstract_algebra_polynomial_ring_plot_01_division_and_gcd.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_division_and_gcd.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_division_and_gcd.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_division_and_gcd.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_