Changed implementation of scalar operations to use map.
This commit is contained in:
36
tensor.c
36
tensor.c
@@ -1,6 +1,10 @@
|
|||||||
#include "tensor.h"
|
#include "tensor.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* A helper for f.e. the tensor_add_scalar function */
|
||||||
|
dtype _tensor_scalar_wise_helper;
|
||||||
|
|
||||||
|
|
||||||
tensor tensor_new(void)
|
tensor tensor_new(void)
|
||||||
{
|
{
|
||||||
return calloc(1, sizeof(struct _tensor));
|
return calloc(1, sizeof(struct _tensor));
|
||||||
@@ -183,44 +187,42 @@ int tensor_cpy(tensor t1, const tensor t2)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
dtype _tensor_dtype_add_helper(dtype d) { return DTYPE_ADD(d, _tensor_scalar_wise_helper); }
|
||||||
|
dtype _tensor_dtype_sub_helper(dtype d) { return DTYPE_SUB(d, _tensor_scalar_wise_helper); }
|
||||||
|
dtype _tensor_dtype_mul_helper(dtype d) { return DTYPE_MUL(d, _tensor_scalar_wise_helper); }
|
||||||
|
dtype _tensor_dtype_div_helper(dtype d) { return DTYPE_DIV(d, _tensor_scalar_wise_helper); }
|
||||||
|
|
||||||
void tensor_add_scalar(tensor t, dtype n)
|
void tensor_add_scalar(tensor t, dtype n)
|
||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t));
|
assert(!tensor_is_empty(t));
|
||||||
|
|
||||||
int i;
|
_tensor_scalar_wise_helper = n;
|
||||||
for(i = 0; i < t->num_elem; i++) {
|
tensor_map(t, &_tensor_dtype_add_helper);
|
||||||
t->elements[i] = DTYPE_ADD(t->elements[i], n);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tensor_sub_scalar(tensor t, dtype n)
|
void tensor_sub_scalar(tensor t, dtype n)
|
||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t));
|
assert(!tensor_is_empty(t));
|
||||||
|
|
||||||
int i;
|
_tensor_scalar_wise_helper = n;
|
||||||
for(i = 0; i < t->num_elem; i++) {
|
tensor_map(t, &_tensor_dtype_sub_helper);
|
||||||
t->elements[i] = DTYPE_SUB(t->elements[i], n);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tensor_mult_scalar(tensor t, dtype n)
|
void tensor_mul_scalar(tensor t, dtype n)
|
||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t));
|
assert(!tensor_is_empty(t));
|
||||||
|
|
||||||
int i;
|
_tensor_scalar_wise_helper = n;
|
||||||
for(i = 0; i < t->num_elem; i++) {
|
tensor_map(t, &_tensor_dtype_mul_helper);
|
||||||
t->elements[i] = DTYPE_MUL(t->elements[i], n);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tensor_div_scalar(tensor t, dtype n)
|
void tensor_div_scalar(tensor t, dtype n)
|
||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t));
|
assert(!tensor_is_empty(t));
|
||||||
|
|
||||||
int i;
|
_tensor_scalar_wise_helper = n;
|
||||||
for(i = 0; i < t->num_elem; i++) {
|
tensor_map(t, &_tensor_dtype_div_helper);
|
||||||
t->elements[i] = DTYPE_DIV(t->elements[i], n);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_add_inplace(tensor t1, const tensor t2)
|
int tensor_add_inplace(tensor t1, const tensor t2)
|
||||||
|
|||||||
5
tensor.h
5
tensor.h
@@ -6,6 +6,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
/* Defining the datatype of the tensor */
|
/* Defining the datatype of the tensor */
|
||||||
typedef float dtype;
|
typedef float dtype;
|
||||||
@@ -53,10 +54,12 @@ int tensor_cpy(tensor t1, const tensor t2);
|
|||||||
|
|
||||||
void tensor_add_scalar(tensor t, dtype n);
|
void tensor_add_scalar(tensor t, dtype n);
|
||||||
void tensor_sub_scalar(tensor t, dtype n);
|
void tensor_sub_scalar(tensor t, dtype n);
|
||||||
void tensor_mult_scalar(tensor t, dtype n);
|
void tensor_mul_scalar(tensor t, dtype n);
|
||||||
void tensor_div_scalar(tensor t, dtype n);
|
void tensor_div_scalar(tensor t, dtype n);
|
||||||
int tensor_add_inplace(tensor t1, const tensor t2);
|
int tensor_add_inplace(tensor t1, const tensor t2);
|
||||||
int tensor_sub_inplace(tensor t1, const tensor t2);
|
int 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);
|
||||||
|
|
||||||
void tensor_map(tensor t, dtype (*func)(dtype));
|
void tensor_map(tensor t, dtype (*func)(dtype));
|
||||||
void tensor_print(const tensor t);
|
void tensor_print(const tensor t);
|
||||||
|
|||||||
Reference in New Issue
Block a user