Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Matrix.hpp"
- using namespace std;
- struct Matrix::rcMatrix
- {
- int rows;
- int cols;
- double** matrix;
- int refCounter;
- rcMatrix(int r, int c, double** mat)
- {
- refCounter=1;
- rows=r;
- cols=c;
- matrix=mat;
- }
- ~rcMatrix()
- {
- for(int i=0;i<rows;i++)
- {
- delete [] matrix[i];
- }
- delete [] matrix;
- }
- private:
- rcMatrix& operator = (const rcMatrix&);
- };
- Matrix::Matrix(const char* nazwa)
- {
- int r,c; double** tmp;
- fstream plik;
- plik.open(nazwa,ios::in);
- if(plik.good()==true)
- {
- plik>>r;
- plik>>c;
- tmp=new double* [r];
- for(int i=0;i<r;i++)
- {
- tmp[i]=new double [c];
- }
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<c;j++) plik>>tmp[i][j];
- }
- this->data=new rcMatrix(r,c,tmp);
- plik.close();
- }
- else
- {
- cout<<"Brak dostepu do pliku o nazwie '"<<nazwa<<"'!\n";
- throw wrong_file_name();
- }
- }
- Matrix::Matrix(int a, int b)
- {
- double** tmp=new double* [a];
- for(int i=0;i<a;i++) tmp[i]=new double [b];
- for(int i=0;i<a;i++)
- {
- for(int j=0;j<b;j++)tmp[i][j]=0;
- }
- this->data = new rcMatrix(a,b,tmp);
- }
- Matrix::Matrix(const Matrix& M)
- {
- int r=M.data->rows; int c=M.data->cols;
- double** tmp=new double* [r];
- for(int i=0;i<r;i++) tmp[i]=new double [c];
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<c;j++) tmp[i][j]=M.data->matrix[i][j];
- }
- this->data=new rcMatrix(r,c,tmp);
- }
- Matrix::~Matrix() { delete this->data; }
- double Matrix::operator()(int x, int y) const
- {
- if(x>this->data->rows || y>this->data->cols) throw bad_index();
- return data->matrix[x-1][y-1];
- }
- ostream& operator<<(ostream& out, const Matrix& M)
- {
- for(int i=0;i<M.data->rows;i++)
- {
- for(int j=0;j<M.data->cols;j++) out<<M.data->matrix[i][j]<<"\t";
- out<<endl;
- }
- return out;
- }
- bool Matrix::operator==(const Matrix& M) const
- {
- if(this->data->rows!=M.data->rows) return false;
- if(this->data->cols!=M.data->cols) return false;
- for(int i=0;i<this->data->rows;i++)
- {
- for(int j=0;j<this->data->cols;j++) if(this->data->matrix[i][j]!=M.data->matrix[i][j]) return false;
- }
- return true;
- }
- Matrix& Matrix::operator=(const Matrix& M)
- {
- int r=M.data->rows; int c=M.data->cols;
- int rc=M.data->refCounter;
- double** tmp=new double*[r];
- for(int i=0;i<r;i++) tmp[i]=new double[c];
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<c;j++) tmp[i][j]=M.data->matrix[i][j];
- }
- this->data->rows=r;
- this->data->cols=c;
- this->data->refCounter=rc;
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<c;j++) this->data->matrix[i][j]=tmp[i][j];
- }
- return *this;
- }
- Matrix Matrix::operator+(const Matrix& M) const
- {
- Matrix tmp(*this);
- tmp+=M;
- return tmp;
- }
- Matrix& Matrix::operator+=(const Matrix& M)
- {
- if(this->data->rows==M.data->rows && this->data->cols==M.data->cols)
- {
- for(int i=0;i<this->data->rows;i++)
- {
- for(int j=0;j<this->data->cols;j++) this->data->matrix[i][j]=this->data->matrix[i][j]+M.data->matrix[i][j];
- }
- }
- else throw different_sizes();
- return *this;
- }
- Matrix Matrix::operator-(const Matrix& M) const
- {
- Matrix tmp(*this);
- tmp-=M;
- return tmp;
- }
- Matrix& Matrix::operator-=(const Matrix& M)
- {
- if(this->data->rows==M.data->rows && this->data->cols==M.data->cols)
- {
- for(int i=0;i<this->data->rows;i++)
- {
- for(int j=0;j<this->data->cols;j++) this->data->matrix[i][j]=this->data->matrix[i][j]-M.data->matrix[i][j];
- }
- }
- else throw different_sizes();
- return *this;
- }
- Matrix Matrix::operator*(const Matrix& M) const
- {
- Matrix tmp(*this);
- tmp*=M;
- return tmp;
- }
- Matrix& Matrix::operator*=(const Matrix& M)
- {
- Matrix tmp(*this);
- int m, n, p;
- m=this->data->rows;
- n=this->data->cols;
- p=M.data->cols;
- for(int i=0;i<m;i++) delete [] this->data->matrix[i];
- delete [] this->data->matrix;
- this->data->matrix=new double* [m];
- for(int i=0;i<m;i++) this->data->matrix[i]=new double [p];
- if(this->data->cols==M.data->rows)
- {
- this->data->rows=m;
- this->data->cols=p;
- for(int i=0;i<m;i++)
- {
- for(int j=0;j<p;j++)
- {
- this->data->matrix[i][j]=0;
- for(int k=0;k<n;k++)
- {
- this->data->matrix[i][j]=this->data->matrix[i][j]+tmp.data->matrix[i][k]*M.data->matrix[k][j];
- }
- }
- }
- }
- else throw different_sizes();
- return *this;
- }
- int Matrix::getRefCounter() const
- {
- return this->data->refCounter;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement