def normalise_polynomial(polynomial: Poly) -> Poly: """ Normalises polynomial by dividing it by its leading coefficient. :param polynomial: polynomial to normalise :return: normalised polynomial :rtype: Poly """ return polynomial / polynomial.LC()
def check_convergence(numer, denom, n): """ Returns (h, g, p) where -- h is: > 0 for convergence of rate 1/factorial(n)**h < 0 for divergence of rate factorial(n)**(-h) = 0 for geometric or polynomial convergence or divergence -- abs(g) is: > 1 for geometric convergence of rate 1/h**n < 1 for geometric divergence of rate h**n = 1 for polynomial convergence or divergence (g < 0 indicates an alternating series) -- p is: > 1 for polynomial convergence of rate 1/n**h <= 1 for polynomial divergence of rate n**(-h) """ from sympy import Poly npol = Poly(numer, n) dpol = Poly(denom, n) p = npol.degree() q = dpol.degree() rate = q - p if rate: return rate, None, None constant = dpol.LC() / npol.LC() if abs(constant) != 1: return rate, constant, None if npol.degree() == dpol.degree() == 0: return rate, constant, 0 pc = npol.all_coeffs()[1] qc = dpol.all_coeffs()[1] return rate, constant, (qc - pc) / dpol.LC()
def lower_degree_to(polynomial: Poly, max_polynomial_degree: int) -> Poly: """ Lowers the degree of the polynomial by using Chebyshev polynomials. :param polynomial: polynomial to lower the degree of :param max_polynomial_degree: maximum polynomial degree to lower to :return: polynomial with the degree less than or equal to `max_polynomial_degree` :rtype: Poly """ while polynomial.degree() > max_polynomial_degree: normalised_chebyshev_polynomial = get_normalised_nth_chebyshev_polynomial(polynomial.degree()) polynomial -= normalised_chebyshev_polynomial * polynomial.LC() return polynomial
from sympy import symbols, Poly import numpy as np a, b, T, s, w, x, z, I = symbols('a b T s w x z I') bilinear_transform = {s: 2 / T * (z - 1) / (z + 1)} # Continuous time transfer function from I(s) to w(s) of the following plant # model: # dw/dt = a*w + b*I G_s = b / (s - a) G_z_num, G_z_den = G_s.subs(bilinear_transform).as_numer_denom() G_z_den = Poly(G_z_den, z) G_z_num = Poly(G_z_num / G_z_den.LC(), z) # divide by leading coefficient of den G_z_den = G_z_den.monic() # make denominator monic assert (G_z_den.coeffs()[0] == 1) kp = Poly(G_z_num.coeffs()[0], z) G_z_N_p = G_z_num - kp * G_z_den print(kp) print(G_z_N_p) print(G_z_den) from sympy import symbols, Poly import numpy as np a, b, T, s, w, x, z, I = symbols('a b T s w x z I')
q1 = cdiv(prod, triplets) r1 = prod - q1 * triplets print "Number of triplets in prod: ", q1 q2 = cdiv(r1, scalars) r2 = r1 - q2 * scalars print "Number of scalars in prod:", q2 return r2 prod = red(prod) #prod2 = red(prod2) #print "Remainder: ", r2 #%% terms = collections.defaultdict(list) for exp in prod.args: poly = Poly(exp) terms[poly.LC()].append(exp) for key in sorted(terms.keys())[:2]: print "Monomials with coefficient {0}:".format(key) for poly in terms[key]: is_triplet = False for trip in triplets.args: if len((poly / trip).free_symbols) == 0: is_triplet = True print("{0} {1}".format("t" if is_triplet else " ", poly)) print "" #pprint(prod.args(), order = 'grlex')