Declared functions in tensorfunc which will replace the iterators with a more functional aprroach.

This commit is contained in:
2023-09-15 14:06:13 +02:00
parent 0628955e43
commit 8c44b96913
8 changed files with 257 additions and 387 deletions

197
tensor.c
View File

@@ -265,203 +265,6 @@ bool tensor_cpy(tensor t1, const tensor t2)
return true;
}
bool tensor_add_inplace(tensor t1, const tensor t2)
{
/* Adds the values of t2 onto the values of t1. t1 and t2 need to have the
* same shape.
*
* @param t1 The tensor on which the values of t2 are added
* @param t2 The tensor whose values are added
*
* @return true when successful, false otherwise
*/
assert(!tensor_is_empty(t1));
assert(!tensor_is_empty(t2));
uint32_t i;
if(t1->rank != t2->rank) return false;
if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false;
for(i = 0; i < t1->num_elem; i++) {
t1->elements[i] = DTYPE_ADD(t1->elements[i], t2->elements[i]);
}
return true;
}
bool tensor_sub_inplace(tensor t1, const tensor t2)
{
/* Subtracts the values of t2 from the values of t1. t1 and t2 need to have
* the same shape.
*
* @param t1 The tensor from which the values of t2 are subtracted
* @param t2 The tensor whose values are subtracted
*
* @return true when successful, false otherwise
*/
assert(!tensor_is_empty(t1));
assert(!tensor_is_empty(t2));
uint32_t i;
if(t1->rank != t2->rank) return false;
if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false;
for(i = 0; i < t1->num_elem; i++) {
t1->elements[i] = DTYPE_SUB(t1->elements[i], t2->elements[i]);
}
return true;
}
bool tensor_mul_inplace(tensor t1, const tensor t2)
{
/* Multiplies the values of t2 onto t1 element wise. t1 and t2 need to
* have the same shape.
*
* @param t1 The tensor to multiply onto
* @param t2 The tensor to multiply with
*
* @return true when successful, false otherwise
*/
assert(!tensor_is_empty(t1));
assert(!tensor_is_empty(t2));
uint32_t i;
if(t1->rank != t2->rank) return false;
if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false;
for(i = 0; i < t1->num_elem; i++) {
t1->elements[i] = DTYPE_MUL(t1->elements[i], t2->elements[i]);
}
return true;
}
bool tensor_div_inplace(tensor t1, const tensor t2)
{
/* Divides the values of t2 by the values of t1 element wise. t1 and t2
* need to have the same shape.
*
* @param t1 The tensor to devide
* @param t2 The tensor to devide by
*
* @return true when successful, false otherwise
*/
assert(!tensor_is_empty(t1));
assert(!tensor_is_empty(t2));
uint32_t i;
if(t1->rank != t2->rank) return false;
if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false;
for(i = 0; i < t1->num_elem; i++) {
t1->elements[i] = DTYPE_DIV(t1->elements[i], t2->elements[i]);
}
return true;
}
tensor tensor_add(const tensor t1, const tensor t2)
{
/* Adds two tensors returning the result as a tensor. t1 and t2 need to
* have the same shape.
*
* @param t1 The first tensor to add
* @param t2 The second tensor to add
*
* @return The result of the operation, NULL if an error occurs
*/
assert(!tensor_is_empty(t1));
assert(!tensor_is_empty(t2));
tensor t3 = tensor_new();
if(t3 == NULL) return NULL;
if (!tensor_cpy(t3, t1)) {
tensor_destroy(t3);
return NULL;
}
if (!tensor_add_inplace(t3, t2)) {
tensor_destroy(t3);
return NULL;
}
return t3;
}
tensor tensor_sub(const tensor t1, const tensor t2)
{
/* Subtracts two tensors returning the result as a tensor. t1 and t2 need
* to have the same shape.
*
* @param t1 The tensor to subtract from
* @param t2 The tensor that is subtracted
*
* @return The result of the operation, NULL if an error occurs
*/
assert(!tensor_is_empty(t1));
assert(!tensor_is_empty(t2));
tensor t3 = tensor_new();
if(t3 == NULL) return NULL;
if (!tensor_cpy(t3, t1)) {
tensor_destroy(t3);
return NULL;
}
if (!tensor_sub_inplace(t3, t2)) {
tensor_destroy(t3);
return NULL;
}
return t3;
}
tensor tensor_mul(const tensor t1, const tensor t2)
{
/* Multiplies two tensors element wise returning the result as a tensor.
* t1 and t2 need to have the same shape.
*
* @param t1 The first tensor to multipy
* @param t2 The second tensor to multipy
*
* @return The result of the operation, NULL if an error occurs
*/
assert(!tensor_is_empty(t1));
assert(!tensor_is_empty(t2));
tensor t3 = tensor_new();
if(t3 == NULL) return NULL;
if (!tensor_cpy(t3, t1)) {
tensor_destroy(t3);
return NULL;
}
if (!tensor_mul_inplace(t3, t2)) {
tensor_destroy(t3);
return NULL;
}
return t3;
}
tensor tensor_div(const tensor t1, const tensor t2)
{
/* Divides two tensors element wise returning the result as a tensor. t1
* and t2 need to have the same shape.
*
* @param t1 The dividend
* @param t2 The divisor
*
* @return The result of the operation, NULL if an error occurs
*/
assert(!tensor_is_empty(t1));
assert(!tensor_is_empty(t2));
tensor t3 = tensor_new();
if(t3 == NULL) return NULL;
if (!tensor_cpy(t3, t1)) {
tensor_destroy(t3);
return NULL;
}
if (!tensor_div_inplace(t3, t2)) {
tensor_destroy(t3);
return NULL;
}
return t3;
}
void tensor_print(const tensor t)
{
/* Prints a tensor to stdout.