Files
Lina/lina.c
T
2022-01-13 23:09:36 +01:00

99 lines
1.9 KiB
C

#include <stddef.h> // NULL
#include <assert.h> // assert
#include <stdlib.h>
#include "lina.h"
void lina_dot(double *A, double *B, double *C, int m, int n, int l){
assert(m > 0 && n > 0 && l > 0);
assert(A != NULL && B != NULL && C != NULL);
assert(A != C && B != C);
//Actual dot
//Iteration on A's rows
for(int i=0; i < m;i++){
double pos = 0;
//Iteration on B's collumns
for(int k=0; k < l; k++){
//Iteration on the single B collumn for executing the product of sum
for(int j=0; j < n; j++)
pos += A[i * n + j] * B[j * l + k];
//The usage of a support variable 'pos' is for safety purpose (non-zero C matrix)
C[i*l+k] = pos;
}
}
}
void lina_add(double *A, double *B, double *C, int m, int n){
assert(m > 0 && n > 0);
assert(A != NULL && B != NULL && C != NULL);
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
C[i * n + j] = A[i * n + j] + B[i * n + j];
}
void lina_scale(double *A, double *B, double k, int m, int n){
assert(m > 0 && n > 0);
assert(A != NULL && B != NULL);
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
B[i * n + j] = k * A[i * n + j];
}
void lina_transpose(double *A, double *B, int m, int n){
assert(m > 0 && n > 0);
assert(A != NULL && B != NULL);
double *support = malloc(sizeof(*support) * m * n);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
support[i*m + j] = A[j*n + i];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
B[i*m + j] = support[i*m + j];
}
}
free(support);
support = NULL;
/*
| A B C |
| D E F |
| - - - |
| A D - |
| B E - |
| C F - |
| A B C D E F - - - |
| A D - B E - C F - |
*/
#warning "Try to think better algorithm"
}