Welcome, guest | Sign In | My Account | Store | Cart

This implements polynomial functions over a single variable in Python. It represents the polynomial as a list of numbers and allows most arithmetic operations, using conventional Python syntax. It does not do symbolic manipulations. Instead, you can do things like this:

x = SimplePolynomial()
eq = (x-1)*(x*1)
print eq     # prints 'X**2 - 1'
print eq(4)  # prints 15
Python, 294 lines
  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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""\
This class implements polynomial functions over a single variable. It
represents the polynomial as a list of numbers and allows most
arithmetic operations, using conventional Python syntax. It does not
do symbolic manipulations. Instead, you can do things like this:

>>> x = SimplePolynomial()
>>> quadratic = (x+1)*(x-1)
>>> str(quadratic)
'X**2 - 1'
>>> quadratic(4)
15
>>> for i in range(4):
...     polynomial = (x+1)**i
...     i, str(polynomial), polynomial(1)
(0, '1', 1)
(1, 'X + 1', 2)
(2, 'X**2 + 2*X + 1', 4)
(3, 'X**3 + 3*X**2 + 3*X + 1', 8)
"""

from __future__ import division, generators

from operator import add

try:
    from itertools import izip_longest
except ImportError:
    # The izip_longest function was added in version 2.6
    # If we can't find it, use an equivalent implementation.
    from itertools import chain, repeat
    class ZipExhausted(Exception):
        pass
    def izip_longest(*args, **kwds):
        # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
        fillvalue = kwds.get('fillvalue')
        counter = [len(args) - 1]
        def sentinel():
            if not counter[0]:
                raise ZipExhausted
            counter[0] -= 1
            yield fillvalue
        fillers = repeat(fillvalue)
        iterators = [chain(it, sentinel(), fillers) for it in args]
        try:
            while iterators:
                yield tuple(map(next, iterators))
        except ZipExhausted:
            pass

try:
    from numbers import Number
except ImportError:
    # The numbers module was added in version 2.6
    # If we can't find it, use an equivalent implementation.
    Number = (int, float, long, complex)


class SimplePolynomial(object):

    def __init__(self, terms=[0,1]):
        """\
>>> str(SimplePolynomial())
'X'
"""
        try:
            while terms[-1] == 0:
                del terms[-1]
        except IndexError:
            pass
        self.terms = list(terms)

    def __str__(self):
        """\
Needs some work, but adequate.
"""
        l = len(self.terms)
        if l == 0:
            return '0'
        if l == 1:
            return str(self.terms[0])
        result = []
        for i, c in reversed(list(enumerate(self.terms))):
            if c == 0:
                continue
            if c < 0:
                result.append('-')
                c = - c
            else:
                result.append('+')
                
            if c == 1:
                if i == 0:
                    result.append('1')
                elif i == 1:
                    result.append('X')
                else:
                    result.append('X**%g' % i)
            else:
                if i == 0:
                    result.append('%g' % c)
                elif i == 1:
                    result.append('%g*X' % c)
                else:
                    result.append('%g*X**%d' % (c, i))
        if len(result) == 0:
            return '0'
        if result[0] == '-':
            result[1] = '-'+result[1]
        del result[0]
        return ' '.join(result)

    def __add__(self, other):
        """\
>>> str(SimplePolynomial() + 1)
'X + 1'
>>> str(1 + SimplePolynomial())
'X + 1'
>>> str(SimplePolynomial() + SimplePolynomial())
'2*X'
"""
        if len(self.terms) == 0:
            return other
        if isinstance(other, Number):
            terms = self.terms[:]
            terms[0] += other
            return SimplePolynomial(terms)
        return SimplePolynomial([
            add(*pair)
            for pair in izip_longest(self.terms, other.terms, fillvalue=0)
            ])

    # Since addition is commutative, reuse __add__
    __radd__ = __add__

    def __neg__(self):
        """\
>>> str(- SimplePolynomial())
'-X'
"""
        return SimplePolynomial([-c for c in self.terms])

    def __sub__(self, other):
        """\
>>> str(SimplePolynomial() - 1)
'X - 1'
>>> str(SimplePolynomial() - SimplePolynomial())
'0'
"""
        return self + -other

    def __rsub__(self, other):
        """\
>>> str(1 - SimplePolynomial())
'-X + 1'
"""
        return -self + other

    def __mul__(self, other):
        """\
>>> str(SimplePolynomial() * 2)
'2*X'
>>> str(2 * SimplePolynomial())
'2*X'
>>> str(SimplePolynomial() * SimplePolynomial())
'X**2'
"""
        if isinstance(other, Number):
            return SimplePolynomial([c * other for c in self.terms])
        terms = [0]*(len(self.terms)+len(other.terms))
        for i1, c1 in enumerate(self.terms):
            for i2, c2 in enumerate(other.terms):
                terms[i1+i2] += c1*c2
        return SimplePolynomial(terms)

    # Since multiplication is commutative, reuse __mul__
    __rmul__ = __mul__

    def __truediv__(self, other):
        """\
Implements some simple forms of division.  See
http://en.wikipedia.org/wiki/Synthetic_division
for details.

>>> str(SimplePolynomial() / 2)
'0.5*X'
>>> x = SimplePolynomial()
>>> quotient, remainder  = (x**3 - 12*x**2 - 42) / (x - 3)
>>> str(quotient), str(remainder)
('X**2 - 9*X - 27', '-123')
"""
        if isinstance(other, Number):
            return SimplePolynomial([c / other for c in self.terms])
        if len(other.terms) == 1:
            return self/other.terms[0]
        assert len(other.terms) == 2
        dividend = self.terms[:]
        divisor = other.terms[:]
        assert divisor[-1] == 1
        xi = 0
        result = []
        for i in xrange(-1, -len(dividend) - 1, -1):
            xi = dividend[i] - xi * divisor[0]
            result.insert(0, xi)
        return SimplePolynomial(result[1:]), result[0]
        raise NotImplementedError('synthetic division')

    __div__ = __truediv__

    def __eq__(self, other):
        """\
>>> SimplePolynomial() == SimplePolynomial()
True
>>> SimplePolynomial() - SimplePolynomial() == 0
True
"""
        if isinstance(other, SimplePolynomial):
            return self.terms == other.terms
        if isinstance(other, Number):
            if len(self.terms) > 1:
                return False
            try:
                self.terms[0] == other
            except IndexError:
                return other == 0
        return False

    def __ne__(self, other):
        return not self == other

    def copy(self):
        return SimplePolynomial(self.terms[:])

    def __pow__(self, exponent):
        """\
Uses the Russian Peasant Multiplication algorithm.

>>> str(SimplePolynomial() ** 2)
'X**2'
>>> str(SimplePolynomial() ** 0.5)
Traceback (most recent call last):
...
NotImplementedError: exponent is not an integer
>>> str(SimplePolynomial() ** -1)
Traceback (most recent call last):
...
NotImplementedError: exponent is less than zero
"""
        if not isinstance(exponent, int):
            raise NotImplementedError('exponent is not an integer')
        if exponent < 0:
            raise NotImplementedError('exponent is less than zero')
        tmp = self.copy()
        result = SimplePolynomial([1])
        while exponent > 0:
            if exponent & 1:
                result *= tmp
            tmp *= tmp
            exponent >>= 1
        return result

    def __call__(self, x):
        """\
Evaluate the polynomial for the given value using the Horner scheme.

>>> SimplePolynomial()(1)
1
"""
        result = 0
        for c in reversed(self.terms):
            result = result * x + c
        return result

    def derivative(self):
        """\
>>> str(SimplePolynomial().derivative())
'1'
"""
        terms = [i*c for i, c in enumerate(self.terms)]
        return SimplePolynomial(terms[1:])

    def integrate(self, const=0):
        """\
>>> str(SimplePolynomial().integrate())
'0.5*X**2'
"""
        terms = [const]
        terms.extend([c/(i+1) for i, c in enumerate(self.terms)])
        return SimplePolynomial(terms)


if __name__ == '__main__':
    import doctest
    doctest.testmod()

Since I posted the original, I've added doctests and the barest beginnings of polynomial division. In the doctests, I cast values to strings to avoid having to use print, so this is usable as-is in both Python 2.x and 3.x.

3 comments

Gabriel Genellina 14 years, 5 months ago  # | flag

Very nice recipe!!! Just a few notes:

  • you might replace sum_v by operator.add

  • I noticed you very carefully avoided sharing self.terms between instances, but forgot to do so in copy().

  • also, complex coefficients appear to be supported, but integral() uses float(c) and may fail in such cases. c/float(i+1) is enough to avoid integer division (or use from __future__ import division)

  • To evaluate the polynomial, Ruffini's Rule usually gives better results (but is slightly more complex than your very straightforward code)

Gabriel Genellina 14 years, 5 months ago  # | flag

also, itertools seems to be a leftover from earlier attempts...

Sam Denton (author) 14 years, 5 months ago  # | flag

Thanks for the feedback. I've implemented all of your suggestions. In the case of copy(), I'd convinced myself that the sharing was harmless, but it's probably better to be safe than sorry. The float(c) was left over from earlier debugging; after integration my coefficients all appeared to be integers. (It turned out that I was using '%d' in __str__().) At some point I'll probably add synthetic division.