Created
October 30, 2017 20:08
-
-
Save unreadable/1489cefdbef75cad4050e08d6c60a484 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
typedef unsigned int uint; | |
template <typename T> | |
class Matrix { | |
private: | |
T **mat, row, col; | |
public: | |
Matrix() : mat(nullptr), row(0), col(0) {} | |
Matrix(uint l, uint c) { | |
this->row = l; | |
this->col = c; | |
this->mat = new T*[l]; | |
for(uint i = 0; i < l; i++) { | |
this->mat[i] = new T[c]; | |
for(uint j = 0; j < col; j++) { | |
mat[i][j]=0; | |
} | |
} | |
} | |
Matrix(Matrix &other) { | |
if(other.mat) { | |
this->row = other.row; | |
this->col = other.col; | |
this->mat = new T*[other.row]; | |
for(uint i = 0; i < other.row; i++) { | |
this->mat[i] = new T[other.col]; | |
for(uint j = 0; j < col; j++) { | |
mat[i][j]=other.mat[i][j]; | |
} | |
} | |
} | |
} | |
~Matrix() { | |
if(this->mat) { | |
for(int i = 0; i < col; i++) { | |
delete[] this->mat[i]; | |
} | |
delete[] this->mat; | |
} | |
} | |
/* | |
* public methods | |
*/ | |
void fill() { | |
for(uint i = 0; i < this->row; i++) { | |
for(uint j = 0; j < this->col; j++) { | |
std::cout << "[" << i << "][" << j << "]="; | |
std::cin >> mat[i][j]; | |
} | |
} | |
} | |
void print() { | |
for(uint i = 0; i < this->row; i++) { | |
for(uint j = 0; j < col; j++) { | |
std::cout << mat[i][j] <<" "; | |
} | |
std::cout << '\n'; | |
} | |
} | |
Matrix& operator=(Matrix& other) { | |
if(this == &other) | |
return *this; | |
else { | |
if(this->mat) { | |
for(uint i = 0; i < this->row; i++) { | |
delete[] mat[i]; | |
} | |
delete []mat; | |
} else if(other.mat) { | |
this->mat = new T*[other.row]; | |
for(uint i = 0; i < other.row; i++) { | |
this->mat[i] = new T[other.col]; | |
} | |
this->row = other.row; | |
this->col = other.col; | |
} | |
for(uint i = 0; i < this->row; i++) { | |
for(uint j = 0; j < this->col; j++) { | |
this->mat[i][j] = other.mat[i][j]; | |
} | |
} | |
return *this; | |
} | |
} | |
Matrix operator*(Matrix& other) { | |
Matrix temp(this->row, other.col); | |
if(this->mat != nullptr && other.mat != nullptr) { | |
if(this->col != other.row) { | |
throw std::runtime_error("Invalid sizes!"); | |
} | |
for(uint i = 0; i < this->row; i++) { | |
for(uint j = 0; j < other.col; j++) { | |
temp.mat[i][j] = 0; | |
for(uint k=0; k < col; k++) { | |
temp.mat[i][j] += mat[i][k] * other.mat[k][j]; | |
} | |
} | |
} | |
return temp; | |
} else | |
exit(0); | |
} | |
T operator~() { | |
uint var = 1; | |
T a,b,d = 1; | |
// make a copy | |
Matrix temp(this->row,this->col); | |
for(uint i = 0; i < this->row; i++) { | |
for(uint j = 0; j < this->col; j++) { | |
temp.mat[i][j] = mat[i][j]; | |
} | |
} | |
//forma esalon | |
for(uint i = 0; i< temp.row - var; i++) { | |
for(uint j=0; j < temp.col; j++) { | |
if(i == j) { | |
if(temp.mat[i][j] == temp.mat[i+var][j]) { //cazul in care el de pe diag principala==cu elementul de sub el | |
for(uint p = j; p < temp.col; p++) { | |
temp.mat[i+var][p]=temp.mat[i+var][p]-temp.mat[i][p];//scad liniile | |
} | |
var++; | |
i--; | |
} else if(temp.mat[i][j]!=temp.mat[i+var][j] &&temp.mat[i][j]!=0) { //cazul in care el de pe diag princupala!= cu elementul de sub el { | |
a = temp.mat[i][j]; | |
b = temp.mat[i+var][j]; | |
if(b != 0) { | |
for(uint p = j; p < temp.col; p++) { | |
temp.mat[i+var][p] = a * temp.mat[i+var][p] - b * temp.mat[i][p]; | |
if(temp.mat[i][j] != 0) { | |
temp.mat[i+var][p] /= a; //impartim la elem de pe diag principala | |
} | |
} | |
var++; | |
i--; | |
} else { | |
var++; | |
i--; | |
} | |
} else if(temp.mat[i][j] == 0) { | |
d = d * perm(i, j, temp); | |
i--; | |
} | |
if(i+var==temp.lin-1) { //conditia in care se ajunge la finalul liniei | |
i++; | |
var = 1; | |
} | |
} | |
} | |
} | |
//calculare determinant | |
for(uint i = 0; i < this->row; i++) { | |
for(uint j = 0; j < this->col; j++) { | |
if(i==j) { | |
d *= temp.mat[i][j]; | |
} | |
} | |
} | |
return d; | |
} | |
double perm(uint k, uint p, Matrix &temp) { | |
T aux; | |
uint j = p, nr = 1, ok = 0; | |
for(uint i = k; i < temp.row; i++) { | |
if(temp.mat[i][j]==0 && temp.mat[i+nr][j] != 0) { | |
for(j; j < temp.col; j++) { | |
aux = temp.mat[i+nr][j]; | |
temp.mat[i+nr][j] = temp.mat[i][j]; | |
temp.mat[i][j] = aux; | |
} | |
ok =- 1; | |
break; | |
} else { | |
nr++; | |
i--; | |
} | |
if(ok ==- 1) | |
break; | |
} | |
if(ok==-1) | |
return ok; | |
else | |
return ok; | |
} | |
Matrix operator+(Matrix& other) { | |
Matrix temp(this->row, this->col); | |
if(this->mat != nullptr && other.mat != nullptr) { | |
if(this->row != other.row || this->col != other.col) { | |
exit(0); | |
} | |
for(uint i = 0;i < this->row; i++) { | |
for(uint j=0; j < this->col; j++) { | |
temp.mat[i][j] = mat[i][j] + other.mat[i][j]; | |
} | |
} | |
return temp; | |
} | |
else | |
exit(0); | |
} | |
Matrix operator+(int n) { | |
Matrix temp(this->row, this->col); | |
if(this->mat != NULL) { | |
for(uint i = 0; i < this->row; i++) { | |
for(uint j = 0; j < this->col; j++) { | |
temp.mat[i][j] = mat[i][j] + n; | |
} | |
} | |
return temp; | |
} | |
else | |
exit(0); | |
} | |
Matrix operator-(int n) { | |
Matrix temp(this->row, this->col); | |
if(mat != nullptr) { | |
for(uint i = 0; i < this->row; i++) { | |
for(uint j = 0; j < this->col; j++) { | |
temp.mat[i][j] = mat[i][j] - n; | |
} | |
} | |
return temp; | |
} | |
else | |
exit(0); | |
} | |
T& operator()(uint i, uint j) { | |
T empty; | |
if(this->mat != NULL && i < this->row && j < this->col) { | |
return this->mat[i][j]; | |
} | |
else | |
return empty; | |
} | |
}; | |
int main() { | |
Matrix<double> mat(3, 5); | |
} | |
// https://pastebin.com/DdxkBYXs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment