Eigen Library

C++

Posted by JXLIU on July 29, 2024

Tensor

Tensor 降阶, 切片, 转换为 Matrix

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
#include <Eigen/Dense>
#include <unsupported/Eigen/CXX11/Tensor>
#include <iostream>

int main()
{
    // 定义一个三维 Tensor,并赋值
    Eigen::Tensor<double, 3> fs;
    fs.resize(3, 3, 3);
    int tempi = 0;
    for (int i = 0; i < fs.dimension(0); i++)
    {
        for (int j = 0; j < fs.dimension(1); j++)
        {
            for (int k = 0; k < fs.dimension(2); k++)
            {
                fs(i, j, k) = 1.0 * tempi;
                tempi++;
            }
        }
    }

    // 打印出 Tensor 的内容
    std::cout << "fs:\n"
              << fs(0, 0, 0) << " " << fs(0, 0, 1) << " " << fs(0, 0, 2) << "\n"
              << fs(0, 1, 0) << " " << fs(0, 1, 1) << " " << fs(0, 1, 2) << "\n"
              << fs(0, 2, 0) << " " << fs(0, 2, 1) << " " << fs(0, 2, 2) << "\n\n"
              << fs(1, 0, 0) << " " << fs(1, 0, 1) << " " << fs(1, 0, 2) << "\n"
              << fs(1, 1, 0) << " " << fs(1, 1, 1) << " " << fs(1, 1, 2) << "\n"
              << fs(1, 2, 0) << " " << fs(1, 2, 1) << " " << fs(1, 2, 2) << "\n\n"
              << fs(2, 0, 0) << " " << fs(2, 0, 1) << " " << fs(2, 0, 2) << "\n"
              << fs(2, 1, 0) << " " << fs(2, 1, 1) << " " << fs(2, 1, 2) << "\n"
              << fs(2, 2, 0) << " " << fs(2, 2, 1) << " " << fs(2, 2, 2) << std::endl;

    // 做切片,以下代码保留 Tensor 的第三层,即 fs(2, :, :)
    int off0 = 2;
    int off1 = 0;
    int off2 = 0;
    int w0 = 1;
    int w1 = fs.dimension(1);
    int w2 = fs.dimension(2);
    Eigen::array<Eigen::Index, 3> offsets = {off0, off1, off2};
    Eigen::array<Eigen::Index, 3> extents = {w0, w1, w2};
    Eigen::Tensor<double, 3> fs_slice = fs.slice(offsets, extents); // 提取第 3 层(从第 6 行开始,3x3 矩阵)

    std::cout << "fs.slice({" << off0 << ", " << off1 << ", " << off2
              << "}, {" << w0 << ", " << w1 << ", " << w2 << "}):\n";
    for (int i = 0; i < fs_slice.dimension(0); i++)
    {
        for (int j = 0; j < fs_slice.dimension(1); j++)
        {
            for (int k = 0; k < fs_slice.dimension(2); k++)
            {
                std::cout << fs_slice(i, j, k) << " ";
            }
            std::cout << "\n"
                      << std::endl;
        }
        std::cout << "\n"
                  << std::endl;
    }
    // chip(m,n): 返回一个低一维度的 Tensor,返回第 n 个维度上的第 m 个 Tensor 切片
    // 这里返回的 fs_slice 就是 fs_slice_2d(:,:) = fs(0, :, :)
    Eigen::Tensor<double, 2> fs_slice_2d = fs_slice.chip(0, 0);
    std::cout << "fs_slice_2d:\n"
              << fs_slice_2d << std::endl;

    Eigen::MatrixXd matrix2d = Eigen::Map<const Eigen::Matrix<double, 3, 3>>(fs_slice_2d.data());
    std::cout << "matrix2d:\n"
              << matrix2d << std::endl;
    // 定义标量 psio
    double psio = 2;

    // 定义结果矩阵 jac
    Eigen::MatrixXd jac(3, 3);

    // 执行逐元素除法操作
    jac.array() = matrix2d.array() / psio;
    // 打印结果矩阵 jac
    std::cout << "Matrix jac:\n"
              << jac << std::endl;

    return 0;