def in_chebyshev_basis(self): res = poly.Polynomial({poly.ChebyshevVector({}): 1}) for v, p in self: c = poly2cheb([0] * p + [1]) basis = poly.ChebyshevVector.construct_basis([v], p) res *= poly.Polynomial(dict(zip(basis, c))) return res
def in_monomial_basis(self): res = poly.Polynomial({poly.MonomialVector({}): 1}) for v, p in self: c = cheb2poly([0] * p + [1]) basis = poly.MonomialVector.construct_basis([v], p) res *= poly.Polynomial(dict(zip(basis, c))) return res
def make_polynomial(cls, other): if isinstance(other, Number): return poly.Polynomial({cls({}): other}) elif isinstance(other, poly.Variable): return poly.Polynomial({cls({other: 1}): 1}) else: raise TypeError( f'cannot make a polynomial out of a {type(other).__name__}.')
def __mul__(self, cheb): self._verify_multiplicand(cheb) variables = set(self.variables() + cheb.variables()) prod_powers = ((self[v] + cheb[v], abs(self[v] - cheb[v])) for v in variables) coef = .5**len(variables) multiplication = poly.Polynomial({}) for powers in product(*prod_powers): cheb = ChebyshevVector(dict(zip(variables, powers))) multiplication += poly.Polynomial({cheb: coef}) return multiplication
def derivative(self, variable): power = self[variable] if power == 0: monomial = MonomialVector({}) else: monomial = deepcopy(self) monomial[variable] -= 1 return poly.Polynomial({monomial: power})
def substitute(self, evaluation_dict): coefficient = prod([ self._call_univariate(self[var], val) for var, val in evaluation_dict.items() ]) vector = type(self)( {v: p for v, p in self if not v in evaluation_dict}) return poly.Polynomial({vector: coefficient})
def integral(self, variable): power = self[variable] integral = poly.Polynomial({}) cheb = deepcopy(self) cheb[variable] = power + 1 integral[cheb] = .5 / (1 + power) cheb = deepcopy(self) cheb[variable] = abs(power - 1) integral[cheb] += .25 if power == 1 else .5 / (1 - power) return integral
def derivative(self, variable): ''' Uses the formula for the derivative in terms of the polynomials of the second kind, then express the polynomial of the second kind as a sum of polynomials of the first. ''' power = self[variable] derivative = poly.Polynomial({}) for q in range(power): if power % 2 ^ q % 2: cheb = deepcopy(self) cheb[variable] = q derivative[cheb] = power if q == 0 else power * 2 return derivative
def integral(self, variable): monomial = deepcopy(self) monomial[variable] += 1 return poly.Polynomial({monomial: 1 / (self[variable] + 1)})
def __mul__(self, monomial): self._verify_multiplicand(monomial) variables = set(self.variables() + monomial.variables()) monomial = MonomialVector({v: self[v] + monomial[v] for v in variables}) return poly.Polynomial({monomial: 1})