-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeutils.c
244 lines (193 loc) · 4.94 KB
/
geutils.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/***
readFile()
read file of matrix coefficients
data[][]: 2D array to store coefficients (output)
n: number of rows (input)
fp: pointer to FILE struct for input file (input)
****/
void readFile(double *data, int n, FILE *fp) {
int i, ok;
for (i=0; i< n * (n+1); i++) {
ok = fscanf(fp, "%lf", &data[i]);
}
}
/***
printRow()
print row r of mat[][]
mat[][]: nR x nR+1 matrix (input)
r: row to print
nR: number of rows
***/
void printRow(double *mat, int r, int nR) {
int j;
for (j=0; j<nR+1; j++) {
printf("%f ", mat[r * (nR+1) + j]);
}
printf("\n");
}
void fprintRow(FILE *fp, double *mat, int r, int nR) {
int j;
for (j=0; j<nR+1; j++) {
fprintf(fp, "%f ", mat[r * (nR+1) + j]);
}
fprintf(fp, "\n");
}
/***
reduce()
row-reduce a row, based on pivot row, from startIndex to endIndex inclusive
myRow: row to be reduced (input/output)
pivotRow: pivot row (input)
startIndex: index of 1st element in row to be reduced (input)
endIndex: index of last element in row to be reduced (input)
****/
void reduce(double *myRow, double *pivotRow, int startIndex, int endIndex) {
int i;
float factor;
float oldMr;
factor = -myRow[startIndex];
myRow[startIndex] = 0;
for (i=startIndex+1; i<=endIndex; i++) {
oldMr = myRow[i];
myRow[i] = myRow[i] + pivotRow[i] * factor;
}
}
void par_reduce(double *myRow, double *pivot, int start, int end) {
int i;
float factor;
factor = -myRow[start]/pivot[start];
myRow[start] = 0.0;
for (i = start+1; i < end; i++) {
myRow[i] = myRow[i] + pivot[i] * factor;
}
}
/***
backSub()
perform back substitution on nR x nR+1 system of equations
s: array of nR solutions (output)
mat: upper triangular matrix (nR x nR+1) of coefficients (input)
nR: number of rows in mat
***/
void backSub(double *s, double *mat, int nR) {
int i, j;
double sum = 0;
for (i=nR-1; i>=0; i--) {
sum = 0;
for (j=nR-1; j>i; j--) {
sum += mat[i*(nR+1) + j] * s[j];
}
s[i] = (mat[i*(nR+1) + nR] - sum) / mat[i*(nR+1) + i];
}
}
/***
checkSoln()
check solutions by substituting into original system of equations
s: array of nR solutions (input)
mat: matrix of nR x nR+1 coefficients
nR: number of rows in mat
***/
int checkSoln(double *s, double *mat, int nR) {
int i, j;
int ok = 1;
double sum;
double diff;
for (i=0; i<nR; i++) {
sum = 0;
for (j=0; j<nR; j++) {
sum += s[j] * mat[i * (nR+1) + j];
}
diff = sum - mat[i * (nR+1) + nR];
if (isnan(diff) || diff >= .005 || diff < -.005) {
ok = 0;
printf("Mismatch at row %d %f %f %f\n", i, diff, sum, mat[i * (nR+1) + nR]);
}
}
return ok;
}
int checkEqn(double *mat0, double *mat1, int nR) {
int i, j;
double diff;
int ok=1;
for (i=0; i<nR; i++) {
for (j=0; j<nR+1; j++) {
diff = fabs(mat0[i * (nR+1) + j] - mat1[i * (nR+1) + j]);
if (diff >= .005) {
ok = 0;
printf("Mismatch at %d %d %f %f %f\n", i, j, diff,
mat0[i * (nR+1) + j], mat1[i * (nR+1) + j]);
}
}
}
}
/***
findMaxRow()
find row i with max(eqn[][pivotIndex]), return i
eqn: nR x nR+1 matrix of coefficients (input)
pivotIndex: index of column for max search (input)
nR: number of rows in eqn[][] (input)
***/
int findMaxRow(double *eqn, int pivotIndex, int nR) {
int i, maxI;
double max;
max = fabs(eqn[pivotIndex * (nR+1) + pivotIndex]);
maxI = pivotIndex;
for (i=pivotIndex+1; i<nR; i++) {
if (fabs(eqn[i*(nR+1) + pivotIndex]) > max) {
max = fabs(eqn[i*(nR+1) + pivotIndex]);
maxI = i;
}
}
return maxI;
}
int par_findMaxRow(double *my_eqn, int pivotRow, int pivotCol, int nR, int nC) {
int i, maxI;
double max;
max = fabs(my_eqn[pivotRow * nC + pivotCol]);
maxI = pivotRow;
for (i=pivotRow+1; i<nR; i++) {
if (fabs(my_eqn[i*nC + pivotCol]) > max) {
max = fabs(my_eqn[i*nC + pivotCol]);
maxI = i;
}
}
return maxI;
}
/***
swapRows()
swap two rows in mat[][]
mat: nR x nR+1 matrix of coefficients (input/output)
maxRow: index of one row (input)
pivotIndex: index of other row (input)
nR: number of rows in mat[][] (input)
***/
void swapRows(double *mat, int maxRow, int pivotIndex, int nR) {
double temp;
int i;
for (i=0; i<nR+1; i++) {
temp = mat[maxRow * (nR+1) + i];
mat[maxRow * (nR+1) + i] = mat[pivotIndex * (nR+1) + i];
mat[pivotIndex * (nR+1) + i] = temp;
}
}
/***
dump file of row-reduced system and solutions
***/
void dumpData(double *s, double *mat, int nR) {
FILE *fp;
int i, j;
fp = fopen("dump.out", "w");
fprintf(fp, "%d\n", nR);
for (i=0; i<nR; i++) {
fprintf(fp, "%d ", i);
for (j=0; j<nR+1; j++) {
fprintf(fp, "%f ", mat[i * (nR+1) + j]);
}
fprintf(fp, "\n");
}
for (i=0; i<nR; i++) {
fprintf(fp, "%d %f\n", i, s[i]);
}
fclose(fp);
}