added tests and fixed bug

This commit is contained in:
cozis
2022-01-14 01:05:01 +01:00
parent 591a6d3d10
commit 01e3863880
2 changed files with 89 additions and 10 deletions
+5 -8
View File
@@ -20,7 +20,7 @@ void lina_dot(double *A, double *B, double *C, int m, int n, int l){
for(int k=0; k < l; k++){ for(int k=0; k < l; k++){
double pos = 0; double pos = 0;
//Iteration over the single B column for executing the product of sum //Iteration over the single B column for executing the product of sum
for(int j=0; j < n; j++) for(int j=0; j < n; j++)
pos += A[i * n + j] * B[j * l + k]; pos += A[i * n + j] * B[j * l + k];
@@ -71,14 +71,11 @@ void lina_transpose(double *A, double *B, int m, int n){
memcpy(support, A, sizeof(*support) * m * n); memcpy(support, A, sizeof(*support) * m * n);
for(int i=0;i<n;i++){ for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
for(int j=0;j<m;j++){ B[j*n + i] = support[i*m + j];
B[i*m + j] = support[i*m + j];
}
}
free(support); free(support);
} }
+84 -2
View File
@@ -4,7 +4,7 @@
#include "lina.h" #include "lina.h"
//Print the matrix A with size m by n //Print the matrix A with size m by n
void pmatrix(FILE *fp, double *A, int m, int n); static void pmatrix(FILE *fp, double *A, int m, int n);
struct { struct {
double A[9], // Left argument double A[9], // Left argument
@@ -50,6 +50,60 @@ struct {
}, },
}; };
struct {
double A[9], B[9];
} transp_tests[] = {
{
.A = {
1, 0, 0,
0, 2, 0,
0, 0, 3,
},
.B = {
1, 0, 0,
0, 2, 0,
0, 0, 3,
},
},
{
.A = {
1, 2, 3,
0, 0, 0,
0, 0, 0,
},
.B = {
1, 0, 0,
2, 0, 0,
3, 0, 0,
},
},
{
.A = {
0, 1, 0,
0, 2, 0,
0, 3, 0,
},
.B = {
0, 0, 0,
1, 2, 3,
0, 0, 0,
},
},
{
.A = {
0, 0, 1,
0, 0, 2,
0, 0, 3,
},
.B = {
0, 0, 0,
0, 0, 0,
1, 2, 3,
},
},
};
int main() int main()
{ {
// Evaluate dot product tests. // Evaluate dot product tests.
@@ -106,10 +160,38 @@ int main()
} }
fprintf(stderr, "\n\t%d scalings out of %d were succesful.\n\n", scale_passed, scale_total); fprintf(stderr, "\n\t%d scalings out of %d were succesful.\n\n", scale_passed, scale_total);
} }
// Evaluate transposition tests.
{
int transp_passed = 0;
int transp_total = sizeof(transp_tests) / sizeof(*transp_tests);
for(int i = 0; i < transp_total; i += 1)
{
double R[9];
lina_transpose(transp_tests[i].A, R, 3, 3);
if(!memcmp(R, transp_tests[i].B, sizeof(R)))
{
fprintf(stderr, "Transposition test %d passed.\n", i);
transp_passed += 1;
}
else
{
fprintf(stderr, "Transposition test %d failed:\n got matrix:\n\n", i);
pmatrix(stderr, R, 3, 3);
fprintf(stderr, " instead of:\n\n");
pmatrix(stderr, transp_tests[i].B, 3, 3);
}
}
fprintf(stderr, "\n\t%d transpositions out of %d were succesful.\n\n", transp_passed, transp_total);
}
return 0; return 0;
} }
void pmatrix(FILE *fp, double *A,int m,int n){ static void pmatrix(FILE *fp, double *A,int m,int n){
for(int i = 0; i<m; i++){ for(int i = 0; i<m; i++){
fprintf(fp, " | "); fprintf(fp, " | ");