# Polynomial

> Polynomial, In algebra, an expression consisting of numbers and variables grouped according to certain patterns. Specifically, polynomials are sums of monomials of the form axn, where a (the coefficient) can be any real number and n (the degree) must be a whole number. A polynomial’s degree is that of its monomial of highest degree. Like whole numbers, polynomials may be prime or factorable into products of primes.[$^1$](https://www.britannica.com/science/polynomial)
<br>

> A Polynomial is any algebraic expression that consists of more than 2 terms in which the only allowable exponents are whole numbers, the variable is never used as a divisor and never appears under a radical sign. Technically, many consider polynomials to consist of 4 terms or more because 2 and 3 term equations are named. [$^2$](https://www.answers.com/Q/What_is_a_polynomial)

<br>

> The roots of a polynomial are also called its zeroes, because the roots are the x values at which the function equals zero.[$^3$](https://sciencing.com/how-to-find-the-roots-of-a-polynomial-13712254.html) 

## Polynomial function
> Can be defined as: [$f'(x) = a_nx^n+a_{n-1}x^{n-1} + .........a_1x^1+a_0$](https://www.youtube.com/watch?v=-ZwTxxKfj2o)
#### Constraints[$^4$](https://www.youtube.com/watch?v=-ZwTxxKfj2o)

- Any terms cannot be equal to zero: $a_n\neq0$
- $n$ should be non-negative integer; any rational exponents, negative numbers and negative exponents cannot be part of polynomial functions.

##### Example of non-polynomial
$f'(x) = 0$<br>
$f'(x) = x^\frac{1}{2}$<br>
$f'(x) = 13x1$<br>
$f'(x) = x^{-2}$






$--------$

1. The Editors of Encyclopaedia Britannica, "Polynomial," Encyclopædia Britannica, September 30, 2013, , accessed April 19, 2019, https://www.britannica.com/science/polynomial.
2. "What Is a Polynomial?" Answers.com, , accessed April 19, 2019, https://www.answers.com/Q/What_is_a_polynomial.
3. Lisa Maloney, "How to Find the Roots of a Polynomial," Sciencing, March 02, 2019, , accessed April 19, 2019, https://sciencing.com/how-to-find-the-roots-of-a-polynomial-13712254.html.
4. Brian McLogan, "What Is a Polynomial Function," YouTube, August 29, 2012, , accessed April 20, 2019, https://www.youtube.com/watch?v=_AXlBBfVJQU. 



```python
from sympy import *
#init_printing()
init_printing(use_unicode=False, wrap_line=False)
#init_printing(use_unicode=True)
```


```python
import numpy as np
import math
from math import floor, ceil, log, sqrt

```


```python
import pandas as pd
```


```python
from statistics import mean
```


```python
from matplotlib import pyplot as plt
from matplotlib.pyplot import figure
from matplotlib.pyplot import xticks
```

#### Sample problem: 
Find all the zeroes of $y = 2x^5 + 3x^4 – 30x^3 – 57x^2 – 2x + 24$
> Asking to find the zeroes of a polynomial function, y equals (polynomial), means the same thing as asking to find the solutions to a polynomial equation, (polynomial) equals (zero). The zeroes of a polynomial are the values of x that make the polynomial equal to zero. Either task may be referred to as "solving the polynomial".[$^5$](https://www.purplemath.com/modules/solvpoly.htm)

$--------$
5. Elizabeth Stapel, "Solving Polynomials," Purplemath, , accessed April 19, 2019, https://www.purplemath.com/modules/solvpoly.htm.


```python
x, y = symbols('x y')
```


```python
y = 2*x**5 + 3*x**4 - 30*x**3 - 57*x**2 -2*x + 24
#print y
#init_printing()
y
```


```python
#Get the factorization; takes a polynomial and factors it into irreducible factors over the rational numbers
factorization = factor(y)
factorization
```


```python
apart(2*x**5 + 3*x**4 - 30*x**3 - 57*x**2 -2*x + 24)
```


```python
equation = y.as_poly()
equation
```


```python
#Get the roots from the coefficient of y
coeff = [2, 3, -30, -57, -2, 24]
root = np.roots(coeff)
root

```




    array([ 4.        , -3.56155281, -1.5       , -1.        ,  0.56155281])




```python
result = solve(factorization)
result
```


```python
# Plug the values of x from the original equation through substitution
x_value1 = -1
x_value2 = 4

y_set1 = y.subs(x, x_value1)
y_set2 = y.subs(x, x_value2)

print(y_set1)
print(y_set2)
# All equate to zero
```

    0
    0



```python
# Plug the values of x from the factorization equation through substitution
y_sets1 = factorization.subs(x, x_value1)
y_sets2 = factorization.subs(x, x_value2)

print(y_sets1)
print(y_sets2)
# All equate to zero
```

    0
    0



```python
#Test original equation to factorization for checking
from random import randint
x1 = randint(3,100)

factor1 = factorization.subs(x, x1)
original = y.subs(x, x1)

original == factor1

```




    True




```python
#Find the y-intercept in the original equation; that is x-intercept == 0, plug all x with 0

y_intercept = factorization.subs(x, 0)
print('y-intercept: (0, {})'.format(y_intercept))
```

    y-intercept: (0, 24)



```python
# Plot x and y intercept
df = pd.DataFrame({'x': range(-10, 11)})
df['y'] = (df['x'] - 4)*(df['x'] + 1)*(2*df['x']+3)*(df['x']**2+3*df['x'] - 2)
m, b = np.polyfit(df.x, df.y, 1)

df
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>x</th>
      <th>y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-10</td>
      <td>-145656</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-9</td>
      <td>-81120</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-8</td>
      <td>-41496</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-7</td>
      <td>-18876</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-6</td>
      <td>-7200</td>
    </tr>
    <tr>
      <th>5</th>
      <td>-5</td>
      <td>-2016</td>
    </tr>
    <tr>
      <th>6</th>
      <td>-4</td>
      <td>-240</td>
    </tr>
    <tr>
      <th>7</th>
      <td>-3</td>
      <td>84</td>
    </tr>
    <tr>
      <th>8</th>
      <td>-2</td>
      <td>24</td>
    </tr>
    <tr>
      <th>9</th>
      <td>-1</td>
      <td>0</td>
    </tr>
    <tr>
      <th>10</th>
      <td>0</td>
      <td>24</td>
    </tr>
    <tr>
      <th>11</th>
      <td>1</td>
      <td>-60</td>
    </tr>
    <tr>
      <th>12</th>
      <td>2</td>
      <td>-336</td>
    </tr>
    <tr>
      <th>13</th>
      <td>3</td>
      <td>-576</td>
    </tr>
    <tr>
      <th>14</th>
      <td>4</td>
      <td>0</td>
    </tr>
    <tr>
      <th>15</th>
      <td>5</td>
      <td>2964</td>
    </tr>
    <tr>
      <th>16</th>
      <td>6</td>
      <td>10920</td>
    </tr>
    <tr>
      <th>17</th>
      <td>7</td>
      <td>27744</td>
    </tr>
    <tr>
      <th>18</th>
      <td>8</td>
      <td>58824</td>
    </tr>
    <tr>
      <th>19</th>
      <td>9</td>
      <td>111300</td>
    </tr>
    <tr>
      <th>20</th>
      <td>10</td>
      <td>194304</td>
    </tr>
  </tbody>
</table>
</div>




```python
%matplotlib inline
figure(num=None, figsize=(10, 7), dpi=80, facecolor='w', edgecolor='k')
plt.plot((df.x), (df.y / 10**4), linestyle='None', markersize = 7.0, marker='o', label='y = {}'.format(factorization))
sup = str.maketrans("2", "²")
plt.title('Graph of y = {}'.format(factorization), color='m')
plt.xlabel('X')
plt.ylabel('Y')
plt.axhline(color='k')
plt.axvline(color='k')
plt.legend(loc='upper left')
plt.annotate('x-intercept1', (4, 0), color='r')
plt.annotate('x-intercept2', (-1, 0), color='g')
plt.annotate('y-intercept', (0, 24), color='m')
xticks(df.x)

plt.show()
```


```python
#Find the slope
m
```


```python
#Find intercept
# Can graph using slope-intercept form y = mx + b; x as df['x']
b

```


```python
#Example of getting the roots of polynomial
bb = (4*x + 6*x**2) / (2*x) 
bb

```


```python
# Simpler form
factor(bb)
```

bbf5e0b67b7bfa1bee310b7a3865f7cb518415fc
ipynb
Jupyter Notebook
Polynomial/Polynomial.ipynb
arnelimperial/Math-and-Stat
341654dc406bd9f9032d8b7fe89f48fb2d06ee93
CC0-1.0
Polynomial/Polynomial.ipynb
arnelimperial/Math-and-Stat
341654dc406bd9f9032d8b7fe89f48fb2d06ee93
CC0-1.0
Polynomial/Polynomial.ipynb
arnelimperial/Math-and-Stat
341654dc406bd9f9032d8b7fe89f48fb2d06ee93
CC0-1.0
Qwen/Qwen-72B
1. YES
2. YES