Compare commits
No commits in common. "e5cbdc825d7620ab99f7a42fddbd279680d86f1c" and "8c434b7e50a7d7e60673bfb9a67daf36b988560d" have entirely different histories.
e5cbdc825d
...
8c434b7e50
6 changed files with 15 additions and 116 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -1,5 +1,3 @@
|
||||||
build
|
build
|
||||||
cmake-build-debug
|
|
||||||
cmake-build-release
|
|
||||||
.cache
|
.cache
|
||||||
.idea
|
|
||||||
|
|
|
||||||
8
.idea/.gitignore
generated
vendored
8
.idea/.gitignore
generated
vendored
|
|
@ -1,8 +0,0 @@
|
||||||
# 디폴트 무시된 파일
|
|
||||||
/shelf/
|
|
||||||
/workspace.xml
|
|
||||||
# 에디터 기반 HTTP 클라이언트 요청
|
|
||||||
/httpRequests/
|
|
||||||
# Datasource local storage ignored files
|
|
||||||
/dataSources/
|
|
||||||
/dataSources.local.xml
|
|
||||||
|
|
@ -4,10 +4,8 @@ project(mathematical-algorithms)
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED true)
|
set(CMAKE_CXX_STANDARD_REQUIRED true)
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -funroll-loops")
|
|
||||||
|
|
||||||
add_compile_options(-Wall -Wextra -Wpedantic -O2 -funroll-loops)
|
add_compile_options(-Wall -Wextra -Wpedantic)
|
||||||
|
|
||||||
|
add_executable(mathematical-algorithms src/main.cpp)
|
||||||
|
|
||||||
add_executable(karatsuba src/karatsuba.cpp)
|
|
||||||
add_executable(pseudorandom src/pseudorandom.cpp)
|
|
||||||
add_executable(types src/types.cpp)
|
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,7 @@ template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<Real> random_real_vector(int size) {
|
||||||
vector<Real> random_real_vector(size_t size) {
|
|
||||||
auto result = vector<Real>();
|
auto result = vector<Real>();
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
result.push_back((double)rand() / (double)RAND_MAX);
|
result.push_back((double)rand() / (double)RAND_MAX);
|
||||||
|
|
@ -85,7 +84,6 @@ template <typename R> vector<R> poly_mult_basic(vector<R> &a, vector<R> &b) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define THRESHOLD 1
|
|
||||||
// TODO Reduce allocations
|
// TODO Reduce allocations
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -95,7 +93,7 @@ template <typename R> vector<R> poly_mult_basic(vector<R> &a, vector<R> &b) {
|
||||||
template <typename R>
|
template <typename R>
|
||||||
vector<R> poly_mult_Karatsuba_step(const size_t deg_bnd, vector<R> &a,
|
vector<R> poly_mult_Karatsuba_step(const size_t deg_bnd, vector<R> &a,
|
||||||
vector<R> &b) {
|
vector<R> &b) {
|
||||||
if (deg_bnd <= THRESHOLD)
|
if (deg_bnd <= 1)
|
||||||
return poly_mult_basic(a, b);
|
return poly_mult_basic(a, b);
|
||||||
|
|
||||||
const auto next_bnd = deg_bnd >> 1;
|
const auto next_bnd = deg_bnd >> 1;
|
||||||
|
|
@ -150,8 +148,8 @@ void basic_vs_Karatsuba(size_t size) {
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
{
|
{
|
||||||
auto p = vector<int>{1, 2};
|
auto p = vector<Real>{1.0, 2.0};
|
||||||
auto q = vector<int>{3, 4, 5};
|
auto q = vector<Real>{3.0, 4.0, 5.0};
|
||||||
cout << "P: " << p << endl;
|
cout << "P: " << p << endl;
|
||||||
cout << "Q: " << q << endl;
|
cout << "Q: " << q << endl;
|
||||||
cout << "P + Q: " << poly_add(p, q) << endl;
|
cout << "P + Q: " << poly_add(p, q) << endl;
|
||||||
|
|
@ -169,13 +167,13 @@ int main() {
|
||||||
cout << "Karatsuba P * Q: " << poly_mult_Karatsuba(p, q) << endl;
|
cout << "Karatsuba P * Q: " << poly_mult_Karatsuba(p, q) << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
basic_vs_Karatsuba(128);
|
basic_vs_Karatsuba(125);
|
||||||
basic_vs_Karatsuba(256);
|
basic_vs_Karatsuba(250);
|
||||||
basic_vs_Karatsuba(512);
|
basic_vs_Karatsuba(500);
|
||||||
basic_vs_Karatsuba(1024);
|
basic_vs_Karatsuba(1000);
|
||||||
basic_vs_Karatsuba(2048);
|
basic_vs_Karatsuba(2000);
|
||||||
basic_vs_Karatsuba(4096);
|
// Approaches at this point
|
||||||
basic_vs_Karatsuba(8192);
|
basic_vs_Karatsuba(4000);
|
||||||
|
|
||||||
// {
|
// {
|
||||||
// auto p = random_real_vector(4000);
|
// auto p = random_real_vector(4000);
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
#include <cstdint>
|
|
||||||
#include <iostream>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
uint16_t linear_congruence(uint16_t a, uint16_t c, uint16_t prev) {
|
|
||||||
return a * prev + c;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
cout << "Hello World!" << endl;
|
|
||||||
}
|
|
||||||
|
|
@ -1,76 +0,0 @@
|
||||||
/*
|
|
||||||
* Until next time, let's boilerplate later
|
|
||||||
*/
|
|
||||||
#include <cmath>
|
|
||||||
#include <iostream>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
template<typename R>
|
|
||||||
struct rational {
|
|
||||||
R num;
|
|
||||||
R denom;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename R>
|
|
||||||
rational<R> operator+(rational<R> &l, rational<R> &r) {
|
|
||||||
return rational(l.num * r.denom + r.num * l.denom, l.denom * r.denom);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename R>
|
|
||||||
rational<R> operator-(rational<R> &l, rational<R> &r) {
|
|
||||||
return rational(l.num * r.denom - r.num * l.denom, l.denom * r.denom);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename R>
|
|
||||||
rational<R> operator*(rational<R> &l, rational<R> &r) {
|
|
||||||
return rational(l.num * r.num, l.denom * r.denom);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename R>
|
|
||||||
rational<R> operator/(rational<R> &l, rational<R> &r) {
|
|
||||||
return rational(l.num * r.denom, r.denom * l.num);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO Take care of normalization
|
|
||||||
template<typename R>
|
|
||||||
bool operator==(rational<R> &l, rational<R> &r) {
|
|
||||||
return l.num == r.num && l.denom == r.denom;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename R>
|
|
||||||
bool operator!=(rational<R> &l, rational<R> &r) {
|
|
||||||
return l.num != r.num || l.denom != r.denom;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
template<typename R>
|
|
||||||
ostream operator<< (ostream &os, rational<R> &r) {
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
template<unsigned int>
|
|
||||||
struct prime_field {
|
|
||||||
size_t number;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<unsigned int p>
|
|
||||||
prime_field<p> operator+(const prime_field<p> &l, const prime_field<p> &r) {
|
|
||||||
return prime_field(l.number + r.number % p);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<unsigned int p>
|
|
||||||
prime_field<p> operator-(const prime_field<p> &l, const prime_field<p> &r) {
|
|
||||||
return prime_field(l.number - r.number % p);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<unsigned int p>
|
|
||||||
prime_field<p> operator*(const prime_field<p> &l, const prime_field<p> &r) {
|
|
||||||
return prime_field(l.number * r.number % p);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO /, ==, !=
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
cout << "Hello World!" << endl;
|
|
||||||
}
|
|
||||||
Reference in a new issue