# some simple univariate polynomials

* computing with real univariate polynomials ...
* it seems that the gcd function for floating point numbers does not give what we expect
* can you fix this?
    * hint 1: Euclid's algorithm is used for the gcd computations
    * hint 2: this problem has been studied
    * hint 3: the product of the two polynomials is not the exact product
    * hint 4: check out the code relating to domains 


```python
# example how to use numpy arrays to define polynomial systems ...

from sympy import symbols

from numpy import arange, dot
from numpy.random import rand

x = symbols('x')
d = 3  # degree
n = 2  # size of system

# array of monomials xb, degree d
ie = arange(d+1)
xb = x**ie  # Stetter denotes this by b(x)

# random coefficients
C = rand(n,d+1)

# polynomial system
P = dot(C,xb)

print("random polynomial system: \n {}\n".format(P))

# convert to sympy polynomial (P[i] are just expressions)
print("polynomial P[0]: {}".format(P[0].as_poly()))
```


```python
# division with remainder
from sympy import div
q, r = div(P[1], P[0])
print(" quotient  q = {} \n remainder r = {}".format(q,r))

# check result
print(" should be zero: {}".format(q*P[0] + r - P[1]))
```


```python
# get some help
help(div)
```


```python
# compute the gcd
from sympy import gcd
q = gcd(P[0],P[1])
print(" gcd of P[0] and P[1] : {}".format(q))
```


```python
# a less trivial case of gcd ???? try a few different random polynomials ...
q = gcd(P[0]*P[1], P[0])
print(" gcd of P[0]*P[1] and P[0] : {}".format(q))
```


```python
# get some help!
help(gcd)
```


```python
# do the same computation with rationals
from sympy import Rational
from numpy import vectorize # (not necessarily fast)
q_vect = vectorize(Rational)

Cq = q_vect(C)
Q = dot(Cq,xb)

q = gcd(Q[0]*Q[1], Q[0])
print(" gcd of Q[0]*Q[1] and Q[0] : {}".format(q))

_, r = div(Q[0], q)
print(" remainder of division Q[0]/q : {}".format(r))

print(" difference Q-P : {}".format(Q-P))

print(" difference Q[0]*Q[1] - P[0]*P[1] : {}".format((Q[0]*Q[1]-P[0]*P[1]).as_poly()))
```

e02ff11400cbffe6a0b50f9698d20b550bca7e52
ipynb
Jupyter Notebook
Coding/RealPolynomials.ipynb
Chr1sWilliams/NumericalPolyonomialAlgebra
a99701bc68ee1db052d5c0b1aabb1588800b6cc3
MIT
Coding/RealPolynomials.ipynb
Chr1sWilliams/NumericalPolyonomialAlgebra
a99701bc68ee1db052d5c0b1aabb1588800b6cc3
MIT
Coding/RealPolynomials.ipynb
Chr1sWilliams/NumericalPolyonomialAlgebra
a99701bc68ee1db052d5c0b1aabb1588800b6cc3
MIT
Qwen/Qwen-72B
1. YES
2. YES