forked from flintlib/python-flint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflint_base.pyx
More file actions
183 lines (154 loc) · 4.95 KB
/
Copy pathflint_base.pyx
File metadata and controls
183 lines (154 loc) · 4.95 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from flint.flint_base.flint_context cimport thectx
cdef class flint_elem:
def __repr__(self):
if thectx.pretty:
return self.str()
else:
return self.repr()
def __str__(self):
return self.str()
cdef class flint_scalar(flint_elem):
pass
cdef class flint_poly(flint_elem):
"""
Base class for polynomials.
"""
def __iter__(self):
cdef long i, n
n = self.length()
for i in range(n):
yield self[i]
def coeffs(self):
"""
Returns the coefficients of ``self`` as a list
>>> from flint import fmpz_poly
>>> f = fmpz_poly([1,2,3,4,5])
>>> f.coeffs()
[1, 2, 3, 4, 5]
"""
return list(self)
def str(self, bint ascending=False):
"""
Convert to a human-readable string (generic implementation for
all polynomial types).
If *ascending* is *True*, the monomials are output from low degree to
high, otherwise from high to low.
"""
coeffs = [str(c) for c in self]
if not coeffs:
return "0"
s = []
coeffs = enumerate(coeffs)
if not ascending:
coeffs = reversed(list(coeffs))
for i, c in coeffs:
if c == "0":
continue
else:
if c.startswith("-") or (" " in c):
c = "(" + c + ")"
if i == 0:
s.append("%s" % c)
elif i == 1:
if c == "1":
s.append("x")
else:
s.append("%s*x" % c)
else:
if c == "1":
s.append("x^%s" % i)
else:
s.append("%s*x^%s" % (c, i))
return " + ".join(s)
def roots(self):
"""
Computes all the roots in the base ring of the polynomial.
Returns a list of all pairs (*v*, *m*) where *v* is the
integer root and *m* is the multiplicity of the root.
To compute complex roots of a polynomial, instead use
the `.complex_roots()` method, which is available on
certain polynomial rings.
>>> from flint import fmpz_poly
>>> fmpz_poly([1, 2]).roots()
[]
>>> fmpz_poly([2, 1]).roots()
[(-2, 1)]
>>> fmpz_poly([12, 7, 1]).roots()
[(-3, 1), (-4, 1)]
>>> (fmpz_poly([-5,1]) * fmpz_poly([-5,1]) * fmpz_poly([-3,1])).roots()
[(3, 1), (5, 2)]
"""
factor_fn = getattr(self, "factor", None)
if not callable(factor_fn):
raise NotImplementedError("Polynomial has no factor method, roots cannot be determined")
roots = []
factors = self.factor()
for fac, m in factors[1]:
if fac.degree() == fac[1] == 1:
v = - fac[0]
roots.append((v, m))
return roots
def complex_roots(self):
raise AttributeError("Complex roots are not supported for this polynomial")
cdef class flint_mpoly(flint_elem):
"""
Base class for multivariate polynomials.
"""
cdef class flint_series(flint_elem):
"""
Base class for power series.
"""
def __iter__(self):
cdef long i, n
n = self.length()
for i in range(n):
yield self[i]
def coeffs(self):
return list(self)
cdef class flint_mat(flint_elem):
"""
Base class for matrices.
"""
def repr(self):
if thectx.pretty:
return str(self)
# XXX
return "%s(%i, %i, [%s])" % (type(self).__name__,
self.nrows(), self.ncols(), (", ".join(map(str, self.entries()))))
def str(self, *args, **kwargs):
tab = self.table()
if len(tab) == 0 or len(tab[0]) == 0:
return "[]"
tab = [[r.str(*args, **kwargs) for r in row] for row in tab]
widths = []
for i in xrange(len(tab[0])):
w = max([len(row[i]) for row in tab])
widths.append(w)
for i in xrange(len(tab)):
tab[i] = [s.rjust(widths[j]) for j, s in enumerate(tab[i])]
tab[i] = "[" + (", ".join(tab[i])) + "]"
return "\n".join(tab)
def entries(self):
cdef long i, j, m, n
m = self.nrows()
n = self.ncols()
L = [None] * (m * n)
for i from 0 <= i < m:
for j from 0 <= j < n:
L[i*n + j] = self[i, j]
return L
def __iter__(self):
cdef long i, j, m, n
m = self.nrows()
n = self.ncols()
for i from 0 <= i < m:
for j from 0 <= j < n:
yield self[i, j]
def table(self):
cdef long i, m, n
m = self.nrows()
n = self.ncols()
L = self.entries()
return [L[i*n : (i+1)*n] for i in range(m)]
# supports mpmath conversions
tolist = table