- initial commit

This commit is contained in:
2022-06-28 19:13:17 +02:00
commit 18a452544d
4 changed files with 312 additions and 0 deletions
+24
View File
@@ -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}/*
+20
View File
@@ -0,0 +1,20 @@
#include <cstdio>
#include <cmath>
#include <blaze/Math.h>
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;
}
+117
View File
@@ -0,0 +1,117 @@
#include <blaze/Math.h>
#include <iostream>
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<int, rowVector> vec = { 0, 0, 0, 0, 0, 0, 0, 1 };
cout << "vec : " << endl;
for (int i=0; i < vec.size(); i++)
{
cout << vec;
std::vector<size_t> 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<int, rowVector> vec = { 1, 0, 0, 0, 0, 0, 0, 0 };
cout << "vec : " << endl;
for (int i=0; i < vec.size(); i++)
{
cout << vec;
std::vector<size_t> 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<int, rowVector> 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<int, rowVector> 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<int, rowVector> 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<int, rowVector> 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<int, rowVector> 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;
}
}
}
+151
View File
@@ -0,0 +1,151 @@
#include <blaze/Math.h>
#include <iostream>
using namespace std;
using namespace blaze;
void static_vector()
{
StaticVector<int, 3, rowVector> vec_row{ 1, 2, 3 };
StaticVector<int, 3, columnVector> vec_col{ 4, 5, 6 };
StaticMatrix<int,1,3> mat_row{{1, 2, 3}};
StaticMatrix<int,3,1> 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<int, rowVector> vec_row{ 1, 2, 3 };
DynamicVector<int, columnVector> vec_col{ 4, 5, 6 };
DynamicMatrix<int> mat_row{{1, 2, 3}};
DynamicMatrix<int> 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<int, 8, rowVector> 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<int> 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<int> 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<int, 8> 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<int, 8> 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<int> idx = {1, 2, 3, 4, 5, 6, 7, 0};
StaticMatrix<int, 8, 8> 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<int, 8, rowVector> vec = coeff * mat;
cout << "vec:" << endl;
cout << vec;
// StaticMatrix receives dot product n * M
StaticMatrix<int, 1, 8> mat2;
row(mat2,0) = coeff * mat;
cout << "mat2:" << endl;
cout << mat2;
// Extend Dynamic Matrix with reserve()
DynamicMatrix<int> 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<int, rowVector> asc = coeff << 2;
cout << "asc" << endl << asc << endl;
}