C++菜鸟记录-Eigen

目录

学习资料

Eigen环境配置

Eigen-Getting Started

The Matrix class 初始化

Matrix and vector arithmetic 运算

The Array class and coefficient-wise operations 逐点运算

Block operations 块运算

Slicing and Indexing 切片与索引

Advanced initialization 高级初始化

Linear algebra and decompositions 线性代数


学习资料

数字几何处理课程

课程主页:Digital Geometry Processing

作业代码:https://github.com/USTC-GCL-F/AMMesh

GAMES101课程

课程主页:GAMES101: 现代计算机图形学入门

作业代码:http://games-cn.org/forums/topic/allhw/

Eigen环境配置

参考:games101【作业0上】:超详细的MacOS环境搭建 - 掘金 (juejin.cn)

Apple M1 Pro

brew install eigen
brew link --overwrite eigen

安装位置:/opt/homebrew/Cellar/eigen/3.4.0_1

cmake_minimum_required (VERSION 3.10)
project (PA0)
find_package(Eigen3 3.3 REQUIRED)
# 设置路径
SET(EIGEN3_INCLUDE_DIR "/opt/homebrew/Cellar/eigen/3.4.0_1/include")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
include_directories(${EIGEN3_INCLUDE_DIR})
add_executable (main main.cpp)
target_link_libraries( main ${Eigen3_LIBS} )
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/main",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/build",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

Eigen-Getting Started

官方文档:Eigen: Getting started

目录树

.
├── CMakeLists.txt
├── build <- 新建文件夹
└── main.cpp

编译

mkdir build
cd build
cmake ..
make

The Matrix class 初始化

Eigen defines the following Matrix typedefs:

  • MatrixNt for Matrix<type, N, N>. For example, MatrixXi for Matrix<int, Dynamic, Dynamic>.
  • MatrixXNt for Matrix<type, Dynamic, N>. For example, MatrixX3i for Matrix<int, Dynamic, 3>.
  • MatrixNXt for Matrix<type, N, Dynamic>. For example, Matrix4Xd for Matrix<d, 4, Dynamic>.
  • VectorNt for Matrix<type, N, 1>. For example, Vector2f for Matrix<float, 2, 1>.
  • RowVectorNt for Matrix<type, 1, N>. For example, RowVector3d for Matrix<double, 1, 3>.

Where:

  • N can be any one of 234, or X (meaning Dynamic).
  • t can be any one of i (meaning int), f (meaning float), d (meaning double), cf (meaning complex<float>), or cd (meaning complex<double>). The fact that typedefs are only defined for these five types doesn't mean that they are the only supported scalar types. For example, all standard integer types are supported, see Scalar types.

初始化

#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    // m(3,3)以逗号分隔的方式初始化矩阵
    Eigen::Matrix3f m;
    m << 1, 2, 3,
         4, 5, 6,
         7, 8, 9;
    std::cout << m << std::endl;
    // 列向量
    Eigen::Vector3f v(1, 2, 3);
    std::cout << "v shape : " << v.rows() << "x" << v.cols() << std::endl;
    // 行向量
    Eigen::RowVector3f rv(1, 2, 3);
    std::cout << "rv shape : " << rv.rows() << "x" << rv.cols()  << std::endl;
    return 0;
}

Matrix and vector arithmetic 运算

矩阵乘法`A*B`

向量点乘`v.dot(v.transpose())` ,`v.transpose().dot(v)` 

#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    // reduce
    Eigen::Matrix2d mat;
    mat << 1, 2, 3, 4;
    std::cout << "Here is mat.sum():       " << mat.sum()       << std::endl;
    std::cout << "Here is mat.prod():      " << mat.prod()      << std::endl;
    std::cout << "Here is mat.mean():      " << mat.mean()      << std::endl;
    std::cout << "Here is mat.minCoeff():  " << mat.minCoeff()  << std::endl;
    std::cout << "Here is mat.maxCoeff():  " << mat.maxCoeff()  << std::endl;
    std::cout << "Here is mat.trace():     " << mat.trace()     << std::endl;
    
    std::ptrdiff_t i, j;
    float minOfM = mat.minCoeff(&i,&j);
    std::cout << "Here is the matrix m:\n" << mat << std::endl;
    std::cout << "Its minimum coefficient (" << minOfM 
              << ") is at position (" << i << "," << j << ")" << std::endl;
    
    Eigen::RowVector4i v(-1, -2, -3, -4);
    int maxOfV = v.maxCoeff(&i);
    std::cout << "Its maximum coefficient (" << maxOfV 
              << ") is at position " << i << std::endl;
    return 0;
}

The Array class and coefficient-wise operations 逐点运算

  • Matrix类主要用于线性运算,Array类用于逐点运算。
  • Array类支持1维(ArrayNt)或2维(ArrayNNt)数组。
  • Matrix类`m.array()`与Array类`a.matrix()`可以相互转换。
#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    Eigen::Array22d a;
    a << 1, 2, 3, 4;
    Eigen::Array22d b;
    b << 5, 6, 7, 8;
    
    std::cout << "a * b = " << std::endl << a * b << std::endl;
    std::cout << "a.m * b.m = " << std::endl << a.matrix() * b.matrix() << std::endl;
    return 0;
}

Block operations 块运算

#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    Eigen::Matrix3d a;
    a << 1, 2, 3, 4, 5, 6, 7, 8, 9;

    Eigen::Matrix2d b;
    b = a.block(0, 0, 2, 2);
    std::cout << b << std::endl;

    Eigen::RowVector3d a1;
    a1 = a.row(1);
    std::cout << a1 << std::endl;

    Eigen::Vector3d a2;
    a2 = a.col(0);
    std::cout << a2 << std::endl;
    return 0;
}

Slicing and Indexing 切片与索引

  • 类似于numpy
#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    Eigen::Matrix4d a;
    a << 1,   2,  3,  4, 
         5,   6,  7,  8, 
         9,  10, 11, 12, 
         13, 14, 15, 16;
    std::cout << a(Eigen::all, Eigen::seq(1,a.cols()-1,2)) << std::endl;
    return 0;
}

Advanced initialization 高级初始化

  • 逗号分隔可以用于拼接
#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    Eigen::Matrix2d a,b;
    a << 1, 2, 3, 4;
    b << 5, 6, 7, 8;

    Eigen::MatrixXd c(2,4);
    c << a, b;
    std::cout << c << std::endl;

    Eigen::MatrixXd d(4,2);
    d << a, b;
    std::cout << d << std::endl;
    return 0;
}
  • 常值初始化
#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    Eigen::Matrix2d a = Eigen::MatrixXd::Zero(2,2);
    std::cout << a << std::endl;
    Eigen::Matrix2d b = Eigen::MatrixXd::Ones(2,2);
    std::cout << b << std::endl;
    Eigen::Matrix2d c = Eigen::MatrixXd::Constant(2,2,3);
    std::cout << c << std::endl;
    Eigen::Matrix2d d = Eigen::MatrixXd::Identity(2,2);
    std::cout << d << std::endl;
}
  • 与块运算结合
#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    const int n = 4;
    Eigen::MatrixXf A = Eigen::MatrixXf::Zero(n, n);
    A.topLeftCorner(n/2, n/2) = Eigen::MatrixXf::Identity(n/2, n/2);
    A.bottomRightCorner(n/2, n/2) = Eigen::MatrixXf::Constant(n/2, n/2, 8);
    std::cout << "A = \n" << A << std::endl;
    return 0;
}

Linear algebra and decompositions 线性代数

Eigen: Linear algebra and decompositions

  • 求解线性方程组
#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    Eigen::Matrix3f A;
    A << 1, 2, 3,
         0, 4, 5,
         0, 0, 6;
    Eigen::Vector3f b;
    b << 6, 9, 6;
    Eigen::Vector3f x = A.colPivHouseholderQr().solve(b);
    std::cout << "The solution is:\n" << x << std::endl;
    return 0;
}
  • 特征值与特征向量
#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    Eigen::Matrix3f A;
    A << 1, 2, 3,
         0, 4, 5,
         0, 0, 6;
    Eigen::SelfAdjointEigenSolver<Eigen::Matrix3f> eigensolver(A);
    if (eigensolver.info() != Eigen::Success) abort();
    std::cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << std::endl;
    std::cout << "Here's a matrix whose columns are eigenvectors of A \n"
        << "corresponding to these eigenvalues:\n"
        << eigensolver.eigenvectors() << std::endl;
    return 0;
}
  • 矩阵求逆与行列式
#include<eigen3/Eigen/Dense>
#include<iostream>

int main(){
    Eigen::Matrix3f A;
    A << 1, 2, 3,
         0, 4, 5,
         0, 0, 6;
    std::cout << A.inverse() << std::endl;
    std::cout << A.determinant() << std::endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值