@@ -2183,16 +2183,15 @@ cdef class Polynomial(CommutativeAlgebraElement):
2183
2183
2184
2184
def _mul_karatsuba (self , right , K_threshold = None ):
2185
2185
r """
2186
- Returns the product of two polynomials using the Karatsuba divide
2186
+ Compute the product of two polynomials using the Karatsuba divide
2187
2187
and conquer multiplication algorithm. This is only used over a
2188
2188
generic base ring. ( Special libraries like Flint are used, e. g. , for
2189
2189
the integers and rationals, which are much faster. )
2190
2190
2191
2191
INPUT:
2192
2192
2193
2193
- ``self`` - Polynomial
2194
- - ``right`` - Polynomial ( over same base ring as
2195
- self)
2194
+ - ``right`` - Polynomial ( over same base ring as self)
2196
2195
- ``K_threshold`` - ( optional) Integer. A threshold to fall back to
2197
2196
schoolbook algorithm. In the recursion, if one of the polynomials is
2198
2197
of degree less that K_threshold then the classic quadratic polynomial
@@ -2232,16 +2231,14 @@ cdef class Polynomial(CommutativeAlgebraElement):
2232
2231
2233
2232
g0 + g1* x^ n + g2* x^ {2n} + ... + gq* x^ {nq}
2234
2233
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.
2238
2237
2239
2238
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:
2245
2242
2246
2243
7. 53* n** 1. 59 additions and 1. 46* n** 1. 59 products on the base ring.
2247
2244
@@ -2250,7 +2247,7 @@ cdef class Polynomial(CommutativeAlgebraElement):
2250
2247
2251
2248
8. 11* m** 0. 59* n additions and 1. 56* m** 0. 59* n products.
2252
2249
2253
- Although for bigger degrees the bound might be worse.
2250
+ ( The bound might be worse for larger degrees . )
2254
2251
2255
2252
EXAMPLES::
2256
2253
@@ -6544,13 +6541,16 @@ cdef class Polynomial(CommutativeAlgebraElement):
6544
6541
6545
6542
cdef do_schoolbook_product(x, y):
6546
6543
"""
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.
6552
6546
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::
6554
6554
6555
6555
sage: K = ZZ[x]
6556
6556
sage: f = K.random_element(8)
@@ -6566,10 +6566,10 @@ cdef do_schoolbook_product(x, y):
6566
6566
return y
6567
6567
elif d1 == 0 :
6568
6568
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
6570
6570
elif d2 == 0 :
6571
6571
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
6573
6573
coeffs = []
6574
6574
for k from 0 <= k <= d1+ d2:
6575
6575
start = 0 if k <= d2 else k- d2 # max(0, k-d2)
@@ -6582,8 +6582,9 @@ cdef do_schoolbook_product(x, y):
6582
6582
6583
6583
cdef do_karatsuba_different_size(left, right, Py_ssize_t K_threshold):
6584
6584
"""
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.
6587
6588
6588
6589
INPUT:
6589
6590
@@ -6593,11 +6594,11 @@ cdef do_karatsuba_different_size(left, right, Py_ssize_t K_threshold):
6593
6594
quadratic algorithm. During Karatsuba recursion, if one of the lists
6594
6595
has length <= K_threshold the classical product is used instead.
6595
6596
6596
- This method is indirectly doctested in _mul_karatsuba
6597
+ TESTS:
6597
6598
6598
- We use fibonacci numbers that need deepest recursion in this method .
6599
+ This method is indirectly doctested in _mul_karatsuba .
6599
6600
6600
- TESTS::
6601
+ Here, we use Fibonacci numbers that need deepest recursion in this method.
6601
6602
6602
6603
sage: K = ZZ[x]
6603
6604
sage: f = K.random_element(28)
@@ -6614,14 +6615,14 @@ cdef do_karatsuba_different_size(left, right, Py_ssize_t K_threshold):
6614
6615
return [c* a for a in right]
6615
6616
if m == 1 :
6616
6617
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
6618
6619
if n <= K_threshold or m <= K_threshold:
6619
6620
return do_schoolbook_product(left,right)
6620
6621
if n == m:
6621
6622
return do_karatsuba(left, right, K_threshold, 0 , 0 , n)
6622
6623
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
6625
6626
q = n // m
6626
6627
r = n % m
6627
6628
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):
6660
6661
6661
6662
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):
6662
6663
"""
6663
- Core routine for karatsuba multiplicacion . This function works for two
6664
+ Core routine for Karatsuba multiplication . This function works for two
6664
6665
polynomials of the same degree.
6665
6666
6666
6667
Input:
6667
6668
6668
6669
- left: a list containing a slice representing a polynomial
6669
6670
- right: a list containing the slice representing a polynomial with the
6670
- same length of left
6671
+ same length as left
6671
6672
- K_threshold: an integer. For lists of length <= K_threshold, the
6672
6673
quadratic polynomial multiplication is used.
6673
6674
- start_l: the index of left where the actual polynomial starts
6674
6675
- 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 .
6676
6677
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.
6679
6682
6680
6683
Output:
6681
6684
6682
6685
- a list representing the product of the polynomials
6683
6686
6684
6687
Doctested indirectly in _mul_karatsuba
6685
6688
6686
- We use this representation in order to avoid creating slices of lists and
6687
- create smaller lists.
6688
-
6689
- TESTS::
6689
+ TESTS::
6690
6690
6691
6691
sage: K.<x> = ZZ[]
6692
6692
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
6707
6707
if num_elts == 1 :
6708
6708
return [left[start_l]* right[start_r]]
6709
6709
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
6711
6711
if num_elts == 2 :
6712
6712
b = left[start_l]
6713
6713
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
6716
6716
return [b* d, a* d+ b* c, a* c]
6717
6717
return do_schoolbook_product(left[start_l:start_l+ num_elts], right[start_r:start_r+ num_elts])
6718
6718
if num_elts == 2 :
6719
- # beware of noncommutative rings
6719
+ # beware of noncommutative rings
6720
6720
b = left[start_l]
6721
6721
a = left[start_l+ 1 ]
6722
6722
d = right[start_r]
0 commit comments