matrix.h #ifndef __SW_MATRIX_H__ #define __SW_MATRIX_H__ /* #include //#include #include */ // uncomment to disable assert() // #define NDEBUG #include namespace SW { template class Matrix { private: std::vector > data; mutable unsigned rows; mutable unsigned cols; public: Matrix(); Matrix(unsigned _rows, unsigned _cols); Matrix(unsigned _rows, unsigned _cols, const T& _initial); //Matrix(T&&... elements); /* template //Matrix(T element, E&&... elements); Matrix(E&&... elements); //Matrix(unsigned _rows, unsigned _cols, T element, E&&... elements); */ /** * Constructs a Matrix whose elements are initialized with the provided values * * @param element the first value * * @param elements all subsequent values * *
Matrix matrix(1, 2, 3, 1, 2, 3, 1, 2 ,3);
* */ //template //Matrix(unsigned _rows, unsigned _cols, T element, E&&... elements); Matrix(const Matrix& rhs); virtual ~Matrix(); //~Matrix(); /* template bool operator==(const Matrix& lhs, const Matrix& rhs) noexcept; template bool operator!=(const Matrix& lhs, const Matrix& rhs) noexcept; template std::ostream& operator<<(std::ostream& stream, const Matrix& matrix); template std::istream& operator>>(std::istream& stream, Matrix& matrix); */ /// Privileged free-functions /* friend std::ostream& operator<<<>(std::ostream&, const Matrix&); friend std::istream& operator>><>(std::istream&, const Matrix&); friend bool operator!=<>(const Matrix& lhs, const Matrix&); friend bool operator==<>(const Matrix& lhs, const Matrix& rhs); */ //Matrix& operator==<>(const Matrix& lhs, const Matrix& rhs); //bool operator==(const Matrix& lhs, const Matrix& rhs); //Matrix& operator==(Matrix& rhs); //bool operator==(Matrix& rhs); // Operator overloading, for "standard" mathematical matrix operations. Matrix& operator=(const Matrix& rhs); // Operator overloading, for comparitive operations. bool operator==(const Matrix& rhs) const; bool operator!=(const Matrix& rhs) const; bool operator<(const Matrix& rhs) const; bool operator<=(const Matrix& rhs) const; bool operator>(const Matrix& rhs) const; bool operator>=(const Matrix& rhs) const; // Arithmetic operators. Matrix operator - () const; // negate operator Matrix operator ++ (); // prefix increment operator Matrix operator ++ (int); // postfix increment operator Matrix operator -- (); // prefix decrement operator Matrix operator -- (int); // postfix decrement operator // Matrix mathematical operations. Matrix operator+(const Matrix& rhs); Matrix& operator+=(const Matrix& rhs); Matrix operator-(const Matrix& rhs); Matrix& operator-=(const Matrix& rhs); Matrix operator*(const Matrix& rhs); Matrix& operator*=(const Matrix& rhs); Matrix operator%(const Matrix& rhs); Matrix& operator%=(const Matrix& rhs); Matrix operator^(const Matrix& rhs); Matrix& operator^=(const Matrix& rhs); //Matrix operator ^ (const Matrix& rhs, const int power); //Matrix operator^(const int power); Matrix& operator^=(const int power); Matrix abs(); void print(); Matrix sqrt(); //sequence_generator Matrix transpose(); // Matrix/scalar operations. Matrix operator+(const T& rhs); Matrix operator-(const T& rhs); Matrix operator*(const T& rhs); Matrix operator/(const T& rhs); // Matrix/vector operations. std::vector operator*(const std::vector& rhs); std::vector diag_vec(); // Access the individual elements. T& operator()(const unsigned& row, const unsigned& col); const T& operator()(const unsigned& row, const unsigned& col) const; // Access the row and column sizes. unsigned get_rows() const; unsigned get_cols() const; // vector operations Matrix get_row(unsigned r) const; Matrix get_col(unsigned c) const; Matrix& set_col(unsigned c, const Matrix& C); Matrix& set_row(unsigned r, const Matrix& R); Matrix delete_row(unsigned r) const; Matrix delete_col(unsigned c) const; bool swap_rows(unsigned i, unsigned j); /**/ // I/O stream functions. template friend std::ostream & operator<<(std::ostream &, const Matrix&); template friend std::istream& operator>>(std::istream&, const Matrix&); }; /* template ostream& operator<<(ostream& o, const Matrix& M) { o << endl; for (int i = 0; i Matrix::Matrix() { rows = 0; cols = 0; } // Parameter Constructor. template Matrix::Matrix(unsigned _rows, unsigned _cols) { data.resize(_rows); for (unsigned i = 0; i Matrix::Matrix(unsigned _rows, unsigned _cols, const T& _initial) { data.resize(_rows); for (unsigned i = 0; i //template //Matrix::Matrix(T first, E&&... elements) noexcept : values{ { first, std::forward(static_cast(elements))... } } /* template template //Matrix::Matrix(T first, E&&... elements) noexcept : values{ { first, std::forward(static_cast(elements))... } } //Matrix::Matrix(T first, E&&... elements) : data{ { first, std::forward(static_cast(elements))... } } Matrix::Matrix(E&&... elements) : data{ { std::forward(static_cast(elements))... } } { data.push_back() } */ /* template template //Matrix::Matrix(unsigned _rows, unsigned _cols, T first, E&&... elements) : mat{ { first, std::forward(static_cast(elements))... } } //Matrix::Matrix(unsigned _rows, unsigned _cols, T first, E&&... elements) : mat{ { first, std::forward(static_cast(elements))... } } Matrix::Matrix(unsigned _rows, unsigned _cols, T first, E&&... elements) : mat{ { first, std::forward(static_cast(elements))... } } { data.resize(_rows); for (unsigned i = 0; i::Matrix(T&&... elements) { std::forward(static_cast(elements))...; } */ // Copy Constructor. template Matrix::Matrix(const Matrix& rhs) { data = rhs.data; rows = rhs.get_rows(); cols = rhs.get_cols(); } // (Virtual) Destructor. template Matrix::~Matrix() { } // Assignment Operator. template Matrix& Matrix::operator=(const Matrix& rhs) { if (&rhs == this) return *this; unsigned new_rows = rhs.get_rows(); unsigned new_cols = rhs.get_cols(); data.resize(new_rows); for (unsigned i = 0; i bool Matrix::operator==(const Matrix& rhs) const { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); if (get_rows() != rows) return false; if (get_cols() != cols) return false; for (unsigned i = 0; idata[i][j] != rhs(i, j)) return false; } } return true; } // Comparison Operator !=. template bool Matrix::operator!=(const Matrix& rhs) const { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); if (get_rows() != rows) return true; if (get_cols() != cols) return true; for (unsigned i = 0; idata[i][j] == rhs(i, j)) return false; } } return false; } // Comparison Operator <. template bool Matrix::operator<(const Matrix& rhs) const { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); if (get_rows() != rows) return false; if (get_cols() != cols) return false; for (unsigned i = 0; idata[i][j] >= rhs(i, j)) return false; } } return true; } // Comparison Operator <=. template bool Matrix::operator<=(const Matrix& rhs) const { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); if (get_rows() != rows) return false; if (get_cols() != cols) return false; for (unsigned i = 0; idata[i][j] > rhs(i, j)) return false; } } return true; } // Comparison Operator <. template bool Matrix::operator>(const Matrix& rhs) const { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); if (get_rows() != rows) return false; if (get_cols() != cols) return false; for (unsigned i = 0; idata[i][j] <= rhs(i, j)) return false; } } return true; } // Comparison Operator >=. template bool Matrix::operator>=(const Matrix& rhs) const { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); if (get_rows() != rows) return false; if (get_cols() != cols) return false; for (unsigned i = 0; idata[i][j] < rhs(i, j)) return false; } } return true; } // Unary - operator template Matrix Matrix::operator -() const { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j]; } } return result; } // Prefix increment operator template Matrix Matrix::operator ++ () { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j] + 1; } } return *this = result; } // Postfix increment operator template Matrix Matrix::operator ++ (int) { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j] + 1; } } return result; } // Prefix decrement operator template Matrix Matrix::operator -- () { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j] - 1; } } return *this = result; } // Postfix decrement operator template Matrix Matrix::operator -- (int) { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j] - 1; } } return result; } // Addition of two matrices. template Matrix Matrix::operator+(const Matrix& rhs) { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j] + rhs(i, j); } } return result; } // Cumulative addition of this matrix and another. template Matrix& Matrix::operator+=(const Matrix& rhs) { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); for (unsigned i = 0; idata[i][j] += rhs(i, j); } } return *this; } // Subtraction of this matrix and another. template Matrix Matrix::operator-(const Matrix& rhs) { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j] - rhs(i, j); } } return result; } // Cumulative subtraction of this matrix and another. template Matrix& Matrix::operator-=(const Matrix& rhs) { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); for (unsigned i = 0; idata[i][j] -= rhs(i, j); } } return *this; } // Left multiplication of this matrix and another. template Matrix Matrix::operator*(const Matrix& rhs) { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][k] * rhs(k, j); } } } return result; } // Cumulative left multiplication of this matrix and another. template Matrix& Matrix::operator*=(const Matrix& rhs) { Matrix result = (*this) * rhs; (*this) = result; return *this; } //************************************************************************************************ // Left mod of this matrix and another. template Matrix Matrix::operator%(const Matrix& rhs) { unsigned rows = rhs.get_rows(); unsigned cols = rhs.get_cols(); Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][k] % rhs(k, j); } } } return result; } // Cumulative left mod of this matrix and another. template Matrix& Matrix::operator%=(const Matrix& rhs) { Matrix result = (*this) % rhs; (*this) = result; return *this; } // Left raise of this matrix and another. template Matrix Matrix::operator^(const Matrix& rhs) { // unsigned rows = rhs.get_rows(); // unsigned cols = rhs.get_cols(); Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][k] ^ rhs(k, j); } } } return result; } // Cumulative left raise of this matrix and another. template Matrix& Matrix::operator^=(const Matrix& rhs) { Matrix result = (*this) % rhs; (*this) = result; return *this; } /* template //Matrix Matrix::operator^(const Matrix& rhs, const int power) Matrix Matrix::operator^(const int power) { std::cout << "Z" << std::endl; //unsigned rows = rhs.get_rows(); //unsigned cols = rhs.get_cols(); Matrix result(rows, cols, 0.0); //result = rhs; for (unsigned i = 0; idata[i][j]; std::cout << "T=" << std::endl; //T temp = 2; //T temp = rhs[i][j]; // not <= below \/ because first time counts as 2 for (int k = 2; k<=power; k++) //result(i, j) += this->data[i][j] * this->data[i][j]; result(i, j) = result(i, j) * temp; } } return result; } */ // Left raise of this matrix and another. template Matrix& Matrix::operator^=(const int power) { Matrix result = (*this); unsigned rows = result.get_rows(); unsigned cols = result.get_cols(); for (unsigned i = 0; idata[i][j]; // not <= below \/ because first time counts as 2 for (int k = 2; k <= power; k++) result(i, j) = result(i, j) * temp; } } //(*this) = result; *this = result; return *this; } //********************************************************************************************* // Converts all values to their absolute value within the matrix. template Matrix Matrix::abs() { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j]); } } return result; } template void Matrix::print() { for (unsigned int i = 0; i < rows; i++) { for (unsigned int j = 0; j Matrix Matrix::sqrt() { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[j][i]); } } return result; } // Calculate a transpose of this matrix. template Matrix Matrix::transpose() { Matrix result(cols, rows, 0.0); for (unsigned i = 0; idata[j][i]; } } return result; } // Matrix/scalar addition. template Matrix Matrix::operator+(const T& rhs) { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j] + rhs; } } return result; } // Matrix/scalar subtraction. template Matrix Matrix::operator-(const T& rhs) { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j] - rhs; } } return result; } // Matrix/scalar multiplication. template Matrix Matrix::operator*(const T& rhs) { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j] * rhs; } } return result; } // Matrix/scalar division. template Matrix Matrix::operator/(const T& rhs) { Matrix result(rows, cols, 0.0); for (unsigned i = 0; idata[i][j] / rhs; } } return result; } // Multiply a matrix with a vector. template std::vector Matrix::operator*(const std::vector& rhs) { std::vector result(rhs.size(), 0.0); for (unsigned i = 0; idata[i][j] * rhs[j]; } } return result; } // Obtain a vector of the diagonal elements. template std::vector Matrix::diag_vec() { std::vector result(rows, 0.0); for (unsigned i = 0; idata[i][i]; } return result; } // Access the individual elements. template T& Matrix::operator()(const unsigned& row, const unsigned& col) { assert(row >= 0); assert(col >= 0); assert(row < rows); assert(col < cols); return this->data[row][col]; } // Access the individual elements (const). template const T& Matrix::operator()(const unsigned& row, const unsigned& col) const { assert(row >= 0); assert(col >= 0); assert(row < rows); assert(col < cols); return this->data[row][col]; } // Get the number of rows of the matrix. template unsigned Matrix::get_rows() const { return this->rows; } // Get the number of columns of the matrix. template unsigned Matrix::get_cols() const { return this->cols; } // Returns a row. template Matrix Matrix::get_row(unsigned r) const { Matrix result(1, cols, 0.0); for (unsigned i = 0; idata[r][i]; } return result; } template Matrix Matrix::get_col(unsigned c) const { Matrix result(rows, 1, 0.0); for (unsigned i = 0; idata[i][c]; } return result; } template Matrix& Matrix::set_col(unsigned c, const Matrix& C) { } template Matrix& Matrix::set_row(unsigned r, const Matrix& R) { } template Matrix Matrix::delete_row(unsigned r) const { } template Matrix Matrix::delete_col(unsigned c) const { } // swap_rows() uses a row's worth of memory for better run-time performance // if you want pairwise swap, just write it yourself template bool Matrix::swap_rows(unsigned i, unsigned j) { if (i == j) return; /* Matrix temp = (*this)[i]; (*this)[i] = (*this)[j]; (*this)[j] = temp; */ unsigned max = (*this)[i].size(); for (unsigned ii = 0; ii std::ostream & operator << (std::ostream &s, const Matrix& m) { unsigned rows = m.get_rows(); unsigned cols = m.get_cols(); for (unsigned i = 0; i std::istream & operator >> (std::istream &s, Matrix& m) { /* std::string temp(10000, ' '); s >> temp; v = Verylong(temp); */ return s; } } // end namespace SW. #endif