2023-02-24 19:10:32 +01:00
|
|
|
#ifndef TENSOR_H_INCLUDED
|
|
|
|
|
#define TENSOR_H_INCLUDED
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <time.h>
|
2023-03-13 15:45:30 +01:00
|
|
|
#include <assert.h>
|
2023-02-24 19:10:32 +01:00
|
|
|
|
|
|
|
|
#define PRINT_STRING " %4.1f "
|
|
|
|
|
|
2023-02-24 20:53:00 +01:00
|
|
|
typedef float dtype;
|
2023-02-24 19:10:32 +01:00
|
|
|
|
|
|
|
|
typedef struct _tensor {
|
2023-02-24 20:53:00 +01:00
|
|
|
dtype *elements;
|
2023-02-24 19:10:32 +01:00
|
|
|
int dimension;
|
|
|
|
|
int *size;
|
2023-03-13 15:45:30 +01:00
|
|
|
int *index_offsets;
|
2023-02-24 19:10:32 +01:00
|
|
|
int num_elem;
|
|
|
|
|
} *tensor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tensor tensor_new(void);
|
|
|
|
|
void tensor_destroy(tensor t);
|
|
|
|
|
|
2023-02-24 19:17:31 +01:00
|
|
|
int tensor_is_empty(const tensor t);
|
2023-03-12 20:29:55 +01:00
|
|
|
int tensor_is_equal(const tensor t1, const tensor t2);
|
2023-02-24 19:10:32 +01:00
|
|
|
|
|
|
|
|
int _tensor_check_size(const int *size, int dim);
|
|
|
|
|
int _tensor_set_size(tensor t, const int *size, int dim);
|
|
|
|
|
|
2023-02-24 20:53:00 +01:00
|
|
|
int tensor_set(tensor t, const int *index, dtype val);
|
|
|
|
|
dtype tensor_get(const tensor t, const int *index, int *success);
|
2023-02-24 19:10:32 +01:00
|
|
|
|
|
|
|
|
int tensor_init_one(tensor t, int dimension, const int *size);
|
|
|
|
|
int tensor_init_zero(tensor t, int dimension, const int *size);
|
2023-02-24 19:20:09 +01:00
|
|
|
int tensor_init_rand(tensor t, int dimension, const int *size, int max);
|
2023-03-18 20:52:26 +01:00
|
|
|
int tensor_cpy(tensor t1, const tensor t2);
|
2023-02-24 19:10:32 +01:00
|
|
|
|
2023-03-18 20:52:26 +01:00
|
|
|
void tensor_add_scalar(tensor t, dtype n);
|
|
|
|
|
void tensor_sub_scalar(tensor t, dtype n);
|
|
|
|
|
void tensor_mult_scalar(tensor t, dtype n);
|
|
|
|
|
void tensor_div_scalar(tensor t, dtype n);
|
2023-02-27 14:45:21 +01:00
|
|
|
int tensor_add(tensor t1, const tensor t2);
|
|
|
|
|
|
2023-02-24 20:53:00 +01:00
|
|
|
void tensor_for_each_elem(tensor t, dtype (*func)(dtype));
|
2023-02-24 19:17:31 +01:00
|
|
|
void tensor_print(const tensor t);
|
2023-02-24 19:10:32 +01:00
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|