From 5d68c5bf914c3077e2509a4a617561c7e70cc39f Mon Sep 17 00:00:00 2001 From: lordcobisco Date: Thu, 19 Mar 2015 21:44:55 -0300 Subject: [PATCH 1/2] Sobrecarga para operador () MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inserindo um intervalo, é possível obter um conjunto de linhas e colunas predefinidas em um intervalo inferior e superior. --- LinAlg/matrix.h | 11 +++++++++-- LinAlg/src/linalg.hpp | 2 +- LinAlg/src/matrix.hpp | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/LinAlg/matrix.h b/LinAlg/matrix.h index b6c646e..e1eaf7b 100644 --- a/LinAlg/matrix.h +++ b/LinAlg/matrix.h @@ -33,8 +33,13 @@ namespace LinAlg { bool isSquare (); Type& operator() (unsigned row, unsigned column); - Type operator() (unsigned row, unsigned column) const; - void operator() (unsigned row, unsigned column, Type number); + Type operator() (unsigned row, unsigned column) const; + void operator() (unsigned row, unsigned column, Type number); +// LinAlg::Matrix& operator() (LinAlg::Matrix rows_to_be_returned, LinAlg::Matrix columns_to_be_returned); + class rangehandler; + LinAlg::Matrix operator() (rangehandler range, rangehandler range2) const; + LinAlg::Matrix operator() (unsigned row, rangehandler range2) const; + LinAlg::Matrix operator() (rangehandler range, unsigned col) const; void operator= (std::string rhs); LinAlg::Matrix& operator= (const LinAlg::Matrix& otherMatrix); @@ -136,6 +141,8 @@ namespace LinAlg { template bool operator!= (const LinAlg::Matrix& lhs, const LinAlg::Matrix& rhs) {return !(lhs == rhs);} +// unsigned* operator, (unsigned number1, unsigned number2); + template void Zeros (LinAlg::Matrix& Mat); diff --git a/LinAlg/src/linalg.hpp b/LinAlg/src/linalg.hpp index 3705fab..cf189a6 100644 --- a/LinAlg/src/linalg.hpp +++ b/LinAlg/src/linalg.hpp @@ -193,4 +193,4 @@ LinAlg::Matrix LinAlg::EigenValues(const LinAlg::Matrix &matrix_to_g } return ret; -} \ No newline at end of file +} diff --git a/LinAlg/src/matrix.hpp b/LinAlg/src/matrix.hpp index 0916f8d..d91c5cd 100644 --- a/LinAlg/src/matrix.hpp +++ b/LinAlg/src/matrix.hpp @@ -302,6 +302,45 @@ void LinAlg::Matrix::operator() (unsigned row, unsigned column, Type numbe this->Add(row, column, number); } +template +class LinAlg::Matrix::rangehandler{ +public: + rangehandler(int lower, int upper): lower(lower), upper(upper){}; + int lower, upper; + +}; + +template +LinAlg::Matrix LinAlg::Matrix::operator() (rangehandler range_rows, rangehandler range_cols) const +{ + LinAlg::Matrix Ret(range_rows.upper - range_rows.lower + 1, range_cols.upper - range_cols.lower + 1); + for(unsigned i = range_rows.lower; i <= range_rows.upper; i++) + for(unsigned j = range_cols.lower; j <= range_cols.upper ; j++) + Ret(i-range_rows.lower+1,j-range_cols.lower+1) = this->mat[i-1][j-1]; + + return Ret; +} + +template +LinAlg::Matrix LinAlg::Matrix::operator() (unsigned row, rangehandler range_cols) const +{ + LinAlg::Matrix Ret(1, range_cols.upper - range_cols.lower + 1); + for(unsigned j = range_cols.lower; j <= range_cols.upper ; j++) + Ret(1,j-range_cols.lower+1) = this->mat[row-1][j-1]; + + return Ret; +} + +template +LinAlg::Matrix LinAlg::Matrix::operator() (rangehandler range_rows, unsigned col) const +{ + LinAlg::Matrix Ret(range_rows.upper - range_rows.lower + 1, col); + for(unsigned i = range_rows.lower; i <= range_rows.upper; i++) + Ret(i-range_rows.lower+1,1) = this->mat[i-1][col-1]; + + return Ret; +} + template void LinAlg::Matrix::operator= (std::string Mat) { From 66c2bb8e4c5c400aa0c943959c6983ba3c86e3c0 Mon Sep 17 00:00:00 2001 From: lordcobisco Date: Sat, 21 Mar 2015 09:57:22 -0300 Subject: [PATCH 2/2] Ajeitando Matrizes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Vo tentar realizar as alterações q vc pediu no sistema de controle. --- LinAlg/identifiers.h | 29 +++++++++++++++ LinAlg/matrix.h | 21 +++++------ LinAlg/src/matrix.hpp | 85 +++++++++++++++++++++++++++++-------------- 3 files changed, 97 insertions(+), 38 deletions(-) create mode 100644 LinAlg/identifiers.h diff --git a/LinAlg/identifiers.h b/LinAlg/identifiers.h new file mode 100644 index 0000000..6a34c2e --- /dev/null +++ b/LinAlg/identifiers.h @@ -0,0 +1,29 @@ +#ifndef IDENTIFIERS_H +#define IDENTIFIERS_H + +namespace Identifiers{ + +struct from{ + from(unsigned interval) : interval(interval) {}; + + unsigned interval; +}; + +struct to{ + to(unsigned interval) : interval(interval) {}; + + unsigned interval; +}; + +unsigned* operator, (from from_interval, to to_interval) +{ + unsigned* ret = new unsigned[2]; + + ret[0] = from_interval.interval; + ret[1] = to_interval.interval; + + return ret; +} +} + +#endif // IDENTIFIERS_H diff --git a/LinAlg/matrix.h b/LinAlg/matrix.h index e1eaf7b..c2032f6 100644 --- a/LinAlg/matrix.h +++ b/LinAlg/matrix.h @@ -7,6 +7,10 @@ #include #include +#include "identifiers.h" + +using namespace Identifiers; + namespace LinAlg { template class Matrix @@ -34,12 +38,10 @@ namespace LinAlg { Type& operator() (unsigned row, unsigned column); Type operator() (unsigned row, unsigned column) const; - void operator() (unsigned row, unsigned column, Type number); -// LinAlg::Matrix& operator() (LinAlg::Matrix rows_to_be_returned, LinAlg::Matrix columns_to_be_returned); - class rangehandler; - LinAlg::Matrix operator() (rangehandler range, rangehandler range2) const; - LinAlg::Matrix operator() (unsigned row, rangehandler range2) const; - LinAlg::Matrix operator() (rangehandler range, unsigned col) const; + + LinAlg::Matrix operator() (unsigned* row_interval, unsigned column) const; + LinAlg::Matrix operator() (unsigned row, unsigned* column_interval) const; + LinAlg::Matrix operator() (unsigned* row_interval, unsigned* column_interval) const; void operator= (std::string rhs); LinAlg::Matrix& operator= (const LinAlg::Matrix& otherMatrix); @@ -76,10 +78,7 @@ namespace LinAlg { //Should be declared as friend. template - friend void swap (LinAlg::Matrix& lhs, LinAlg::Matrix& rhs) {lhs.swap(rhs);}; - - //Should be declared as friend. - + friend void swap (LinAlg::Matrix& lhs, LinAlg::Matrix& rhs) {lhs.swap(rhs);}; private: void Init (std::string Mat); @@ -141,7 +140,7 @@ namespace LinAlg { template bool operator!= (const LinAlg::Matrix& lhs, const LinAlg::Matrix& rhs) {return !(lhs == rhs);} -// unsigned* operator, (unsigned number1, unsigned number2); + unsigned* operator, (from from_interval, to to_interval); template void Zeros (LinAlg::Matrix& Mat); diff --git a/LinAlg/src/matrix.hpp b/LinAlg/src/matrix.hpp index d91c5cd..a7dd42e 100644 --- a/LinAlg/src/matrix.hpp +++ b/LinAlg/src/matrix.hpp @@ -297,46 +297,77 @@ Type LinAlg::Matrix::operator() (unsigned row, unsigned column) const } template -void LinAlg::Matrix::operator() (unsigned row, unsigned column, Type number) -{ - this->Add(row, column, number); -} - -template -class LinAlg::Matrix::rangehandler{ -public: - rangehandler(int lower, int upper): lower(lower), upper(upper){}; - int lower, upper; - -}; +LinAlg::Matrix LinAlg::Matrix::operator ()(unsigned* row_interval, unsigned* column_interval)const +{ + LinAlg::Matrix Ret; + + if(row_interval[0] < row_interval[1]){ + if(column_interval[0] < column_interval[1]){ + Ret.Init(row_interval[1] - row_interval[0] + 1, column_interval[1] - column_interval[0] + 1); + for(unsigned i = row_interval[0]; i <= row_interval[1]; ++i) + for(unsigned j = column_interval[0]; j <= column_interval[1]; ++j) + Ret.mat[i - row_interval[0]][j - column_interval[0]] = this->mat[i-1][j-1]; + + }else{ + Ret.Init(row_interval[1] - row_interval[0] + 1, column_interval[0] - column_interval[1] + 1); + for(unsigned i = row_interval[0]; i <= row_interval[1]; ++i) + for(unsigned j = column_interval[0]; j >= column_interval[1]; --j) + Ret.mat[i - row_interval[0]][column_interval[0] - j] = this->mat[i-1][j - 1]; + } -template -LinAlg::Matrix LinAlg::Matrix::operator() (rangehandler range_rows, rangehandler range_cols) const -{ - LinAlg::Matrix Ret(range_rows.upper - range_rows.lower + 1, range_cols.upper - range_cols.lower + 1); - for(unsigned i = range_rows.lower; i <= range_rows.upper; i++) - for(unsigned j = range_cols.lower; j <= range_cols.upper ; j++) - Ret(i-range_rows.lower+1,j-range_cols.lower+1) = this->mat[i-1][j-1]; + } else{ + if(column_interval[0] < column_interval[1]){ + Ret.Init(row_interval[0] - row_interval[1] + 1, column_interval[1] - column_interval[0] + 1); + for(unsigned i = row_interval[0]; i >= row_interval[1]; --i) + for(unsigned j = column_interval[0]; j <= column_interval[1]; ++j) + Ret.mat[row_interval[0] - i][j - column_interval[0]] = this->mat[i-1][j-1]; + }else{ + Ret.Init(row_interval[0] - row_interval[1] + 1, column_interval[0] - column_interval[1] + 1); + for(unsigned i = row_interval[0]; i >= row_interval[1]; --i) + for(unsigned j = column_interval[0]; j >= column_interval[1]; --j) + Ret.mat[row_interval[0] - i][column_interval[0] - j] = this->mat[i-1][j - 1]; + } + } return Ret; } template -LinAlg::Matrix LinAlg::Matrix::operator() (unsigned row, rangehandler range_cols) const +LinAlg::Matrix LinAlg::Matrix::operator ()(unsigned row, unsigned* column_interval)const { - LinAlg::Matrix Ret(1, range_cols.upper - range_cols.lower + 1); - for(unsigned j = range_cols.lower; j <= range_cols.upper ; j++) - Ret(1,j-range_cols.lower+1) = this->mat[row-1][j-1]; + LinAlg::Matrix Ret; + + + if(column_interval[0] < column_interval[1]){ + Ret.Init(1, column_interval[1] - column_interval[0] + 1); + for(unsigned j = column_interval[0]; j <= column_interval[1]; ++j) + Ret.mat[row - 1][j - column_interval[0]] = this->mat[row - 1][j - 1]; + + }else{ + Ret.Init(1, column_interval[0] - column_interval[1] + 1); + for(unsigned j = column_interval[0]; j >= column_interval[1]; --j) + Ret.mat[row - 1][column_interval[0] - j] = this->mat[row - 1][j - 1]; + } return Ret; } template -LinAlg::Matrix LinAlg::Matrix::operator() (rangehandler range_rows, unsigned col) const +LinAlg::Matrix LinAlg::Matrix::operator ()(unsigned* row_interval, unsigned column)const { - LinAlg::Matrix Ret(range_rows.upper - range_rows.lower + 1, col); - for(unsigned i = range_rows.lower; i <= range_rows.upper; i++) - Ret(i-range_rows.lower+1,1) = this->mat[i-1][col-1]; + LinAlg::Matrix Ret; + + if(row_interval[0] < row_interval[1]){ + Ret.Init(row_interval[1] - row_interval[0] + 1, 1); + for(unsigned i = row_interval[0]; i <= row_interval[1]; ++i) + Ret.mat[i - row_interval[0]][column - 1] = this->mat[i - 1][column - 1]; + } else{ + unsigned aux = row_interval[0] - row_interval[1] + 1; + + Ret.Init(row_interval[0] - row_interval[1] + 1, 1); + for(unsigned i = row_interval[0]; i >= row_interval[1]; --i) + Ret.mat[row_interval[0] - i][column - 1] = this->mat[i - 1][column - 1]; + } return Ret; }