我是C ++程序的初学者。我必须实现一个程序,计算矩阵的伪逆。正如Eigen教程建议的那样,我编写了这样的代码:
#include <stdio.h>
#include <stdlib.h>
#include <Core>
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/SVD>
#include <Eigen/Eigen>
using namespace Eigen;
using namespace std;
void pinv(MatrixXf& pinvmat)
{
ei_assert(m_isInitialized && "SVD is not initialized.");
double pinvtoler=1.e-6; // choose tolerance
SingularValuesType m_sigma_inv=m_sigma;
for ( long i=0; i<m_workMatrix.cols(); ++i) {
if ( m_sigma(i) > pinvtoler )
m_sigma_inv(i)=1.0/m_sigma(i);
else m_sigma_inv(i)=0;
}
pinvmat = (m_matV*m_sigma_inv.asDiagonal()*m_matU.transpose());
}
int main()
{
MatrixXf A(3,2);
A<<1,2,3,4,5,6;
pinv(A);
cout << "pinv =" << endl << A << endl;
return 0;
}
如果我尝试编译它,我会得到错误:
tut_eigen/pinv.cpp: In function ‘void pinv(Eigen::MatrixXf&)’: tut_eigen/pinv.cpp:18:14: error: ‘m_isInitialized’ was not declared in this scope tut_eigen/pinv.cpp:18:58: error: ‘ei_assert’ was not declared in this scope tut_eigen/pinv.cpp:20:4: error: ‘SingularValuesType’ was not declared in this scope tut_eigen/pinv.cpp:20:23: error: expected ‘;’ before ‘m_sigma_inv’ tut_eigen/pinv.cpp:21:22: error: ‘m_workMatrix’ was not declared in this scope tut_eigen/pinv.cpp:22:19: error: ‘m_sigma’ was not declared in this scope tut_eigen/pinv.cpp:23:19: error: ‘m_sigma_inv’ was not declared in this scope tut_eigen/pinv.cpp:24:22: error: ‘m_sigma_inv’ was not declared in this scope tut_eigen/pinv.cpp:26:15: error: ‘m_matV’ was not declared in this scope tut_eigen/pinv.cpp:26:22: error: ‘m_sigma_inv’ was not declared in this scope tut_eigen/pinv.cpp:26:47: error: ‘m_matU’ was not declared in this scope
为什么?它们未在SVD文件中声明?
答案 0 :(得分:2)
我怀疑你谈论这个"tutorial"这不是一个教程,而是一个常见问题解答,假设你已经了解了一下这个库(如果你链接到你的信息来源,BTW会很有帮助)
这说明您可以从外部将pinv()
方法添加到SVD
“。我假设它们意味着您可以从SVD
派生并在派生类中提供pinv()
方法。只是在某处键入函数并不会给编译器提供必要的上下文来确定引用名称的位置。