【问题标题】:c++/eigen:compute the orthonormal basis for the null spacec++/eigen:计算零空间的正交基
【发布时间】:2017-06-21 11:44:22
【问题描述】:

matlab有一个功能:

Z = null(A);

这个 MATLAB 函数是获得的 A 的零空间的正交基 从奇异值分解。

我必须在 eigen 库上将 MATLAB 代码转换为 C++,但我不知道该怎么做。 我试过了:

MatrixXf m = MatrixXf::Random(3,5);
cout << "Here is the matrix m:" << endl << m << endl;
MatrixXf ker = m.fullPivLu().kernel();
cout << "Here is a matrix whose columns form a basis of the kernel of m:"
<< endl << ker << endl;

输出:

Here is the matrix m:
0.68   0.597   -0.33   0.108   -0.27
-0.211   0.823   0.536 -0.0452  0.0268
 0.566  -0.605  -0.444   0.258   0.904
Here is a matrix whose columns form a basis of the kernel of m:
-0.219   0.763
0.00335  -0.447
  0       1
  1       0
 -0.145  -0.285

这不是零空间的“正交”基。

【问题讨论】:

  • 单独的LU分解只能提取零空间的任意基。要获得正交标准,您需要一些 QR 分解或 SVD。
  • @ggael 但我不知道如何使用 QR 分解或 SVD 来计算零空间的正交基......

标签: c++ matrix linear-algebra eigen eigen3


【解决方案1】:

您可以使用CompleteOrthogonalDecomposition 获得零空间的正交基:

template <typename Number> // 'Number' can be 'double' or 'std::complex<double>'
Eigen::Matrix<Number, Eigen::Dynamic, Eigen::Dynamic> kernel_COD(
    const Eigen::Matrix<Number, Eigen::Dynamic, Eigen::Dynamic>& M) {
  Eigen::CompleteOrthogonalDecomposition<
      Eigen::Matrix<Number, Eigen::Dynamic, Eigen::Dynamic>>
      cod;
  cod.compute(M);
  unsigned rk = cod.rank();
  Eigen::Matrix<Number, Eigen::Dynamic, Eigen::Dynamic> P =
      cod.colsPermutation();
  Eigen::Matrix<Number, Eigen::Dynamic, Eigen::Dynamic> V =
      cod.matrixZ().transpose();
  Eigen::Matrix<Number, Eigen::Dynamic, Eigen::Dynamic> Kernel =
      P * V.block(0, rk, V.rows(), V.cols() - rk);
  return Kernel;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2011-02-28
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多