From e635749a57cc08c5c989c8df9d146594e6115903 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 4 Nov 2018 01:08:20 -0500 Subject: [PATCH] mjo/interpolation.py: partial conversion to generator expressions. --- mjo/interpolation.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/mjo/interpolation.py b/mjo/interpolation.py index a5ad78d..d3b406a 100644 --- a/mjo/interpolation.py +++ b/mjo/interpolation.py @@ -18,7 +18,7 @@ def lagrange_denominator(k, xs): The product of all xs[j] with j != k. """ - return product([xs[k] - xs[j] for j in xrange(len(xs)) if j != k]) + return product( xs[k] - xs[j] for j in xrange(len(xs)) if j != k ) def lagrange_coefficient(k, x, xs): @@ -97,8 +97,7 @@ def lagrange_polynomial(x, xs, ys): """ ls = [ lagrange_coefficient(k, x, xs) for k in xrange(len(xs)) ] - sigma = sum([ ys[k] * ls[k] for k in xrange(len(xs)) ]) - return sigma + return sum( ys[k] * ls[k] for k in xrange(len(xs)) ) @@ -161,8 +160,7 @@ def divided_difference_coefficients(xs): [1/2/pi^2, -1/pi^2, 1/2/pi^2] """ - coeffs = [ QQ(1)/lagrange_denominator(k, xs) for k in xrange(len(xs)) ] - return coeffs + return [ ~lagrange_denominator(k, xs) for k in xrange(len(xs)) ] def divided_difference(xs, ys): @@ -249,16 +247,8 @@ def newton_polynomial(x, xs, ys): True """ - degree = len(xs) - 1 - - N = SR(0) - - for k in xrange(degree+1): - term = divided_difference(xs[:k+1], ys[:k+1]) - term *= lagrange_psi(x, xs[:k]) - N += term - - return N + return sum( divided_difference(xs[:k+1], ys[:k+1])*lagrange_psi(x, xs[:k]) + for k in xrange(len(xs)) ) def hermite_coefficient(k, x, xs): @@ -346,11 +336,11 @@ def hermite_interpolant(x, xs, ys, y_primes): True """ - s1 = sum([ ys[k] * hermite_coefficient(k, x, xs) - for k in xrange(len(xs)) ]) + s1 = sum( ys[k] * hermite_coefficient(k, x, xs) + for k in xrange(len(xs)) ) - s2 = sum([ y_primes[k] * hermite_deriv_coefficient(k, x, xs) - for k in xrange(len(xs)) ]) + s2 = sum( y_primes[k] * hermite_deriv_coefficient(k, x, xs) + for k in xrange(len(xs)) ) return (s1 + s2) @@ -375,4 +365,4 @@ def lagrange_psi(x, xs): """ - return product([ (x - xj) for xj in xs ]) + return product( (x - xj) for xj in xs ) -- 2.44.2