Skip to content

Commit 48dfa7e

Browse files
authoredFeb 26, 2023
Update polyvander.py
1 parent d81c95c commit 48dfa7e

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed
 

‎Interpolation/polyvander.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def P(x,k=0):
5858
return np.array([ horner(du_dx*(x-a)-1, coeff[:,i], k) * du_dx ** k for i in range(coeff.shape[1])]).T
5959
return P
6060

61-
def horner(x, a, k):
61+
def horner(x, a, k=0):
6262
"""
6363
Recursive Horner's rule
6464
@@ -80,34 +80,40 @@ def horner(x, a, k):
8080
val : int, float, or complex
8181
The value of the k-th derivative/antiderivative of the polynomial given by the coefficients a at x.
8282
"""
83-
if isinstance(x, list) or isinstance(x,tuple):
84-
x = np.asarray(x)/1.0
85-
86-
if type(x)==int:
87-
x /= 1.0
88-
89-
if type(k) is not int:
90-
raise TypeError('Argument k must be an integer.')
83+
a = np.asarray(a)
84+
if a.ndim==0:
85+
return horner(x, [a.item()], k)
86+
if a.ndim>1:
87+
return np.array([horner(x, a[i], k) for i in range(a.shape[0])]).T
9188
else:
92-
pass
93-
94-
n = len(a)
95-
d = n - 1
96-
if k > d:
97-
val = np.zeros_like(x)
98-
elif k == d:
99-
val = np.full_like(x, factorial(k) * a[k])
100-
elif k == 0:
101-
if n==1:
102-
val = np.full_like(x, a[0])
103-
elif n==2:
104-
val = a[0] + x * a[1]
89+
if isinstance(x, list) or isinstance(x,tuple):
90+
x = np.asarray(x)/1.0
91+
92+
if type(x)==int:
93+
x /= 1.0
94+
95+
if type(k) is not int:
96+
raise TypeError('Argument k must be an integer.')
10597
else:
106-
val = a[0] + x * horner(x, a[1:], k)
107-
elif k<d and k>0:
108-
val = horner(x, a[1:] * np.arange(1,n), k-1)
109-
elif k<0:
110-
val = horner(x, np.hstack(( 0, a / np.arange(1,n+1) )), k+1)
111-
else:
112-
pass
113-
return val
98+
pass
99+
100+
n = len(a)
101+
d = n - 1
102+
if k > d:
103+
val = np.zeros_like(x)
104+
elif k == d:
105+
val = np.full_like(x, factorial(k) * a[k])
106+
elif k == 0:
107+
if n==1:
108+
val = np.full_like(x, a[0])
109+
elif n==2:
110+
val = a[0] + x * a[1]
111+
else:
112+
val = a[0] + x * horner(x, a[1:], k)
113+
elif k<d and k>0:
114+
val = horner(x, a[1:] * np.arange(1,n), k-1)
115+
elif k<0:
116+
val = horner(x, np.hstack(( 0, a / np.arange(1,n+1) )), k+1)
117+
else:
118+
pass
119+
return val

0 commit comments

Comments
 (0)