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