-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: vector algebra (for parallelization)
- Loading branch information
1 parent
d725366
commit 987815d
Showing
15 changed files
with
239 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
add_library(generator_lib | ||
genarray.cc | ||
genvector.cc | ||
genstring.cc | ||
genlist.cc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "genarray.h" | ||
|
||
double * | ||
GenArray::RandomArray(int n, int m) { | ||
std::random_device rd; | ||
std::mt19937 gen(rd()); | ||
std::uniform_real_distribution<> dis(0, m); | ||
double *arr = new double[n]; | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = dis(gen); | ||
} | ||
return arr; | ||
} | ||
|
||
double * | ||
GenArray::ZeroArray(int n) { | ||
double *arr = new double[n]; | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = 0; | ||
} | ||
return arr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef __GENERATOR_GENARRAY_H__ | ||
#define __GENERATOR_GENARRAY_H__ | ||
|
||
#include <random> | ||
|
||
namespace GenArray { | ||
|
||
double *RandomArray(int n, int m); | ||
double *ZeroArray(int n); | ||
|
||
}; // namespace GenArray | ||
|
||
#endif // __GENERATOR_GENARRAY_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
#ifndef __GENERATOR_GENARRAY_H__ | ||
#define __GENERATOR_GENARRAY_H__ | ||
#ifndef __GENERATOR_GENVECTOR_H__ | ||
#define __GENERATOR_GENVECTOR_H__ | ||
|
||
#include <random> | ||
#include <vector> | ||
|
||
namespace GenVector { | ||
|
||
std::vector<int> RandomVector(int n, int m); | ||
std::vector<double> RandomVectorDouble(int n, int l, int u); | ||
std::vector<std::vector<int>> RandomSquareMatrix(int n, int m); | ||
|
||
}; // namespace GenVector | ||
|
||
#endif // __GENERATOR_GENARRAY_H__ | ||
#endif // __GENERATOR_GENVECTOR_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
add_library(math_lib | ||
vectoralgebra.cc | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "vectoralgebra.h" | ||
|
||
#include <cmath> | ||
|
||
/** @brief solve a cubic equation | ||
* | ||
* @param a Coefficient of x^3 | ||
* @param b Coefficient of x^2 | ||
* @param c Coefficient of x | ||
* @param d Constant term | ||
* @param x Vector of x values (input) | ||
* | ||
* @return Vector of y values (output) | ||
*/ | ||
std::vector<double> | ||
MathVectorAlgebra::Cubic(int a, int b, int c, int d, std::vector<double> &x) { | ||
int n = x.size(); | ||
std::vector<double> y = std::vector<double>(n); | ||
for (int i = 0; i < n; i += 1) { | ||
y[i] = a * x[i] * x[i] * x[i] + b * x[i] * x[i] + c * x[i] + d; | ||
} | ||
return y; | ||
} | ||
|
||
/** @brief convolve vector x with smaller vector h | ||
* | ||
* offset defined such that h[offset] is the center of h | ||
* | ||
* @param offset Offset of h | ||
* @param h Pointer to array of h values (input) | ||
* @param x Pointer to array of x values (input) | ||
* | ||
* @return Pointer to array of y values (output) | ||
*/ | ||
std::vector<double> | ||
MathVectorAlgebra::Convolve(int offset, std::vector<double> &h, std::vector<double> &x) { | ||
int m = h.size(); | ||
int n = x.size(); | ||
int sum_h = 0; | ||
std::vector<double> y = std::vector<double>(n); | ||
for (int i = 0; i < m; i += 1) { | ||
sum_h += std::abs(h[i]); | ||
} | ||
if (sum_h == 0) { | ||
sum_h = 1; | ||
} | ||
for (int i = 0; i < n; i += 1) { | ||
y[i] = 0; | ||
for (int j = 0; j < m; j += 1) { | ||
if (i - offset + j >= 0 && i - offset + j < n) { | ||
y[i] += h[j] * x[i - offset + j] / sum_h; | ||
} | ||
} | ||
} | ||
return y; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef __MATH_VECTORALGEBRA_H__ | ||
#define __MATH_VECTORALGEBRA_H__ | ||
|
||
#include <vector> | ||
|
||
namespace MathVectorAlgebra { | ||
|
||
std::vector<double> Cubic(int a, int b, int c, int d, std::vector<double> &x); | ||
std::vector<double> Convolve(int offset, std::vector<double> &h, std::vector<double> &x); | ||
|
||
}; // namespace MathVectorAlgebra | ||
|
||
#endif // !__MATH_VECTORALGEBRA_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <catch2/benchmark/catch_benchmark.hpp> | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include "generator/genvector.h" | ||
#include "math/vectoralgebra.h" | ||
|
||
TEST_CASE("VectorAlgebra::Cubic::Test", "[test][math][vectoralgebra]") { | ||
std::vector<double> x0 = {1, 2, 3, 4, 5}; | ||
std::vector<double> x1 = {0, 1, 2, 3, 4, 10}; | ||
std::vector<double> y0 = MathVectorAlgebra::Cubic(1, -1, -2, 4, x0); | ||
std::vector<double> y1 = MathVectorAlgebra::Cubic(1, 0, 0, 10, x1); | ||
std::vector<double> y2 = MathVectorAlgebra::Cubic(0, 5, -3, -4, x1); | ||
std::vector<double> y3 = MathVectorAlgebra::Cubic(1, 3, -4, 4, x1); | ||
std::vector<double> y0ref = {2, 4, 16, 44, 94}; | ||
std::vector<double> y1ref = {10, 11, 18, 37, 74, 1010}; | ||
std::vector<double> y2ref = {-4, -2, 10, 32, 64, 466}; | ||
for (int i = 0; i < 5; i++) { | ||
REQUIRE(y0[i] == y0ref[i]); | ||
} | ||
for (int i = 0; i < 5; i++) { | ||
REQUIRE(y1[i] == y1ref[i]); | ||
} | ||
for (int i = 0; i < 5; i++) { | ||
REQUIRE(y2[i] == y2ref[i]); | ||
} | ||
for (int i = 0; i < 5; i++) { | ||
REQUIRE(y3[i] != y2ref[i]); | ||
} | ||
} | ||
|
||
TEST_CASE("VectorAlgebra::Cubic::Benchmark", "[benchmark][math][vectoralgebra]") { | ||
std::vector<double> x0 = GenVector::RandomVectorDouble(1000, -10, 10); | ||
BENCHMARK("Cubic(1000)") { return MathVectorAlgebra::Cubic(1, -1, -2, 4, x0); }; | ||
std::vector<double> x1 = GenVector::RandomVectorDouble(10000, -10, 10); | ||
BENCHMARK("Cubic(10000)") { return MathVectorAlgebra::Cubic(-2, 1, 3, -8, x1); }; | ||
std::vector<double> x2 = GenVector::RandomVectorDouble(100000, -10, 10); | ||
BENCHMARK("Cubic(10000)") { return MathVectorAlgebra::Cubic(3, -1, 1, 3, x2); }; | ||
} | ||
|
||
TEST_CASE("VectorAlgebra::Convolve::Test", "[test][math][vectoralgebra]") { | ||
std::vector<double> x0 = {1, 2, 3, 4, 5}; | ||
std::vector<double> h0 = {-1, 5, -1}; | ||
std::vector<double> h1 = {-1, 4, -1}; | ||
|
||
std::vector<double> y0 = MathVectorAlgebra::Convolve(1, h0, x0); | ||
std::vector<double> y1 = MathVectorAlgebra::Convolve(1, h1, x0); | ||
std::vector<double> y0ref = {0.428, 0.857, 1.286, 1.714, 3.000}; | ||
std::vector<double> y1ref = {0.333, 0.667, 1.000, 1.333, 2.667}; | ||
for (int i = 0; i < 5; i++) { | ||
REQUIRE(std::abs(y0[i] - y0ref[i]) < 0.002); | ||
} | ||
for (int i = 0; i < 5; i++) { | ||
REQUIRE(std::abs(y1[i] - y1ref[i]) < 0.002); | ||
} | ||
} | ||
|
||
TEST_CASE("VectorAlgebra::Convolve::Benchmark", "[benchmark][math][vectoralgebra]") { | ||
std::vector<double> x0 = GenVector::RandomVectorDouble(1000, -10, 10); | ||
std::vector<double> h0 = GenVector::RandomVectorDouble(3, -1, 1); | ||
BENCHMARK("Convolve(1000, 3)") { return MathVectorAlgebra::Convolve(1, h0, x0); }; | ||
std::vector<double> x1 = GenVector::RandomVectorDouble(10000, -10, 10); | ||
std::vector<double> h1 = GenVector::RandomVectorDouble(3, -1, 1); | ||
BENCHMARK("Convolve(10000, 3)") { return MathVectorAlgebra::Convolve(1, h1, x1); }; | ||
std::vector<double> x2 = GenVector::RandomVectorDouble(10000, -10, 10); | ||
std::vector<double> h2 = GenVector::RandomVectorDouble(15, -1, 1); | ||
BENCHMARK("Convolve(10000, 15)") { return MathVectorAlgebra::Convolve(7, h2, x2); }; | ||
} |