This package calculates a few orthogonal polynomials such as Jacobi polynomials $P_n^{\alpha,\beta}(x)$, Chebyshev polynomials of the first and second kinds, $T_n(x)$ and $U_n(x)$ and Legendre Polynomials $P_n(x) = P_n^{0,0}(x)$.
The polynomials can be calculated pointwise or a Poly
structure can be obtained, which is basically made up of the polynomial coefficients.
The respective derivatives can also be calculated.
In [2]:
using Jacobi
To calculate the Jacobi polynomial $P_5^{0.2, 0.1}(0.3)$:
In [4]:
x = 0.3
a = 0.2
b = 0.1
m = 5
jacobi(x, m, a, b)
Out[4]:
The derivative $\left.\frac{dP_5^{0.2, 0.1}(x)}{dx}\right|_{x=0.3}$:
In [5]:
djacobi(x, m, a, b)
Out[5]:
The Legendre, Chebyshev (first and second kind) are calculated as follows:
In [6]:
y1 = legendre(x, m) # Legendre polynomial of degree m
y2 = chebyshev(x, m) # Chebyshev polynomial of first kind of degree m
y3 = chebyshev2(x, m) # Chebyshev polynomial of second kind of degree m
[y1, y2, y3]
Out[6]:
Finally, the respective derivatives:
In [7]:
y1 = dlegendre(x, m) # Derivative of Legendre polynomial of degree m
y2 = dchebyshev(x, m) # Derivative of Chebyshev polynomial of first kind of degree m
y3 = dchebyshev2(x, m) # Derivative ofChebyshev polynomial of second kind of degree m
[y1, y2, y3]
Out[7]:
Sometimes it is useful to calculate the coefficients of the polynomials. The functions with prefix poly_
return a Poly object available in the package Polynomials.
The naming follows the pointwise calculation of polynomials already described above.
In [8]:
using Polynomials
T5 = poly_chebyshev(5, Int, :x)
Out[8]:
In [10]:
T8 = poly_chebyshev(8, Float64, :y)
In [12]:
T4 = poly_chebyshev(8)
Out[12]:
In [13]:
dT4dx = poly_dchebyshev(4)
Out[13]:
In [14]:
U5 = poly_chebyshev2(5)
Out[14]:
In [15]:
dU5dx = poly_dchebyshev2(5)
Out[15]:
In [16]:
P6 = poly_legendre(6) # Legendre Polynomial
Out[16]:
In [17]:
dP6 = polyder(P6)# Derivative using Polynomials Package
dp6b = poly_dlegendre(6) # Syntatic sugar calling the above line.
Out[17]:
In [18]:
err = dP6 - dp6b # Both procedures above should be the same!
Out[18]:
In [19]:
P7ab = poly_jacobi(7, a, b) # Jacobi polynomials
Out[19]:
In [20]:
dP7abdx = poly_djacobi(7, a, b)
Out[20]:
In [21]:
using PyPlot # Seeing is believing ...
In [22]:
x = linspace(-1.0, 1.0, 401);
In [24]:
y1 = chebyshev(x, 8);
plot(x, y1);
There is the mutating function:
In [27]:
y2 = zeros(x);
chebyshev!(x, 5, y2)
plot(x, y2)
Out[27]:
The same thing is implemented for Jacobi and Legendre polynomials and their derivative:
In [30]:
y3 = jacobi(x, 4, -0.5, 0.5)
y4 = djacobi(x, 4, -0.5, 0.5)
plot(x, y3, "b-", x, y4, "r--")
Out[30]:
In [39]:
x = linspace(-1, 1, 401);
y = chebyshev(x, 6);
z = chebyshev_zeros(6)
plot(x, y)
axhline(y=0.0, color="black")
[axvline(x=zz, color="red") for zz in z];
Again there are mutating versions of this function:
In [42]:
z = zeros(5)
jacobi_zeros!(5, a, b, z)
z
Out[42]:
In [43]:
jacobi(float32(0.5), 8, 0.5, 1.0)
Out[43]:
In [44]:
chebyshev(23//47, 7)
Out[44]:
In [45]:
djacobi(BigFloat(0.6), 10, 0.5, -0.5)
Out[45]:
In [46]:
jacobi_zeros(7, 1, -1, BigFloat)
Out[46]:
In [ ]: