This repository was archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix.cpp
204 lines (187 loc) · 6.47 KB
/
Matrix.cpp
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
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2017 Ivan Vaccari <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "Matrix.h"
Matrix::Matrix():maxColumnNumber(0){
}
Matrix::Matrix(unsigned int rows,unsigned int columns):maxColumnNumber(columns){
matrixData.resize(rows);
for(unsigned int i=0;i<rows;i++){
matrixData[i].resize(columns);
}
}
void Matrix::print(){
for(unsigned int i=0;i<matrixData.size();++i){
for(unsigned int k=0;k<matrixData[i].size();++k){
std::cout << std::left << std::setw(10) << std::setfill(' ') << matrixData[i][k];
//std::cout<<matrixData[i][k]<<"\t";
}
std::cout<<std::endl;
}
}
void Matrix::setRawCell(unsigned int row,unsigned int column,float data){
matrixData[row][column]=data;
}
float Matrix::getRawCell(unsigned int row,unsigned int column) const{
return matrixData[row][column];
}
unsigned int Matrix::sizeh() const{
return matrixData.size();
}
float Matrix::diagonalDeterminant(){
float determinant=1;
for(unsigned int row=0;row<matrixData.size();++row){
determinant*=matrixData[row][row];
}
return determinant;
}
bool Matrix::isInferiorTriangular(){
for(unsigned int row=0;row<matrixData.size()-1;++row){
for(unsigned int column=row+1;column<matrixData.size();++column){
if (matrixData[row][column]!=0)
return false;
}
}
return true;
}
bool Matrix::isSuperiorTriangular(){
for(unsigned int row=0;row<matrixData.size();++row){
for(unsigned int column=0;column<row;++column){
if (matrixData[row][column]!=0)
return false;
}
}
return true;
}
float Matrix::determinant(){
if(matrixData.size()!=maxColumnNumber){
throw std::string("Cant calculate determinant. Matrix is not square");
}else if (maxColumnNumber==2){
return matrixData[0][0]*matrixData[1][1]-matrixData[0][1]*matrixData[1][0];
}else{
float det=0;
int mul=-1;
for(unsigned int i=0;i<matrixData.size();++i){
mul=mul*(-1);
Matrix min=minor(i,0);
det+=mul*matrixData[i][0]*min.determinant();
}
return det;
}
}
void Matrix::replaceColumn(unsigned int columnNumber, std::vector<float> newColumn){
if (matrixData.size()!=newColumn.size())
throw std::string("Can't replace a column with the one provided. different sizes.");
if (maxColumnNumber<columnNumber)
throw std::string("Can't replace a column with the one provided. The matrix don't have this column number.");
for(unsigned int i=0;i<matrixData.size();++i)
matrixData[i][columnNumber]=newColumn[i];
}
void Matrix::swapRows(unsigned int row1,unsigned int row2){
for(unsigned int col=0;col<sizeh();col++){
float temp=matrixData[row1][col];
matrixData[row1][col]=matrixData[row2][col];
matrixData[row2][col]=temp;
}
}
void Matrix::makeIdentity(){
for(unsigned int i=0;i<matrixData.size();++i){
for(unsigned int k=0;k<matrixData[i].size();++k){
matrixData[i][k]=(k==i);
}
}
}
void Matrix::makeZero(){
for(unsigned int i=0;i<matrixData.size();++i){
for(unsigned int k=0;k<matrixData[i].size();++k){
matrixData[i][k]=0;
}
}
}
Matrix Matrix::minor(unsigned int row,unsigned int column){
Matrix m(matrixData.size()-1,maxColumnNumber-1);
int offset_column=0;
int offset_row=0;
for(unsigned int i=0;i<matrixData.size();++i){
if (i==row){
offset_row=-1;
continue;
}
for(unsigned int k=0;k<matrixData[i].size();++k){
if(k==column){
offset_column=-1;
continue;
}
m.setRawCell(i+offset_row,k+offset_column,matrixData[i][k]);
}
}
return m;
}
bool Matrix::loadFromFile(const std::string &fileName, const std::string &matrixName){
bool ret=false;
std::ifstream file(fileName.c_str());
std::string matrix_section("[matrix ");
matrix_section.append(matrixName);
matrix_section.append("]");
bool process=false;
int lineNumber=0;
if (file.is_open()){
std::string line;
while (std::getline(file,line)){
while ((line.length()>0) && (line.at(0)==' '))
line=line.substr(1);
while ((line.length()>0) && (line.at(line.length()-1)==' '))
line.resize(line.length()-1);
if (process && line.length()==0){
process=false;
ret=true;
break;
}else if (process){
//removing double spaces
line.append(" ");
std::string::size_type pos=line.find(" ");
while(pos!=std::string::npos){
line.replace(pos,2," ");
pos=line.find(" ");
}
pos=line.find(" ");
std::string::size_type start_pos=0;
matrixData.push_back(std::vector<float>());
while(pos!=std::string::npos){
std::string num=line.substr(start_pos,pos-start_pos);
matrixData[lineNumber].push_back(std::atof(num.c_str()));
start_pos=pos+1;
pos=line.find(" ",start_pos);
}
if (maxColumnNumber<matrixData[lineNumber].size())
maxColumnNumber=matrixData[lineNumber].size();
lineNumber++;
}else if (line==matrix_section){
process=true;
}
}
file.close();
for(unsigned int i=0;i<matrixData.size();++i){
while (matrixData[i].size()<maxColumnNumber){
matrixData[i].push_back(0);
}
}
}else{
throw std::string("Unable to open file.");
}
return ret;
}