c++矩阵封装

参考:https://www.oschina.net/code/snippet_2329255_46732
//矩阵模板
template < class T> class mat  
{
public :
     mat()  //默认构造
     {
         setDim(1, 1);
     }
     mat(unsigned row, unsigned cow) //指定行列构造
     {
         setDim(row, cow);
     }
     mat(vector<T>&r)  //由vector作为行向量构造矩阵
     {
         setDim(r.size(), 1);
         elem[0] = r;
     }
     ~mat()
     {
         for (unsigned i = 0; i < elem.size(); i++)
         {
             elem[i].clear();
             elem[i].shrink_to_fit();
         }
         elem.clear();
         elem.shrink_to_fit();
     }
 
     void setDim(unsigned row,unsigned cow)
     {
         elem.resize(row);
         for (unsigned i = 0; i < row; i++)
         {
             elem[i].resize(cow);
         }
     }
 
     const vector< double >& operator[](unsigned row) const
     {
         return elem[row];
     }
     vector< double >& operator[](unsigned row)
     {
         return elem[row];
     }
     unsigned getRow() const   //行数
     {
         return elem.size();
     }
     unsigned getCow() const  //列数
     {
         return getRow() ? elem[0].size() : 0;
     }
     mat<T> getRowMat(unsigned r)  const //取第r行向量
     {
         mat<T> R(1, getCow());
         for (unsigned i = 0; i < getCow(); i++)
         {
             R[0][i]=elem[r - 1][i];
         }
         return R;
     }
     mat<T> getCowMat(unsigned c)  const //取第c列向量
     {
         mat<T> C(getRow(), 1);
         for (unsigned i = 0; i < getRow(); i++)
         {
             C[i][0]=elem[i][c - 1];
         }
         return C;
     }
 
     friend mat<T>  operator+( const mat<T>&A, const mat< T>&B)     //矩阵加法
     {
         mat<T> C(A.getRow(),A.getCow());
 
         for (unsigned i = 0; i < C.getRow(); i++)
         {
             for (unsigned j = 0; j < C.getCow(); j++)
             {
                 C[i][j]=A[i][j] + B[i][j];
             }
         }
         return C;
     }
     friend mat<T>  operator-( const mat<T>&A, const mat< T>&B)     //矩阵减法
     {
         mat<T> C(A.getRow(), A.getCow());
 
         for (unsigned i = 0; i <C.getRow(); i++)
         {
             for (unsigned j = 0; j < C.getCow(); j++)
             {
                 C[i][j] = A[i][j] - B[i][j];
             }
         }
         return C;
     }
     friend mat<T>  operator*( const mat<T>&A, const mat< T>&B)   //矩阵乘法
     {
         unsigned i, j, k;
         mat<T> C(A.getRow(), B.getCow());
         for (i = 0; i <A.getRow(); i++)
         {
             for (j = 0; j < B.getCow(); j++)
             {
                 for (k = 0; k < A.getCow(); k++)
                 {
                     C[i][j]+=A[i][k] * B[k][j];
                 }
             }
         }
         return C;
     }
     friend mat<T>  operator*( const T& e, const mat<T>&A)     //矩阵数乘
     {
         mat<T> C(A.getRow(), A.getCow());
 
         for (unsigned i = 0; i < C.getRow(); i++)
         {
             for (unsigned j = 0; j < C.getCow(); j++)
             {
                 C[i][j] = A[i][j]*e;
             }
         }
         return C;
     }
     friend mat<T>  operator/( const mat<T>&A, const T& e)     //矩阵除以一个数
     {
         mat<T> C(A.getRow(), A.getCow());
 
         for (unsigned i = 0; i < C.getRow(); i++)
         {
             for (unsigned j = 0; j < C.getCow(); j++)
             {
                 C[i][j]= A[i][j]/e;
             }
         }
         return C;
     }
 
     bool isSquare() const  //判断是否为方阵
     {
         return getRow() == getCow();
     }
 
private :
     vector<vector <T>> elem;
};

/**
*guozhongzhou@aliyun.com
*简单演示了一下矩阵的乘法
*/
#include <iostream>
#include "mat.h"
using namespace std;
int main()
{
     mat< double > A(4, 4),B(3,4),C(4,3),D;
 
     //为A赋值
     cout << "A=" << endl;
     for (unsigned i = 0; i < A.getRow(); i++)
     {
         for (unsigned j = 0; j < A.getCow(); j++)
         {
             A[i][j] = i+j;
             cout << A[i][j]<< "\t" ;
         }
         cout << endl;
     }
     cout << endl;
 
     //为B赋值
     cout << "B=" << endl;
     for (unsigned i = 0; i < B.getRow(); i++)
     {
         for (unsigned j = 0; j < B.getCow(); j++)
         {
             B[i][j] = i + j;
             cout << B[i][j] << "\t" ;
         }
         cout << endl;
     }
     cout << endl;
 
     //为C赋值
     cout << "C=" << endl;
     for (unsigned i = 0; i <C.getRow(); i++)
     {
         for (unsigned j = 0; j < C.getCow(); j++)
         {
             C[i][j] = i*j;
             cout << C[i][j] << "\t" ;
         }
         cout << endl;
     }
     cout << endl;
 
     //D=B*C
     D = B*C;
     cout << "D = B*C=" << endl;
     for (unsigned i = 0; i <D.getRow(); i++)
     {
         for (unsigned j = 0; j < D.getCow(); j++)
         {
             D[i][j] = i*j;
             cout <<D[i][j] << "\t" ;
         }
         cout << endl;
     }
 
     cin.get();
}

?
 

?
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值