def d2psi(x, c): """Second derivative of psi""" from numpy.polynomial.hermite import hermval, hermder yp = hermval(x, hermder(hermder(c))) * np.exp(-x**2 / 2) yp += -x * hermval(x, hermder(c)) * np.exp(-x**2 / 2) yp += -psi(x, c) - x * dpsi(x, c) return yp
def test_hermder_axis(self): # get_inds that axis keyword works c2d = np.random.random((3, 4)) tgt = np.vstack([herm.hermder(c) for c in c2d.T]).T res = herm.hermder(c2d, axis=0) assert_almost_equal(res, tgt) tgt = np.vstack([herm.hermder(c) for c in c2d]) res = herm.hermder(c2d, axis=1) assert_almost_equal(res, tgt)
def test_hermder_axis(self): # check that axis keyword works c2d = np.random.random((3, 4)) tgt = np.vstack([herm.hermder(c) for c in c2d.T]).T res = herm.hermder(c2d, axis=0) assert_almost_equal(res, tgt) tgt = np.vstack([herm.hermder(c) for c in c2d]) res = herm.hermder(c2d, axis=1) assert_almost_equal(res, tgt)
def evaluate_basis_derivative_all(self, x=None, k=0): if x is None: x = self.mesh(False, False) V = self.vandermonde(x) M = V.shape[1] X = x[:, np.newaxis] if k == 1: D = np.zeros((M, M)) D[:-1, :] = hermite.hermder(np.eye(M), 1) W = np.dot(V, D) W -= V*X V = W*np.exp(-X**2/2) V *= self.factor(np.arange(M))[np.newaxis, :] elif k == 2: W = (X**2 - 1 - 2*np.arange(M)[np.newaxis, :])*V V = W*self.factor(np.arange(M))[np.newaxis, :]*np.exp(-X**2/2) #D = np.zeros((M, M)) #D[:-1, :] = hermite.hermder(np.eye(M), 1) #W = np.dot(V, D) #W = -2*X*W #D[-2:] = 0 #D[:-2, :] = hermite.hermder(np.eye(M), 2) #W += np.dot(V, D) #W += (X**2-1)*V #W *= np.exp(-X**2/2)*self.factor(np.arange(M))[np.newaxis, :] #V[:] = W elif k == 0: V *= np.exp(-X**2/2)*self.factor(np.arange(M))[np.newaxis, :] else: raise NotImplementedError return V
def test_hermder(self): # get_inds exceptions assert_raises(TypeError, herm.hermder, [0], .5) assert_raises(ValueError, herm.hermder, [0], -1) # get_inds that zeroth derivative does nothing for i in range(5): tgt = [0] * i + [1] res = herm.hermder(tgt, m=0) assert_equal(trim(res), trim(tgt)) # get_inds that derivation is the inverse of integration for i in range(5): for j in range(2, 5): tgt = [0] * i + [1] res = herm.hermder(herm.hermint(tgt, m=j), m=j) assert_almost_equal(trim(res), trim(tgt)) # get_inds derivation with scaling for i in range(5): for j in range(2, 5): tgt = [0] * i + [1] res = herm.hermder(herm.hermint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt))
def test_hermder(self) : # check exceptions assert_raises(ValueError, herm.hermder, [0], .5) assert_raises(ValueError, herm.hermder, [0], -1) # check that zeroth deriviative does nothing for i in range(5) : tgt = [0]*i + [1] res = herm.hermder(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 = herm.hermder(herm.hermint(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 = herm.hermder(herm.hermint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt))
def evaluate_basis_derivative(self, x=None, i=0, k=0, output_array=None): if x is None: x = self.mesh(False, False) x = np.atleast_1d(x) v = eval_hermite(i, x, out=output_array) if k == 1: D = np.zeros((self.N, self.N)) D[:-1, :] = hermite.hermder(np.eye(self.N), 1) V = np.dot(v, D) V -= v*x[:, np.newaxis] V *= np.exp(-x**2/2)[:, np.newaxis]*self.factor(i) v[:] = V elif k == 2: W = (x[:, np.newaxis]**2 - 1 - 2*i)*V v[:] = W*self.factor(i)*np.exp(-x**2/2)[:, np.newaxis] elif k == 0: v *= np.exp(-x**2/2)[:, np.newaxis]*self.factor(i) else: raise NotImplementedError return v
def dpsi(x, c): from numpy.polynomial.hermite import hermval, hermder yp = hermval(x, hermder(c)) * np.exp(-x**2 / 2) - x * psi(x, c) return yp