add coord line in dimension repo
This commit is contained in:
@@ -1,6 +1,74 @@
|
||||
#include "dimension_t/dimension_t.h"
|
||||
|
||||
bool endian=true;
|
||||
|
||||
bool isLessEqThan(long int a, long int b) { return a <= b; }
|
||||
bool isLessThan(long int a, long int b) { return a < b; }
|
||||
bool isGreatEqThan(long int a, long int b) { return a >= b; }
|
||||
bool isGreatThan(long int a, long int b) { return a > b; }
|
||||
long int incr(long int i) { return i + 1; }
|
||||
long int decr(long int i) { return i - 1; }
|
||||
|
||||
|
||||
|
||||
dimension *
|
||||
create_dim(size_t sz){
|
||||
return CREATE_PERMUTATION_TYPE_SIZE_T(sz);
|
||||
}
|
||||
|
||||
void updateRankDim(dimension *dim){
|
||||
dim->rank=1;
|
||||
for(size_t i=0; i<dim->size; ++i)
|
||||
dim->rank *=dim->perm[i];
|
||||
}
|
||||
|
||||
size_t LineFromCoord(size_t *coo, dimension dim){
|
||||
long int begin = 0;
|
||||
long int end = dim.size - 1;
|
||||
long int (*iter)(long int); iter = &incr;
|
||||
bool (*cond)(long int, long int); cond = &isLessEqThan;
|
||||
|
||||
if (endian) {
|
||||
begin = dim.size - 1; end = 0;
|
||||
iter = &decr; cond = &isGreatEqThan;
|
||||
}
|
||||
|
||||
long int pp = 1;
|
||||
long int sm = 0;
|
||||
for (long int i = begin; cond(i, end); i = iter(i)) {
|
||||
sm += (coo[i] * pp);
|
||||
pp *= dim.perm[i];
|
||||
}
|
||||
return sm;
|
||||
|
||||
}
|
||||
|
||||
|
||||
size_t* CoordFromLin(size_t line, dimension dim){
|
||||
size_t *ret;
|
||||
ret=malloc(dim.size*sizeof(size_t));
|
||||
long int begin = 0, end = dim.size - 1;
|
||||
long int (*iter)(long int) = incr;
|
||||
bool (*cond)(long int, long int) = isLessThan;
|
||||
if (endian == false) {
|
||||
//if (endian) {
|
||||
begin = dim.size - 1; end = 0;
|
||||
iter = decr; cond = isGreatThan;
|
||||
}
|
||||
//prlong intf("to coor begin = %d end = %d \n", begin, end);
|
||||
|
||||
long int sm = line;
|
||||
long int pp = dim.rank;
|
||||
for (long int i = begin; cond(i, end); i = iter(i)) {
|
||||
//prlong intf(" i: %d ", i);
|
||||
pp /= dim.perm[i];
|
||||
ret[i] = sm / pp;
|
||||
sm %= pp;
|
||||
//prlong intf("sm[%d] = %d , pp=%d ; ", i, sm, pp);
|
||||
}
|
||||
ret[end] = sm;
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,17 @@
|
||||
|
||||
#include "permutation_t/permutation_t.h"
|
||||
|
||||
extern bool endian;
|
||||
|
||||
typedef struct PERMUTATION_TYPE_SIZE_T dimension ;
|
||||
|
||||
dimension * create_dim(size_t);
|
||||
dimension * create_dim(size_t size);
|
||||
|
||||
void updateRankDim(dimension *dim);
|
||||
size_t LineFromCoord(size_t *coo, dimension dim);
|
||||
size_t* CoordFromLin(size_t line, dimension dim);
|
||||
|
||||
|
||||
|
||||
#endif /* __DIMENSION_T__H__ */
|
||||
//int compare_dimension(dimension *d1, dimension *d2);
|
||||
|
||||
@@ -23,7 +23,36 @@ TEST(dimension0){
|
||||
EXPECT_EQ(D->size,5);
|
||||
|
||||
}
|
||||
TEST(rank){
|
||||
dimension *D=create_dim(4);
|
||||
D->perm[0]=2;
|
||||
D->perm[1]=3;
|
||||
D->perm[2]=5;
|
||||
D->perm[3]=6;
|
||||
|
||||
updateRankDim(D);
|
||||
EXPECT_EQ(D->rank, 180);
|
||||
|
||||
}
|
||||
|
||||
TEST(Coord_linear){
|
||||
dimension *D=create_dim(4);
|
||||
D->perm[0]=2;
|
||||
D->perm[1]=3;
|
||||
D->perm[2]=5;
|
||||
D->perm[3]=6;
|
||||
|
||||
updateRankDim(D);
|
||||
|
||||
size_t line=255;
|
||||
size_t *coord = CoordFromLin(line,*D);
|
||||
|
||||
for(size_t i=0; i<D->size; ++i){
|
||||
LOG("coo[%ld]=%ld\n",i,coord[i]);
|
||||
}
|
||||
|
||||
EXPECT_EQ(line, LineFromCoord(coord, *D));
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
#include "tensor_t/tensor_t.h"
|
||||
|
||||
#define GEN_FUNC_TENSOR(type)\
|
||||
struct tensor_##type CREATE_TENSOR_##type(struct dimension_t dim){\
|
||||
struct tensor_##type r_tens=malloc(sizeof(tensor_t##type));\
|
||||
r_tens->x=malloc(sizeof(type)*dim->size);\
|
||||
return r_tens;\
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,15 @@
|
||||
|
||||
#include "dimension_t/dimension_t.h"
|
||||
|
||||
#define GENERATE_TENSOR_TYPE(type) \
|
||||
struct tensor_##type{\
|
||||
dimension dim;\
|
||||
type *x;\
|
||||
};\
|
||||
typedef struct tensor_##type tensor_##type;\
|
||||
struct tensor_##type CREATE_TENSOR_##type(struct dimension_t dim); \
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* __TENSOR_T__H__ */
|
||||
|
||||
Reference in New Issue
Block a user