Compare commits
4 commits
8c434b7e50
...
e5cbdc825d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5cbdc825d | ||
|
|
b2e7b3d070 | ||
|
|
a3c8969b23 | ||
|
|
a523defb6b |
6 changed files with 116 additions and 15 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -1,3 +1,5 @@
|
||||||
build
|
build
|
||||||
|
cmake-build-debug
|
||||||
|
cmake-build-release
|
||||||
.cache
|
.cache
|
||||||
|
.idea
|
||||||
|
|
|
||||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
# 디폴트 무시된 파일
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 에디터 기반 HTTP 클라이언트 요청
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
|
@ -4,8 +4,10 @@ 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)
|
add_compile_options(-Wall -Wextra -Wpedantic -O2 -funroll-loops)
|
||||||
|
|
||||||
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,7 +21,8 @@ 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);
|
||||||
|
|
@ -84,6 +85,7 @@ 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
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -93,7 +95,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 <= 1)
|
if (deg_bnd <= THRESHOLD)
|
||||||
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;
|
||||||
|
|
@ -148,8 +150,8 @@ void basic_vs_Karatsuba(size_t size) {
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
{
|
{
|
||||||
auto p = vector<Real>{1.0, 2.0};
|
auto p = vector<int>{1, 2};
|
||||||
auto q = vector<Real>{3.0, 4.0, 5.0};
|
auto q = vector<int>{3, 4, 5};
|
||||||
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;
|
||||||
|
|
@ -167,13 +169,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(125);
|
basic_vs_Karatsuba(128);
|
||||||
basic_vs_Karatsuba(250);
|
basic_vs_Karatsuba(256);
|
||||||
basic_vs_Karatsuba(500);
|
basic_vs_Karatsuba(512);
|
||||||
basic_vs_Karatsuba(1000);
|
basic_vs_Karatsuba(1024);
|
||||||
basic_vs_Karatsuba(2000);
|
basic_vs_Karatsuba(2048);
|
||||||
// Approaches at this point
|
basic_vs_Karatsuba(4096);
|
||||||
basic_vs_Karatsuba(4000);
|
basic_vs_Karatsuba(8192);
|
||||||
|
|
||||||
// {
|
// {
|
||||||
// auto p = random_real_vector(4000);
|
// auto p = random_real_vector(4000);
|
||||||
11
src/pseudorandom.cpp
Normal file
11
src/pseudorandom.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#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;
|
||||||
|
}
|
||||||
76
src/types.cpp
Normal file
76
src/types.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* 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