def gr_roots(n): """Gauss Radau roots and weights for [-1, 1] with -1 first """ leg = orthogonal.legendre(n - 1) l = (leg + orthogonal.legendre(n))/np.poly1d((1, 1)) x = np.r_[-1, l[0].roots] w = (1 - x)/(n * leg(x))**2 return x, w
def gr_roots(n): """Gauss Radau roots and weights for [-1, 1] with -1 first """ leg = orthogonal.legendre(n - 1) l = (leg + orthogonal.legendre(n)) / np.poly1d((1, 1)) x = np.r_[-1, l[0].roots] w = (1 - x) / (n * leg(x))**2 return x, w
def test_p_roots(): weightf = orth.legendre(5).weight_func verify_gauss_quad(orth.p_roots, orth.eval_legendre, weightf, -1., 1., 5) verify_gauss_quad(orth.p_roots, orth.eval_legendre, weightf, -1., 1., 25, atol=1e-13) verify_gauss_quad(orth.p_roots, orth.eval_legendre, weightf, -1., 1., 100, atol=1e-12) x, w = orth.p_roots(5, False) y, v, m = orth.p_roots(5, True) assert_allclose(x, y, 1e-14, 1e-14) assert_allclose(w, v, 1e-14, 1e-14) muI, muI_err = integrate.quad(weightf, -1, 1) assert_allclose(m, muI, rtol=muI_err) assert_raises(ValueError, orth.p_roots, 0) assert_raises(ValueError, orth.p_roots, 3.3)
def get_model(self, x=None, param=None): """ return the model for the given data. The modelization is based on legendre polynomes that expect x to be between -1 and 1. This will create a reshaped copy of x to scale it between -1 and 1 but if x is already as such, save time by setting reshapex to False Returns ------- array (size of x) """ if param is not None: self.setup(param) if x is not None: self.set_xsource(x) if self.use_legendre: model = np.asarray([ orthogonal.legendre(i)(self.xsource_scaled) for i in range(self.DEGREE) ]) else: model = np.asarray([self.xfit**i for i in range(self.DEGREE)]) return np.dot(model.T, self.parameters.T).T
def gl_roots(n): """Gauss Lobatto roots and weights for [-1, 1] with -1 first and 1 last """ leg = orthogonal.legendre(n - 1) x = np.r_[-1, leg.deriv().roots, 1] w = 2 / (n * (n - 1) * leg(x)**2) return x, w
def gl_roots(n): """Gauss Lobatto roots and weights for [-1, 1] with -1 first and 1 last """ leg = orthogonal.legendre(n - 1) x = np.r_[-1, leg.deriv().roots, 1] w = 2/(n*(n - 1)*leg(x)**2) return x, w
def test_p_roots(): weightf = orth.legendre(5).weight_func verify_gauss_quad(orth.p_roots, orth.eval_legendre, weightf, -1.0, 1.0, 5) verify_gauss_quad(orth.p_roots, orth.eval_legendre, weightf, -1.0, 1.0, 25, atol=1e-13) verify_gauss_quad(orth.p_roots, orth.eval_legendre, weightf, -1.0, 1.0, 100, atol=1e-12) x, w = orth.p_roots(5, False) y, v, m = orth.p_roots(5, True) assert_allclose(x, y, 1e-14, 1e-14) assert_allclose(w, v, 1e-14, 1e-14) muI, muI_err = integrate.quad(weightf, -1, 1) assert_allclose(m, muI, rtol=muI_err) assert_raises(ValueError, orth.p_roots, 0) assert_raises(ValueError, orth.p_roots, 3.3)
def fit_polynome(x, y, degree, variance=None): """ """ from scipy.optimize import fmin from scipy.special import orthogonal xdata = (x - np.min(x)) / (np.max(x) - np.min(x)) * 2 - 1. basemodel = np.asarray( [orthogonal.legendre(i)(xdata) for i in range(degree)]) def get_model(parameters): """ """ return np.dot(basemodel.T, parameters.T).T def get_chi2(parameters): res = (y - get_model(parameters))**2 if variance is not None: res /= variance return np.sum(res) guess = np.zeros(degree) guess[0] = np.median(y) param = fmin(get_chi2, guess, disp=0) return get_model(param)
def test_sh_legendre(self): # P*_n(x) = P_n(2x-1) psub = np.poly1d([2, -1]) Ps0 = orth.sh_legendre(0) Ps1 = orth.sh_legendre(1) Ps2 = orth.sh_legendre(2) Ps3 = orth.sh_legendre(3) Ps4 = orth.sh_legendre(4) Ps5 = orth.sh_legendre(5) pse0 = orth.legendre(0)(psub) pse1 = orth.legendre(1)(psub) pse2 = orth.legendre(2)(psub) pse3 = orth.legendre(3)(psub) pse4 = orth.legendre(4)(psub) pse5 = orth.legendre(5)(psub) assert_array_almost_equal(Ps0.c, pse0.c, 13) assert_array_almost_equal(Ps1.c, pse1.c, 13) assert_array_almost_equal(Ps2.c, pse2.c, 13) assert_array_almost_equal(Ps3.c, pse3.c, 13) assert_array_almost_equal(Ps4.c, pse4.c, 12) assert_array_almost_equal(Ps5.c, pse5.c, 12)
def test_sh_legendre(self): # P*_n(x) = P_n(2x-1) psub = np.poly1d([2,-1]) Ps0 = orth.sh_legendre(0) Ps1 = orth.sh_legendre(1) Ps2 = orth.sh_legendre(2) Ps3 = orth.sh_legendre(3) Ps4 = orth.sh_legendre(4) Ps5 = orth.sh_legendre(5) pse0 = orth.legendre(0)(psub) pse1 = orth.legendre(1)(psub) pse2 = orth.legendre(2)(psub) pse3 = orth.legendre(3)(psub) pse4 = orth.legendre(4)(psub) pse5 = orth.legendre(5)(psub) assert_array_almost_equal(Ps0.c,pse0.c,13) assert_array_almost_equal(Ps1.c,pse1.c,13) assert_array_almost_equal(Ps2.c,pse2.c,13) assert_array_almost_equal(Ps3.c,pse3.c,13) assert_array_almost_equal(Ps4.c,pse4.c,12) assert_array_almost_equal(Ps5.c,pse5.c,12)