一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C/C++ - C++矩陣運算的實現簡單

C++矩陣運算的實現簡單

2021-12-29 13:55victor_JZ C/C++

本文主要介紹了C++矩陣運算的實現簡單,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

利用C++實現矩陣的構造,通過運算符的重載實現矩陣的乘法、加法等。并且實現矩陣形狀的打印,矩陣的打印。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#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.得到其逆矩陣。

主要函數如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//得到給定矩陣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|:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//按第一行展開計算|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;
}

計算伴隨矩陣:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//計算每一行每一列的每個元素所對應的余子式,組成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++ 矩陣運算內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/victor_JZ/article/details/120069521

延伸 · 閱讀

精彩推薦
  • C/C++c++ 單線程實現同時監聽多個端口

    c++ 單線程實現同時監聽多個端口

    這篇文章主要介紹了c++ 單線程實現同時監聽多個端口的方法,幫助大家更好的理解和學習使用c++,感興趣的朋友可以了解下...

    源之緣11542021-10-27
  • C/C++學習C++編程的必備軟件

    學習C++編程的必備軟件

    本文給大家分享的是作者在學習使用C++進行編程的時候所用到的一些常用的軟件,這里推薦給大家...

    謝恩銘10102021-05-08
  • C/C++C語言實現電腦關機程序

    C語言實現電腦關機程序

    這篇文章主要為大家詳細介紹了C語言實現電腦關機程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    xiaocaidayong8482021-08-20
  • C/C++C/C++經典實例之模擬計算器示例代碼

    C/C++經典實例之模擬計算器示例代碼

    最近在看到的一個需求,本以為比較簡單,但花了不少時間,所以下面這篇文章主要給大家介紹了關于C/C++經典實例之模擬計算器的相關資料,文中通過示...

    jia150610152021-06-07
  • C/C++C語言中炫酷的文件操作實例詳解

    C語言中炫酷的文件操作實例詳解

    內存中的數據都是暫時的,當程序結束時,它們都將丟失,為了永久性的保存大量的數據,C語言提供了對文件的操作,這篇文章主要給大家介紹了關于C語言中文件...

    針眼_6702022-01-24
  • C/C++深入理解goto語句的替代實現方式分析

    深入理解goto語句的替代實現方式分析

    本篇文章是對goto語句的替代實現方式進行了詳細的分析介紹,需要的朋友參考下...

    C語言教程網7342020-12-03
  • C/C++C++之重載 重定義與重寫用法詳解

    C++之重載 重定義與重寫用法詳解

    這篇文章主要介紹了C++之重載 重定義與重寫用法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下...

    青山的青6062022-01-04
  • C/C++詳解c語言中的 strcpy和strncpy字符串函數使用

    詳解c語言中的 strcpy和strncpy字符串函數使用

    strcpy 和strcnpy函數是字符串復制函數。接下來通過本文給大家介紹c語言中的strcpy和strncpy字符串函數使用,感興趣的朋友跟隨小編要求看看吧...

    spring-go5642021-07-02
主站蜘蛛池模板: 久久精品热只有精品 | 456在线观看 | 亚洲乱码一区二区三区国产精品 | 国产成人精品高清在线观看99 | 日韩欧美中文在线 | 欧美一卡二卡科技有限公司 | 交换朋友夫妇3中文字幕 | 国产在线欧美精品 | 亚洲视频免费在线看 | 九九九九九九精品免费 | 欧美特黄一级大片 | 被老头肉至怀孕小说 | 亚洲好骚综合 | 久草色视频 | 国产亚洲一区二区三区 | 亚洲AV久久久噜噜噜久久 | 亚洲天堂男人的天堂 | 亚洲日本在线观看网址 | 青草国内精品视频在线观看 | 日本人添下面的全过程 | 日本高清在线看免费观看 | 好吊色视频988gao在线观看 | 亚洲成人aa | 久草在线精彩免费视频 | 2019国内精品久久久久久 | 加勒比一本大道香蕉在线视频 | 欧美专区在线观看 | 免费看男人使劲躁女人小说 | girlfriend动漫在线播放 | 国产91在线精品 | 久久亚洲网站 | 美女无遮挡 | 国产精品高清一区二区三区 | 欧美高清在线不卡免费观看 | 日本一本二本三区免费 | 日本韩国一区二区三区 | 单亲乱l仑在线观看免费观看 | www.四虎.com| 国内精品福利丝袜视频_速 国内精品91久久久久 | 久久学生精品国产自在拍 | 热久久免费视频 |