-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhelpers.c
153 lines (134 loc) · 3.06 KB
/
helpers.c
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*----------------------------------------------------------------
* File: helpers.c
*----------------------------------------------------------------
*
* Author: Marek Rychlik ([email protected])
* Date: Sun Sep 22 15:46:13 2024
* Copying: (C) Marek Rychlik, 2020. All rights reserved.
*
*----------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "helpers.h"
void matrix_times_vector(int n, const double A[n][n], const double x[n],
double y[n])
{
for(int i = 0; i < n; ++i) {
y[i] = 0;
for(int j = 0; j < n; ++j)
y[i] += A[i][j] * x[j];
}
}
double norm(const int n, const double x[n])
{
double S = 0;
for(int j = 0; j < n; ++j) {
S += x[j] * x[j];
}
return sqrt(S);
}
double norm_dist(const int n, const double x[n], const double y[n])
{
double S = 0;
for(int j = 0; j < n; ++j) {
S += (x[j]-y[j]) * (x[j]-y[j]);
}
return sqrt(S);
}
double frobenius_norm(const int n, const double X[n][n])
{
double S = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
S += X[i][j] * X[i][j];
}
}
return sqrt(S);
}
double frobenius_norm_dist(const int n, const double X[n][n], const double Y[n][n])
{
double S = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
S += (X[i][j]-Y[i][j]) * (X[i][j]-Y[i][j]);
}
}
return sqrt(S);
}
void print_vector(int n, double x[n])
{
for(int i=0; i<n; ++i) {
printf("%g\n", x[i]);
}
puts("\n");
}
void print_matrix(int n, double A[n][n], int flag)
{
switch(flag) {
case FLAG_WHOLE:
for(int i = 0; i<n; ++i) {
for(int j = 0; j<n; ++j) {
printf("%12.6g\t", A[i][j]);
}
puts("\n");
}
break;
case FLAG_LOWER_PART:
for(int i = 0; i<n; ++i) {
for(int j = 0; j<i; ++j) {
printf("%12.6g\t", A[i][j]);
}
printf("%12.6g\t", 1.0);
for(int j = i+1; j<n; ++j) {
printf("%12.6g\t", 0.0);
}
puts("\n");
}
break;
case FLAG_UPPER_PART:
for(int i = 0; i<n; ++i) {
for(int j = 0; j<i; ++j) {
printf("%12.6g\t", 0.0);
}
for(int j = i; j<n; ++j) {
printf("%12.6g\t", A[i][j]);
}
puts("\n");
}
break;
}
puts("\n");
}
// Function to generate a random square matrix of size 'n'
void generate_random_matrix(int n, double matrix[n][n])
{
// Seed the random number generator with the current time
srand(time(0));
// Fill the matrix with random integers between 0 and 99
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = rand() % 100;
}
}
}
void create_matrix(int n, double (**matrix)[n])
{
double *store = (double *)malloc(n * n * sizeof(double));
double (*A)[n] = (double (*)[n])store;
*matrix = A;
}
void destroy_matrix(int n, double (*matrix)[n])
{
double *store = (double *)matrix;
free(store);
}
void copy_matrix(int n, const double A[n][n], double A_copy[n][n])
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A_copy[i][j] = A[i][j];
}
}
}