def findRootParameters(self, coordinates): left = self.left right = self.right p0 = left.location - coordinates p1 = left.rightHandle - coordinates p2 = right.leftHandle - coordinates p3 = right.location - coordinates a = p3 - 3 * p2 + 3 * p1 - p0 b = 3 * p2 - 6 * p1 + 3 * p0 c = 3 * (p1 - p0) coeffs = [0] * 6 coeffs[0] = c.dot(p0) coeffs[1] = c.dot(c) + b.dot(p0) * 2.0 coeffs[2] = b.dot(c) * 3.0 + a.dot(p0) * 3.0 coeffs[3] = a.dot(c) * 4.0 + b.dot(b) * 2.0 coeffs[4] = a.dot(b) * 5.0 coeffs[5] = a.dot(a) * 3.0 poly = Polynomial(coeffs, [0.0, 1.0], [0.0, 1.0]) roots = poly.roots() realRoots = [float(min(max(root.real, 0), 1)) for root in roots] return realRoots
def propagate2spherical_old(initial_y, initial_angle, distance_to_interface, component_diameter, interface_radius): poly_coefficients = Polynomial([ initial_y**2 + 2 * distance_to_interface * interface_radius + distance_to_interface**2, 2 * initial_y * tan(initial_angle) - 2 * interface_radius - 2 * distance_to_interface, 1 + tan(initial_angle)**2 ]) roots = poly_coefficients.roots() if interface_radius > 0: end_x = min(roots) else: end_x = max(roots) end_y = initial_y + end_x * tan(initial_angle) normal_angle = -asin(end_y / interface_radius) ray_angle = -normal_angle + initial_angle if abs(2 * end_y) > component_diameter: warnings.warn("The ray is out of bounds for your optical component.") return { "x": end_x, "y": end_y, "ray_angle": ray_angle, "normal_angle": normal_angle }
def kelly(moms): coefs = [] for i in range(0, len(moms)): coefs.append((-1)**(i) * moms[i]) p = Polynomial(coefs) sol = p.roots() out = min(filter(isreal, sol)) return out.real
def find_roots(coefficients): p = Polynomial(coefficients) print "\nRoots of U(x) with E = %d are:" % fabs(coefficients[0]) for i, r in enumerate(p.roots()): if r.imag != 0.0: sign = "-" if r.imag < 0 else "+" print "x%d: %.1f %s %.1fi" % (i+1, r.real, sign, fabs(r.imag)) else: print "x%d: %.1f" % (i + 1, r.real)
def RaizP(vector): # max=int(input("Ingrese el grado del polinomio: ")) # B=arraysito.array('d',(0 for i in range(0,max+1))) # i=0 # for n in B: # B[i]=float(input("ingrese el coeficiente de X%s: " %(i))) # i+=1 cafe = Pol(vector) print(cafe) return cafe.roots()
def newton(width: int, height: int, *, p: Polynomial, a: complex, xr: Range = (-2.5, 1), yr: Range = (-1, 1), max_iterations: int = 100) -> (np.array, np.array): """ """ # To make navigation easier we calculate these values x_from, x_to = xr y_from, y_to = yr # Here the actual algorithm starts x = np.linspace(x_from, x_to, width).reshape((1, width)) y = np.linspace(y_from, y_to, height).reshape((height, 1)) z = x + 1j * y # Compute the derivative dp = p.deriv() # Compute roots roots = p.roots() epsilon = 1e-5 # Set the initial conditions a = np.full(z.shape, a) # To keep track in which iteration the point diverged div_time = np.zeros(z.shape, dtype=int) # To keep track on which points did not converge so far m = np.full(a.shape, True, dtype=bool) # To keep track which root each point converged to r = np.full(a.shape, 0, dtype=int) for i in range(max_iterations): z[m] = z[m] - a[m] * p(z[m]) / dp(z[m]) for j, root in enumerate(roots): converged = (np.abs(z.real - root.real) < epsilon) & (np.abs(z.imag - root.imag) < epsilon) m[converged] = False r[converged] = j + 1 div_time[m] = i return div_time, r
def update_coeff(self, a1, a2): '''update the AR(2) coefficients choice and the roots of the AR polynomial''' # Update the title ax_coeff.set_title('Choose AR(2) coefficients: ' '$(a_1, a_2)$=(%.2f, %.2f) \n' 'process $X_k = a_1 X_{k-1} + a_2 X_{k-2} + Z_k$'\ % (a1, a2)) # Move the circle circ.center = a1, a2 # Move the guiding lines coeff_line.set_data([a1, a1, 0], [0, a2, a2]) # Move the roots: poly_ar2 = Polynomial([-a2, -a1, 1]) root1, root2 = poly_ar2.roots().astype(complex) roots_line.set_data([root1.real, root2.real], [root1.imag, root2.imag])
from numpy.polynomial import Polynomial p = Polynomial([1.25, -3.875, 2.125, 2.75, -3.5, 1.0]) print(p.roots())
### Roots of z^2 - a1 z - a2 (related to stability of the filter) ax_roots = fig_coeff.add_subplot(212, title='Roots of $P(z)=z^2 - a_1 z - a_2$', xlabel='Re(z)', ylabel='Im(z)') ax_roots.patch.set_facecolor('#CCCCDD') # Light blue background circ_roots = patches.Circle((0, 0), 1, fc='white', lw=0) ax_roots.add_patch(circ_roots) ax_roots.set_xlim(-1.5, 1.5) ax_roots.set_ylim(-1.5, 1.5) ax_roots.set_aspect('equal') # Compute the roots numerically (the lazy way) poly_ar2 = Polynomial([-a2, -a1, 1]) root1, root2 = poly_ar2.roots().astype(complex) roots_line, = ax_roots.plot([root1.real, root2.real], [root1.imag, root2.imag], '*', color='red', ms=10) fig_coeff.tight_layout() ### Response plots ############################################################# fig_res = plt.figure('AR(2) - responses') fig_res.clear() # 1) Impulse response plot ax_ir = fig_res.add_subplot(211, title='Impulse Response $h(k)$', xlabel='instant k')
S21, S11, filterOrder, w1, w2, fc=fc, method=extractMethod, startFreq=captureStartFreq, stopFreq=captureStopFreq, isSymmetric=isSymmetric) polyF = Polynomial(coefF) polyP = Polynomial(coefP) polyE = Polynomial.fromroots(rootE) rootF = polyF.roots() rootP = polyP.roots() matrixMethod = 5 transversalMatrix = CP.FPE2M(epsilon, epsilonE, coefF, coefP, rootE, method=matrixMethod) # print(np.round(transversalMatrix, 3)) # extractedMatrix, msg = CP.FPE2MComprehensive(epsilon, epsilonE, rootF, rootP, rootE, topology, method = matrixMethod) # arrowM = CP.RotateM2Arrow(transversalMatrix, isComplex = True) # ctcqM, ctcqPoint = CP.RotateArrow2CTCQ(arrowM, topology, rootP) # print(np.round(np.real(transversalMatrix), 3))
f_prime = f.deriv(1) # Draw the X-axis plt.axhline(y=0, color='k') # Generate 100 values of x. x = np.linspace(-2.5, 1.6, 100) # Calculate y-values of corresponding x-values of f(x). y = f(x) # Plot the graph of f(x) using x-values and y-values plt.plot(x, y) # Plot the roots of the graph plt.plot(f.roots(), f(f.roots()), 'ro') # Print the roots of the function f(x) print(f.roots()) # Calcuate y-values for corresponding x-values of f'(x). y = f_prime(x) # Plot the graph of f'(x) plt.plot(x, y) # Plot the roots of f'(x). Notice that, where f'(x) is zero the slop of f(x) is zero. plt.plot(f_prime.roots(), f_prime(f_prime.roots()), 'bo') # Print the roots of f'(x). print(f_prime.roots())
alpha2 = sqrt(alpha22) alpha3 = -x0dot * y0 + y0dot * x0 mprint('alpha1') mprint('alpha2', 'alpha22') mprint('alpha3') # xi2, xi3, eta1, eta2 # wielomian Phi(xi) cp4 = 2 * alpha1 cp3 = 2 * GM cp2 = 2 * alpha1 * c2 - alpha22 cp1 = 2 * GM * c2 cp0 = c2 * (alpha3**2 - alpha22) poly = Polynomial([cp0, cp1, cp2, cp3, cp4]) proots = poly.roots() mprint('proots') xiroots = real(proots) mprint('xiroots') def Phi(xi): return cp4 * xi**4 + cp3 * xi**3 + cp2 * xi**2 + cp1 * xi + cp0 def dPhi(xi): return 4 * cp4 * xi**3 + 3 * cp3 * xi**2 + 2 * cp2 * xi + cp1 xiroot1 = float(xiroots[2]) mprint('xiroot1') xiroot2 = float(xiroots[3]) mprint('xiroot2')
# polyEResult = polyE1(aaa) # r = polyEResult * polyEResult.conj() - polyF(aaa) * polyF(aaa).conj() - polyP(aaa) * polyP(aaa).conj() / (epsilon * epsilon.conj()) # r = np.real(r) # for m in np.arange(len(aaa)): # for n in np.arange(len(rootE1)): # tempJ[m, n] = -polyE1(aaa[m]) * polyE1(aaa[m]).conj() / (aaa[m] - rootE1[n]) # J = 2 * np.hstack((np.real(tempJ), -np.imag(tempJ))) # delta = -0.8 * np.linalg.solve(J.T.dot(J), J.T.dot(r)) # rootE1 += delta[:N] + 1j * delta[N:] # rootE1 = -np.abs(np.real(rootE1)) + 1j * np.imag(rootE1) # cost[i] = r.dot(r) #rootE = rootE1.copy() polyF = Polynomial(coefF) rootF = polyF.roots() normalizedFreq = np.arange(-5.5, 5.5, 0.01) #polyF = Polynomial(coefF) #polyE = Polynomial(coefE) #rootF = polyF.roots() #rootE = polyE.roots() epsilonE = epsilon S11_old, S21_old = CP.FPE2S(epsilon, epsilonE, rootF, rootP, rootE, normalizedFreq) #print(rootF) #print(rootP) #print(rootE) topology = np.eye(N + 2, dtype=int) topology[0, 0] = 0