From: Michael Orlitzky Date: Fri, 22 Nov 2024 19:38:08 +0000 (-0500) Subject: mjo/interpolation.py: don't import sage.all in global scope X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=4e31642e5b258a2b4b937c64565f42744bec96c3;p=sage.d.git mjo/interpolation.py: don't import sage.all in global scope --- diff --git a/mjo/interpolation.py b/mjo/interpolation.py index 6e0ecb3..6c5b9f9 100644 --- a/mjo/interpolation.py +++ b/mjo/interpolation.py @@ -1,6 +1,9 @@ -from sage.all import * -product = prod - +r""" +Elementary interpolation methods. This only needs Sage to support +symbolic constants like ``pi``, and the lazy use of symbolic +expressions like ``x**2`` to represent the function that squares its +one argument. +""" def lagrange_denominator(k, xs): """ @@ -18,7 +21,8 @@ def lagrange_denominator(k, xs): The product of all xs[j] with j != k. """ - return product( xs[k] - xs[j] for j in range(len(xs)) if j != k ) + from sage.misc.all import prod # math.prod doesn't work on symbolics! + return prod( xs[k] - xs[j] for j in range(len(xs)) if j != k ) def lagrange_coefficient(k, x, xs): @@ -96,8 +100,9 @@ def lagrange_polynomial(x, xs, ys): True """ - ls = [ lagrange_coefficient(k, x, xs) for k in range(len(xs)) ] - return sum( ys[k] * ls[k] for k in range(len(xs)) ) + from math import sumprod + ls = ( lagrange_coefficient(k, x, xs) for k in range(len(xs)) ) + return sumprod(ys, ls) @@ -209,10 +214,9 @@ def divided_difference(xs, ys): f(x1)/(x1 - x2) - f(x2)/(x1 - x2) """ + from math import sumprod coeffs = divided_difference_coefficients(xs) - v_cs = vector(coeffs) - v_ys = vector(ys) - return v_cs.dot_product(v_ys) + return sumprod(coeffs,ys) def newton_polynomial(x, xs, ys): @@ -364,5 +368,5 @@ def lagrange_psi(x, xs): A symbolic expression in one variable, `x`. """ - - return product( (x - xj) for xj in xs ) + from sage.misc.all import prod # math.prod doesn't work on symbolics! + return prod( (x - xj) for xj in xs )