前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

矩阵

作者头像
青木
发布2018-05-28 15:48:35
4140
发布2018-05-28 15:48:35
举报

在说矩阵前,先说一小小点关于数组的知识: 数组分为两种:

  • 行主映射 从第一行开始,依次对没一行的索引从左至右连续编号。
  • 列主映射 对索引的编号从最左列开始,依次对每一列的索引从上到下连续编号。

  • 不规则的二维数组 但一个二维数组有两行或者更多的行,每一行的元素个数不等时,这个数组就被称为不规则数组。对应的,所谓规则数组,即为每行元素个数相同的二维数组。

一个m×n的矩阵,是一个m行、n列的表,m和n是矩阵的维数。 矩阵主要完成的操作有三种:

  • 矩阵相加
  • 矩阵转置
  • 矩阵相乘 这三个概念,大学线性代数的课程里都讲过,这里就不赘述。 下面直接上代码: matrix.cpp
代码语言:javascript
复制
/*
 * 矩阵测试代码
 * matrix.cpp
*/
#include<iostream>
#include"matrix.h"

using namespace std;

int main(void)
{
   //测试矩阵类
    matrix<int> x(3,2),y,z;
    int i,j;
    for(i = 1;i<=3;i++)
        for(j = 1;j<=2;j++)
            x(i,j) = 2*i+j;
    cout << "Initalized x(x,j) = 2*i + j"<<endl;
    cout <<"x(3,1) = "<<x(3,1)<<endl;
    cout<<"The metrix x is:"<<endl;
    cout <<x;

    //矩阵赋值
    y = x;
    cout<<"The matrix y is:"<<endl;
    cout <<y;

    //两个矩阵相加结果
    z = y + x;
    cout <<"y + x is"<<endl;
    cout<<z;

    //矩阵求负
    cout<<"-(y+x) is "<<endl;
    cout<<-z;

    //矩阵相乘
    matrix<int> w(2,3);
    for(i=1;i<=2;i++)
    {
        for(j=1;j<=3;j++)
        {
            w(i,j) = i+j;
        }
    }
    cout<<"Initialized w(i,j) = i+j"<<endl;
    cout<<"w is "<<endl;
    cout<< w <<endl;
    z = y*w;
    cout<<"y * w is"<<endl;
    cout<< z<<endl;


    cout<<"Hello World!"<<endl;
    return 0;
}

matrix.h

代码语言:javascript
复制
/*
 * 矩阵类,实现了矩阵的一些基础性质:矩阵相加,相乘,矩阵转置
 * matrix.h
*/
#ifndef MATRIX_H
#define MATRIX_H

#include"myexceptions.h"

using namespace std;

template<class T>
class matrix
{
    friend ostream& operator<<(ostream&,const matrix<T>&);
public:
    //构造函数
    matrix(int theRows = 0,int theColumns = 0);

    //复制构造函数
    matrix(const matrix<T>&);

    //析构函数
    ~matrix(){delete [] element;}

    int rows() const {return theRows;}//返回数组行数
    int columns() const {return theColumns;}//返回数组列数

    T& operator()(int i,int j) const;//对"()"进行运算符重载
    matrix<T>& operator=(const matrix<T>&);//对"="进行运算符重载
    matrix<T> operator+()const;//非数组“+”
    matrix<T> operator+(const matrix<T>&) const;//数组相加
    matrix<T> operator-()const;//非数组减
    matrix<T> operator-(const matrix<T>&) const;//数组减
    matrix<T> operator*(const matrix<T>&) const;//数组乘
    matrix<T>& operator+=(const T&);
private:
    int theRows;//数组的行数
    int theColumns;//数组的列数
    T *element;//元素数组
};

/*
 * 以下是对类内各种函数的具体实现
*/

//构造函数
template<class T>
matrix<T>::matrix(int theRows, int theColumns)
{
    if(theColumns < 0 || theRows < 0)
        throw illegalParameterValue("Rows and Columns must be >= 0");
    if((theRows == 0 || theColumns == 0)
            && (theRows != 0 || theColumns != 0))
        throw illegalParameterValue("Either both or neither rows and columns should be zero");

    ///创建矩阵
    this->theColumns = theColumns;
    this->theRows = theRows;
    element = new T[theRows*theColumns];
}

//复制构造函数
template<class T>
matrix<T>::matrix(const matrix<T> &m)
{
    theRows = m.theRows;
    theColumns = m.theColumns;
    element = new  T [theRows*theColumns];

    copy(m.element,
         m.element+theColumns*theRows,
         element);
}

//“=”重载
template<class T>
matrix<T>& matrix<T>::operator=(const matrix<T>& m)
{
    if(this != &m)
    {
        delete [] element;
        theRows = m.theRows;
        theColumns = m.theColumns;
        element = new T[theColumns*theRows];

        copy(m.element,m.element+theRows*theColumns,element);
    }
    return *this;
}

//"()"重载
template<class T>
T& matrix<T>::operator ()(int i,int j) const
{
    if(i<1 || i > theRows
        || j < 1 || j > theColumns)
        throw matrixIndexOutOfBounds();
    return element[(i-1)*theColumns+j-1];
}

//"+"重载
template<class T>
matrix<T> matrix<T>::operator+(const matrix<T>& m)const
{
    if(theRows != m.theRows
            || theColumns != m.theColumns)
        throw matrixSizeMismatch();

    matrix<T> w(theRows,theColumns);//创建结果矩阵w
    for(int i = 0;i< theRows*theColumns;i++)
        w.element[i] = element[i] + m.element[i];
    return w;
}

//"-"重载
template<class T>
matrix<T> matrix<T>::operator - (const matrix<T>& m)const
{
    if(theRows != m.theRows
            || theColumns != m.theColumns)
        throw matrixSizeMismatch();

    matrix<T> w(theRows,theColumns);//创建结果数组
    for(int i = 0;i< theColumns*theRows;i++)
        w.element[i] = element[i]-m.element[i];

    return w;
}

template<class T>
matrix<T> matrix<T>::operator - () const
{
    matrix<T> w(theRows,theColumns);
    for(int i = 0;i<theColumns*theRows;i++)
        w.element[i] = -element[i];
    return w;
}

//"*"重载,实现数组乘法
template<class T>
matrix<T> matrix<T>::operator * (const matrix<T>& m) const
{
    //计算(*this)*m的结果
    if(theColumns != m.theRows)
        throw matrixSizeMismatch();

    matrix<T> w(theRows,m.theColumns);
    int ct = 0,cm = 0,cw = 0;
    for(int i = 1;i <= theRows;i++)
    {
        for(int j = 1;j <= m.theColumns;j++)
        {
            T sum = element[ct] * m.element[cm];

            for(int k = 2;k<=theColumns;k++)
            {
                ct++;
                cm += m.theColumns;
                sum += element[ct]*m.element[cm];
            }
            w.element[cw++] = sum;
            ct -= theColumns - 1;
            cm = j;
        }
        ct += theColumns;
        cm = 0;
    }

    return w;
}

template<class T>
ostream& operator<<(ostream& out,const matrix<T>& m)
{
    int k = 0;
    for(int i = 0;i < m.theRows;i++)
    {
        for(int j = 0;j < m.theRows;j++)
            out << m.element[k++] << " ";

        out <<endl;
    }
    return out;
}

ostream& operator <<(ostream& out,const matrix<int>& m)
{
    int k = 0;
    for(int i = 0;i < m.theRows;i++)
    {
        for(int j = 0;j < m.theColumns;j++)
            out << m.element[k++] <<" ";
        out <<endl;
    }
    return out;
}

#endif // MATRIX_H

myExceptions.h

代码语言:javascript
复制
/*
 *异常类
 * myExceptions.h
*/
#ifndef MYEXCEPTIONS_H
#define MYEXCEPTIONS_H

#include<string>
using namespace std;

//参数值不合法
class illegalParameterValue
{
  public:
    illegalParameterValue(string theMessage = "Illegal parameter value")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout << message <<endl;
    }
private:
    string message;
};

//矩阵索引值越界
class matrixIndexOutOfBounds
{
  public:
    matrixIndexOutOfBounds
                  (string theMessage = "Matrix index out of bounds")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout <<message <<endl;
    }
private:
    string message;
};

class matrixSizeMismatch
{
  public:
    matrixSizeMismatch
             (string theMessage = "The size of the two matrics doesn't match")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout << message <<endl;
    }
private:
    string message;
};

#endif // MYEXCEPTIONS_H
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档