I’m trying to optimize a polynomial implementation of mine. In particular I’m dealing with polynomials with coefficients modulo n(might be >2^64) and modulo a polynomial in the form x^r - 1(r is < 2^64). At the moment I represent the coefficient as a list of integers(*) and I’ve implemented all the basic operations in the most straightforward way.
I’d like the exponentiation and multiplication to be as fast as possible, and to obtain this I’ve already tried different approaches. My current approach is to convert the lists of coefficients into huge integers multiply the integers and unpack back the coefficients.
The problem is that packing and unpacking takes a lot of time.
So, is there a way of improving my “pack/unpack” functions?
def _coefs_to_long(coefs, window):
'''Given a sequence of coefficients *coefs* and the *window* size return a
long-integer representation of these coefficients.
'''
res = 0
adder = 0
for k in coefs:
res += k << adder
adder += window
return res
#for k in reversed(coefs): res = (res << window) + k is slower
def _long_to_coefs(long_repr, window, n):
'''Given a long-integer representing coefficients of size *window*, return
the list of coefficients modulo *n*.
'''
mask = 2**window - 1
coefs = [0] * (long_repr.bit_length() // window + 1)
for i in xrange(len(coefs)):
coefs[i] = (long_repr & mask) % n
long_repr >>= window
# assure that the returned list is never empty, and hasn't got an extra 0.
if not coefs:
coefs.append(0)
elif not coefs[-1] and len(coefs) > 1:
coefs.pop()
return coefs
Note that I do not choose n, it is an input from the user, and my program wants to prove its primality(using the AKS test), so I can’t factorize it.
(*) I’ve tried several approaches:
- Using a
numpyarray instead of a list and multiply usingnumpy.convolve. It’s fast forn < 2^64but terribly slow forn > 2^64[also I’d like to avoid using external libraries] - Using
scipy.fftconvolve. Doesn’t work at all forn > 2^64. - Represent the coefficients as integers from the start(without converting them every time). The problem is that I don’t know of an easy way to do the
mod x^r -1operation without converting the integer to a list of coefficients(which defeats the reason of using this representation).
I found a way to optimize the conversions, even though I still hope that someone could help me improve them even more, and hopefully find some other clever idea.
Basically what’s wrong with those functions is that they have some kind of quadratic memory allocation behaviour, when packing the integer, or when unpacking it.
(See this post of Guido van Rossum for an other example of this kind of behaviour).
After I realized this I’ve decided to give a try with the Divide et Impera principle, and I’ve obtained some results. I simply divide the array in two parts, convert them separately and eventually join the results(later I’ll try to use an iterative version similar to the
f5in Rossum’s post[edit: it doesn’t seem to be much faster]).The modified functions:
And the results:
As you can see this version gives quite a speed up to the conversion, from
4to8times faster(and bigger the input, bigger is the speed up).A similar result is obtained with the second function:
I’ve tried to avoid more memory reallocation in the first function passing around the start and end indexes and avoiding slicing, but it turns out that this slows the function down quite much for small inputs and it’s a just a bit slower for real-case inputs.
Maybe I could try to mix them, even though I don’t think I’ll obtain much better results.
I’ve edited my question in the last period therefore some people gave me some advice with a different aim then what I required recently. I think it’s important to clarify a bit the results pointed out by different sources in the comments and the answers, so that they can be useful for other people looking to implement fast polynomials and or AKS test.
n(read: word-size number) and you don’t mind external dependencies,then go fornumpyand usenumpy.convolveorscipy.fftconvolvefor the multiplication. It will be much faster than anything you can write. Unfortunately ifnis not word size you can’t usescipy.fftconvolveat all, and alsonumpy.convolvebecomes slow as hell.numpyarrays to operate efficiently.n, then my solution seems to be the fastest. Even though I did not try to implement fft multiplication between arrays of coefficients in python(which may be faster).