added some asserts and made minor api changes

This commit is contained in:
cozis
2022-01-13 17:13:44 +01:00
parent 7350184904
commit 459ca17fac
4 changed files with 37 additions and 31 deletions
Executable
+1
View File
@@ -0,0 +1 @@
gcc test.c lina.c -o test -Wall -Wextra -g
+24 -27
View File
@@ -1,14 +1,14 @@
#include <stddef.h> // NULL
#include <assert.h> // assert
#include "lina.h"
// Evaluate the dot product C = A*B where C is of size m by l, A is m by n and B is n by l.
// NOTE: C can't be the same pointer of A or B.
void lina_dot(double **A, double **B, double **C, unsigned int m, unsigned int n, unsigned int l){
void lina_dot(double *A, double *B, double *C, int m, int n, int l){
//Control for the sanity of the inputs:
//C must point to a different memory than A and B
if(C == A || C == B)
//Implement some Cozis-like-good-way method for error signaling
return;
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
@@ -20,14 +20,11 @@ void lina_dot(double **A, double **B, double **C, unsigned int m, unsigned int n
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 = pos + A[i][j] * B[j][k];
}
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][k] = pos;
C[i*l+k] = pos;
}
@@ -37,34 +34,34 @@ void lina_dot(double **A, double **B, double **C, unsigned int m, unsigned int n
// Evaluate C = A + B where A,B,C are m by n.
// NOTE: C can be the same location of A or B.
void lina_add(double **A, double **B, double **C, unsigned int m, unsigned int n){
void lina_add(double *A, double *B, double *C, int m, int n){
for(int i=0;i<m;i++){
assert(m >= 0 && n >= 0);
assert(A != NULL && B != NULL && C != NULL);
for(int j=0;j<n;j++){
C[i][j] = A[i][j] + B[i][j];
}
}
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];
}
// Evaluate B = k*A where A and B are m by n matrices.
// NOTE: B can be the same location of A.
void lina_scale(double **A, double **B, double k, unsigned int m, unsigned int n){
void lina_scale(double *A, double *B, double k, int m, int n){
for(int i=0;i<m;i++){
assert(m >= 0 && n >= 0);
assert(A != NULL && B != NULL);
for(int j=0;j<n;j++){
B[i][j] = k * A[i][j];
}
}
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
B[i * n + j] = k * A[i * n + j];
}
// Evaluate B = A^t (the transpose of A) where A is m by n.
// NOTE: B can be the same location of A.
void lina_transpose(double **A, double **B, unsigned int m, unsigned int n){
void lina_transpose(double *A, double *B, int m, int n){
}
+4 -4
View File
@@ -1,16 +1,16 @@
// Evaluate the dot product C = A*B where C is of size m by l, A is m by n and B is n by l.
// NOTE: C can't be the same pointer of A or B.
void lina_dot(double **A, double **B, double **C, unsigned int m, unsigned int n, unsigned int l);
void lina_dot(double *A, double *B, double *C, int m, int n, int l);
// Evaluate C = A + B where A,B,C are m by n.
// NOTE: C can be the same location of A or B.
void lina_add(double **A, double **B, double **C, unsigned int m, unsigned int n);
void lina_add(double *A, double *B, double *C, int m, int n);
// Evaluate B = k*A where A and B are m by n matrices.
// NOTE: B can be the same location of A.
void lina_scale(double **A, double **B, double k, unsigned int m, unsigned int n);
void lina_scale(double *A, double *B, double k, int m, int n);
// Evaluate B = A^t (the transpose of A) where A is m by n.
// NOTE: B can be the same location of A.
void lina_transpose(double **A, double **B, unsigned int m, unsigned int n);
void lina_transpose(double *A, double *B, int m, int n);
+8
View File
@@ -0,0 +1,8 @@
#include <stdio.h>
#include "lina.h"
int main()
{
fprintf(stderr, "There's nothing here yet..\n");
return 0;
}