gpt4 book ai didi

c++ - 特征:在复杂矩阵上选择函数

转载 作者:行者123 更新时间:2023-11-28 02:03:54 28 4
gpt4 key购买 nike

我有这段 matlab 代码,我想用 Eigen 编写:

[V_K,D_K] = eig(K);
d_k = diag(D_K);
ind_k = find(d_k > 1e-8);
d_k(ind_k) = d_k(ind_k).^(-1/2);
K_half = V_K*diag(d_k)*V_K';

到目前为止,我编写的代码是(最后一部分基于以下 this 示例):

Eigen::EigenSolver<Eigen::MatrixXf> es (K,true);
Eigen::MatrixXcf v = es.eigenvalues();
v = (v.array()).select(1/std::sqrt(v),v);

这显然是错误的(首先,你不能在 Eigen::MatrixXcf 上应用 std::sqrt(v)),但我没有弄清楚怎么写。你能帮帮我吗?

最佳答案

在您链接的示例中,您需要将 .array() (.real()?) 与 > 1e-8 并使用 Eigens 函数(或 unaryExpr )来操作矩阵的值:

v = (v.array().real() 
// ^^^
// can't compare complex numbers

> 1e-8)
// ^^^
// Actually compare the number
.select(v.cwiseSqrt().cwiseInverse(), v);
// ^^^
// Use Eigens functions or alternatively unaryExpr

// In one line:
v = (v.array().real() > 1e-8).select(v.cwiseSqrt().cwiseInverse(), v);

关于c++ - 特征:在复杂矩阵上选择函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38324877/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com