|
| 1 | +from flint.flint_base.flint_context cimport thectx |
| 2 | + |
| 3 | +cdef class flint_elem: |
| 4 | + def __repr__(self): |
| 5 | + if thectx.pretty: |
| 6 | + return self.str() |
| 7 | + else: |
| 8 | + return self.repr() |
| 9 | + |
| 10 | + def __str__(self): |
| 11 | + return self.str() |
| 12 | + |
| 13 | +cdef class flint_scalar(flint_elem): |
| 14 | + pass |
| 15 | + |
| 16 | +# TODO: |
| 17 | +# We cannot include this class until we can import |
| 18 | +# acb_poly, so for now we leave this class as a global |
| 19 | +# inside pyflint.pyx |
| 20 | +# |
| 21 | +# cdef class flint_poly(flint_elem): |
| 22 | +# """ |
| 23 | +# Base class for polynomials. |
| 24 | +# """ |
| 25 | + |
| 26 | +# def __iter__(self): |
| 27 | +# cdef long i, n |
| 28 | +# n = self.length() |
| 29 | +# for i in range(n): |
| 30 | +# yield self[i] |
| 31 | + |
| 32 | +# def coeffs(self): |
| 33 | +# return list(self) |
| 34 | + |
| 35 | +# def str(self, bint ascending=False): |
| 36 | +# """ |
| 37 | +# Convert to a human-readable string (generic implementation for |
| 38 | +# all polynomial types). |
| 39 | + |
| 40 | +# If *ascending* is *True*, the monomials are output from low degree to |
| 41 | +# high, otherwise from high to low. |
| 42 | +# """ |
| 43 | +# coeffs = [str(c) for c in self] |
| 44 | +# if not coeffs: |
| 45 | +# return "0" |
| 46 | +# s = [] |
| 47 | +# coeffs = enumerate(coeffs) |
| 48 | +# if not ascending: |
| 49 | +# coeffs = reversed(list(coeffs)) |
| 50 | +# for i, c in coeffs: |
| 51 | +# if c == "0": |
| 52 | +# continue |
| 53 | +# else: |
| 54 | +# if c.startswith("-") or (" " in c): |
| 55 | +# c = "(" + c + ")" |
| 56 | +# if i == 0: |
| 57 | +# s.append("%s" % c) |
| 58 | +# elif i == 1: |
| 59 | +# if c == "1": |
| 60 | +# s.append("x") |
| 61 | +# else: |
| 62 | +# s.append("%s*x" % c) |
| 63 | +# else: |
| 64 | +# if c == "1": |
| 65 | +# s.append("x^%s" % i) |
| 66 | +# else: |
| 67 | +# s.append("%s*x^%s" % (c, i)) |
| 68 | +# return " + ".join(s) |
| 69 | + |
| 70 | +# def roots(self, **kwargs): |
| 71 | +# """ |
| 72 | +# Isolates the complex roots of *self*. See :meth:`.acb_poly.roots` |
| 73 | +# for details. |
| 74 | +# """ |
| 75 | +# # TODO: |
| 76 | +# # To avoid circular imports, we import within the method |
| 77 | +# from XXX.XXX.acb_poly import acb_poly |
| 78 | +# return acb_poly(self).roots(**kwargs) |
| 79 | + |
| 80 | +cdef class flint_mpoly(flint_elem): |
| 81 | + """ |
| 82 | + Base class for multivariate polynomials. |
| 83 | + """ |
| 84 | + |
| 85 | +cdef class flint_series(flint_elem): |
| 86 | + """ |
| 87 | + Base class for power series. |
| 88 | + """ |
| 89 | + def __iter__(self): |
| 90 | + cdef long i, n |
| 91 | + n = self.length() |
| 92 | + for i in range(n): |
| 93 | + yield self[i] |
| 94 | + |
| 95 | + def coeffs(self): |
| 96 | + return list(self) |
| 97 | + |
| 98 | + |
| 99 | +cdef class flint_mat(flint_elem): |
| 100 | + """ |
| 101 | + Base class for matrices. |
| 102 | + """ |
| 103 | + |
| 104 | + def repr(self): |
| 105 | + if thectx.pretty: |
| 106 | + return str(self) |
| 107 | + # XXX |
| 108 | + return "%s(%i, %i, [%s])" % (type(self).__name__, |
| 109 | + self.nrows(), self.ncols(), (", ".join(map(str, self.entries())))) |
| 110 | + |
| 111 | + def str(self, *args, **kwargs): |
| 112 | + tab = self.table() |
| 113 | + if len(tab) == 0 or len(tab[0]) == 0: |
| 114 | + return "[]" |
| 115 | + tab = [[r.str(*args, **kwargs) for r in row] for row in tab] |
| 116 | + widths = [] |
| 117 | + for i in xrange(len(tab[0])): |
| 118 | + w = max([len(row[i]) for row in tab]) |
| 119 | + widths.append(w) |
| 120 | + for i in xrange(len(tab)): |
| 121 | + tab[i] = [s.rjust(widths[j]) for j, s in enumerate(tab[i])] |
| 122 | + tab[i] = "[" + (", ".join(tab[i])) + "]" |
| 123 | + return "\n".join(tab) |
| 124 | + |
| 125 | + def entries(self): |
| 126 | + cdef long i, j, m, n |
| 127 | + m = self.nrows() |
| 128 | + n = self.ncols() |
| 129 | + L = [None] * (m * n) |
| 130 | + for i from 0 <= i < m: |
| 131 | + for j from 0 <= j < n: |
| 132 | + L[i*n + j] = self[i, j] |
| 133 | + return L |
| 134 | + |
| 135 | + def __iter__(self): |
| 136 | + cdef long i, j, m, n |
| 137 | + m = self.nrows() |
| 138 | + n = self.ncols() |
| 139 | + for i from 0 <= i < m: |
| 140 | + for j from 0 <= j < n: |
| 141 | + yield self[i, j] |
| 142 | + |
| 143 | + def table(self): |
| 144 | + cdef long i, m, n |
| 145 | + m = self.nrows() |
| 146 | + n = self.ncols() |
| 147 | + L = self.entries() |
| 148 | + return [L[i*n : (i+1)*n] for i in range(m)] |
| 149 | + |
| 150 | + # supports mpmath conversions |
| 151 | + tolist = table |
0 commit comments