-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.c
172 lines (159 loc) · 3.97 KB
/
Matrix.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// Created by Samuel Prevost on 20/12/2019.
//
#include <stdio.h>
#include <assert.h>
#include "Matrix.h"
Matrix* mat(int rows, int cols, const double arr[rows][cols]){
Matrix* tail = NULL;
Matrix* head = NULL;
for (int i = 0; i < cols; ++i) {
Matrix* new_tail = malloc(sizeof(Matrix));
if(head == NULL){
head = new_tail;
}
if(tail != NULL){
tail->next = new_tail;
}
tail = new_tail;
tail->row = array_to_vect(rows, arr[i]);
}
return head;
}
Matrix* tr(const Matrix* mat){
Matrix* tail = copy_mat(mat);
Matrix* head = tail;
int* shape = shape_mat(mat);
for (int j = 0; j < shape[1]; ++j) {
tail->row = get_col(mat, j);
tail = tail->next;
}
return head;
}
double get_val(const Matrix* mat, int i, int j){
if (i == 0){
return get(mat->row, j);
}
if (mat->next != NULL){
return get_val(mat->next, i-1, j);
} else {
fprintf(stderr, "Index error !\n");
exit(1);
}
}
Matrix* copy_mat(const Matrix* mat){
Matrix* tail = NULL;
Matrix* head = NULL;
while (mat != NULL) {
Matrix* new_tail = malloc(sizeof(Matrix));
if(head == NULL){
head = new_tail;
}
if(tail != NULL){
tail->next = new_tail;
}
tail = new_tail;
tail->row = copy_vect(mat->row);
mat = mat->next;
}
return head;
}
int* shape_mat(const Matrix *mat){
int* shape_array = malloc(sizeof(int)*2);
shape_array[0] = 0;
shape_array[1] = len(mat->row);
while (mat != NULL) {
mat = mat->next;
shape_array[0]++;
}
return shape_array;
}
void print_mat(const Matrix *mat){
while (mat != NULL) {
Vector* row = mat->row;
while (row != NULL) {
printf("%9.5f, ", row->val);
row = row->next;
}
printf("\n");
mat = mat->next;
}
}
Vector* get_row(const Matrix* mat, int i){
while (mat != NULL && i--) {
mat = mat->next;
}
if (mat == NULL){
fprintf(stderr, "Index error !\n");
exit(1);
}
return mat->row;
}
Vector* get_col(const Matrix* mat, int j){
Vector* tail = NULL;
Vector* head = NULL;
while (mat != NULL) {
Vector* new_tail = malloc(sizeof(Vector));
if (head == NULL){
head = new_tail;
}
if (tail != NULL){
tail->next = new_tail;
}
tail = new_tail;
tail->val = get(mat->row, j);
mat = mat->next;
}
return head;
}
void set_inplace(Matrix* mat, double val, int i, int j){
while (mat != NULL && i--) {
mat = mat->next;
}
if (mat == NULL){
fprintf(stderr, "Index error !\n");
exit(1);
}
Vector* row = mat->row;
while (row != NULL && j--) {
row = row->next;
}
if (row == NULL){
fprintf(stderr, "Index error !\n");
exit(1);
}
row->val = val;
}
Matrix* set(const Matrix* mat, double val, int i, int j){
Matrix* res = copy_mat(mat);
set_inplace(res, val, i, j);
return res;
}
Matrix* invert2d(const Matrix* mat){
int* shape = shape_mat(mat);
assert(shape[0] == 2);
assert(shape[1] == 2);
Matrix* res = copy_mat(mat);
double det_val = det2d(mat);
if(fabs(det_val) < NULL_EPS){
fprintf(stderr, "Not invertible !\n");
exit(1);
}
Vector* row0 = mat->row;
Vector* row1 = mat->next->row;
set_inplace(res, get(row1, 1)/det_val, 0, 0);
set_inplace(res, get(row0, 0)/det_val, 1, 1);
set_inplace(res, -get(row0, 1)/det_val, 0, 1);
set_inplace(res, -get(row1, 0)/det_val, 1, 0);
return res;
}
double det2d(const Matrix* mat){
int* shape = shape_mat(mat);
assert(shape[0] == 2);
assert(shape[1] == 2);
Vector* row0 = mat->row;
double a = get(row0, 0), b = get(row0, 1);
Vector* row1 = mat->next->row;
double c = get(row1, 0), d = get(row1, 1);
return a*d - b*c;
}