def test_basis_func(self): p = poly.Chebyshev([1, 2, 3]) assert_equal(self.as_latex(p), r'$x \mapsto 1.0\,{T}_{0}(x) + 2.0\,{T}_{1}(x) + 3.0\,{T}_{2}(x)$') # affine input - check no surplus parens are added p = poly.Chebyshev([1, 2, 3], domain=[-1, 0]) assert_equal(self.as_latex(p), r'$x \mapsto 1.0\,{T}_{0}(1.0 + 2.0x) + 2.0\,{T}_{1}(1.0 + 2.0x) + 3.0\,{T}_{2}(1.0 + 2.0x)$')
def __get_collocation_nodes(self, m, kind): """Computes the collocation nodes.""" # basis coefs coefs = np.zeros(m + 1) coefs[-1] = 1 if kind == 'polynomial': nodes = polynomial.Polynomial(coefs).roots() elif kind == 'chebyshev': domain = [self.a, self.b] nodes = polynomial.Chebyshev(coefs, domain).roots() elif kind == 'legendre': domain = [self.a, self.b] nodes = polynomial.Legendre(coefs, domain).roots() elif kind == 'laguerre': domain = [self.a, self.b] nodes = polynomial.Laguerre(coefs, domain).roots() elif kind == 'hermite': domain = [self.a, self.b] nodes = polynomial.Hermite(coefs, domain).roots() else: raise ValueError return nodes
def test_set_default_printoptions(): p = poly.Polynomial([1, 2, 3]) c = poly.Chebyshev([1, 2, 3]) poly.set_default_printstyle("ascii") assert_equal(str(p), "1.0 + 2.0 x**1 + 3.0 x**2") assert_equal(str(c), "1.0 + 2.0 T_1(x) + 3.0 T_2(x)") poly.set_default_printstyle("unicode") assert_equal(str(p), "1.0 + 2.0·x¹ + 3.0·x²") assert_equal(str(c), "1.0 + 2.0·T₁(x) + 3.0·T₂(x)") with pytest.raises(ValueError): poly.set_default_printstyle("invalid_input")
def __get_cheb_yhat(self, coefs): """Constructs a Chebyshev polynomial approximation to the solution.""" # the solution y is in R^n n = coefs.shape[0] # create the approximation yhat domain = [self.a, self.b] yhat = [polynomial.Chebyshev(coefs[i], domain) for i in range(n)] # create the approximation of yhat_prime yhat_prime = [yhat[i].deriv() for i in range(n)] return [yhat, yhat_prime]
def __init__(self, rect, n=11): super().__init__() self.image = pygame.Surface(rect.size, depth=32) self.rect = self.image.get_rect(topleft=rect.topleft) self.image.set_colorkey(self.clear) p = P.Chebyshev(tycho).linspace(n) # scale x terms to rect width w2 = self.rect.w / 2 X = (p[0] * w2) + w2 # scale y terms to rect height Y = abs(p[1] - p[1][0]) Y *= (self.rect.h - 10) / Y.max() # self.points = [self.rect.rect.topleft pts = [(int(x), int(y)) for x, y in zip(X, Y)] self.pads = [LandingPad(p, 30) for p in pts[1:-1]] self.points = [pts[0]] for p in self.pads: self.points.append(p.rect.bottomleft) self.points.append(p.rect.bottomright) self.points.extend( [pts[-1], self.rect.bottomright, self.rect.bottomleft]) # relocate the pad to world coordinates for pad in self.pads: x, y = pad.rect.center a, b = self.rect.topleft pad.rect.center = a + x, b + y self.render()
def test_chebyshev_str(self): res = repr(poly.Chebyshev([0, 1])) tgt = 'Chebyshev([0., 1.], domain=[-1, 1], window=[-1, 1])' assert_equal(res, tgt)
def test_chebyshev_str(self): res = str(poly.Chebyshev([0, 1])) tgt = 'cheb([0. 1.])' assert_equal(res, tgt)
def test_chebyshev_str(self): res = repr(poly.Chebyshev([0,1])) tgt = 'Chebyshev([0., 1.], [-1., 1.], [-1., 1.])' assert_(res, tgt)
def test_chebyshev_str(self): res = str(poly.Chebyshev([0,1])) tgt = 'leg([0., 1.])' assert_(res, tgt)
def test_chebyshev_str(self, inp, tgt): res = str(poly.Chebyshev(inp)) assert_equal(res, tgt)
# In[24]: p3 # In[25]: p3.roots() # In[26]: p2 # In[27]: c1 = P.Chebyshev([1, 2, 3]) # In[28]: c1 # In[29]: c1.roots() # In[30]: c = P.Chebyshev.fromroots([-1, 1]) # In[31]:
def test_chebyshev_str(self): res = repr(poly.Chebyshev([0, 1])) tgt = ("Chebyshev([0., 1.], domain=[-1, 1], window=[-1, 1], " "symbol='x')") assert_equal(res, tgt)
# Programa de Franco Benassi # Proyecto #3 Interfaces Graficas 2020 # Ejercicio 6 import numpy as np from numpy.linalg import linalg import numpy.polynomial as P import matplotlib.pyplot as pt data = np.load("/home/franco-os/Documentos/Informática/Interfaces de Grafica de Usuario/Proyecto 3/datas/cheby.npy") x = data[0] y = data[1] deg = len(x)-1 A = P.chebyshev.chebvander(x,deg) c = linalg.solve(A,y) Resutl = P.Chebyshev(c) xx = np.linspace(x.min(),x.max(),100) pt.plot(xx,Resutl(xx),'r',label="Interpolación Chebychev") pt.scatter(x,y,label="Puntos de datos") pt.ylabel("F(x)") pt.xlabel("T(s)") pt.legend() pt.show()