Eigen3中对称矩阵的行列式

问题描述 投票:0回答:1
  • 程序将值放入矩阵中以形成3×3对称矩阵X,然后计算X的行列式。
  • 该程序使用g++ -Ofast编译
  • 程序是否仅在X中使用6个浮点数来计算行列式,因为X是对称的?

程序

#include <Eigen/Eigen>
#include <iostream>
#include <cstdlib>
#include <ctime>
template <typename T>
T symdet(T a) {
    Eigen::Matrix<T, 3, 3> X;
    X(0, 0) = a;
    X(1, 1) = 2 * a;
    X(2, 2) = 3 * a;
    X(0, 1) = X(1, 0) = 4 * a;
    X(0, 2) = X(2, 0) = 5 * a;
    X(1, 2) = X(2, 1) = 6 * a;
    return X.determinant();
}
int main() {
    srand (static_cast <unsigned> (time(0)));
    float a = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
    std::cout << symdet(a) << "\n";
}
c++ eigen eigen3
1个回答
0
投票

可能它可以,因为它无论如何都不使用矢量化操作。我安装了编译器资源管理器的本地实例:https://github.com/mattgodbolt/compiler-explorer

C ++代码:

#include <Eigen/Eigen>
float symdet(float a, float b, float c, float d, float e, float f) {
    Eigen::Matrix<float, 3, 3> X;
    X(0, 0) = a;
    X(1, 1) =  b;
    X(2, 2) =  c;
    X(0, 1) = X(1, 0) = d;
    X(0, 2) = X(2, 0) = e;
    X(1, 2) = X(2, 1) = f;
    return X.determinant();
}

汇编(g ++ - 7 -Ofast -I):

symdet(float, float, float, float, float, float):
  movaps xmm6, xmm1
  mulss xmm1, xmm4
  movaps xmm7, xmm5
  mulss xmm6, xmm2
  mulss xmm7, xmm5
  mulss xmm2, xmm3
  subss xmm6, xmm7
  mulss xmm0, xmm6
  movaps xmm6, xmm4
  mulss xmm6, xmm5
  subss xmm2, xmm6
  mulss xmm2, xmm3
  mulss xmm3, xmm5
  subss xmm0, xmm2
  subss xmm3, xmm1
  mulss xmm4, xmm3
  addss xmm0, xmm4
  ret

ss操作是标量,仅适用于1个浮点值,而不是向量运算。只有6个输入值xmm[0 - 5]xmm[6, 7]持有中间体。

© www.soinside.com 2019 - 2024. All rights reserved.