```python
import sympy as sp
x,y,z = sp.symbols('x,y,z')
sp.init_printing(use_unicode=False, wrap_line=False, no_global=True)

%run ../display_helpers.py
```

# SymPy - Polynomials

http://docs.sympy.org/latest/modules/polys/index.html

----
## Basics
http://docs.sympy.org/latest/modules/polys/basics.html

----

### Polynomial Division


```python
subhead("Polynomial Division - Rational (Q)")

f = 5*x**2 + 10*x +3
g = 2*x + 2
q,r = sp.div(f, g, domain='QQ')

print_eq(('{} = {},\;r\;{}', f/g, sp.factor(q), r))
```


```python
subhead("Polynomial Division - Integer (Z)")

q,r = sp.div(f, g, domain='ZZ')

print_eq(('{} = {},\;r\;{}', f/g, q, r))
```


```python
subhead("Polynomial Division - Multiple Variables")

f = x*y + y*z
g = 3*x + 3*z
q,r = sp.div(f, g, domain='QQ')

print_eq(('{} = {},\;r\;{}', f/g, q, r))
```

----

### Greatest Common Divisor (GCD) / Lowest Common Multiple (LCM)


```python
subhead("Greatest Common Divisor (GCD)")

f = (12*x + 12) * x
g = 16*x**2
q = sp.gcd(f, g)

print_eq(
    ('gcd \Big(  {}\;,\;{}  \Big) = {}', f, g, q)
)
```


```python
subhead("Greatest Common Divisor (GCD)")
desc('If the coefficients are rational, the polynomial answer is monic')
f = 3 * x **2 /2
g = 9 * x/4
q = sp.gcd(f, g)

print_eq(
    ('gcd \Big(  {}\;,\;{}  \Big) = {}', f, g, q)
)
```


```python
subhead("LCM with GCD")
f = x * y**2 + x**2 * y
g = x**2 * y**2
q = sp.gcd(f, g)
r = sp.lcm(f, g)

print_eq(
    ('gcd \Big( {}\;,\;{} \Big) &= {}', f, g, q),
    ('lcm \Big( {}\;,\;{} \Big) &= {}', f, g, r)
)
```


```python
subhead("LCM with GCD")
f = x * y**2 + x**2 * y
g = x**2 * y**2
q = sp.gcd(f, g)
r = sp.lcm(f, g)

print_eq(
    ('f &= {}', f),
    ('g &= {}', g),
    ('gcd \Big( f\;,\;g \Big) &= {}', q),
    ('lcm \Big( f\;,\;g \Big) &= {}', r),
    ('f.g &= {}', (f*g).expand() )
)
```

----
### Factorization



```python
subhead("Square-Free Factorization (SQF)")
desc('For univariate polynomials')
f = 2 * x**2 + 5 * x**3 + 4 * x**4 + x**5
q = sp.sqf_list(f)
r = sp.sqf(f)

print_eq(
    ('f &= {}', f),
    ('&= {}', r),
)

print('\n\nsqf_list(f) = ', q)
```


```python
subhead("Factorization")
desc('For univariate & multivariate polynomials with rational coefficient')
f = x**4/2 + 5*x**3/12 - x**2/3
q = sp.factor(f)

print_eq(
    ('a)\; f &= {}', f),
    ('&= {}', q),
)

print('\n\n\n')

f = x**2 + 4*x*y + 4*y**2
q = sp.factor(f)
print_eq(
    ('b)\; f &= {}', f),
    ('&= {}', q),
)
```


```python
subhead("Groebner Bases")
desc('Buchberger’s algorithm is implemented, supporting various monomial orders')
f = [x**2 + 1, y**4*x + x**3]
q = sp.groebner( f, x, y, order='lex')

print_eq(
    ('f &= {}', f)
)
print(q)

print('\n\n')

f = [x**2 + 1, y**4*x + x**3, x*y*z**3]
q = sp.groebner( f, x, y, z, order='grevlex')
print_eq(
    ('f &= {}', f)
)
print(q)
```


```python
subhead("Solve Equations")
desc('solve')
f = x**3 + 2*x + 3
q = sp.solve( f, x )

print_eq(
    ('f &= {}', f),
    ('x &= {}', q)
)

print('\n\n\n')
f = x**2 + y*x + z
q = sp.solve( f, x )

print_eq(
    ('f &= {}', f),
    ('x &= {}', q)
)
```


```python
desc('solve poly system')
f = [y-x, x-5]
q = sp.solve_poly_system(f, x, y)

print_eq(
    ('{}', f),
    ('x = {}', q)
)

print('\n\n')

f = [y**2 - x**3 + 1, y*x]
q = sp.solve_poly_system(f, x, y)

print_eq(
    ('{}', f)
)
print_eq(
    ('x = {}', q)
)
```

----

## Examples

http://docs.sympy.org/latest/modules/polys/wester.html

Simple univariate poylnomial factorization
Univariate GCD, resultant and factorization
Multivarite GCD, and factorization
Support for symbols in exponents
Testing if polynomials have common zeros
Normalizing simple rational functions
Expanding expressions and factoring back
Factoring in terms of cuclotomic polynomials
Univarite factoring over Gaussian numbers
Computing with automatic field extensions
Univariate factoring over various domains
Factoring polynomials into linear factors
Advanced factoring over finite fields
Working with Expressions as polynomials
Computing reduced Gröbner bases
Multivariate factoring over algebraic numbers
Partial fraction decomposition


- factor
- primitive
- expand
- resultant
- apart
- cancel
- solve
- groebner
- 

----

- Examples
- Polynomial Manipulation
- AGCA (Algebraic Geometry * Cummutative Algebra Module)
- Internals
- Series Maniuplation
- Literature

### Glossary

#### Associative
An expression is associative *if* $(a * b) * c = a * (b * c)$

#### Commutative
An expression is commutative *if* $a * b = b * a$

#### Distributive
An expression is distributive *if* $a \times (b + c) = a \times b + a \times c$

#### Group
Examples - clock/modular arithmetic, symmetries, integer arithmetic
Definition
- Set of elements (generally G)
- Operations (such as + or $\times$) (generall- y *)
- Closed under operation (produces another value in the set) $x, y \in G \implies x*y \in G$
- Inverses $x^{-1}$ exists for all x, and $x.x^{-1} = e$
- Identity x*e = e*x = x
- Associative $(a * b) * c = a * (b * c)$
- G may or may not be commutative $x*y \ne y*x$ (symmetries are not commutative)
    - If G is commutative, then it is a Commutative/Abelian Group
    - Otherwise it is a Noncommutative/Non-Albian Group

#### Identity
The identity of a value and operation will produce the same value. $n + 0 = n$, $n \times 1 = n$

#### Inverses
When an inverse of a value/operation are applied to a value, the result is the identity. $n + (-n) = 0$ and $n.n^{-1} = 1$

#### Monic
A Univratiate polynomial, where the leading coefficient (non-zero) is 1. i.e. $x^3 + 2x^2 - 8x +4 $
- Closed under multiplication (2 monic polys multiplied make another monic

#### Ring
Set of elements that allow + - and $\times$ operations. $\div$ and commutative $\times$ are not necessary. Examples - integers, vectors, matricies, polynomials with integer coefficients. 
- Set of elements
- 2 operations + and $\times$ (subtraction can be performed with -ve numbers)
- Addition is commutative
- Multiplication is associative (but not necessarily commutative)
- They're distributive

#### Univariate Polynomial
Single-variable polynomial, i.e. $x^4 + 3x^3 - 7x + 1$

5a1a1473ce952738d04d8587a0fee59345e374e0
ipynb
Jupyter Notebook
notebooks/python-data-science/sympy/sympy-polynomials.ipynb
sparkboom/my_jupyter_notes
9255e4236b27f0419cdd2c8a2159738d8fc383be
MIT
notebooks/python-data-science/sympy/sympy-polynomials.ipynb
sparkboom/my_jupyter_notes
9255e4236b27f0419cdd2c8a2159738d8fc383be
MIT
notebooks/python-data-science/sympy/sympy-polynomials.ipynb
sparkboom/my_jupyter_notes
9255e4236b27f0419cdd2c8a2159738d8fc383be
MIT
Qwen/Qwen-72B
1. YES
2. YES