From 3430407a93f1f7a2ef52ea54be8486991e4e1ccc Mon Sep 17 00:00:00 2001 From: fanasina Date: Thu, 26 Oct 2023 09:45:22 +0200 Subject: [PATCH] create dimension_t --- .../src/dimension_t/dimension.cpp | 181 ++++++++++++++++++ permutation_test/src/dimension_t/dimension.h | 31 +++ .../src/dimension_t/dimension.hpp | 90 +++++++++ .../src/dimension_t/dimension_t.h | 8 + 4 files changed, 310 insertions(+) create mode 100644 permutation_test/src/dimension_t/dimension.cpp create mode 100644 permutation_test/src/dimension_t/dimension.h create mode 100644 permutation_test/src/dimension_t/dimension.hpp create mode 100644 permutation_test/src/dimension_t/dimension_t.h diff --git a/permutation_test/src/dimension_t/dimension.cpp b/permutation_test/src/dimension_t/dimension.cpp new file mode 100644 index 0000000..aba7d8a --- /dev/null +++ b/permutation_test/src/dimension_t/dimension.cpp @@ -0,0 +1,181 @@ +#include +#include + +#include + +#include +#include + + + +//#include "/home/fanasina/progr_/ptens0neD/dimension/dimension.h" + +//#include "/home/fanasina/progr_/ptens0neD/permutation/permutation.h" + + +#include "dimension/dimension.hpp" + +#include "permutation/permutation.hpp" +//#include "permutation.h" + +/*void dimension::initDim(int* arr, bool end = true) { + endian = end; + delete[]dim; + dim = new int[rank]; + size = 1; + for (int i = 0; i < rank; ++i) { + dim[i] = arr[i]; + size *= dim[i]; + } +}*/ + +dimension& dimension::operator=(const dimension& d) { + int oldRank = rank; + rank = d.rank; + size = d.size; + initDim(d.dim, oldRank); + //for (int i = 0; i < rank; i++) dim[i] = d.dim[i]; + return *this; +} + +dimension& dimension::operator+=(const dimension& d) { + int oldRank = rank; + int* t = new int[rank + d.rank]; + for (int i = 0; i < rank; i++) t[i] = dim[i]; + for (int i = 0; i < d.rank; i++) t[rank + i] = d.dim[i]; + size *= d.size; + rank += d.rank; + initDim(t, oldRank); + return *this; +} + +void dimension::LinearToCoord(int* ret, int lin) const { + int begin = 0, end = rank - 1; + int (*iter)(int) = incr; + bool (*cond)(int, int) = isLessThan; + if (endian == false) { + //if (endian) { + begin = rank - 1; end = 0; + iter = decr; cond = isGreatThan; + } + //printf("to coor begin = %d end = %d \n", begin, end); + + int sm = lin; + int pp = size; + for (int i = begin; cond(i, end); i = iter(i)) { + //printf(" i: %d ", i); + pp /= dim[i]; + ret[i] = sm / pp; + sm %= pp; + //printf("sm[%d] = %d , pp=%d ; ", i, sm, pp); + } + ret[end] = sm; +} + +int dimension::CoordToLinear(int* coo) const { + int begin = 0; + int end = rank - 1; + int (*iter)(int); iter = &incr; + bool (*cond)(int, int); cond = &isLessEqThan; + + if (endian) { + begin = rank - 1; end = 0; + iter = &decr; cond = &isGreatEqThan; + } + + int pp = 1; + int sm = 0; + for (int i = begin; cond(i, end); i = iter(i)) { + sm += (coo[i] * pp); + pp *= dim[i]; + } + return sm; +} + +bool isLessEqThan(int a, int b) { return a <= b; } +bool isLessThan(int a, int b) { return a < b; } +bool isGreatEqThan(int a, int b) { return a >= b; } +bool isGreatThan(int a, int b) { return a > b; } +int incr(int i) { return i + 1; } +int decr(int i) { return i - 1; } + + +void add(dimension& d, const dimension& d0, const dimension& d1) { + int oldRank = d.rank; + int* t = new int[d0.rank + d1.rank]; + for (int i = 0; i < d0.rank; i++) t[i] = d0.dim[i]; + for (int i = 0; i < d1.rank; i++) t[d0.rank + i] = d1.dim[i]; + d.rank = d0.rank + d1.rank; + d.initDim(t, oldRank); +} + +void max(dimension& d, const dimension& d0, const dimension& d1) { + if (d0.rank > d1.rank) { + d = d0; + } + else if (d0.rank < d1.rank) { + d = d1; + } + else {// d0.rank = d1.rank + d = d0; + for (int i = 0; i < d.rank; i++) { + if (d.dim[i] < d1.dim[i]) d.dim[i] = d1.dim[i]; + } + } +} + +void min(dimension& d, const dimension& d0, const dimension& d1) { + if (d0.rank > d1.rank) { + d = d1; + } + else if (d0.rank < d1.rank) { + d = d0; + } + else {// d0.rank = d1.rank + d = d0; + for (int i = 0; i < d.rank; i++) { + if (d.dim[i] > d1.dim[i]) d.dim[i] = d1.dim[i]; + } + } +} + +void minReverse(dimension& d, const dimension& d0, const dimension& d1, bool& rev) { + if (d0.rank > d1.rank) { + d = d1; + rev = true; + } + else if (d0.rank < d1.rank) { + d = d0; + rev = false; + } + else {// d0.rank = d1.rank + d = d0; + for (int i = 0; i < d.rank; i++) { + if (d.dim[i] > d1.dim[d.rank - 1 - i]) d.dim[i] = d1.dim[d.rank - 1 - i]; + } + rev = false; + } +} + +void reverseArray(int* arr, int sz) { + int tmp[sz], i = 0; + for (; i < sz / 2; i++) { + tmp[i] = arr[i]; + arr[i] = arr[sz - 1 - i]; + } + for (; i < sz; i++) { + arr[i] = tmp[sz - 1 - i]; + } +} + +void transform(dimension& dDst, const dimension& dSrc, int* perm, int sz) { + dDst = dSrc; + setInit setIn(sz); + if (sz == dSrc.rank) { + if (isPermutation(perm, setIn, sz)) { + for (int i = 0; i < sz; i++) dDst.dim[i] = dSrc.dim[perm[i]]; + } + } +} + + diff --git a/permutation_test/src/dimension_t/dimension.h b/permutation_test/src/dimension_t/dimension.h new file mode 100644 index 0000000..d522d2d --- /dev/null +++ b/permutation_test/src/dimension_t/dimension.h @@ -0,0 +1,31 @@ +#ifndef __DIM__ +#define __DIM__ + +#include +#include + +struct dimension +{ + unsigned int rank; + unsigned int* dim; + size_t size; +}; +typedef dimension dimension; + + +void print_dimension(dimension d); + + +void add(dimension* d, const dimension* d0, const dimension* d1); + +void max(dimension* d, const dimension* d0, const dimension* d1); + +void min(dimension* d, const dimension* d0, const dimension* d1); + +bool minReverse(dimension* d, const dimension* d0, const dimension* d1); + +void transform(dimension* dDst, const dimension* dSrc, int* perm); + + +#endif + diff --git a/permutation_test/src/dimension_t/dimension.hpp b/permutation_test/src/dimension_t/dimension.hpp new file mode 100644 index 0000000..cf8bf66 --- /dev/null +++ b/permutation_test/src/dimension_t/dimension.hpp @@ -0,0 +1,90 @@ +#ifndef __DIMENSION__ +#define __DIMENSION__ + +#include +#include + +#include + +//#include "tensor.h" + +//#include "dimension.h" + +static int iArray1[1] = { 1 }; + + + +struct dimension { + //friend dimension& operator+(const dimension& d, const dimension& d1); + friend void add(dimension& d, const dimension& d0, const dimension& d1); + friend void max(dimension& d, const dimension& d0, const dimension& d1); + friend void min(dimension& d, const dimension& d0, const dimension& d1); + friend void minReverse(dimension& d, const dimension& d0, const dimension& d1, bool& Rev); + friend bool checkMatchProdTensor(dimension& d0, const dimension& d1, int nestingDepth); + friend bool checkMatchProdTensorReverse(dimension& d0, const dimension& d1, int nestingDepth); + friend void extractDimNestingDepth(dimension& dM, const dimension& d0, const dimension& d1, int nestingDepth); + + + int rank; + int* dim; + size_t size; + bool endian; //LitleEndian : true, BigEndian : false, + void initDim(int* arr, int oldRank) { + + //delete[]dim; + //dim = new int[rank]; + if (rank > oldRank) { + free(dim); + dim = (int*)malloc(rank * sizeof(int)); + } + size = 1; + for (int i = 0; i < rank; ++i) { + dim[i] = arr[i]; + size *= dim[i]; + } + } + void initDim(bool end = true) { + endian = end; + //delete[]dim; + //dim = new int[rank]; + + if (dim != NULL) free(dim); + dim = (int*)malloc(rank * sizeof(int)); + } + dimension& operator=(const dimension& d); + dimension& operator+=(const dimension& d); + //dimension& operator*=(const dimension& d); + dimension(int d = 1, int* arr = iArray1, bool end = true) { + endian = end; + rank = d; + //dim = new int[d]; + dim = (int*)malloc(d * sizeof(int)); + initDim(arr, rank); + } + void print() const { printf(" rank: %d\n", rank);for (int i = 0; i < rank; i++) printf(" %d ", dim[i]);printf("\nsize:%ld\n", size); } + void LinearToCoord(int* ret, int lin) const; + int CoordToLinear(int* coo) const; +}; + +bool isLessEqThan(int a, int b); // { return a <= b; } +bool isLessThan(int a, int b); // { return a < b; } +bool isGreatEqThan(int a, int b); // { return a >= b; } +bool isGreatThan(int a, int b); // { return a > b; } +int incr(int i); // { return i + 1; } +int decr(int i); // { return i - 1; } + + + +void add(dimension& d, const dimension& d0, const dimension& d1); + +void max(dimension& d, const dimension& d0, const dimension& d1); + +void min(dimension& d, const dimension& d0, const dimension& d1); + +void minReverse(dimension& d, const dimension& d0, const dimension& d1, bool& rev); + +void transform(dimension& dDst, const dimension& dSrc, int* perm, int sz); + + +#endif + diff --git a/permutation_test/src/dimension_t/dimension_t.h b/permutation_test/src/dimension_t/dimension_t.h new file mode 100644 index 0000000..353ab7f --- /dev/null +++ b/permutation_test/src/dimension_t/dimension_t.h @@ -0,0 +1,8 @@ +#ifndef __DIMENSION_T__H__ +#define __DIMENSION_T__H__ + +#include "permutation_t/permutation_t.h" + +typedef dimension struct PERMUTATION_TYPE_SIZE_T; + +//int compare_dimension(dimension *d1, dimension *d2);