-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrow_simplify.h
162 lines (128 loc) · 3.54 KB
/
row_simplify.h
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
//
// Created by 彭伟浩 on 2018/7/4.
//
#ifndef PROJECT_ROW_SIMPLIFY_H
#define PROJECT_ROW_SIMPLIFY_H
#include <stdlib.h>
#include "matrix_base.h"
void row_add(matrix m, int target, int source, double n, int start) { //将source行乘上n加到target行,从start一直计算到结束
for (int i = start; i < m.c; ++i) {
dex(m, target, i) += n * dex(m, source, i);
}
}
void row_multiply(matrix m, int target, double n, int start) {
for (int i = start; i < m.c; ++i) {
dex(m, target, i) *= n;
}
}
void row_swap(matrix m, int target, int source, int start) {
double temp;
for (int i = start; i < m.c; ++i) {
temp = dex(m, target, i);
dex(m, target, i) = dex(m, source, i);
dex(m, source, i) = temp;
}
}
//A化简为最简阶梯矩阵,LB与其同步,返回1则A可逆,返回0则A不可逆,返回-1则A和LB的行不相等
int rowSim(matrix A, matrix LB) {
if(A.r!=LB.r){
fprintf(stderr,"矩阵一的行数和矩阵二的行数不相等");
return -1;
}
int reversible=1;
int *main_dex = malloc(sizeof(int)*A.r);
int cl = 0;
int i,j;
double temp;
for (i = 0; i < A.r; ) {
if(cl>=A.c){
break;
}
//主元不能为零
if(dex(A,i,cl)==0)
for (j = i+1; j < A.r; ++j) {
if(dex(A,j,cl) != 0) {
rowChange(A,i,j,cl);
rowChange(LB,i,j,0);
}
};
//如果这列全是零
if(j>=A.r){
reversible=0;
++cl;
continue;
}
maindex[i]=cl;
//主元归一
temp=1.0/dex(A,i,cl);
rowMuti(A,i,temp,cl);
rowMuti(LB,i,temp,0);
//主元下方都归零
for (int k = i+1; k < A.r; ++k) {
if(dex(A,k,cl) != 0){
temp=-dex(A,k,cl);
rowAdd(A,k,i,temp,cl);
rowAdd(LB,k,i,temp,0);
}
}
cl++;
i++;
}
for (i = i-1; i > 0; --i) {
for (j = 0; j < i; ++j) {
if(dex(A,j,maindex[i]) != 0){
temp=-dex(A,j,maindex[i]);
rowAdd(A,j,i,temp,maindex[i]);
rowAdd(LB,j,i,temp,0);
}
}
}
return reversible;
}
//未测试。。。
//如果矩阵不可逆则返回零,可逆返回1
int reverseMatrix(matrix A,matrix LB){
if(A.r!=A.c) {
printf( "矩阵不可逆");
return 0;
}
int i,j;
double temp;
for (i = 0; i < A.r;i++) {
//主元不能为零
if(dex(A,i,i)==0)
for (j = i+1; j < A.r; ++j) {
if(dex(A,j,i) != 0) {
rowChange(A,i,j,i);
rowChange(LB,i,j,0);
}
};
//如果这列全是零
if(j>=A.r){
printf("矩阵不可逆");
return 0;
}
//主元归一
temp=1.0/dex(A,i,i);
rowMuti(A,i,temp,i);
rowMuti(LB,i,temp,0);
//主元下方都归零
for (int k = i+1; k < A.r; ++k) {
if(dex(A,k,i) != 0){
temp=-dex(A,k,i);
rowAdd(A,k,i,temp,i);
rowAdd(LB,k,i,temp,0);
}
}
}
//上三角化为单位阵
for (i = i-1; i > 0; --i) {
for (j = 0; j < i; ++j) {
if(dex(A,j,i) != 0){
rowAdd(LB,j,i,-dex(A,j,i),0);
}
}
}
return 1;
}
#endif //PROJECT_ROW_SIMPLIFY_H