Files
ctensor/tensor.h

71 lines
2.1 KiB
C
Raw Normal View History

#ifndef TENSOR_H_INCLUDED
#define TENSOR_H_INCLUDED
2023-05-07 14:38:41 +02:00
#include <_types/_uint32_t.h>
#include <_types/_uint8_t.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stdarg.h>
2023-05-07 14:38:41 +02:00
#include <stdint.h>
/* Defining the datatype of the tensor */
typedef float dtype;
/* dtype needs to implement add, sub, mul and div, */
#define DTYPE_ADD(a, b) ((a) + (b))
#define DTYPE_SUB(a, b) ((a) - (b))
#define DTYPE_MUL(a, b) ((a) * (b))
#define DTYPE_DIV(a, b) ((a) / (b))
/* equal and not equal,*/
#define DTYPE_EQ(a, b) ((a) == (b))
#define DTYPE_NE(a, b) ((a) != (b))
/* one and zero, */
#define DTYPE_ONE 1.0
#define DTYPE_ZERO 0.0
2023-05-07 14:38:41 +02:00
/* and a random and pruint8_t function */
#define DTYPE_RAND(max) ((float) rand() / RAND_MAX * (max))
#define DTYPE_PRINT(a) (printf(" %4.1f ", (a)))
typedef struct _tensor {
dtype *elements;
2023-05-07 14:38:41 +02:00
uint8_t rank;
uint32_t *size;
uint32_t *index_offsets;
uint32_t num_elem;
} *tensor;
tensor tensor_new(void);
void tensor_destroy(tensor t);
2023-05-07 14:38:41 +02:00
uint8_t tensor_is_empty(const tensor t);
uint8_t tensor_is_equal(const tensor t1, const tensor t2);
2023-05-07 14:38:41 +02:00
uint8_t _tensor_check_size(const uint32_t *size, uint8_t rank);
uint8_t _tensor_set_size(tensor t, const uint32_t *size, uint8_t rank);
2023-05-07 14:38:41 +02:00
uint8_t tensor_set(tensor t, const uint32_t *index, dtype val);
dtype tensor_get(const tensor t, const uint32_t *index, uint8_t *success);
2023-05-07 14:38:41 +02:00
uint8_t tensor_init_one(tensor t, uint8_t rank, const uint32_t *size);
uint8_t tensor_init_zero(tensor t, uint8_t rank, const uint32_t *size);
uint8_t tensor_init_rand(tensor t, uint8_t rank, const uint32_t *size, dtype max);
uint8_t tensor_cpy(tensor t1, const tensor t2);
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_mul_scalar(tensor t, dtype n);
2023-03-18 20:52:26 +01:00
void tensor_div_scalar(tensor t, dtype n);
2023-05-07 14:38:41 +02:00
uint8_t tensor_add_inplace(tensor t1, const tensor t2);
uint8_t tensor_sub_inplace(tensor t1, const tensor t2);
tensor tensor_add(const tensor t1, const tensor t2);
tensor tensor_sub(const tensor t1, const tensor t2);
2023-02-27 14:45:21 +01:00
void tensor_print(const tensor t);
#endif