source: http://calculuswithjulia.github.io/precalc/polynomial.html

Polynomials

For our purposes, a monomial is simply a non-negative integer power of \(x\) (or some other indeterminate symbol) possibly multiplied by a scalar constant.

A polynomial is a sum of monomials. After combining terms with same powers, a non-zero polynomial my be written uniquely as:

\[ a_n x^n + a_{n-1}x^{n-1} + \cdots a_1 x + a_0, \quad a_n \neq 0 \]

The numbers \(a_0, a_1, \ldots, a_n\) are the coefficients of the polynomial. With the convention that \(x=x^1\) and \(1=x^0\), the monomials above have their power match their coefficient’s index, e.g., \(a_ix^i\). Outside the coefficient \(a_n\), the other coefficients may be negative, positive, or \(0\). Except for the zero polynomial, the largest power \(n\) is called the degree. The degree of the zero polynomial is typically not defined or defined to be \(-1\), so as to make certain statements easier to express. The term \(a_n\) is called the leading coefficient. When the leading coefficient is \(1\), the polynomial is called a monic polynomial. The monomial \(a_nx^n\) is the leading term.

Lower degree polynomials have special names: a degree \(0\) polynomial \((a_0)\) is a non-zero constant, the degree 1 polynomial \((a_0 + a_1x)\) is called linear, a degree \(2\) polynomial is quadratic, and a degree \(3\) polynomial is called cubic.

Symbolic math in Julia

using CalculusWithJulia   # loads the `SymPy` package
a,b,c=symbols("a,b,c")
## (a, b, c)
x=symbols("x", real=true)
## x
@vars h t
## (h, t)
p=-16x^2 + 100
##           2
## 100 - 16*x
typeof(p)
## Sym
quad = a*x^2 + b*x + c
##    2          
## a*x  + b*x + c
sin(a*(x-b*pi)+c)
## sin(a*(-pi*b + x) + c)
quad + quad^2 - quad^3
##                                  3                   2
##    2             /   2          \    /   2          \ 
## a*x  + b*x + c - \a*x  + b*x + c/  + \a*x  + b*x + c/

Substitution: subs, replace

p=-16x^2+100
##           2
## 100 - 16*x
p(x => (x-1)^2)
##                 4
## 100 - 16*(x - 1)
y=p(x=>2)
## 36
typeof(y)
## Sym
p(4) # substitutes x=>4
## -156
@vars a b c E F
## (a, b, c, E, F)
p=a*x^2 + b*x + c
##    2          
## a*x  + b*x + c
p(x=>x - E) + F
##               2                 
## F + a*(-E + x)  + b*(-E + x) + c
expand(p(x=>x - E) + F)
##  2                            2          
## E *a - 2*E*a*x - E*b + F + a*x  + b*x + c

Conversion of symbolic numbers to Julia numbers

p=-16x^2 + 100
##           2
## 100 - 16*x
y=p(2)
## 36
N(y)
## 36
N(PI, 60)
## 3.141592653589793238462643383279502884197169399375105820974939

Graphical properties of polynomials

plot(x^5 - x + 1, -3/2, 3/2)

plot(x^0.0, -1.2, 1.2)

plot(x^1, -1.2, 1.2)

plot(x^2, -1.2, 1.2)

plot(x^3, -1.2, 1.2)

plot(x^4, -1.2, 1.2)

Factoring polynomials

expand((x-1)*(x-2)*(x-3))
##  3      2           
## x  - 6*x  + 11*x - 6
factor(x^3 -6x^2+11x-6)
## (x - 3)*(x - 2)*(x - 1)
factor(x^5 - 5x^4 + 8x^3 -8x^2 + 7x - 3)
##                2 / 2    \
## (x - 3)*(x - 1) *\x  + 1/

Polynomial functions and polynomials.

f(x)=-16x^2+100
## f (generic function with 1 method)
f(x)
##           2
## 100 - 16*x
p = -16*x^2 + 100
##           2
## 100 - 16*x
p(2)
## 36