added some tests and fixed a bug in the dot product routine

This commit is contained in:
cozis
2022-01-14 00:47:44 +01:00
parent 4949f43de7
commit 591a6d3d10
2 changed files with 114 additions and 121 deletions
+14 -29
View File
@@ -1,8 +1,11 @@
#include <stddef.h> // NULL #include <stddef.h>
#include <assert.h> // assert #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "lina.h" #include "lina.h"
#define check assert
void lina_dot(double *A, double *B, double *C, int m, int n, int l){ void lina_dot(double *A, double *B, double *C, int m, int n, int l){
assert(m > 0 && n > 0 && l > 0); assert(m > 0 && n > 0 && l > 0);
@@ -10,15 +13,15 @@ void lina_dot(double *A, double *B, double *C, int m, int n, int l){
assert(A != C && B != C); assert(A != C && B != C);
//Actual dot //Actual dot
//Iteration on A's rows //Iteration over A's rows
for(int i=0; i < m;i++){ for(int i=0; i < m;i++){
double pos = 0; //Iteration over B's columns
//Iteration on B's collumns
for(int k=0; k < l; k++){ for(int k=0; k < l; k++){
//Iteration on the single B collumn for executing the product of sum double pos = 0;
//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];
@@ -60,16 +63,13 @@ void lina_transpose(double *A, double *B, int m, int n){
assert(m > 0 && n > 0); assert(m > 0 && n > 0);
assert(A != NULL && B != NULL); assert(A != NULL && B != NULL);
#warning "Try to think better algorithm"
double *support = malloc(sizeof(*support) * m * n); double *support = malloc(sizeof(*support) * m * n);
for(int i=0;i<n;i++){ check(support != NULL);
for(int j=0;j<m;j++){ memcpy(support, A, sizeof(*support) * m * n);
support[i*m + j] = A[j*n + i];
}
}
for(int i=0;i<n;i++){ for(int i=0;i<n;i++){
@@ -81,19 +81,4 @@ void lina_transpose(double *A, double *B, int m, int n){
} }
free(support); 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"
} }
+99 -91
View File
@@ -1,117 +1,125 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <string.h>
#include "lina.h" #include "lina.h"
#define NUM_TESTS 6 //Number of tests case to create
#define MAX_VALUE 15 //Max value a matrix can have
typedef struct matrix_t
{
double *A;
int m;
int n;
}matrix_t;
//Dinamically create NUM_TESTS tests case randomly generating matrices with random values (whom max is MAX_VALUE)
//index from 0 to NUM_TESTS/3 will contain 2x3 matrices, index from NUM_TESTS/3 to 2*NUM_TESTS/3 3x3, and index from 2*NUM_TESTS/3 to NUM_TESTS 4x3
void create_tests(matrix_t **tests);
//Free the heap memory from the test structure
void delete_tests(matrix_t **tests);
//Print the matrix A with size m by n //Print the matrix A with size m by n
void pmatrix(double *A, int m, int n); void pmatrix(FILE *fp, double *A, int m, int n);
struct {
double A[9], // Left argument
B[9], // Right argument
C[9]; // Expected result
} dot_tests[] = {
{
.A = {
1, 0, 0,
0, 1, 0,
0, 0, 1,
},
.B = {
1, 0, 0,
0, 1, 0,
0, 0, 1,
},
.C = {
1, 0, 0,
0, 1, 0,
0, 0, 1,
},
},
};
struct {
double A[9], // Input
B[9], // Expected output
factor; // Scale factor
} scale_tests[] = {
{
.A = {
1, 1, 1,
1, 1, 1,
1, 1, 1,
},
.B = {
2, 2, 2,
2, 2, 2,
2, 2, 2,
},
.factor = 2,
},
};
int main() int main()
{ {
matrix_t *tests[NUM_TESTS]; // Evaluate dot product tests.
{
int dot_passed = 0;
int dot_total = sizeof(dot_tests) / sizeof(*dot_tests);
create_tests(tests); for(int i = 0; i < dot_total; i += 1)
{
double R[9];
for(int i=0;i< NUM_TESTS;i++) lina_dot(dot_tests[i].A, dot_tests[i].B, R, 3, 3, 3);
pmatrix(tests[i]->A,tests[i]->m,tests[i]->n);
double *C = malloc(sizeof(*C)*tests[0]->m*tests[0]->n); if(!memcmp(R, dot_tests[i].C, sizeof(R)))
{
fprintf(stderr, "Dot product test %d passed.\n", i);
dot_passed += 1;
}
else
{
fprintf(stderr, "Dot product test %d failed:\n got matrix:\n\n", i);
pmatrix(stderr, R, 3, 3);
fprintf(stderr, " instead of:\n\n");
pmatrix(stderr, dot_tests[i].C, 3, 3);
}
}
fprintf(stderr, "\n\t%d dot products out of %d were succesful.\n\n", dot_passed, dot_total);
}
lina_scale(tests[0]->A,C,1.5,tests[0]->m,tests[0]->n);
pmatrix(C,tests[0]->m,tests[0]->n); // Evaluate scaling tests.
{
int scale_passed = 0;
int scale_total = sizeof(scale_tests) / sizeof(*scale_tests);
delete_tests(tests); for(int i = 0; i < scale_total; i += 1)
{
double R[9];
lina_scale(scale_tests[i].A, R, scale_tests[i].factor, 3, 3);
if(!memcmp(R, scale_tests[i].B, sizeof(R)))
{
fprintf(stderr, "Scaling test %d passed.\n", i);
scale_passed += 1;
}
else
{
fprintf(stderr, "Scaling test %d failed:\n got matrix:\n\n", i);
pmatrix(stderr, R, 3, 3);
fprintf(stderr, " instead of:\n\n");
pmatrix(stderr, scale_tests[i].B, 3, 3);
}
}
fprintf(stderr, "\n\t%d scalings out of %d were succesful.\n\n", scale_passed, scale_total);
}
return 0; return 0;
} }
void pmatrix(double *A,int m,int n){ 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, " | ");
for(int j = 0; j< n; j++) for(int j = 0; j< n; j++)
printf("%f\t",A[i*n + j]); fprintf(fp, "%g ",A[i*n + j]);
printf("\n"); fprintf(fp, "|\n");
} }
printf("\n"); fprintf(fp, "\n");
}
void create_tests(matrix_t **tests){
srand(1);
for(int i=0;i<NUM_TESTS;i++){
int rows;
int cols;
if(i < NUM_TESTS/3){
rows = 2;
cols = 3;
}
else if (i >= NUM_TESTS/3 && i < 2*NUM_TESTS/3)
{
rows = 3;
cols = 3;
}
else{
rows = 4;
cols = 3;
}
double *ptr = malloc(sizeof(*ptr)*rows*cols);
for(int j=0;j<rows*cols;j++){
ptr[j] = rand() % MAX_VALUE;
}
matrix_t *m_point = malloc(sizeof(matrix_t));
m_point->A = ptr;
m_point->m = rows;
m_point->n = cols;
tests[i] = m_point;
}
}
void delete_tests(matrix_t **tests){
for(int i=0;i<NUM_TESTS;i++){
free(tests[i]->A);
free(tests[i]);
}
tests = NULL;
} }