Eigen矩阵和向量入门及简要函数说明

1.安装

内有安装Eigen的安装教程https://editor.csdn.net/md/?articleId=106730686

2.定义Eigen

#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
template <typename T> using Mat2 = Matrix<T, 2 , 2>;//创建一个矩阵定义方式1
int main(int argc, char *argv[]){
    Mat2<int> mat1;
    mat1 << 1,1,
            1,1;//矩阵赋值
    cout << mat1 << endl;//输出矩阵的值
    Vector2d vec1;//创建一个矩阵定义方式2
    vec1 << 1 ,2 ;
    cout << vec1 << endl;
    MatrixXd mat(2,2);//创建一个矩阵定义方式3
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            mat(i , j) = 1;
            cout << mat(i,j)  << " ";
        }
        cout << endl;
    }
    return 0;
}

在这里插入图片描述

3.全零阵,全一阵,单位阵和随机阵的定义方式

#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
    //全零阵,全一阵,单位阵和随机阵的定义方式
    MatrixXd matrix1 = MatrixXd::Zero(2,2);
    MatrixXd matrix2 = MatrixXd::Ones(2,2);
    MatrixXd matrix3 = MatrixXd::Identity(2,2);
    MatrixXd matrix4 = MatrixXd::Random(2,2);
    cout << matrix1 << endl;
    cout << matrix2 << endl;
    cout << matrix3 << endl;
    cout << matrix4 << endl;
    return 0;
    }

在这里插入图片描述

4.矩阵行列数赋值时的变化

#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
    //矩阵行列数赋值时的变化
    MatrixXd a(2,2);
    MatrixXd b(3,3);
    //cout << a.rows() << "  " << a.cols() << endl;
    //a = b;
    //cout << a.rows() << "  " << a.cols() << endl;
    cout << b.rows() << "  " << b.cols() << endl;
    b = a;
    cout << b.rows() << "  " << b.cols() << endl;
    return 0;
}

在这里插入图片描述

5.矩阵的四则运算

#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
    //矩阵的四则运算
    Matrix2d mat1;
    mat1 << 5,6,
            7,8;
    Matrix2d mat2;
    mat2 << 1,2,
            3,4;
    cout << mat1 + mat2 << endl;
    cout << mat1 - mat2 << endl;
    cout << mat1 * mat2 << endl;
    cout << mat1 / 2 << endl;
	cout << mat1.setZero() << endl;//置0
    return 0;
    }

在这里插入图片描述

6.特殊矩阵

#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
    //矩阵的特殊矩阵
    Matrix2d mat1;
    mat1 << 5,6,
           7,8;
	//mat1.transposeInPlace();
    cout <<  "转置矩阵" << endl << mat1.transpose()<< endl;//转置矩阵
    cout << "共轭矩阵" << endl << mat1.conjugate() << endl;//共轭矩阵
    cout << "逆矩阵" << endl << mat1.inverse() << endl;//逆矩阵
    cout << "伴随矩阵" << endl << mat1.adjoint() << endl;//伴随矩阵
    return 0;
    }

在这里插入图片描述

7.矩阵的快速取元素操作

7.1 矩阵的快速取元素操作1

#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
    //矩阵的快速取元素操作
    ArrayXd vec(10);
    vec << 1,2,3,4,5,6,7,8,9,0;
    cout << vec <<endl;
    cout << vec.head(3) << endl;//前三个
    cout << vec.tail(4 ) << endl;//后几个
    cout << vec.segment(3,8) << endl;//中间的几个
    
    return 0;
}

在这里插入图片描述7.2矩阵的快速取元素操作2

#include <iostream>
#include <eigen3/Eigen/Dense>//引用Eigen库
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]){
    //矩阵的快速取元素操作
    MatrixXd mat(4,4);
    mat <<  1,2,3,4,
            5,6,7,8,
            9,10,11,12,
            13,14,15,16;
    cout << mat.col(2) << endl;
    cout << mat.row(2) << endl;
    //取矩阵中的某块
    cout << mat.block(1,1,2,2) << endl;//参考点行数,参考点列数,取行数的个数,取列数的个数
    cout << mat.block<2,2>(1,1) << endl;
    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值