Polynomials

Accompanying notebook centered around learning Chapter 1 of the Programmer’s introduction to Mathematics.

A single variable polynomial with real coefficients is a function of the form \(f\) that takes a real number as input, produces a real number as output, and has the form:

\[f(x)=a_0+a_1x+a_2x^2+...+a_nx^n\]

where \(a_0,a_1,...,a_n\) are real numbers. The number \(a_i\) are called the coefficients of f. The degree of the polynomial is the integer \(n\).


source

Polynomial

 Polynomial (function_name:str='f', variable_name:str='x',
             degree:Union[int,str]='n', coefficient_name:str='a')

A class used to represent a general polynomial, which is a function that both takes and returns real numbers. Currently only supports single-variable polynomial functions. Polynomials require: - coefficients - degree

Type Default Details
function_name str f name of function
variable_name str x name of variable
degree typing.Union[int, str] n degree of polynomial
coefficient_name str a str for name of coefficients
Polynomial().equality()

\(\displaystyle f{\left(x \right)} = \sum_{i=0}^{n} x^{i} {a}_{i}\)

print(Polynomial())
f(x) = a[0] * x**0 + a[1] * x**1 + ... + a[n] * x**n
test_eq(Polynomial().right_hand_side(), Polynomial().equality().rhs)
x = variable('x')
n = variable('n')
i = variable('i')
a = sp.IndexedBase('a')

f_x = sp.Sum(a[i] * x**i, (i, 0, n))

f_x

test_eq(Polynomial().right_hand_side(), f_x)
f_x

\(\displaystyle \sum_{i=0}^{n} x^{i} {a}_{i}\)

# create g(t) = 2 + 0t + 4t^2 - t^3
g = Polynomial(variable_name='t', function_name='g', degree='n', coefficient_name='a')
g.set_degree(3)

\(\displaystyle g{\left(t \right)} = t^{3} {a}_{3} + t^{2} {a}_{2} + t {a}_{1} + {a}_{0}\)

g.set_coefficients([2, 0, 4, -1])

\(\displaystyle g{\left(t \right)} = - t^{3} + 4 t^{2} + 2\)

If you want to set a specific polynomial all at once, here’s a helper function for that:


source

polynomial

 polynomial (degree:int, coefficients:list)

Returns a polynomial function with the given degree and coefficients

Type Details
degree int degree of polynomial
coefficients list list of coefficients starting with the lowest indexed coefficient

Sometimes it might be better to directly generate a polynomial with the equation function.

For example, to create the above polynomial as a one liner, see below. It’s up to you to decide which is clearer.

equation('g', variable('t'), 2 + (0*variable('t')) + (4*variable('t')**2) + (-1*variable('t')**3))

\(\displaystyle g{\left(t \right)} = - t^{3} + 4 t^{2} + 2\)

Existence and Uniqueness for Polynomials

For any integer \(n\geq 0\), and any list of \(n+1\) points \((x_0,y_0),(x_1,y_1),...,(x_n,y_n)\) in R^2 there is a unique polynomial \(f\) of degree at most \(n\) such that \(f(x_i)=y_i\) for all \(i=0,1,...,n\).

polynomial_to_test = polynomial(3, [2, 0, 4, -1])
simple_polynomial = polynomial(1, [1, 1])

polynomial_to_test.polynomial

\(\displaystyle f{\left(x \right)} = - x^{3} + 4 x^{2} + 2\)

simple_polynomial.polynomial

\(\displaystyle f{\left(x \right)} = x + 1\)

sample_pairs = make_examples('real', 6, 5, positive_only=True, increasing_only=True)
for pair in sample_pairs:
    print(pair)
    print('bigger:', polynomial_to_test.get_output(pair[0]))
    print('simple:', simple_polynomial.get_output(pair[0]))
(97, 5)
bigger: Eq(f(97), -875035)
simple: Eq(f(97), 98)
(187, 5)
bigger: Eq(f(187), -6399325)
simple: Eq(f(187), 188)
(261, 5)
bigger: Eq(f(261), -17507095)
simple: Eq(f(261), 262)
(315, 5)
bigger: Eq(f(315), -30858973)
simple: Eq(f(315), 316)
(338, 5)
bigger: Eq(f(338), -38157494)
simple: Eq(f(338), 339)
(382, 5)
bigger: Eq(f(382), -55159270)
simple: Eq(f(382), 383)