-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaussElimination.cpp
More file actions
185 lines (162 loc) · 3.29 KB
/
GaussElimination.cpp
File metadata and controls
185 lines (162 loc) · 3.29 KB
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
/** An Example to solve Equations Ax = b **/
/*
* Using Gaussian Elimination and Column Pivot Element
*
*/
#include <iostream>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include "assert.h"
using namespace std;
const double eps = pow(10,-10);
double absDou(double a)
{
return (a >= 0)?a:(-a);
}
// Use functions to get martix A and vector b
void getA(vector<vector<double> >& A, int n)
{
/*
double a; // a number in A
vector<double> tmp(n, 0.0);
cout<<"Please input the matrix A:"<<endl;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cin>>a;
tmp.push_back(a); // Save row vector in tmp first
}
A.push_back(tmp); // Then put tmp into A
tmp.clear(); // Clean tmp for next row vector
}
*/
cout<<"Please input the matrix A:"<<endl;
double a;
for(int i = 0; i < n; i++)
for(int j = 0; j < n ;j++)
{
cin>>a;
A[i][j] = a;
}
}
void getb(vector<double>& b, int n)
{
/*
double a; // a number in b
cout<<"Please input the vector b:"<<endl;
for (int i = 0; i < n; i++)
{
cin>>a;
b.push_back(a);
}
*/
cout<<"Please input the vector b:"<<endl;
double a;
for(int i = 0; i < n; i++)
{
cin>>a;
b[i] = a;
}
}
//Solve the problem by Gaussian Elimination
void GaussElimination(vector<vector<double> >& A, vector<double>& b, vector<double>& x)
{
//assert(A.size() == A[0].size() && A.size() == b.size());
int n = A.size();
double tar;
int pivot;
vector<double> swa;
for(int j = 0; j < n; j++)
{
// Find column pivot element,
// the largest number in column vector
pivot = j;
for (int i = j + 1; i < n; i++)
{
if(absDou(A[i][j]) > absDou(A[pivot][j]))
{
pivot = i;
}
}
// swap A[j] with the vector has largest number in j
if(absDou(A[pivot][j]) < eps)
{ // at this time, det(A) = 0
cerr<<"Error! The equations may have no solution."<<endl;
exit(1);
}
if(pivot != j)
{
swa = A[j];
A[j] = A[pivot];
A[pivot] = swa;
tar = b[j];
b[j] = b[pivot];
b[pivot] = tar;
}
// Gaussian Elimination
for(int i = j + 1; i < n; i++)
{
double c = A[i][j]/A[j][j]; // The rate to minus
A[i][j] = 0;
for(int k=j+1;k<n;k++)
{
A[i][k] = A[i][k] - c*A[j][k];
}
b[i] = b[i] - c*b[j];
}
}
// Return to get vector x
for(int i = n - 1; i >= 0; i--)
{
tar = 0.0; //b[i] should minus something
for(int k = i + 1; k < n; k++)
{
tar += x[k] * A[i][k];
}
x[i] = (b[i] - tar)/A[i][i];
}
}
int main()
{
int n = 0; // The rank of A
cout<<"Solving Ax = b by Gaussian Elimination"<<endl;
cout<<"--------------------------------------"<<endl;
cout<<"This program can only solve questions "
"with just one solution.\n"<<endl;
while(n<1)
{
cout<<"Please input the rank of A:";
cin>>n;
if (n<1)
cout<<"Please input a number larger than 1!"<<endl;
}
/*
vector<vector<double> > A;
vector<double> b, x;
*/
vector<vector<double> > A(n, vector<double>(n, 0.0));
vector<double> b(n, 0.0), x(n, 0.0);
getA(A ,n);
getb(b, n);
/*
n = 2;
vector<vector<double> > A(n, vector<double>(n, 0.0));
vector<double> b(n, 0.0), x(n, 0.0);
A[0][0] = 2; A[0][1] = 3;
A[1][0] = 5; A[1][1] = 4;
b[0] = 8;
b[1] = 13;
*/
GaussElimination(A, b, x);
// To Solve
cout<<"The solution x is:"<<endl;
//Output
for(int i = 0; i < n; i++)
{
cout<<x[i]<<" ";
}
cout<<endl;
return 0;
}