Added init function for identity tensor.
This commit is contained in:
35
tensor.c
35
tensor.c
@@ -32,7 +32,7 @@ int _tensor_check_size(const int *size, int dim)
|
|||||||
int _tensor_set_size(tensor t, const int *size, int dim)
|
int _tensor_set_size(tensor t, const int *size, int dim)
|
||||||
{
|
{
|
||||||
int *temp;
|
int *temp;
|
||||||
t_type *t_temp;
|
dtype *t_temp;
|
||||||
int i, num_elem = 1;
|
int i, num_elem = 1;
|
||||||
|
|
||||||
for(i = 0; i < dim; i++) {
|
for(i = 0; i < dim; i++) {
|
||||||
@@ -44,7 +44,7 @@ int _tensor_set_size(tensor t, const int *size, int dim)
|
|||||||
temp = realloc(t->size, dim * sizeof(int));
|
temp = realloc(t->size, dim * sizeof(int));
|
||||||
if(temp == NULL && dim != 0) return 0;
|
if(temp == NULL && dim != 0) return 0;
|
||||||
/* Try allocating memory for the Tensor */
|
/* Try allocating memory for the Tensor */
|
||||||
t_temp = realloc(t->elements, num_elem * sizeof(t_type));
|
t_temp = realloc(t->elements, num_elem * sizeof(dtype));
|
||||||
if(t_temp == NULL) {
|
if(t_temp == NULL) {
|
||||||
/* Revert to before the function call and return */
|
/* Revert to before the function call and return */
|
||||||
t->size = realloc(temp, t->dimension * sizeof(int));
|
t->size = realloc(temp, t->dimension * sizeof(int));
|
||||||
@@ -66,7 +66,7 @@ int _tensor_set_size(tensor t, const int *size, int dim)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_set(tensor t, const int *index, t_type val)
|
int tensor_set(tensor t, const int *index, dtype val)
|
||||||
{
|
{
|
||||||
int i, offset = 0;
|
int i, offset = 0;
|
||||||
if(tensor_is_empty(t)) return 0;
|
if(tensor_is_empty(t)) return 0;
|
||||||
@@ -82,7 +82,7 @@ int tensor_set(tensor t, const int *index, t_type val)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
t_type tensor_get(const tensor t, const int *index, int *success)
|
dtype tensor_get(const tensor t, const int *index, int *success)
|
||||||
{
|
{
|
||||||
int i, offset = 0;
|
int i, offset = 0;
|
||||||
if(tensor_is_empty(t)) return 0;
|
if(tensor_is_empty(t)) return 0;
|
||||||
@@ -111,7 +111,7 @@ int tensor_init_one(tensor t, int dimension, const int *size)
|
|||||||
|
|
||||||
if(!_tensor_set_size(t, size, dimension)) return 0;
|
if(!_tensor_set_size(t, size, dimension)) return 0;
|
||||||
for(i = 0; i < t->num_elem; i++) {
|
for(i = 0; i < t->num_elem; i++) {
|
||||||
t->elements[i] = (t_type) 1;
|
t->elements[i] = (dtype) 1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -122,7 +122,7 @@ int tensor_init_zero(tensor t, int dimension, const int *size)
|
|||||||
|
|
||||||
if(!_tensor_set_size(t, size, dimension)) return 0;
|
if(!_tensor_set_size(t, size, dimension)) return 0;
|
||||||
for(i = 0; i < t->num_elem; i++) {
|
for(i = 0; i < t->num_elem; i++) {
|
||||||
t->elements[i] = (t_type) 0;
|
t->elements[i] = (dtype) 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -134,12 +134,31 @@ int tensor_init_rand(tensor t, int dimension, const int *size, int max)
|
|||||||
|
|
||||||
if(!_tensor_set_size(t, size, dimension)) return 0;
|
if(!_tensor_set_size(t, size, dimension)) return 0;
|
||||||
for(i = 0; i < t->num_elem; i++) {
|
for(i = 0; i < t->num_elem; i++) {
|
||||||
t->elements[i] = (t_type) (rand() % max);
|
t->elements[i] = (dtype) (rand() % max);
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tensor_for_each_elem(tensor t, t_type (*func)(t_type))
|
int tensor_init_identity(tensor t, int dimension, int size)
|
||||||
|
{
|
||||||
|
int i ,j;
|
||||||
|
int *indx = malloc(sizeof(int) * dimension);
|
||||||
|
|
||||||
|
for(i = 0; i < dimension; i++) indx[i] = size;
|
||||||
|
if(!tensor_init_zero(t, dimension, indx)) {
|
||||||
|
free(indx);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for(j = 0; j < size; j++) {
|
||||||
|
for(i = 0; i < dimension; i++) indx[i] = j;
|
||||||
|
tensor_set(t, indx, 1);
|
||||||
|
}
|
||||||
|
free(indx);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void tensor_for_each_elem(tensor t, dtype (*func)(dtype))
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|||||||
11
tensor.h
11
tensor.h
@@ -8,10 +8,10 @@
|
|||||||
|
|
||||||
#define PRINT_STRING " %4.1f "
|
#define PRINT_STRING " %4.1f "
|
||||||
|
|
||||||
typedef float t_type;
|
typedef float dtype;
|
||||||
|
|
||||||
typedef struct _tensor {
|
typedef struct _tensor {
|
||||||
t_type *elements;
|
dtype *elements;
|
||||||
int dimension;
|
int dimension;
|
||||||
int *size;
|
int *size;
|
||||||
int num_elem;
|
int num_elem;
|
||||||
@@ -26,14 +26,15 @@ int tensor_is_empty(const tensor t);
|
|||||||
int _tensor_check_size(const int *size, int dim);
|
int _tensor_check_size(const int *size, int dim);
|
||||||
int _tensor_set_size(tensor t, const int *size, int dim);
|
int _tensor_set_size(tensor t, const int *size, int dim);
|
||||||
|
|
||||||
int tensor_set(tensor t, const int *index, t_type val);
|
int tensor_set(tensor t, const int *index, dtype val);
|
||||||
t_type tensor_get(const tensor t, const int *index, int *success);
|
dtype tensor_get(const tensor t, const int *index, int *success);
|
||||||
|
|
||||||
int tensor_init_one(tensor t, int dimension, const int *size);
|
int tensor_init_one(tensor t, int dimension, const int *size);
|
||||||
int tensor_init_zero(tensor t, int dimension, const int *size);
|
int tensor_init_zero(tensor t, int dimension, const int *size);
|
||||||
int tensor_init_rand(tensor t, int dimension, const int *size, int max);
|
int tensor_init_rand(tensor t, int dimension, const int *size, int max);
|
||||||
|
int tensor_init_identity(tensor t, int dimension, int size);
|
||||||
|
|
||||||
void tensor_for_each_elem(tensor t, t_type (*func)(t_type));
|
void tensor_for_each_elem(tensor t, dtype (*func)(dtype));
|
||||||
void tensor_print(const tensor t);
|
void tensor_print(const tensor t);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user