This repository has been archived on 2025-04-04. You can view files and clone it, but cannot push or open issues or pull requests.
mathematical-algorithms-cpp/src/karatsuba.cpp

267 lines
7.2 KiB
C++
Raw Normal View History

2025-03-24 21:14:35 +09:00
#include <algorithm>
2025-03-23 23:14:25 +09:00
#include <chrono>
#include <complex>
2025-03-23 21:28:03 +09:00
#include <iostream>
2025-03-23 23:14:25 +09:00
#include <vector>
2025-03-23 21:28:03 +09:00
using namespace std;
2025-03-23 23:14:25 +09:00
typedef complex<double> Complex;
typedef double Real;
/**
* Operator overloading for printing vectors.
* @tparam T
* @param os
* @param v
* @return
*/
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (size_t i = 0; i < v.size(); i++) {
os << v[i] << " ";
}
return os;
}
2025-03-24 21:14:35 +09:00
template <typename T> ostream &operator<<(ostream &os, const span<T> &v) {
for (auto x : v) {
os << x << " ";
}
return os;
}
2025-03-24 08:37:00 +00:00
vector<int> random_int_vector(size_t size) {
auto result = vector<int>();
for (size_t i = 0; i < size; i++) {
result.push_back(rand() % 100);
}
return result;
}
2025-03-24 16:41:47 +09:00
2025-03-24 16:30:01 +09:00
vector<Real> random_real_vector(size_t size) {
2025-03-23 23:14:25 +09:00
auto result = vector<Real>();
2025-03-24 08:37:00 +00:00
for (size_t i = 0; i < size; i++) {
2025-03-23 23:14:25 +09:00
result.push_back((double)rand() / (double)RAND_MAX);
}
return result;
}
vector<Complex> vector_as_complex(vector<Real> &v) {
auto result = vector<Complex>(v.size());
for (size_t i = 0; i < v.size(); i++) {
result[i] = Complex(v[i], 0);
}
return result;
}
template <typename R> vector<R> poly_add(vector<R> &a, vector<R> &b) {
auto res = vector<R>(max(a.size(), b.size()));
for (size_t i = 0; i < a.size(); i++) {
res[i] = a[i];
}
for (size_t i = 0; i < b.size(); i++) {
res[i] += b[i];
}
return res;
}
template <typename R> vector<R> poly_sub(vector<R> &a, vector<R> &b) {
auto res = vector<R>(max(a.size(), b.size()));
for (size_t i = 0; i < a.size(); i++) {
res[i] = a[i];
}
for (size_t i = 0; i < b.size(); i++) {
res[i] -= b[i];
}
return res;
}
2025-03-24 19:35:24 +09:00
// Add polynomials in-place assuming the size is allocated
template <typename R>
2025-03-24 20:09:11 +09:00
void add_inplace(span<R> &a, span<R> &b, span<R> &result) {
2025-03-24 19:35:24 +09:00
for (size_t i = 0; i < a.size(); i++)
result[i] = a[i] + b[i];
}
// Subtract polynomials in-place assuming the size is allocated
template <typename R>
2025-03-24 20:09:11 +09:00
void sub_inplace(span<R> &a, span<R> &b, span<R> &result) {
2025-03-24 19:35:24 +09:00
for (size_t i = 0; i < a.size(); i++)
result[i] = a[i] - b[i];
}
2025-03-25 18:25:23 +09:00
template <typename R>
vector<R> poly_normalize(vector<R> &a) {
int i;
for (i = a.size() - 1; i >= 0; i--) {
if (a[i] != 0)
break;
}
return vector(a.begin(), a.begin() + i + 1);
}
2025-03-23 23:14:25 +09:00
// Basic polynomial multiplication
template <typename R> vector<R> poly_mult_basic(vector<R> &a, vector<R> &b) {
if (a.size() == 0 && b.size() == 0)
return vector<R>(0);
auto res = vector<R>(a.size() + b.size() - 1, 0);
for (size_t i = 0; i < a.size(); i++) {
// Start with i 0s
auto tmp = vector<R>(i, 0);
for (R bj : b) {
tmp.push_back(a[i] * bj);
}
res = poly_add(res, tmp);
}
return res;
}
2025-03-24 21:14:35 +09:00
#define THRESHOLD 32
2025-03-23 23:20:13 +09:00
// TODO Reduce allocations
2025-03-23 23:14:25 +09:00
/**
* A step of the Karatsuba function.
* @param deg_bnd power-of-2 degree bound
2025-03-24 21:14:35 +09:00
* @param buffer the buffer which is used only throughout the invocation
2025-03-23 23:14:25 +09:00
*/
template <typename R>
2025-03-24 21:14:35 +09:00
void poly_mult_Karatsuba_step(const size_t deg_bnd, span<R> &a, span<R> &b,
span<R> &result, span<R> &buffer) {
2025-03-24 19:35:24 +09:00
if (deg_bnd <= THRESHOLD) {
auto vec_a = vector(a.begin(), a.end());
auto vec_b = vector(b.begin(), b.end());
2025-03-24 21:14:35 +09:00
auto result_vec = poly_mult_basic(vec_a, vec_b);
copy(result_vec.begin(), result_vec.end(), result.begin());
return;
2025-03-24 19:35:24 +09:00
}
2025-03-23 23:14:25 +09:00
const auto next_bnd = deg_bnd >> 1;
2025-03-24 21:14:35 +09:00
auto a0 = a.subspan(0, next_bnd);
auto a1 = a.subspan(next_bnd, next_bnd);
auto b0 = b.subspan(0, next_bnd);
auto b1 = b.subspan(next_bnd, next_bnd);
auto a01 = buffer.subspan(0, next_bnd);
auto b01 = buffer.subspan(next_bnd, next_bnd);
auto prod0 = result.subspan(0, deg_bnd);
auto prod1 = result.subspan(deg_bnd, deg_bnd);
auto prod_add = buffer.subspan(2 * next_bnd, deg_bnd);
// Buffer which does not overlap with currently used memory
2025-03-25 18:25:23 +09:00
// auto buffer_next = buffer.subspan(4 * next_bnd, 4 * next_bnd);
2025-03-24 21:14:35 +09:00
auto new_buffer_vec = vector<R>(4 * next_bnd);
auto new_buffer = span(new_buffer_vec);
2025-03-25 18:25:23 +09:00
auto new_buffer_2 = vector<R>(4 * next_bnd);
auto new_buffer_2 = span(new_buffer_2)
2025-03-24 21:14:35 +09:00
// correctly put into prod0 and prod1 position
poly_mult_Karatsuba_step(next_bnd, a0, b0, prod0, new_buffer); // need new_buffer to avoid rewrite; why?
2025-03-25 18:25:23 +09:00
poly_mult_Karatsuba_step(next_bnd, a1, b1, prod1, new_buffer);
2025-03-24 21:14:35 +09:00
add_inplace(a0, a1, a01);
add_inplace(b0, b1, b01);
2025-03-25 18:25:23 +09:00
poly_mult_Karatsuba_step(next_bnd, a01, b01, prod_add, new_buffer);
2025-03-24 20:09:11 +09:00
// adjust prod_add
2025-03-24 21:14:35 +09:00
sub_inplace(prod_add, prod0, prod_add);
sub_inplace(prod_add, prod1, prod_add);
2025-03-24 20:09:11 +09:00
// Add middle term at X^next_bnd position
2025-03-24 21:14:35 +09:00
auto result_mid = result.subspan(next_bnd, deg_bnd);
add_inplace(prod_add, result_mid, result_mid);
2025-03-23 23:14:25 +09:00
}
template <typename R>
vector<R> poly_mult_Karatsuba(vector<R> &a, vector<R> &b) {
size_t deg_bound = 1;
while (deg_bound < max(a.size(), b.size()))
deg_bound = deg_bound << 1;
2025-03-24 20:09:11 +09:00
a.resize(deg_bound);
b.resize(deg_bound);
2025-03-24 21:14:35 +09:00
auto result = vector<R>(deg_bound << 1);
auto buffer = vector<R>(deg_bound * 4);
2025-03-24 19:35:24 +09:00
auto span_a = span(a);
auto span_b = span(b);
2025-03-24 21:14:35 +09:00
auto span_result = span(result);
auto span_buffer = span(buffer);
poly_mult_Karatsuba_step(deg_bound, span_a, span_b, span_result, span_buffer);
return result;
2025-03-23 23:14:25 +09:00
}
void basic_vs_Karatsuba(size_t size) {
2025-03-24 08:37:00 +00:00
auto p = random_int_vector(size);
auto q = random_int_vector(size);
2025-03-23 23:14:25 +09:00
cout << "Degree " << size - 1 << endl;
auto begin = chrono::high_resolution_clock::now();
2025-03-25 18:25:23 +09:00
auto basic = poly_mult_basic(p, q);
2025-03-23 23:14:25 +09:00
auto end = chrono::high_resolution_clock::now();
auto spent = chrono::duration<double>(end - begin);
cout << "Basic took " << spent.count() << "s" << endl;
begin = chrono::high_resolution_clock::now();
2025-03-25 18:25:23 +09:00
auto karat = poly_mult_Karatsuba(p, q);
2025-03-23 23:14:25 +09:00
end = chrono::high_resolution_clock::now();
spent = chrono::duration<double>(end - begin);
cout << "Karatsuba took " << spent.count() << "s" << endl;
2025-03-25 18:25:23 +09:00
if (basic != karat)
cout << "Mismatch" << endl;
2025-03-23 23:14:25 +09:00
}
2025-03-24 21:14:35 +09:00
void only_Karatsuba(size_t size) {
auto p = random_int_vector(size);
auto q = random_int_vector(size);
cout << "Degree " << size - 1 << endl;
auto begin = chrono::high_resolution_clock::now();
poly_mult_Karatsuba(p, q);
auto end = chrono::high_resolution_clock::now();
auto spent = chrono::duration<double>(end - begin);
cout << "Karatsuba took " << spent.count() << "s" << endl;
}
2025-03-23 21:28:03 +09:00
int main() {
2025-03-23 23:14:25 +09:00
{
2025-03-24 16:30:01 +09:00
auto p = vector<int>{1, 2};
auto q = vector<int>{3, 4, 5};
2025-03-23 23:14:25 +09:00
cout << "P: " << p << endl;
cout << "Q: " << q << endl;
cout << "P + Q: " << poly_add(p, q) << endl;
cout << "basic P * Q: " << poly_mult_basic(p, q) << endl;
2025-03-25 18:25:23 +09:00
auto karat = poly_mult_Karatsuba(p, q);
cout << "Karatsuba P * Q: " << karat << endl;
cout << "normalized: " << poly_normalize(karat) << endl;
2025-03-23 23:14:25 +09:00
cout << endl;
}
{
2025-03-24 08:37:00 +00:00
auto p = random_int_vector(6);
auto q = random_int_vector(8);
2025-03-23 23:14:25 +09:00
cout << "P: " << p << endl;
cout << "Q: " << q << endl;
cout << "basic P * Q: " << poly_mult_basic(p, q) << endl;
cout << "Karatsuba P * Q: " << poly_mult_Karatsuba(p, q) << endl;
}
2025-03-24 16:30:01 +09:00
basic_vs_Karatsuba(128);
basic_vs_Karatsuba(256);
basic_vs_Karatsuba(512);
basic_vs_Karatsuba(1024);
basic_vs_Karatsuba(2048);
basic_vs_Karatsuba(4096);
basic_vs_Karatsuba(8192);
2025-03-24 19:35:24 +09:00
basic_vs_Karatsuba(16384);
2025-03-24 21:14:35 +09:00
only_Karatsuba(32768);
only_Karatsuba(65536);
only_Karatsuba(131072);
2025-03-23 23:14:25 +09:00
return 0;
2025-03-23 21:28:03 +09:00
}