-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_mat.cpp
110 lines (88 loc) · 2.31 KB
/
lib_mat.cpp
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
/*
* lib_mat.cpp
*
* Created on: May 7, 2022
* Author: d-w-h
*/
#include <math.h>
#include <stdio.h>
#include <time.h>
#include "lib_testing.hpp"
#include "lib_testing_ref.hpp"
#include "user_types.hpp"
double rand_num(double min, double max) {
double val = (double) rand() / (RAND_MAX + 1.0);
return val * (max - min) - (max - min) / 2;
}
double rand_di(int min, int max) {
return rand() % (max - min) + min;
}
void init_mat(int n, double ** mat) {
srand((unsigned) time(NULL));
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
double rand_num_loc = rand_num(-25, 25);
if(fabs(rand_num_loc) <= SMALL_NUM) { rand_num_loc = 0.0; }
mat[i][j] = rand_num_loc;
}
}
}
void set_mat_to_vec2D(double ** mat, int n, i_real_matrix & mat_inv) {
for(int i = 0; i < n; ++i) {
i_real_vector vec_loc;
for(int j = 0; j < n; ++j) {
vec_loc.push_back(mat[i][j]);
}
mat_inv.push_back(vec_loc);
}
}
void set_mat_to_matrix(double ** mat, int n, matrix & m) {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
m(i, j) = mat[i][j];
}
}
}
void set_mat_to_matxd(double ** mat, int n, MatrixXd & mat_out) {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
mat_out(i, j) = mat[i][j];
}
}
}
void set_mat(double ** mat, int n, double ** mat_store) {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
mat_store[i][j] = mat[i][j];
}
}
}
void print_mat(double ** mat, int n) {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
printf("%.5f ", mat[i][j]);
}
printf("\n");
}
printf("\n");
}
void print_matxd(MatrixXd mat, int n) {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
printf("%.5f ", mat(i, j));
}
printf("\n");
}
printf("\n");
}
void mat_mult_sq(double ** A, double ** A_inv, int n, double ** mat_res) {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
double sum_loc = 0;
for(int k = 0; k < n; ++k) {
sum_loc = sum_loc + A[i][k] * A_inv[k][j];
}
mat_res[i][j] = sum_loc;
}
}
}