separate dimension repo

This commit is contained in:
2023-11-29 10:44:06 +01:00
parent f5f140dc30
commit 55852146cb
5 changed files with 13 additions and 0 deletions
+181
View File
@@ -0,0 +1,181 @@
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include <vector>
#include <algorithm>
//#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]];
}
}
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef __DIM__
#define __DIM__
#include <stdio.h>
#include <stdlib.h>
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
+90
View File
@@ -0,0 +1,90 @@
#ifndef __DIMENSION__
#define __DIMENSION__
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
//#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
+8
View File
@@ -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);