matrix.cpp #include //#include #include "matrix.h" namespace SW { /* // Example #include "matrix.h" #include int main(int argc, char **argv) { Matrix mat1(10, 10, 1.0); Matrix mat2(10, 10, 2.0); Matrix mat3 = mat1 + mat2; for (int i = 0; i Matrix::Matrix() { rows = 0; cols = 0; } /**/ // Parameter Constructor. template Matrix::Matrix(unsigned _rows, unsigned _cols) { mat.resize(_rows); for (unsigned i = 0; i Matrix::Matrix(unsigned _rows, unsigned _cols, const T& _initial) { mat.resize(_rows); for (unsigned i = 0; i Matrix::Matrix(const Matrix& rhs) { mat = rhs.mat; rows = rhs.get_rows(); cols = rhs.get_cols(); } // (Virtual) Destructor. template Matrix::~Matrix() { } /**/ // 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; imat[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; imat[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; imat[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; imat[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; imat[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; imat[i][j] < rhs(i, j)) return false; } } return true; } // 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(); mat.resize(new_rows); for (unsigned i = 0; i Matrix Matrix::operator -() const { Matrix result(rows, cols, 0.0); for (unsigned i = 0; imat[i][j]; } } return result; } // Prefix increment operator template Matrix Matrix::operator ++ () { Matrix result(rows, cols, 0.0); for (unsigned i = 0; imat[i][j] + 1; } } return *this = result; } // Postfix increment operator template Matrix Matrix::operator ++ (int) { Matrix result(rows, cols, 0.0); for (unsigned i = 0; imat[i][j] + 1; } } return result; } // Prefix decrement operator template Matrix Matrix::operator -- () { Matrix result(rows, cols, 0.0); for (unsigned i = 0; imat[i][j] - 1; } } return *this = result; } // Postfix decrement operator template Matrix Matrix::operator -- (int) { Matrix result(rows, cols, 0.0); for (unsigned i = 0; imat[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; imat[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; imat[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; imat[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; imat[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; imat[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; imat[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; imat[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; imat[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->mat[i][j] * this->mat[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; imat[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; } // Concerts all values to their absolute value within the matrix. template Matrix Matrix::abs() { Matrix result(rows, cols, 0.0); for (unsigned i = 0; imat[j][i]); } } return result; } // Concerts all values to their square roots within the matrix. template Matrix Matrix::sqrt() { Matrix result(rows, cols, 0.0); for (unsigned i = 0; imat[j][i]); } } return result; } // Calculate a transpose of this matrix. template Matrix Matrix::transpose() { Matrix result(rows, cols, 0.0); for (unsigned i = 0; imat[j][i]; } } return result; } // Matrix/scalar addition. template Matrix Matrix::operator+(const T& rhs) { Matrix result(rows, cols, 0.0); for (unsigned i = 0; imat[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; imat[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; imat[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; imat[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; imat[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; imat[i][i]; } return result; } // Access the individual elements. template T& Matrix::operator()(const unsigned& row, const unsigned& col) { return this->mat[row][col]; } // Access the individual elements (const). template const T& Matrix::operator()(const unsigned& row, const unsigned& col) const { return this->mat[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; } template 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