欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++矩陣運(yùn)算的實(shí)現(xiàn)簡(jiǎn)單

 更新時(shí)間:2021年09月03日 09:55:30   作者:victor_JZ  
本文主要介紹了C++矩陣運(yùn)算的實(shí)現(xiàn)簡(jiǎn)單,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

利用C++實(shí)現(xiàn)矩陣的構(gòu)造,通過(guò)運(yùn)算符的重載實(shí)現(xiàn)矩陣的乘法、加法等。并且實(shí)現(xiàn)矩陣形狀的打印,矩陣的打印。

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

class Matrix{
    public:
        Matrix(int row, int col);   //構(gòu)造函數(shù)
        Matrix(int row, int col, int num);//構(gòu)造函數(shù)重載
        ~Matrix();                  //析構(gòu)函數(shù)
        Matrix(const Matrix & other); //賦值函數(shù)
        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);      //構(gòu)造一個(gè)(2,1)矩陣
    Matrix b(1,2);      //構(gòu)造一個(gè)(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;
    }
}

矩陣求逆算法及程序?qū)崿F(xiàn)

 在做課題時(shí),遇到了求多項(xiàng)式問(wèn)題,利用了求逆方法。矩陣求逆一般使用簡(jiǎn)單的算法,還有快速算法 如全選主元高斯-約旦消元法,但本文程序主要寫(xiě)了簡(jiǎn)單的矩陣求逆算法定義法之伴隨矩陣求逆公式如下,其中A可逆:A^{-1}=\frac{A^*}{|A|},其中A^*是A的伴隨矩陣。。

  1.給定一個(gè)方陣,非奇異(不是也可,程序有考慮);

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

  3.求其伴隨矩陣A^*;

  4.得到其逆矩陣。

主要函數(shù)如下:

//得到給定矩陣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;

}

計(jì)算|A|:

//按第一行展開(kāi)計(jì)算|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;
}

計(jì)算伴隨矩陣:

//計(jì)算每一行每一列的每個(gè)元素所對(duì)應(yīng)的余子式,組成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];
            }
        }
    }
}

到此這篇關(guān)于C++矩陣運(yùn)算的實(shí)現(xiàn)簡(jiǎn)單的文章就介紹到這了,更多相關(guān)C++ 矩陣運(yùn)算內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • QT5編譯使用QFtp的方法步驟

    QT5編譯使用QFtp的方法步驟

    這篇文章主要介紹了QT5編譯使用QFtp的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • C語(yǔ)言利用面試真題理解指針的使用

    C語(yǔ)言利用面試真題理解指針的使用

    C語(yǔ)言這門(mén)課程在計(jì)算機(jī)的基礎(chǔ)教學(xué)中一直占有比較重要的地位,然而要想突破C語(yǔ)言的學(xué)習(xí),對(duì)指針的掌握是非常重要的,本文將具體針對(duì)指針的基礎(chǔ)做詳盡的介紹
    2022-08-08
  • C++預(yù)處理連接的示例詳解

    C++預(yù)處理連接的示例詳解

    C++預(yù)處理連接(Preprocessor?Concatenation)是一種宏定義技巧,用于將兩個(gè)或多個(gè)符號(hào)(如變量、字符串等)連接成一個(gè)符號(hào)。這篇文章主要通過(guò)一些示例為大家講解一下預(yù)處理連接,需要的可以參考一下
    2023-03-03
  • C++用read()和write()讀寫(xiě)二進(jìn)制文件的超詳細(xì)教程

    C++用read()和write()讀寫(xiě)二進(jìn)制文件的超詳細(xì)教程

    二進(jìn)制的文件肉眼我們是讀不懂的,如果通過(guò)二進(jìn)制的讀寫(xiě)操作就可以讀懂,下面這篇文章主要給大家介紹了關(guān)于C++用read()和write()讀寫(xiě)二進(jìn)制文件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • c語(yǔ)言中scanf的基本用法

    c語(yǔ)言中scanf的基本用法

    這篇文章主要給大家介紹了關(guān)于c語(yǔ)言中scanf的基本用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C/C++實(shí)現(xiàn)目錄監(jiān)視器的方法詳解

    C/C++實(shí)現(xiàn)目錄監(jiān)視器的方法詳解

    這篇文章主要介紹了C/C++ 實(shí)現(xiàn)目錄監(jiān)視器的方法,然后網(wǎng)上查到的基本就有三種方法,使用FindFirstChangeNotification等系列函數(shù),使用ReadDirectoryChangesW函數(shù)和使用change journals,本文使用了第二種方式來(lái)實(shí)現(xiàn)一個(gè)目錄監(jiān)視,需要的朋友可以參考下
    2024-04-04
  • 配置CLion管理Qt項(xiàng)目國(guó)際化支持的方法

    配置CLion管理Qt項(xiàng)目國(guó)際化支持的方法

    這篇文章主要介紹了配置CLion管理Qt項(xiàng)目國(guó)際化支持的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • VScode+ESP32簡(jiǎn)單環(huán)境搭建

    VScode+ESP32簡(jiǎn)單環(huán)境搭建

    本文章向大家介紹ESP32-C3搭建環(huán)境教程,主要包括ESP32-C3搭建環(huán)境教程使用實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • C++在vscode中的多文件編程問(wèn)題解讀

    C++在vscode中的多文件編程問(wèn)題解讀

    這篇文章主要介紹了C++在vscode中的多文件編程問(wèn)題解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • c語(yǔ)言如何實(shí)現(xiàn)DES加密解密

    c語(yǔ)言如何實(shí)現(xiàn)DES加密解密

    這篇文章主要介紹了c語(yǔ)言如何實(shí)現(xiàn)DES加密解密問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04

最新評(píng)論