1  #include <iomanip>
  2  #include <sstream>
  3
  4  #include "matrix3.h"
  5
  6  string MatrixIndexException::format_message(int n)
  7  {
  8     ostringstream outstr;
  9     outstr << "Matrix index " << n << " out of range";
 10     return outstr.str();
 11  }
 12
 13  Matrix::Matrix(int r, int c)
 14  {
 15     rows = r;
 16     columns = c;
 17     elements = new double[rows * columns];
 18     for (int i = 0; i < rows * columns; i++)
 19        elements[i] = 0;
 20  }
 21
 22  Matrix& Matrix::operator=(const Matrix& other)
 23  {
 24     if (this != &other)
 25     {
 26        free();
 27        copy(other);
 28     }
 29     return *this;
 30  }
 31
 32  void Matrix::copy(const Matrix& other)
 33  {
 34     rows = other.rows;
 35     columns = other.columns;
 36     elements = new double[rows * columns];
 37     for (int i = 0; i < rows * columns; i++)
 38        elements[i] = other.elements[i];
 39  }
 40
 41  double& Matrix::operator()(int i, int j) throw (MatrixIndexException)
 42  {
 43     if (i < 0 || i >= rows)
 44        throw MatrixIndexException(i);
 45     if (j < 0 || j >= columns)
 46        throw MatrixIndexException(j);
 47     return elements[i * get_columns() + j];
 48  }
 49
 50  double Matrix::operator()(int i, int j) const throw (MatrixIndexException)
 51  {
 52     if (i < 0 || i >= rows)
 53        throw MatrixIndexException(i);
 54     if (j < 0 || j >= columns)
 55        throw MatrixIndexException(j);
 56     return elements[i * get_columns() + j];
 57  }
 58
 59  Matrix& Matrix::operator+=(const Matrix& right)
 60     throw (MatrixMismatchException)
 61  {
 62     if (rows != right.rows || columns != right.columns)
 63        throw MatrixMismatchException();
 64     for (int i = 0; i < rows; i++)
 65        for (int j = 0; j < columns; j++)
 66           (*this)(i, j) += right(i, j);
 67     return *this;
 68  }
 69
 70  Matrix operator+(const Matrix& left, const Matrix& right)
 71     throw (MatrixMismatchException)
 72  {
 73     Matrix result = left;
 74     result += right;
 75     return result;
 76  }
 77
 78  Matrix operator*(const Matrix& left, const Matrix& right)
 79     throw (MatrixMismatchException)
 80  {
 81     if (left.get_columns() != right.get_rows())
 82        throw MatrixMismatchException();
 83     Matrix result(left.get_rows(), right.get_columns());
 84     for (int i = 0; i < left.get_rows(); i++)
 85        for (int j = 0; j < right.get_columns(); j++)
 86           for (int k = 0; k < left.get_columns(); k++)
 87              result(i, j) += left(i, k) * right(k, j);
 88     return result;
 89  }
 90
 91  Matrix operator*(const Matrix& left, double right)
 92  {
 93     Matrix result(left);
 94     for (int i = 0; i < result.get_rows(); i++)
 95        for (int j = 0; j < result.get_columns(); j++)
 96           result(i, j) *= right;
 97     return result;
 98  }
 99
100  ostream& operator<<(ostream& left, const Matrix& right)
101  {
102     const int WIDTH = 10;
103     for (int i = 0; i < right.get_rows(); i++)
104     {
105        for (int j = 0; j < right.get_columns(); j++)
106           left << setw(WIDTH) << right(i, j);
107        left << "\n";
108     }
109     return left;
110  }
111