def calcular(self, n, norm=False, k_factor=1): # Atencion: Norm = calcular normalizada if n % 2 == 1: k = int((n - 1) / 2) arr = [] for i in range(k + 1): arr.append(get_a(i, n)) poly = legendre.leg2poly(legendre.legint(legendre.legmul(arr, arr))) else: k = int((n - 2) / 2) arr = [] for i in range(k + 1): arr.append(get_a(i, n)) leg_b = legendre.legmul(legendre.legmul(arr, arr), legendre.poly2leg([1, 1])) poly = legendre.leg2poly(legendre.legint(leg_b)) exp = 0 wn, sn, s, sa = sp.symbols("wn sn sa s") for i in range(len(poly)): exp += poly[i] * ((2 * (wn**2) - 1)**i) exp -= poly[i] * ((-1)**i) if n % 2 == 1: exp = exp * 1 / (2 * (k + 1)**2) else: exp = exp * 1 / ((k + 1) * (k + 2)) exp = 1 / (1 + self.getXi(0, n)**2 * exp) exp = exp.subs(wn, sn / 1j) roots = algebra.getRoots(exp, sn) roots[1] = algebra.filterRealNegativeRoots(roots[1]) poles = [] for i in roots[1]: poles.append({"value": i}) exp = algebra.armarPolinomino(poles, [], sn, 1) self.tf_normalized = algebra.conseguir_tf(exp, sn, poles) if not norm: exp = self.plantilla.denormalizarFrecuencias(exp, sa, sn) self.getGainPoints() factor = (self.k1 - self.k2) * norm / 100 + self.k2 exp = self.plantilla.denormalizarAmplitud(exp, s, sa, factor) self.tf = algebra.conseguir_tf(exp, s, []) return self.tf else: return self.tf_normalized
def test_integ(self): p = self.p2.integ() assert_almost_equal(p.coef, leg.legint([1, 2, 3], 1, 0, scl=.5)) p = self.p2.integ(lbnd=0) assert_almost_equal(p(0), 0) p = self.p2.integ(1, 1) assert_almost_equal(p.coef, leg.legint([1, 2, 3], 1, 1, scl=.5)) p = self.p2.integ(2, [1, 2]) assert_almost_equal(p.coef, leg.legint([1, 2, 3], 2, [1, 2], scl=.5))
def test_integ(self) : p = self.p2.integ() assert_almost_equal(p.coef, leg.legint([1,2,3], 1, 0, scl=.5)) p = self.p2.integ(lbnd=0) assert_almost_equal(p(0), 0) p = self.p2.integ(1, 1) assert_almost_equal(p.coef, leg.legint([1,2,3], 1, 1, scl=.5)) p = self.p2.integ(2, [1, 2]) assert_almost_equal(p.coef, leg.legint([1,2,3], 2, [1,2], scl=.5))
def Hamiltonian_Legendre_polynomial(c, potential, domain, N): #potential is a constant in this case x = np.linspace(-domain / 2, domain / 2, N) delta_x = domain / (N - 1) #here, the normalized legendre polynomical has been used # for the nth polynomials, normalization constant is sqrt(2/(2n + 1)) #kinetic term K = np.zeros((N, N)) for ii in range(N): legen_left = np.zeros(N) legen_left[ii] = mt.sqrt((2 * ii + 1) / 2) for jj in range(N): deriva_array = np.zeros(N + 2) deriva_array[jj] = mt.sqrt((2 * jj + 1) / 2) legen_right_deriva = legen.legder(deriva_array, 2) #multiply them legen_multiply = legen.legmul(legen_left, legen_right_deriva) #integral legen_integral = legen.legint(legen_multiply) #calculate the matrix elements K[ii][jj] = legen.legval(domain / 2, legen_integral) - \ legen.legval(-domain / 2, legen_integral) #the S matrix, inside the [-1, 1] domain, the legendre ploynomial can be treatedas basis and satisfying <xi|xj> = delta ij, thus S matrix is a identity matrix S = np.zeros((N, N)) for ii in range(N): legen_left_S = np.zeros(N) legen_left_S[ii] = mt.sqrt((2 * ii + 1) / 2) legen_multiply_S = legen.legmul(legen_left_S, legen_left_S) legen_integral_S = legen.legint(legen_multiply_S) S[ii][ii] = legen.legval(domain / 2, legen_integral_S) - \ legen.legval(-domain / 2, legen_integral_S) K = K * -1 * c #because the potential is just a constant here, we can calculate the V matrix simply by multiply the matrix S a constant potential value V = potential * S ##divide the obtained Hamiltonian by the S matrix H = K + V return H
def __init__(self, *args, **kwargs): PolynomialBasisFunctions.__init__(self, *args, **kwargs) # Standard linear basis functions if the polynomial order is 1 if self.polynomialOrder is 1: self._functions = [Legendre((0.5, -0.5)), Legendre((0.5, 0.5))] # Integrated Legendre polynomials (first two functions replaced by classic linear ones) elif self.polynomialOrder > 1: # Base self._functions = [ Legendre(coefficients) for coefficients in oneHotArray(self.polynomialOrder + 1) ] # Integrate base functions for index in range(self.polynomialOrder - 1, 0, -1): self._functions[index] = Legendre(legint( np.sqrt(float(index) - 0.5) * self._functions[index].coef, m=1, lbnd=-1.0), domain=self.domain) # Replace first and last functions self._functions[0] = Legendre((0.5, -0.5)) self._functions[-1] = Legendre((0.5, 0.5)) else: raise ValueError("Invalid polynomial order!")
def hamiltonian_matrix(BASIS_SIZE=BASIS_SIZE, BASIS_FUNC=BASIS_FUNC, DOMAIN=DOMAIN, C=C, V=V): '''Calculates the hamiltonian matrix <psi|H|psi>''' if BASIS_FUNC == 'legendre': hmat = np.zeros((BASIS_SIZE, BASIS_SIZE)) for i in range(BASIS_SIZE): for j in range(BASIS_SIZE): coefs_i = [0] * (i) + [1] coefs_j = [0] * (j) + [1] inside = L.legmul(coefs_i, list(hamiltonian(coefs_j))) integ = L.legint(inside) val = L.legval(DOMAIN[1], integ) - L.legval(DOMAIN[0], integ) hmat[i, j] = val if BASIS_FUNC == 'fourier': a = DOMAIN[ 1] #restrict the domain to be symmetric about origin for simplicity hmat = np.zeros((BASIS_SIZE, BASIS_SIZE)) for i in range(BASIS_SIZE): for j in range(BASIS_SIZE): p1 = 2 * V * np.sin(a * j) / j #cos V0 term p2 = 0 #cos cos term cancels due to parity if i != j if i == j: p2 = C * (j**2) * (a + np.sin(2 * a * j) / (2 * j)) hmat[i, j] = p1 + p2 return hmat
def inner_product(): #do I even need this? if BASIS_FUNC == 'legendre': pmat = np.zeros((BASIS_SIZE, BASIS_SIZE)) for i in range(BASIS_SIZE): for j in range(BASIS_SIZE): coefs_i = [0] * (i) + [1] coefs_j = [0] * (j) + [1] inside = L.legmul(coefs_i, coefs_j) integ = L.legint(inside) val = L.legval(DOMAIN[1], integ) - L.legval(DOMAIN[0], integ) pmat[i, j] = val
def test_legder(self) : # check exceptions assert_raises(ValueError, leg.legder, [0], .5) assert_raises(ValueError, leg.legder, [0], -1) # check that zeroth deriviative does nothing for i in range(5) : tgt = [0]*i + [1] res = leg.legder(tgt, m=0) assert_equal(trim(res), trim(tgt)) # check that derivation is the inverse of integration for i in range(5) : for j in range(2, 5) : tgt = [0]*i + [1] res = leg.legder(leg.legint(tgt, m=j), m=j) assert_almost_equal(trim(res), trim(tgt)) # check derivation with scaling for i in range(5) : for j in range(2, 5) : tgt = [0]*i + [1] res = leg.legder(leg.legint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt))
def _int_mat_legendre(n_coeff: int, int_order: int) -> np.ndarray: """Legendre polynomial integration matrix Parameters ---------- n_coeff Number of Legendre coefficients int_order Order of integration Returns ------- Legendre integral matrix """ return legint(np.eye(n_coeff), int_order)
def test_legder(self) : # check exceptions assert_raises(ValueError, leg.legder, [0], .5) assert_raises(ValueError, leg.legder, [0], -1) # check that zeroth deriviative does nothing for i in range(5) : tgt = [0]*i + [1] res = leg.legder(tgt, m=0) assert_equal(trim(res), trim(tgt)) # check that derivation is the inverse of integration for i in range(5) : for j in range(2,5) : tgt = [0]*i + [1] res = leg.legder(leg.legint(tgt, m=j), m=j) assert_almost_equal(trim(res), trim(tgt)) # check derivation with scaling for i in range(5) : for j in range(2,5) : tgt = [0]*i + [1] res = leg.legder(leg.legint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt))
def test_legint_axis(self): # check that axis keyword works c2d = np.random.random((3, 4)) tgt = np.vstack([leg.legint(c) for c in c2d.T]).T res = leg.legint(c2d, axis=0) assert_almost_equal(res, tgt) tgt = np.vstack([leg.legint(c) for c in c2d]) res = leg.legint(c2d, axis=1) assert_almost_equal(res, tgt) tgt = np.vstack([leg.legint(c, k=3) for c in c2d]) res = leg.legint(c2d, k=3, axis=1) assert_almost_equal(res, tgt)
def test_legint(self) : # check exceptions assert_raises(ValueError, leg.legint, [0], .5) assert_raises(ValueError, leg.legint, [0], -1) assert_raises(ValueError, leg.legint, [0], 1, [0,0]) # test integration of zero polynomial for i in range(2, 5): k = [0]*(i - 2) + [1] res = leg.legint([0], m=i, k=k) assert_almost_equal(res, [0, 1]) # check single integration with integration constant for i in range(5) : scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] legpol = leg.poly2leg(pol) legint = leg.legint(legpol, m=1, k=[i]) res = leg.leg2poly(legint) assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd for i in range(5) : scl = i + 1 pol = [0]*i + [1] legpol = leg.poly2leg(pol) legint = leg.legint(legpol, m=1, k=[i], lbnd=-1) assert_almost_equal(leg.legval(-1, legint), i) # check single integration with integration constant and scaling for i in range(5) : scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] legpol = leg.poly2leg(pol) legint = leg.legint(legpol, m=1, k=[i], scl=2) res = leg.leg2poly(legint) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k for i in range(5) : for j in range(2,5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1) res = leg.legint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k for i in range(5) : for j in range(2,5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1, k=[k]) res = leg.legint(pol, m=j, k=range(j)) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd for i in range(5) : for j in range(2,5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1, k=[k], lbnd=-1) res = leg.legint(pol, m=j, k=range(j), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling for i in range(5) : for j in range(2,5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1, k=[k], scl=2) res = leg.legint(pol, m=j, k=range(j), scl=2) assert_almost_equal(trim(res), trim(tgt))
def test_legint(self) : # check exceptions assert_raises(ValueError, leg.legint, [0], .5) assert_raises(ValueError, leg.legint, [0], -1) assert_raises(ValueError, leg.legint, [0], 1, [0, 0]) # test integration of zero polynomial for i in range(2, 5): k = [0]*(i - 2) + [1] res = leg.legint([0], m=i, k=k) assert_almost_equal(res, [0, 1]) # check single integration with integration constant for i in range(5) : scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] legpol = leg.poly2leg(pol) legint = leg.legint(legpol, m=1, k=[i]) res = leg.leg2poly(legint) assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd for i in range(5) : scl = i + 1 pol = [0]*i + [1] legpol = leg.poly2leg(pol) legint = leg.legint(legpol, m=1, k=[i], lbnd=-1) assert_almost_equal(leg.legval(-1, legint), i) # check single integration with integration constant and scaling for i in range(5) : scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] legpol = leg.poly2leg(pol) legint = leg.legint(legpol, m=1, k=[i], scl=2) res = leg.leg2poly(legint) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k for i in range(5) : for j in range(2, 5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1) res = leg.legint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k for i in range(5) : for j in range(2, 5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1, k=[k]) res = leg.legint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd for i in range(5) : for j in range(2, 5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1, k=[k], lbnd=-1) res = leg.legint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling for i in range(5) : for j in range(2, 5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1, k=[k], scl=2) res = leg.legint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt))
def legint(cs, m=1, k=[], lbnd=0, scl=1): from numpy.polynomial.legendre import legint return legint(cs, m, k, lbnd, scl)
def antiderivative(self, fnvals: np.ndarray) -> np.ndarray: """Return the antiderivative.""" coeffs = self.tocoeffs(fnvals) coeffs_int = legint(coeffs, axis=-1) return self.tofnvals(coeffs_int[..., :-1])
def test_legint_zerointord(self): assert_equal(leg.legint((1, 2, 3), 0), (1, 2, 3))