From 18a452544d092575d17128a892afaa10e9aa1b98 Mon Sep 17 00:00:00 2001 From: jens Date: Tue, 28 Jun 2022 19:13:17 +0200 Subject: [PATCH] - initial commit --- Makefile | 24 ++++++++ source/main.cpp | 20 +++++++ source/shift.cpp | 117 ++++++++++++++++++++++++++++++++++++ source/test.cpp | 151 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 312 insertions(+) create mode 100644 Makefile create mode 100644 source/main.cpp create mode 100644 source/shift.cpp create mode 100644 source/test.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e2049e0 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +BLAZE_PATH := $(realpath ../../extlib/blaze) + +SRCS := source/main.cpp source/test.cpp source/shift.cpp +BUILD_DIR := ./build +CXXFLAGS += -g -o0 -std=c++20 +INCLUDES += -I $(BLAZE_PATH) + +LIBS := -lm + + +all: ${BUILD_DIR}/main.elf + +run: ${BUILD_DIR}/main.elf + ${BUILD_DIR}/main.elf + +${BUILD_DIR}/main.elf: ${BUILD_DIR} ${SRCS} + g++ ${CXXFLAGS} ${INCLUDES} ${SRCS} -o $@ ${LIBS} + +${BUILD_DIR}: + mkdir -p $@ + + +clean: + rm -rf ${BUILD_DIR}/* \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp new file mode 100644 index 0000000..af6bb63 --- /dev/null +++ b/source/main.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +void static_vector(); +void dynamic_vector(); +void slicing_vector(); +void shift(); + +int main() +{ + printf("Hallo, Welt!\n"); + + static_vector(); + dynamic_vector(); + slicing_vector(); + shift(); + + return 0; +} \ No newline at end of file diff --git a/source/shift.cpp b/source/shift.cpp new file mode 100644 index 0000000..da03583 --- /dev/null +++ b/source/shift.cpp @@ -0,0 +1,117 @@ +#include +#include + +using namespace std; +using namespace blaze; + +void ror(auto &vec) +{ + vec = elements(vec, [size=vec.size()](size_t i){ return (i-1) % size; }, vec.size()); +} + +void rol(auto &vec) +{ + vec = elements(vec, [size=vec.size()](size_t i){ return (i+1) % size; }, vec.size()); +} + +void insert_left(auto &vec, auto const &x) +{ + ror(vec); + vec[0] = x; +} + +void insert_right(auto &vec, auto const &x) +{ + rol(vec); + vec[vec.size()-1] = x; +} + +void shift() +{ + + { + cout << "Shift ROL with index vector : " << endl; + DynamicVector vec = { 0, 0, 0, 0, 0, 0, 0, 1 }; + cout << "vec : " << endl; + for (int i=0; i < vec.size(); i++) + { + cout << vec; + std::vector idx = {0}; + idx.resize(vec.size()); + size_t k=1; + for (auto i=idx.begin(); i != idx.end()-1; i++) + { + *i = k++; + } + vec = elements(vec, idx); + } + } + { + cout << "Shift ROR with index vector : " << endl; + DynamicVector vec = { 1, 0, 0, 0, 0, 0, 0, 0 }; + cout << "vec : " << endl; + for (int i=0; i < vec.size(); i++) + { + cout << vec; + std::vector idx = {vec.size()-1}; + idx.resize(vec.size()); + size_t k=0; + for (auto i=idx.begin()+1; i != idx.end(); i++) + { + *i = k++; + } + vec = elements(vec, idx); + } + } + { + cout << "Shift ROL with lambda : " << endl; + DynamicVector vec = { 0, 0, 0, 0, 0, 0, 0, 1 }; + cout << "vec : " << endl; + for (int i=0; i < vec.size(); i++) + { + cout << vec; + vec = elements(vec, [size=vec.size()](size_t i){ return (i+1) % size; }, vec.size()); + } + } + { + cout << "Shift ROR with lambda : " << endl; + DynamicVector vec = { 1, 0, 0, 0, 0, 0, 0, 0 }; + cout << "vec : " << endl; + for (int i=0; i < vec.size(); i++) + { + cout << vec; + vec = elements(vec, [size=vec.size()](size_t i){ return (i-1) % size; }, vec.size()); + } + } + { + cout << "insert_left : " << endl; + DynamicVector vec = { 0, 0, 0, 0, 0, 0, 0, 0 }; + for (int i=1; i <= vec.size(); i++) + { + cout << vec; + insert_left(vec, i); + } + } + { + cout << "insert_right : " << endl; + DynamicVector vec = { 0, 0, 0, 0, 0, 0, 0, 0 }; + for (int i=1; i <= vec.size(); i++) + { + cout << vec; + insert_right(vec, i); + } + } + { + cout << "subvector insert_left : " << endl; + DynamicVector vec = { 0, 0, 0, 0, 0, 0, 0, 0 }; + for (int i=1; i <= vec.size(); i++) + { + auto sv1 = subvector(vec, 0, 5); + insert_left(sv1, i+10); + auto sv2 = subvector(vec, 5, 3); + insert_left(sv2, i+100); + cout << vec; + } + } + +} diff --git a/source/test.cpp b/source/test.cpp new file mode 100644 index 0000000..f3828b5 --- /dev/null +++ b/source/test.cpp @@ -0,0 +1,151 @@ +#include +#include + +using namespace std; +using namespace blaze; + +void static_vector() +{ + StaticVector vec_row{ 1, 2, 3 }; + StaticVector vec_col{ 4, 5, 6 }; + StaticMatrix mat_row{{1, 2, 3}}; + StaticMatrix mat_col{{4}, {5}, {6}}; + + cout << "Static" << endl; + + cout << "vec_row" << endl << vec_row; + cout << "vec_col" << endl << vec_col; + cout << "mat_row" << endl << mat_row; + cout << "mat_col" << endl << mat_col; +} + +void dynamic_vector() +{ + DynamicVector vec_row{ 1, 2, 3 }; + DynamicVector vec_col{ 4, 5, 6 }; + DynamicMatrix mat_row{{1, 2, 3}}; + DynamicMatrix mat_col{{4}, {5}, {6}}; + + cout << "Dynamic" << endl; + + cout << "vec_row" << endl << vec_row; + cout << "vec_col" << endl << vec_col; + cout << "mat_row" << endl << mat_row; + cout << "mat_col" << endl << mat_col; +} + +void slicing_vector() +{ + StaticVector coeff{ 1, 2, 3, 4, 5, 6, 7, 8 }; + cout << "coeff" << endl << coeff; + + for (auto& c : coeff) + { + cout << c; + cout << endl; + c *= 2; + } + cout << "coeff" << endl << coeff; + + // Subvector + auto sv = subvector<2,6>(coeff); + cout << "sv" << endl << sv; + + // Elements + elements(coeff, {0,7}) += 8; + cout << "coeff" << endl << coeff; + // Element Shifter with initializer_list + { + coeff = { 0, 0, 0, 0, 0, 0, 0, 1 }; + const initializer_list idx = {1, 2, 3, 4, 5, 6, 7, 0}; + cout << "coeff 1:" << endl; + for (int i=0; i < idx.size(); i++) + { + cout << coeff; + coeff = elements(coeff, idx); + } + } + // Element Shifter with vector + { + coeff = { 0, 0, 0, 0, 0, 0, 0, 1 }; + const vector idx = {1, 2, 3, 4, 5, 6, 7, 0}; + cout << "coeff 2:" << endl; + for (int i=0; i < idx.size(); i++) + { + cout << coeff; + coeff = elements(coeff, idx); + } + } + // Element Shifter with fixed array + { + coeff = { 0, 0, 0, 0, 0, 0, 0, 1 }; + const array idx = {1, 2, 3, 4, 5, 6, 7, 0}; + cout << "coeff 3:" << endl; + for (int i=0; i < idx.size(); i++) + { + cout << coeff; + coeff = elements(coeff, idx); + } + } + // Element Shifter with procedural array + { + coeff = { 0, 0, 0, 0, 0, 0, 0, 1 }; + array idx = {0}; + for (int i=0; i < idx.size()-1; i++) + { + idx[i] = i+1; + } + + cout << "coeff 4:" << endl; + for (int i=0; i < idx.size(); i++) + { + cout << coeff; + coeff = elements(coeff, idx); + } + } + + // Element Shifter with initializer_list and mat + { + // Rotate coeff using indexes and fill matrix with rotated coeffs + coeff = { 1, 2, 3, 4, 5, 6, 7, 8 }; + const initializer_list idx = {1, 2, 3, 4, 5, 6, 7, 0}; + StaticMatrix mat; + for (int i=0; i < idx.size(); i++) + { + cout << coeff; + row(mat, i) = coeff; + coeff = elements(coeff, idx); + } + cout << "mat:" << endl; + cout << mat; + + // StaticVector receives dot product n * M + StaticVector vec = coeff * mat; + cout << "vec:" << endl; + cout << vec; + + // StaticMatrix receives dot product n * M + StaticMatrix mat2; + row(mat2,0) = coeff * mat; + cout << "mat2:" << endl; + cout << mat2; + + // Extend Dynamic Matrix with reserve() + DynamicMatrix mat3; + mat3.reserve(idx.size()); + for (int i=0; i < idx.size(); i++) + { + cout << coeff; + mat3.resize(rows(mat3)+1, coeff.size()); + row(mat3, i) = coeff; + coeff = elements(coeff, idx); + } + cout << "mat3:" << endl; + cout << mat3; + + } + + // Shift + DynamicVector asc = coeff << 2; + cout << "asc" << endl << asc << endl; +}