Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Matrix.cpp
- #include "matrix.h"
- #include <iostream>
- #include <cstdlib>
- #include <time.h>
- #include <string.h>
- Matrix::Matrix(){}
- Matrix::~Matrix(){
- for(int i = 0; i < this->m; i++){
- delete []this->matrix[i];
- }
- delete []this->matrix;
- }
- Matrix::Matrix(int m, int n){
- this->m = m;
- this->n = n;
- allocate(this->m, this->n);
- }
- // KONSTRUKTOR TWORZ¥CY MACIERZ NA PODSTAWIE TABLICY
- Matrix::Matrix(double** array, int m, int n){
- this->m = m;
- this->n = n;
- allocate(this->m, this->n);
- for(int y = 0; y < m; y++){
- for(int x = 0; x < n; x++){
- this->matrix[y][x] = array[y][x];
- }
- }
- }
- Matrix::Matrix(Matrix &matrix){
- this->m = m;
- this->n = n;
- allocate(this->m, this->n);
- int tempM = matrix.getM();
- int tempN = matrix.getN();
- for(int y = 0; y < tempM; y++){
- for(int x = 0; tempN < n; x++){
- this->matrix[y][x] = matrix.matrix[y][x];
- }
- }
- }
- // ZMIANA ROZMIARU MACIERZY (ze strat¹ danych)
- void Matrix::reallocate(double** path, int newM, int newN){
- for(int i = 0; i < newM; i++)
- delete[] path[i];
- delete[] path;
- allocate(newM, newN);
- this->m = newM;
- this->n = newN;
- }
- // ALOKACJA PAMIÊCI dla macierzy o wymiarze m X n
- void Matrix::allocate(int m, int n){
- double** path = new double *[m];
- for(int i = 0; i < m; i++){
- path[i] = new double[n];
- }
- for(int i = 0; i < m; i++){
- for(int j = 0; j < n; j++){
- path[i][j] = 0;
- }
- }
- this->matrix = path;
- }
- void Matrix::allocate(double** paths, int m, int n){
- double** path = new double *[m];
- for(int i = 0; i < m; i++){
- path[i] = new double[n];
- }
- for(int i = 0; i < m; i++){
- for(int j = 0; j < n; j++){
- path[i][j] = 0;
- }
- }
- paths = path;
- }
- // FUNKCJA TWORZ¥CA MACIERZ Z LOSOWYCH DOUBLE
- void Matrix::randomDoubleMatrix(){
- srand(time(NULL));
- if(this->matrix == NULL){
- allocate(this->m, this->n);
- }
- for(int y = 0; y < m; y++){
- for(int x = 0; x < n; x++){
- this->matrix[y][x] = randDouble();
- }
- }
- }
- // FUNKCJA LOSUJ¥CA POJEDYNCZY DOUBLE
- double Matrix::randDouble(){
- int range = 100000;
- double rangeF = double(range);
- double random = (rand() % (range + 1)) / rangeF;
- return random;
- }
- void Matrix::printMatrix(){
- if(this->matrix == NULL){
- std::cout << "Earlier you must init matrix!" << std::endl;
- }
- else{
- std::cout << "Matrix:" << std::endl;
- for(int y = 0; y < m; y++){
- for(int x = 0; x < n; x++){
- std::cout << this->matrix[x][y] << " ";
- }
- std::cout << std::endl;
- }
- }
- }
- // GET LICZBA WIERSZY
- Matrix::getM(){
- return this->m;
- }
- // GET LICZBA KOLUMN
- Matrix::getN(){
- return this->n;
- }
- // ZADANIE 6 - przeciążanie operatorów!
- void Matrix::operator =(Matrix& b){
- m = b.getM();
- n = b.getN();
- allocate(this->m, this->n);
- int tempM = b.getM();
- int tempN = b.getN();
- for(int y = 0; y < tempM; y++){
- for(int x = 0; x < tempN; x++){
- this->matrix[y][x] = b.matrix[y][x];
- }
- }
- }
- void Matrix::operator-(){
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- if(this->matrix[y][x] > 0){
- this->matrix[y][x] *= -1;
- }
- }
- }
- }
- void Matrix::operator+(){
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- if(this->matrix[y][x] < 0){
- this->matrix[y][x] *= -1;
- }
- }
- }
- }
- Matrix& Matrix::operator+(Matrix& b){
- Matrix * tempMatrix = new Matrix(b.getM(), b.getN());
- if(this->getM() != b.getM() || this->getN() != b.getN()){
- // OBSLUGA BLEDU
- std::cout << "Diffrent size of matrixes' + Failed";
- }
- else{
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- tempMatrix->matrix[y][x] = this->matrix[y][x] + b.matrix[y][x];
- }
- }
- }
- return *tempMatrix;
- }
- Matrix& Matrix::operator-(Matrix& b){
- Matrix * tempMatrix = new Matrix(b.getM(), b.getN());
- if(this->getM() != b.getM() || this->getN() != b.getN()){
- // OBSLUGA BLEDU
- std::cout << "Diffrent size of matrixes' - Failed";
- }
- else{
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- //std::cout << " = " << tempMatrix->matrix[y][x] << std::endl;
- tempMatrix->matrix[y][x] = this->matrix[y][x] - b.matrix[y][x];
- }
- }
- }
- return *tempMatrix;
- }
- void Matrix::operator*(float f){
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- this->matrix[y][x] *= f;
- }
- }
- }
- void Matrix::operator*(Matrix& m){
- if(this->getM() != m.getM() || this->getN() != m.getN()){
- // OBSLUGA BLEDU
- std::cout << "Diffrent size of matrixes' * Failed";
- }
- else{
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- this->matrix[y][x] *= m.matrix[y][x];
- }
- }
- }
- }
- Matrix& Matrix::operator+=(Matrix& m){
- if(this->getM() != m.getM() || this->getN() != m.getN()){
- // OBSLUGA BLEDU
- std::cout << "Diffrent size of matrixes' += Failed";
- }
- else{
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- this->matrix[y][x] += m.matrix[y][x];
- }
- }
- }
- return * this;
- }
- Matrix& Matrix::operator-=(Matrix& m){
- if(this->getM() != m.getM() || this->getN() != m.getN()){
- // OBSLUGA BLEDU
- std::cout << "Diffrent size of matrixes' += Failed";
- }
- else{
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- this->matrix[y][x] -= m.matrix[y][x];
- }
- }
- }
- return * this;
- }
- Matrix& Matrix::operator*=(float f){
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- this->matrix[y][x] *= f;
- }
- }
- return * this;
- }
- Matrix& Matrix::operator*=(Matrix& m){
- if(this->getM() != m.getM() || this->getN() != m.getN()){
- // OBSLUGA BLEDU
- std::cout << "Diffrent size of matrixes' += Failed";
- }
- else{
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- this->matrix[y][x] *= m.matrix[y][x];
- }
- }
- }
- return * this;
- }
- bool Matrix::operator==(Matrix& m){
- if(this->getM() != m.getM() || this->getN() != m.getN()){
- // OBSLUGA BLEDU
- std::cout << "Diffrent size of matrixes' += Failed";
- return false;
- }
- else{
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- if(this->matrix[y][x] != m.matrix[y][x]){
- return false;
- }
- }
- }
- }
- return true;
- }
- bool Matrix::operator!=(Matrix& m){
- if(this->getM() != m.getM() || this->getN() != m.getN()){
- // OBSLUGA BLEDU
- std::cout << "Diffrent size of matrixes' += Failed";
- return true;
- }
- else{
- for(int y = 0; y < this->m; y++){
- for(int x = 0; x < this->n; x++){
- if(this->matrix[y][x] != m.matrix[y][x]){
- return true;
- }
- }
- }
- }
- return false;
- }
- double& Matrix::operator[](int k){
- double* tab = new double[this->n];
- for(int i = 0; i < this->n; i++){
- tab[i] = this->matrix[k][i];
- }
- return *tab;
- }
- double Matrix::operator()(int y, int x){
- if( y < 0 || x < 0 || y > this->m || x > this->n){
- std::cout << "Wrong index of Matrix()" << std::endl;
- }
- else{
- return this->matrix[y][x];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement