]> gitweb.michael.orlitzky.com - sage.d.git/commitdiff
mjo/interpolation.py: don't import sage.all in global scope
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 22 Nov 2024 19:38:08 +0000 (14:38 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 22 Nov 2024 19:38:08 +0000 (14:38 -0500)
mjo/interpolation.py

index 6e0ecb3d79c5129a2e6270177b648142085786d1..6c5b9f9720127c4aad1731ed9c27663f1617d1b3 100644 (file)
@@ -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 )