-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_mat.cpp
73 lines (56 loc) · 1.48 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
/*
* lib_mat.cpp
*
* Created on: May 7, 2022
* Author: d-w-h
*/
#include <math.h>
#include <stdio.h>
#include <time.h>
#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_di(-25, 25);
if(fabs(rand_num_loc) <= SMALL_NUM) { rand_num_loc = 0.0; }
mat[i][j] = rand_num_loc;
}
}
}
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("%.3f ", fabs(mat[i][j]));
}
printf("\n");
}
}
void print_mat_singular() {
printf("Input matrix is singular\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;
}
}
}