-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrice.c
More file actions
102 lines (86 loc) · 2.27 KB
/
matrice.c
File metadata and controls
102 lines (86 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <math.h>
double* malloc_vect(int rows){
double* vect = calloc(rows, sizeof(double));
return vect;
}
double** malloc_mat(int rows, int cols){
double** mat = malloc(rows * sizeof(double *));
for(int r = 0; r < rows; ++r) mat[r] = malloc(cols * sizeof(double));
return mat;
}
void free_mat(double** mat, int rows){
for(int r = 0; r < rows; r++) free(mat[r]);
free(mat);
}
void print_vect(double* vect, int rows)
{
for (int r = 0; r < rows; ++r) {
printf("| %.6f ", vect[r]);
printf("|\n");
}
printf("\n");
}
void print_mat(double** mat, int rows, int cols){
printf("mat[%d][%d] = \n", rows, cols);
for (int r = 0; r < rows; r++) {
printf(" |");
for (int c = 0; c < cols; c++) {
printf(" %.2f", mat[r][c]);
}
printf(" |\n");
}
printf("\n");
}
void copy_matA_to_matB(int rows, int cols, double matA[rows][cols], double** matB)
{
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
matB[r][c] = matA[r][c];
}
}
}
void M_times_a_plus_b(double** Mat, double* a, double* b, double* result, int rows, int cols)
{
for (int r = 0; r < rows; r++) {
result[r] = 0;
for (int c = 0; c < cols; c++) {
result[r] += Mat[r][c] * a[c];
}
result[r] = result[r] + b[r];
}
}
double* multiply_mat_vect(double** mat, double* in_vect, int rows, int cols)
{
double* out_vect = calloc(rows, sizeof(double));
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
out_vect[r] += mat[r][c] * in_vect[c];
}
}
return out_vect;
}
double vect_norm(double* vect, size_t n){
double norm = 0;
for (size_t x = 0; x <n ; x++) {
norm += sqrtf(vect[x] * vect[x]);
}
return norm;
}
/* Fills a Vector with random stuff */
void fill_vect(double* vect, int rows){
for (int r = 0; r < rows; ++r) {
vect[r] = drand48() - 0.5;
}
}
/* Fills a Matrix with random stuff */
void fill_mat(double** mat, int rows, int cols){
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
mat[r][c] = drand48() - 0.5;
}
}
}