-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLinearSystemSolver.cpp
More file actions
154 lines (143 loc) · 3.74 KB
/
Copy pathMyLinearSystemSolver.cpp
File metadata and controls
154 lines (143 loc) · 3.74 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
//
// Created by Chenxi Ma on 4/25/22.
//
#include <iostream>
#include <cassert>
#include "MyLinearSystemSolver.h"
MyLinearSystemSolver::MyLinearSystemSolver (std::size_t rows, std::size_t cols, double val)
: _rows{rows},
_cols{cols},
_data(rows * cols, val)
{
assert (rows > 0);
assert (cols > 0);
}
MyLinearSystemSolver::~MyLinearSystemSolver() {
}
void MyLinearSystemSolver::SwapRows(std::size_t i, std::size_t j)
{
for(std::size_t k=0; k<_cols; k++)
{
double temp = (*this)(i,k);
(*this)(i,k)= (*this)(j,k);
(*this)(j,k)=temp;
}
}
void MyLinearSystemSolver::GaussianElimination()
{
for(std::size_t col=0; col<_cols-1; ++col)
{
std::size_t MaxValueIndex=col;
double MaxValue=std::abs((*this)(col,col));
for(std::size_t row=col+1; row<_rows;++row)
{
if(std::abs((*this)(row,col))>std::abs(MaxValue))
{
MaxValue=(*this)(row,col);
MaxValueIndex=row;
}
}
if(MaxValue==0)
{
std::cout<<"the linear system has infinite root"<<std::endl;
exit(0);
}
if(MaxValueIndex!=col)
this->SwapRows(col, MaxValueIndex);
for (std::size_t row=col+1; row<_rows; ++row )
{
double f = (*this)(row,col)/(*this)(col,col);
for (std::size_t i=col; i<_cols; ++i)
{
(*this)(row,i) -= (*this)(col,i) * f;
}
}
}
}
MyLinearSystemSolver MyLinearSystemSolver::BackSub()
{
MyLinearSystemSolver x(_rows,1);
for (int i = _rows-1; i>=0; --i)
{
x._data[i] = double( (*this)(i,_rows));
for (std::size_t j=i+1; j<_rows; ++j)
{
x._data[i] -=double( (*this)(i,j) * x._data[j]);
}
x._data[i] = double(x._data[i]/(*this)(i,i));
}
return x;
}
//this function has not been used
MyLinearSystemSolver MyLinearSystemSolver::MatrixMultiplication(const MyLinearSystemSolver &x)
{
MyLinearSystemSolver y(_rows,x._cols);
for (std::size_t i=0; i<_rows; ++i)
{
for (std::size_t j=0; j<x._cols; ++j) {
for (std::size_t k=0; k<_cols; ++k)
y(i, j) += (*this)(i,k) * x(k,j);
}
}
return y;
}
MyLinearSystemSolver MyLinearSystemSolver::operator-(const MyLinearSystemSolver& x)
{
MyLinearSystemSolver M(_rows,_cols);
for (std::size_t i=0; i<_rows; ++i)
{
for (std::size_t j=0; j<_cols; ++j)
{
M(i,j) = (*this)(i,j) - x(i,j);
}
}
return M;
}
//To verify if two matrix is same.
bool MyLinearSystemSolver::operator==(const MyLinearSystemSolver& x)
{
int n = 0;
if((_rows != x._rows)||(_cols != x._cols))
{
return false;
}
else
{
for (std::size_t i=0; i<_rows; ++i)
{
for (std::size_t j=0; j<_cols; ++j)
{
if (std::abs((*this)(i,j)-x(i,j))>10e-5)
n+=1;
}
}
if (n==0)
return true;
else
return false;
}
}
//To verify if the root we got is correct
MyLinearSystemSolver MyLinearSystemSolver::verify(const MyLinearSystemSolver& sol)
{
MyLinearSystemSolver y(_rows,1);
for (std::size_t i=0; i<_rows; ++i)
{
for (std::size_t k=0; k<_rows; ++k)
y(i, 0) += (*this)(i,k) * sol(k,0);
}
for (std::size_t i=0; i<_rows; ++i)
{
y(i,0) -= (*this)(i,_cols-1);
}
return y;
}
std::ostream& operator<< (std::ostream& os, const MyLinearSystemSolver& a) {
for (std::size_t row=0; row < a.nrows(); ++row) {
for (std::size_t col=0; col < a.ncols(); ++col) {
os << a(row, col) << " ";
}
os << std::endl;
}
return os;
}