Matrix类的一种C++实现

  功能:构建一个Matrix,拥有转置、矩阵加、矩阵减、矩阵乘

  #ifndef Matrix_Wang
  #define Matrix_Wang
  #include "xcept.h"
  #include <iostream>
  using namespace std;
  template <class T> class Matrix
  {
      friend ostream &operator << (ostream &, const Matrix<T>  &);
    public:
      Matrix(int r = 0, int c = 0);
      Matrix(const Matrix<T>  &m); // copy constructor
      Matrix(const void *elem, int r, int c); // zhixiwang
      ~Matrix()
      {
          delete []element;
      };
      int Rows()const
      {
          return rows;
      }
      int Columns()const
      {
          return cols;
      }
      T &operator()(int i, int j)const;
      Matrix < T >  &operator = (const Matrix < T >  &m);
      Matrix < T > operator + ()const; // unary +
      Matrix < T > operator + (const Matrix < T >  &m)const;
      Matrix < T > operator - ()const;
      // unary minus
      Matrix < T > operator - (const Matrix < T >  &m)const;
      Matrix < T > operator *(const Matrix < T >  &m)const;
      Matrix < T >  &operator += (const T &x);
    private:
      int rows, cols;
      // matrix dimensions
      T *element; // element array
  };
  template <class T> Matrix<T>::Matrix(int r, int c)
  {
      // Matrix constructor.
      // validate r and c
      if (r<0 || c<0)
        throw BadInitializers();
      if ((!r || !c) && (r || c))
        throw BadInitializers();
      // create the matrix
      rows = r;
      cols = c;
      element = new T[r *c];
  }  // zhixiwang
  template <class T> Matrix<T>::Matrix(const void *elem, int r, int c)
  {
      // Matrix constructor.
      // validate r and c
      if (r<0 || c<0)
        throw BadInitializers();
      if ((!r || !c) && (r || c))
        throw BadInitializers();
      // create matrix
      rows = r;
      cols = c;
      element = new T[rows *cols]; // copy each element of m
      T *e = (T*)elem;
      for (int i = 0; i<rows *cols; i++)
        element[i] = e[i];
  } 
  template <class T> Matrix<T>::Matrix(const Matrix<T>  &m)
  {
      // Copy constructor for matrices.
      // create matrix
      rows = m.rows;
      cols = m.cols;
      element = new T[rows *cols]; // copy each element of m
      for (int i = 0; i<rows *cols; i++)
        element[i] = m.element[i];
  } 
  template <class T> Matrix<T>  &Matrix<T>::operator = (const Matrix<T>  &m)
  {
      // Assignment. (*this) = m.
      if (this !=  &m)
      {
          // do not copy to self
          delete []element;
          rows = m.rows;
          cols = m.cols;
          element = new T[rows *cols];
          // copy each element
          for (int i = 0; i<rows *cols; i++)
            element[i] = m.element[i];
      };
      return  *this;
  }
  template <class T> T &Matrix<T>::operator()(int i, int j)const
  {
      // Return a reference to element (i,j).
      if (i<0 || i >= rows || j<0 || j >= cols)
        throw OutOfBounds();
      return element[i *cols + j];
  } 
  template <class T> Matrix<T> Matrix<T>::operator + (const Matrix<T>  &m)
    const
  {
      // Return w = (*this) + m.
      if (rows != m.rows || cols != m.cols)
        throw SizeMismatch();
      // create result matrix w
      Matrix<T> w(rows, cols);
      for (int i = 0; i<rows *cols; i++)
        w.element[i] = element[i] + m.element[i];
      return w;
  } 
  template <class T> Matrix<T> Matrix<T>::operator - (const Matrix<T>  &m)
    const
  {
      // Return (*this) - m.
      if (rows != m.rows || cols != m.cols)
        throw SizeMismatch();
      // create result matrix w
      Matrix<T> w(rows, cols);
      for (int i = 0; i<rows *cols; i++)
        w.element[i] = element[i] - m.element[i];
      return w;
  } 
  template <class T> Matrix<T> Matrix<T>::operator - ()const
  {
      // Return w = -(*this).
      // create result matrix w
      Matrix<T> w(rows, cols);
      for (int i = 0; i<rows *cols; i++)
        w.element[i] =  - element[i];
      return w;
  } 
  template <class T> Matrix<T> Matrix<T>::operator *(const Matrix<T>  &m)const
  {
      // Matrix multiply.  Return w = (*this) * m.
      if (cols != m.rows)
        throw SizeMismatch();
      Matrix<T> w(rows, m.cols); // result matrix
      // define cursors for *this, m, and w
      // and initialize to location of (1,1)
      int ct = 0, cm = 0, cw = 0; // compute w(i,j) for all i and j
      for (int i = 1; i <= rows; i++)
      {
          // compute row i of result
          for (int j = 1; j <= m.cols; j++)
          {
              // compute first term of w(i,j)
              T sum = element[ct] *m.element[cm]; // add in remaining terms
              for (int k = 2; k <= cols; k++)
              {
                  ct++; // next term in row i of *this
                  cm += m.cols; // next in column j of m
                  sum += element[ct] *m.element[cm];
              };
              w.element[cw++] = sum; // save w(i,j)
              // reset to start of row and next column
              ct -= cols - 1;
              cm = j;
          } // reset to start of next row and first column
          ct += cols;
          cm = 0;
      }
      return w;
  }
  template <class T> Matrix<T>  &Matrix<T>::operator += (const T &x)
  {
      // Increment all elements of *this by x.
      for (int i = 0; i<rows *cols; i++)
        element[i] += x;
      return  *this;
  } 
  template <class T> ostream &operator << (ostream &out, const Matrix<T>  &x)
  {
      // Put matrix x into the stream out.
      // One row per line.
      int k = 0; // index into element array
      for (int i = 0; i<x.rows; i++)
      {
          // do row i
          for (int j = 0; j<x.cols; j++)
            out << x.element[k++] << "  ";
          // row i finished
          out << endl;
      };
      return out;
  }
#endif

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值