|
7 | 7 | #include <vector> |
8 | 8 |
|
9 | 9 | namespace weilycoder { |
| 10 | +/** |
| 11 | + * @brief Multiply two polynomials using the Karatsuba algorithm. |
| 12 | + * @tparam InputIt Iterator type for input polynomials. |
| 13 | + * @tparam OutputIt Iterator type for output polynomial. |
| 14 | + * @tparam Threshold Size threshold to switch to standard multiplication. |
| 15 | + * @param a_begin Iterator to the beginning of the first polynomial. |
| 16 | + * @param a_end Iterator to the end of the first polynomial. |
| 17 | + * @param b_begin Iterator to the beginning of the second polynomial. |
| 18 | + * @param b_end Iterator to the end of the second polynomial. |
| 19 | + * @param result_begin Iterator to the beginning of the result polynomial. |
| 20 | + */ |
10 | 21 | template <typename InputIt, typename OutputIt, size_t Threshold = 32> |
11 | 22 | void karatsuba_multiply(InputIt a_begin, InputIt a_end, InputIt b_begin, InputIt b_end, |
12 | 23 | OutputIt result_begin) { |
@@ -90,10 +101,21 @@ void karatsuba_multiply(InputIt a_begin, InputIt a_end, InputIt b_begin, InputIt |
90 | 101 | } |
91 | 102 | } |
92 | 103 |
|
93 | | -template <typename T> |
| 104 | +/** |
| 105 | + * @brief Multiply two polynomials using the Karatsuba algorithm. |
| 106 | + * @tparam T Coefficient type of the polynomials. |
| 107 | + * @tparam Threshold Size threshold to switch to standard multiplication. |
| 108 | + * @param a First polynomial coefficients. |
| 109 | + * @param b Second polynomial coefficients. |
| 110 | + * @return Resulting polynomial coefficients after multiplication. |
| 111 | + */ |
| 112 | +template <typename T, size_t Threshold = 32> |
94 | 113 | std::vector<T> karatsuba_multiply(const std::vector<T> &a, const std::vector<T> &b) { |
| 114 | + using I_It = typename std::vector<T>::const_iterator; |
| 115 | + using O_It = typename std::vector<T>::iterator; |
95 | 116 | std::vector<T> result(a.size() + b.size() - 1); |
96 | | - karatsuba_multiply(a.begin(), a.end(), b.begin(), b.end(), result.begin()); |
| 117 | + karatsuba_multiply<I_It, O_It, Threshold>(a.begin(), a.end(), b.begin(), b.end(), |
| 118 | + result.begin()); |
97 | 119 | return result; |
98 | 120 | } |
99 | 121 | } // namespace weilycoder |
|
0 commit comments