3.22. Polynomials

3.22.1. Defining

3.22.1.1. Polynomial of degree three

Listing 516. Polynomial of degree three
Ax^3 + Bx^2 + Cx^1 + D = 0
1x^3 + 2x^2 + 3x^1 + 4 = 0
import numpy as np


np.poly1d([1, 2, 3, 4])
# poly1d([1, 2, 3, 4])
../../_images/polynomial-3deg.png

Figure 73. Polynomial of degree three Ax^3 + Bx^2 + Cx^1 + D = 0 [numpy5]

3.22.1.2. Polynomial of degree six

Listing 517. Polynomial of degree six
Ax^6 + Bx^5 + Cx^4 + Dx^3 + Ex^2 + Fx + G = 0
1x^6 + 2x^5 + 3x^4 + 4x^3 + 5x^2 + 6x + 7 = 0
import numpy as np


np.poly1d([1, 2, 3, 4, 5, 6, 7])
# poly1d([1, 2, 3, 4, 5, 6, 7])
../../_images/polynomial-6deg.png

Figure 74. Polynomial of degree six Ax^6 + Bx^5 + Cx^4 + Dx^3 + Ex^2 + Fx + G = 0 [numpy5]

3.22.2. Find coefficients

  • Find the coefficients of a polynomial with the given sequence of roots

  • Specifying the roots of a polynomial still leaves one degree of freedom, typically represented by an undetermined leading coefficient.

import numpy as np


np.poly([0, 0, 0])
# array([1., 0., 0., 0.])

np.poly([1, 2])
# array([ 1., -3.,  2.])

np.poly([1, 2, 3, 4, 5, 6, 7])
# array([ 1.0000e+00, -2.8000e+01,  3.2200e+02, -1.9600e+03,  6.7690e+03,
#        -1.3132e+04,  1.3068e+04, -5.0400e+03])

3.22.3. Roots

  • Return the roots of a polynomial

import numpy as np


np.roots([1, 2])
# array([-2.])

np.roots([0, 1, 3])
# array([-3.])

np.roots([1, 4, -2, 3])
# array([-4.57974010+0.j , 0.28987005+0.75566815j, 0.28987005-0.75566815j])

np.roots([ 1, -11, 9, 11, -10])
#array([10.+0.0000000e+00j, -1.+0.0000000e+00j,
#       1.+9.6357437e-09j, 1.-9.6357437e-09j])

3.22.4. Derivatives

3.22.4.1. Derivative of a polynomial

import numpy as np


np.polyder([1./4., 1./3., 1./2., 1., 0.])
# array([ 1., 1., 1., 1.])

3.22.4.2. Antiderivative (indefinite integral) of a polynomial

  • Return an antiderivative (indefinite integral) of a polynomial

import numpy as np


np.polyint([1, 1, 1, 1])
# array([ 0.25 , 0.33333333, 0.5 , 1. , 0. ])

3.22.5. Evaluation

3.22.5.1. Evaluate a polynomial at specific values

  • Compute polynomial values

import numpy as np


np.polyval([1, -2, 0, 2], 4)
# 34

3.22.5.2. Least squares polynomial fit

  • Least squares polynomial fit

import numpy as np


x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [0, 2, 1, 3, 7, 10, 11, 19]

np.polyfit(x, y, 2)
# array([ 0.375 , -0.88690476, 1.05357143])

3.22.6. Polynomial Arithmetic

  • np.polyadd()

  • np.polysub()

  • np.polymul()

  • np.polydiv()

3.22.6.1. Sum of two polynomials

import numpy as np


np.polyadd([1, 2], [9, 5, 4])
# array([9, 6, 6])

3.22.7. Assignments

3.22.7.1. Polyfit

  • Complexity level: easy

  • Lines of code to write: 4 lines

  • Estimated time of completion: 5 min

  • Filename: solution/numpy_polyfit.py

English
  1. For given points (see below)

  2. Separate first row (header) from data

  3. Calculate coefficients of best approximating polynomial of 3rd degree

Polish
  1. Dla danych punktów (patrz poniżej)

  2. Odseparuj pierwszy wiersz (nagłówek) do danych

  3. Oblicz współczynniki najlepiej dopasowanego wielomianu 3 stopnia

Input
INPUT = [
    ('x', 'y'),
    (-4.0, 0.0),
    (-3.0, 2.5),
    (-2.0, 2.0),
    (0.0, -2.0),
    (2.0, 0.0),
    (3.0, 7.0)
]