C++矩阵运算的实现简单

利用C++实现矩阵的构造,通过运算符的重载实现矩阵的乘法、加法等。并且实现矩阵形状的打印,矩阵的打印。

#include<iostream>
#include<memory>
#include<assert.h>
#include<stdlib.h>
using namespace std;

class Matrix{
    public:
        Matrix(int row, int col);   //构造函数
        Matrix(int row, int col, int num);//构造函数重载
        ~Matrix();                  //析构函数
        Matrix(const Matrix & other); //赋值函数
        Matrix operator*(const Matrix& other);      //矩阵相乘
        Matrix operator+(const Matrix& other);      //矩阵相加
        Matrix operator-(const Matrix& other);      //矩阵相减
        int **a = nullptr;          //初始化一共空指针
        int row, col;
        void shape();               //打印矩阵形状
        void Ma_pri();              //打印矩阵
};

int main(){
    Matrix a(2,1);      //构造一个(2,1)矩阵
    Matrix b(1,2);      //构造一个(1,2)矩阵
    a.a[0][0] = 4;      //初始化矩阵
    a.a[1][0] = 2;
    b.a[0][0] = 3;
    b.a[0][1] = 5;
    a.shape();          //矩阵形状打印
    b.shape();
    Matrix c = a*b;     //矩阵相乘
    c.shape();
    c.Ma_pri();         //矩阵打印
    Matrix d(3,3,1);
    d.Ma_pri();
    system("pause");
    return 0;
}

Matrix::Matrix(int row, int col){
    this->row = row;
    this->col = col;
    this->a = new int*[row];
    for(int i=0;i<this->row;i++){
        a[i] = new int[this->col];
    }
}

Matrix::Matrix(int row, int col, int num){
    this->row = row;
    this->col = col;
    this->a = new int*[row];
    for(int i=0;i<this->row;i++){
        a[i] = new int[this->col];
    }
    for(int i = 0; i < this->row; i++){
        for(int j =0; j <this->row; j++){
            this->a[i][j] = num;
        }
    }
}

Matrix::~Matrix(){
    for(int i=0;i<this->row;i++){
        if(a[i] != nullptr){
            delete[] a[i];
            a[i] = nullptr;
        }
    }
    if(a != nullptr){
        delete[] a;
        a = nullptr;
    }
}

Matrix::Matrix(const Matrix& other){
    row = other.row;
    col = other.col;
    a = new int*[row];
    for(int i=0;i<row;i++){
        a[i] = new int[col];
        memcpy(a[i], other.a[i],sizeof(int)*col);
    }
}

Matrix Matrix::operator*(const Matrix& other){
    if(this->col != other.row){
        cout<<"shape error"<<endl;
        exit(0);
    }
    Matrix m(this->row,other.col);
    for(int i=0; i<this->row; i++){
        for(int j=0;j<other.col;j++){
            int sum = 0;
            for(int k=0;k<this->col;k++){
                sum += this->a[i][k] * other.a[k][j];
            }
            m.a[i][j] = sum;
        }
    }
    return m;
}

Matrix Matrix::operator+(const Matrix& other){
    if(this->col != other.col or this->row != other.row){
        cout<<"shape error"<<endl;
        exit(0);
    }
    Matrix m(this->row,this->col);
    for(int i = 0;i < this->row; i++){
        for(int j = 0; j < this-> col; j++){
            m.a[i][j] = this->a[i][j] + other.a[i][j];
        }
    }
    return m;
}
Matrix Matrix::operator-(const Matrix& other){
    if(this->col != other.col or this->row != other.row){
        cout<<"shape error"<<endl;
        exit(0);
    }
    Matrix m(this->row,this->col);
    for(int i = 0;i < this->row; i++){
        for(int j = 0; j < this-> col; j++){
            m.a[i][j] = this->a[i][j] - other.a[i][j];
        }
    }
    return m;
}

void Matrix::shape(){
    cout<<"("<<this->row<<","<<this->col<<")"<<endl;
}

void Matrix::Ma_pri(){
    for(int i = 0; i < this->row; i++){
        for(int j =0; j <this->row; j++){
            cout<<this->a[i][j]<<" ";
        }
        cout<<endl;
    }
}

矩阵求逆算法及程序实现

 在做课题时,遇到了求多项式问题,利用了求逆方法。矩阵求逆一般使用简单的算法,还有快速算法 如全选主元高斯-约旦消元法,但本文程序主要写了简单的矩阵求逆算法定义法之伴随矩阵求逆公式如下,其中A可逆:A^{-1}=\frac{A^*}{|A|},其中A^*是A的伴随矩阵。。

  1.给定一个方阵,非奇异(不是也可,程序有考虑);

  2.由矩阵得到其行列式,求其值如|A|;

  3.求其伴随矩阵A^*;

  4.得到其逆矩阵。

主要函数如下:

//得到给定矩阵src的逆矩阵保存到des中。
bool GetMatrixInverse(double src[N][N],int n,double des[N][N])
{
    double flag=getA(src,n);
    double t[N][N];
    if(flag==0)
    {
        return false;
    }
    else
    {
        getAStart(src,n,t);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                des[i][j]=t[i][j]/flag;
            }

        }
    }

    return true;

}

计算|A|:

//按第一行展开计算|A|
double getA(double arcs[N][N],int n)
{
    if(n==1)
    {
        return arcs[0][0];
    }
    double ans = 0;
    double temp[N][N]={0.0};
    int i,j,k;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1;j++)
        {
            for(k=0;k<n-1;k++)
            {
                temp[j][k] = arcs[j+1][(k>=i)?k+1:k];

            }
        }
        double t = getA(temp,n-1);
        if(i%2==0)
        {
            ans += arcs[0][i]*t;
        }
        else
        {
            ans -=  arcs[0][i]*t;
        }
    }
    return ans;
}

计算伴随矩阵:

//计算每一行每一列的每个元素所对应的余子式,组成A*
void  getAStart(double arcs[N][N],int n,double ans[N][N])
{
    if(n==1)
    {
        ans[0][0] = 1;
        return;
    }
    int i,j,k,t;
    double temp[N][N];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            for(k=0;k<n-1;k++)
            {
                for(t=0;t<n-1;t++)
                {
                    temp[k][t] = arcs[k>=i?k+1:k][t>=j?t+1:t];
                }
            }

            ans[j][i]  =  getA(temp,n-1);
            if((i+j)%2 == 1)
            {
                ans[j][i] = - ans[j][i];
            }
        }
    }
}

到此这篇关于C++矩阵运算的实现简单的文章就介绍到这了,更多相关C++ 矩阵运算内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C++ 实现稀疏矩阵的压缩存储的实例

    C++ 实现稀疏矩阵的压缩存储的实例 稀疏矩阵:M*N的矩阵,矩阵中有效值的个数远小于无效值的个数,且这些数据的分布没有规律. 稀疏矩阵的压缩存储:压缩存储值存储极少数的有效数据.使用{row,col,value}三元组存储每一个有效数据,三元组按原矩阵中的位置,以行优先级先后顺序依次存放. 实现代码: #include <iostream> #include <vector> using namespace std; template<class T> struct

  • C++实现矩阵原地转置算法

    本文实例描述了C++实现矩阵原地转置算法,是一个非常经典的算法,相信对于学习C++算法的朋友有很大的帮助.具体如下: 一.问题描述 微软面试题:将一个MxN的矩阵存储在一个一维数组中,编程实现矩阵的转置. 要求:空间复杂度为O(1) 二.思路分析 下面以一个4x2的矩阵A={1,2,3,4,5,6,7,8}进行分析,转置过程如下图: 图中右下角的红色数字表示在一维数组中的下标.矩阵的转置其实就是数组中元素的移动,具体的移动过程如下图: 我们发现,这些移动的元素的下标是一个个环,下标1的元素移动到

  • C++ Eigen库计算矩阵特征值及特征向量

    本文主要讲解利用Eigen库计算矩阵的特征值及特征向量并与Matlab计算结果进行比较. C++Eigen库代码 #include <iostream> #include <Eigen/Dense> #include <Eigen/Eigenvalues> using namespace Eigen; using namespace std; void Eig() { Matrix3d A; A << 1, 2, 3, 4, 5, 6, 7, 8, 9; c

  • C++实现图的邻接矩阵存储和广度、深度优先遍历实例分析

    本文实例讲述了C++实现图的邻接矩阵存储和广度.深度优先遍历的方法.分享给大家供大家参考.具体如下: 示例:建立如图所示的无向图 由上图知,该图有5个顶点,分别为a,b,c,d,e,有6条边. 示例输入(按照这个格式输入): 5 6 abcde 0 1 1 0 2 1 0 3 1 2 3 1 2 4 1 1 4 1 输入结束(此行不必输入) 注:0 1 1表示该图的第0个顶点和第1个定点有边相连,如上图中的a->b所示       0 2 1表示该图的第0个顶点和第2个定点有边相连,如上图中的a

  • C++实现:螺旋矩阵的实例代码

    通过观察发现矩阵的下标有这样一个规律:a行递增后b列递增然后c行递减再d列递减,但是对应值却是逐渐增加的.因此可用4个循环实现,需要注意的是在赋值时不要把之前的值覆盖了.所以在这里选择相同顔色部分赋值,代码如下: 复制代码 代码如下: #include <iostream>#include <iomanip> using namespace std; // 输出螺旋矩阵void Matrix(){    const int size = 10; // 矩阵大小    int mat

  • C++ 中重载和运算符重载加号实现矩阵相加实例代码

     C++ 重载+运算符重载加号 实现矩阵相加 学习C++ 基础知识,这里实现简单的实例,记录下自己学习生活,很简单,大家一起看看吧! 实例代码: #include<iostream> #include<iomanip> using namespace std; class Complex { private: int i,j,n,a[2][3]; public: Complex(); Complex operator+(Complex &c); void display()

  • C/C++实现矩阵的转置(示例代码)

    废话不多说,直接上代码 复制代码 代码如下: #include <iostream>using namespace std; const int N = 5; int matrix[5][5] ={    1,2,3,4,5,    1,2,3,4,5,    1,2,3,4,5,    1,2,3,4,5,    1,2,3,4,5}; void swap(int &a,int &b){    a=a^b;    b=a^b;    a=a^b;}void matrix_tr

  • C++实现稀疏矩阵的压缩存储实例

    什么是稀疏矩阵呢,就是在M*N的矩阵中,有效值的个数远小于无效值的个数,并且这些数据的分布没有规律.在压缩存储稀疏矩阵的时候我们只存储极少数的有效数据.我们在这里使用三元组存储每一个有效数据,三元组按原矩阵中的位置,以行优先级先后次序依次存放.下面我们来看一下代码实现. #include<iostream> #include<vector> #include<assert.h> using namespace std; template<class T> c

  • C++稀疏矩阵的各种基本运算并实现加法乘法

    代码: #include <iostream> #include<malloc.h> #include<cstdio> using namespace std; #define M 4 #define N 4 #define MaxSize 100 typedef int ElemType; typedef struct { int r; int c; ElemType d;///元素值 } TupNode; ///三元组定义 typedef struct { int

  • C++中实现矩阵的加法和乘法实例

    C++中实现矩阵的加法和乘法实例 实现效果图: 实例代码: #include<iostream> using namespace std; class Matrix { int row;//矩阵的行 int col;//矩阵的列 int **a;//保存二维数组的元素 public: Matrix();//默认构造函数 Matrix(int r, int c); Matrix(const Matrix &is);//拷贝构造函数 void Madd(const Matrix &

随机推荐