import sys
import numpy as np
class ChebyshevPoly:
def __init__(self, order, mode=1):
self.order = order
self.length = order + 1
self.mode = mode
self.__gen_poly()
def recursion(self, t_next, t_curr, t_prev):
t_next[0] = -t_prev[0]
for k in range(1, self.length):
t_next[k] = 2 * t_curr[k-1] - t_prev[k]
def __gen_poly(self):
self.polys = np.zeros((self.length, self.length))
self.polys[0, 0] = 1.0
self.polys[1, 1] = 1.0 if self.mode == 1 else 2.0 if self.mode == 2 else 0.0
for k in range(1, self.length-1):
self.recursion(self.polys[k+1, :], self.polys[k, :], self.polys[k-1, :])
def get_poly(self, order=None):
order = self.order if order is None else order
return self.polys[order]
def print_polys(self):
print("order", end="")
for k in range(0, self.length):
space = " " if k < 10 else " "
print(f"{space}x_{k}", end="")
print("")
for m in range(0, self.length):
print(f"{m:5}:", end="")
for n in range(0, m+1):
print(f"{int(self.polys[m,n]):7}", end="")
if m == n:
print("")
else:
print(f" ", end="")
if __name__ == "__main__":
cbsv = ChebyshevPoly(int(sys.argv[1]), mode=int(sys.argv[2]))
cbsv.print_polys()