-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial.py
57 lines (48 loc) · 1.48 KB
/
polynomial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# coding=latin-1
"""
Title: HyperSage - a Sage library for tetration and hyper operations.
Creator: Andrew Robbins
Date: 2008-04-22
Description:
hyperops/polynomial.py contains basic polynomials.
a_poly(x) makes an arbitrary polynomial function,
and h_poly(x) and p_poly(x) make hyperbolic and parabolic
functions, respectively.
"""
from sage.all import *
def a_poly(x, n=5, coeff=[]):
"""
a_poly(x) -- Arbitrary polynomial, with coeffs C1, C2, ...
a_poly(x, n) -- Degree n polynomial, with coeffs C1, C2, ...
a_poly(x, n, [a, b]) -- A polynomial starting with a + b*x + ...
"""
if len(coeff) > n: n = len(coeff)
head = [coeff[i]*x**i
for i in xrange(len(coeff))]
tail = [var('C' + str(i))*x**i
for i in xrange(len(coeff), n + 1)]
return sum(head + tail)
def h_poly(x, n=5):
"""
h_poly(x) -- Hyperbolic polynomial
"""
return a_poly(x, n, coeff=[0])
def p_poly(x, n=5):
"""
p_poly(x) -- Parabolic polynomial
"""
return a_poly(x, n, coeff=[0, 1])
# formerly coeffs_alist_to_vector
def get_coeff_list(ser, x, x0=0, n=5):
# extract coeffs from series
coeffs_alist = ser.coeffs(x)
coeffs = [0 for k in xrange(n + 1)]
for c in coeffs_alist:
try:
coeffs[int(c[1])] = c[0]
except e:
pass
# make sure there are exactly (n+1) coeffs
coeffs.extend([0 for i in xrange(n + 1 - len(coeffs))])
coeffs = coeffs[0 : n + 1]
return coeffs