im fairly new to C.
i've written the following code. and all the following functions are nearly identical. i just want to know is there is a way to reduce the following functions into a generic one
struct tensor add(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] + t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
struct tensor sub(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] - t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
struct tensor mul(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] * t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
there are more functions. one for every operator with the following format
struct tensor genericfunc(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] ?? t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}