Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit f6cd17e

Browse files
committedDec 21, 2013
Improved Karatsuba: cosmetic changes to docstrings and comments
1 parent 4d08ddf commit f6cd17e

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed
 

‎src/sage/rings/polynomial/polynomial_element.pyx

+39-39
Original file line numberDiff line numberDiff line change
@@ -2183,16 +2183,15 @@ cdef class Polynomial(CommutativeAlgebraElement):
21832183

21842184
def _mul_karatsuba(self, right, K_threshold = None):
21852185
r"""
2186-
Returns the product of two polynomials using the Karatsuba divide
2186+
Compute the product of two polynomials using the Karatsuba divide
21872187
and conquer multiplication algorithm. This is only used over a
21882188
generic base ring. (Special libraries like Flint are used, e.g., for
21892189
the integers and rationals, which are much faster.)
21902190
21912191
INPUT:
21922192
21932193
- ``self`` - Polynomial
2194-
- ``right`` - Polynomial (over same base ring as
2195-
self)
2194+
- ``right`` - Polynomial (over same base ring as self)
21962195
- ``K_threshold`` - (optional) Integer. A threshold to fall back to
21972196
schoolbook algorithm. In the recursion, if one of the polynomials is
21982197
of degree less that K_threshold then the classic quadratic polynomial
@@ -2232,16 +2231,14 @@ cdef class Polynomial(CommutativeAlgebraElement):
22322231
22332232
g0 + g1*x^n +g2*x^{2n} + ... + gq*x^{nq}
22342233
2235-
where `gi` are polynomials of degree n, `gq`of degree <= n.
2236-
Then compute each product `gi*right` with karatsuba multiplication and
2237-
reconstruct `self*right` from the partial products.
2234+
where `gi` are polynomials of degree <= n. We then compute each product
2235+
`gi*right` with Karatsuba multiplication and reconstruct `self*right`
2236+
from the partial products.
22382237
22392238
The theoretical complexity for multiplying two polynomials of the same
2240-
degree n is O(n^log(3,2))
2241-
2242-
Through testing of polynomials of degree up to 5000 we get that the
2243-
number of operations for two polynomials of degree up to n-1 is bounded
2244-
by:
2239+
degree n is O(n^log(3,2)). Through testing of polynomials of degree up
2240+
to 5000 we get that the number of operations for two polynomials of
2241+
degree up to n-1 is bounded by:
22452242
22462243
7.53*n**1.59 additions and 1.46*n**1.59 products on the base ring.
22472244
@@ -2250,7 +2247,7 @@ cdef class Polynomial(CommutativeAlgebraElement):
22502247
22512248
8.11*m**0.59*n additions and 1.56*m**0.59*n products.
22522249
2253-
Although for bigger degrees the bound might be worse.
2250+
(The bound might be worse for larger degrees.)
22542251
22552252
EXAMPLES::
22562253
@@ -6544,13 +6541,16 @@ cdef class Polynomial(CommutativeAlgebraElement):
65446541

65456542
cdef do_schoolbook_product(x, y):
65466543
"""
6547-
Method to compute the multiplication of two polynomials represented
6548-
by lists. This is the core of _mul_generic and the code that is used
6549-
by _mul_karatsuba bellow a threshold.
6550-
Doctested indirectly in _mul_generic and _mul_karatsuba. For the doctest we
6551-
use a ring such that default multiplication calls external libraries.
6544+
Compute the multiplication of two polynomials represented by lists, using
6545+
the schoolbook algorithm.
65526546
6553-
TESTS::
6547+
This is the core of _mul_generic and the code that is used by
6548+
_mul_karatsuba bellow a threshold.
6549+
6550+
TESTS:
6551+
6552+
Doctested indirectly in _mul_generic and _mul_karatsuba. For the doctest we
6553+
use a ring such that default multiplication calls external libraries::
65546554
65556555
sage: K = ZZ[x]
65566556
sage: f = K.random_element(8)
@@ -6566,10 +6566,10 @@ cdef do_schoolbook_product(x, y):
65666566
return y
65676567
elif d1 == 0:
65686568
c = x[0]
6569-
return [c*a for a in y] #beware of noncommutative rings
6569+
return [c*a for a in y] # beware of noncommutative rings
65706570
elif d2 == 0:
65716571
c = y[0]
6572-
return [a*c for a in x] #beware of noncommutative rings
6572+
return [a*c for a in x] # beware of noncommutative rings
65736573
coeffs = []
65746574
for k from 0 <= k <= d1+d2:
65756575
start = 0 if k <= d2 else k-d2 # max(0, k-d2)
@@ -6582,8 +6582,9 @@ cdef do_schoolbook_product(x, y):
65826582

65836583
cdef do_karatsuba_different_size(left, right, Py_ssize_t K_threshold):
65846584
"""
6585-
This algorithm is to deal with karatsuba multiplication of two polynomials
6586-
of different degree as explained in _mul_karatsuba.
6585+
Multiply two polynomials of different degrees by splitting the one of
6586+
largest degree in chunks that are multiplied with the other using the
6587+
Karatsuba algorithm, as explained in _mul_karatsuba.
65876588
65886589
INPUT:
65896590
@@ -6593,11 +6594,11 @@ cdef do_karatsuba_different_size(left, right, Py_ssize_t K_threshold):
65936594
quadratic algorithm. During Karatsuba recursion, if one of the lists
65946595
has length <= K_threshold the classical product is used instead.
65956596
6596-
This method is indirectly doctested in _mul_karatsuba
6597+
TESTS:
65976598
6598-
We use fibonacci numbers that need deepest recursion in this method.
6599+
This method is indirectly doctested in _mul_karatsuba.
65996600
6600-
TESTS::
6601+
Here, we use Fibonacci numbers that need deepest recursion in this method.
66016602
66026603
sage: K = ZZ[x]
66036604
sage: f = K.random_element(28)
@@ -6614,14 +6615,14 @@ cdef do_karatsuba_different_size(left, right, Py_ssize_t K_threshold):
66146615
return [c*a for a in right]
66156616
if m == 1:
66166617
c = right[0]
6617-
return [a*c for a in left] #beware of noncommutative rings
6618+
return [a*c for a in left] # beware of noncommutative rings
66186619
if n <= K_threshold or m <= K_threshold:
66196620
return do_schoolbook_product(left,right)
66206621
if n == m:
66216622
return do_karatsuba(left, right, K_threshold, 0, 0, n)
66226623
if n > m:
6623-
#left is the bigger list
6624-
#n is the bigger number
6624+
# left is the bigger list
6625+
# n is the bigger number
66256626
q = n // m
66266627
r = n % m
66276628
output = do_karatsuba(left, right, K_threshold, 0, 0, m)
@@ -6660,33 +6661,32 @@ cdef do_karatsuba_different_size(left, right, Py_ssize_t K_threshold):
66606661

66616662
cdef do_karatsuba(left, right, Py_ssize_t K_threshold,Py_ssize_t start_l, Py_ssize_t start_r,Py_ssize_t num_elts):
66626663
"""
6663-
Core routine for karatsuba multiplicacion. This function works for two
6664+
Core routine for Karatsuba multiplication. This function works for two
66646665
polynomials of the same degree.
66656666
66666667
Input:
66676668
66686669
- left: a list containing a slice representing a polynomial
66696670
- right: a list containing the slice representing a polynomial with the
6670-
same length of left
6671+
same length as left
66716672
- K_threshold: an integer. For lists of length <= K_threshold, the
66726673
quadratic polynomial multiplication is used.
66736674
- start_l: the index of left where the actual polynomial starts
66746675
- start_r: the index of right where the actual polynomial starts
6675-
- num_elts: the length of the polynomial.
6676+
- num_elts: the length of the polynomials.
66766677
6677-
Thus, the actual polynomial we want to multiply are represented by the
6678-
slices: left[ start_l: start_l+num_elts ], left[ right_l: right_l+num_elts ]
6678+
Thus, the actual polynomials we want to multiply are represented by the
6679+
slices: left[ start_l: start_l+num_elts ], left[ right_l: right_l+num_elts ].
6680+
We use this representation in order to avoid creating slices of lists and
6681+
create smaller lists.
66796682
66806683
Output:
66816684
66826685
- a list representing the product of the polynomials
66836686
66846687
Doctested indirectly in _mul_karatsuba
66856688
6686-
We use this representation in order to avoid creating slices of lists and
6687-
create smaller lists.
6688-
6689-
TESTS::
6689+
TESTS::
66906690
66916691
sage: K.<x> = ZZ[]
66926692
sage: f = K.random_element(50) + x^51
@@ -6707,7 +6707,7 @@ cdef do_karatsuba(left, right, Py_ssize_t K_threshold,Py_ssize_t start_l, Py_ssi
67076707
if num_elts == 1:
67086708
return [left[start_l]*right[start_r]]
67096709
if num_elts <= K_threshold:
6710-
#Special case of degree 2, no loop, no function call
6710+
# Special case of degree 2, no loop, no function call
67116711
if num_elts == 2:
67126712
b = left[start_l]
67136713
a = left[start_l+1]
@@ -6716,7 +6716,7 @@ cdef do_karatsuba(left, right, Py_ssize_t K_threshold,Py_ssize_t start_l, Py_ssi
67166716
return [b*d, a*d+b*c, a*c]
67176717
return do_schoolbook_product(left[start_l:start_l+num_elts], right[start_r:start_r+num_elts])
67186718
if num_elts == 2:
6719-
#beware of noncommutative rings
6719+
# beware of noncommutative rings
67206720
b = left[start_l]
67216721
a = left[start_l+1]
67226722
d = right[start_r]

0 commit comments

Comments
 (0)