我是 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 文件中声明吗?
最佳答案
我怀疑你在谈论这个"tutorial"这与其说是一个教程,不如说是一个常见问题解答,假设您已经对这个库有所了解(顺便说一句,如果您链接到您的信息源,这将会很有帮助)。
这说明您可以“从外部”将 pinv()
方法添加到 SVD
中。我认为它们的意思是您可以从 SVD 派生并在派生类中提供 pinv() 方法。仅在某处键入函数并不能为编译器提供必要的上下文来确定引用名称的位置。
关于c++ - 编译错误:. ..未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13218306/