@@ -6,6 +6,8 @@ cimport flint
66cimport libc.stdlib
77cimport cython
88
9+ from flint._global_context cimport thectx
10+
911cdef flint_rand_t global_random_state
1012flint_randinit(global_random_state)
1113
@@ -29,152 +31,8 @@ DEF FMPZ_UNKNOWN = 0
2931DEF FMPZ_REF = 1
3032DEF FMPZ_TMP = 2
3133
32- from flint._global_context cimport thectx
3334ctx = thectx
3435
35- cdef class flint_elem:
36- def __repr__ (self ):
37- if ctx.pretty:
38- return self .str()
39- else :
40- return self .repr()
41-
42- def __str__ (self ):
43- return self .str()
44-
45- cdef class flint_scalar(flint_elem):
46- pass
47-
48- cdef class flint_poly(flint_elem):
49- """
50- Base class for polynomials.
51- """
52-
53- def __iter__ (self ):
54- cdef long i, n
55- n = self .length()
56- for i in range (n):
57- yield self [i]
58-
59- def coeffs (self ):
60- return list (self )
61-
62- def str (self , bint ascending = False ):
63- """
64- Convert to a human-readable string (generic implementation for
65- all polynomial types).
66-
67- If *ascending* is *True*, the monomials are output from low degree to
68- high, otherwise from high to low.
69- """
70- coeffs = [str (c) for c in self ]
71- if not coeffs:
72- return " 0"
73- s = []
74- coeffs = enumerate (coeffs)
75- if not ascending:
76- coeffs = reversed (list (coeffs))
77- for i, c in coeffs:
78- if c == " 0" :
79- continue
80- else :
81- if c.startswith(" -" ) or (" " in c):
82- c = " (" + c + " )"
83- if i == 0 :
84- s.append(" %s " % c)
85- elif i == 1 :
86- if c == " 1" :
87- s.append(" x" )
88- else :
89- s.append(" %s *x" % c)
90- else :
91- if c == " 1" :
92- s.append(" x^%s " % i)
93- else :
94- s.append(" %s *x^%s " % (c, i))
95- return " + " .join(s)
96-
97- def roots (self , **kwargs ):
98- """
99- Isolates the complex roots of *self*. See :meth:`.acb_poly.roots`
100- for details.
101- """
102- return acb_poly(self ).roots(** kwargs)
103-
104- cdef class flint_mpoly(flint_elem):
105- """
106- Base class for multivariate polynomials.
107- """
108-
109-
110- cdef class flint_mat(flint_elem):
111- """
112- Base class for matrices.
113- """
114-
115- def repr (self ):
116- if ctx.pretty:
117- return str (self )
118- # XXX
119- return " %s (%i , %i , [%s ])" % (type (self ).__name__,
120- self .nrows(), self .ncols(), (" , " .join(map (str , self .entries()))))
121-
122- def str (self , *args , **kwargs ):
123- tab = self .table()
124- if len (tab) == 0 or len (tab[0 ]) == 0 :
125- return " []"
126- tab = [[r.str(* args, ** kwargs) for r in row] for row in tab]
127- widths = []
128- for i in xrange (len (tab[0 ])):
129- w = max ([len (row[i]) for row in tab])
130- widths.append(w)
131- for i in xrange (len (tab)):
132- tab[i] = [s.rjust(widths[j]) for j, s in enumerate (tab[i])]
133- tab[i] = " [" + (" , " .join(tab[i])) + " ]"
134- return " \n " .join(tab)
135-
136- def entries (self ):
137- cdef long i, j, m, n
138- m = self .nrows()
139- n = self .ncols()
140- L = [None ] * (m * n)
141- for i from 0 <= i < m:
142- for j from 0 <= j < n:
143- L[i* n + j] = self [i, j]
144- return L
145-
146- def __iter__ (self ):
147- cdef long i, j, m, n
148- m = self .nrows()
149- n = self .ncols()
150- for i from 0 <= i < m:
151- for j from 0 <= j < n:
152- yield self [i, j]
153-
154- def table (self ):
155- cdef long i, m, n
156- m = self .nrows()
157- n = self .ncols()
158- L = self .entries()
159- return [L[i* n : (i+ 1 )* n] for i in range (m)]
160-
161- # supports mpmath conversions
162- tolist = table
163-
164- cdef class flint_series(flint_elem):
165- """
166- Base class for power series.
167- """
168- def __iter__ (self ):
169- cdef long i, n
170- n = self .length()
171- for i in range (n):
172- yield self [i]
173-
174- def coeffs (self ):
175- return list (self )
176-
177-
17836include " fmpz.pyx"
17937include " fmpz_poly.pyx"
18038include " fmpz_mpoly.pyx"
0 commit comments