2017-04-11 167 views
0

我确实有一个实现最小化算法的函数。我并没有包括所有的瓦尔,只是矩阵来说明类型:当分配稀疏矩阵导致稀疏矩阵时出现“no operator found”

typedef Eigen::SparseMatrix<double> SpMat; 
typedef Eigen::VectorXd Vec; 

int lm_solver(void (*f_dz)(Vec* x_, int m, Vec* dz_, SpMat* W_), 
       void (*f_H)(Vec* x_, SpMat* jac_,int n_, int m_), 
       Vec* x, int nx, int mm, int nnz, 
       double tol=1e-9, int max_iter = 100){ 

    SpMat A(mm, nx); 
    SpMat H1(mm, nx); 
    SpMat H2(mm, nx); 
    SpMat H(mm, nx); 
    SpMat W(mm, mm); 
    Vec rhs(nx); 
    Vec dz(nx); 
    Vec dx(nx); 
    Vec a(1); 
    Vec b(1); 

    double f, f_prev, lbmda, rho, nu, tau; 
    bool updateH, converged; 
    int iter_; 

    // reserve matrices memory 
    H.reserve(nnz); 
    W.reserve(mm); 


    while (!converged && iter_ < max_iter){ 

     // get the system matrices 
     if (updateH){ // if the Jacobian computation is not locked... 
      f_dz(x, mm, &dz, &W); // Residual increment (z-h(x)) vector creation or update: fill dz and W 
      f_H(x, &H, nx, mm);  // Jacobian matrix creation or update: fill H 

      // Start forming the auxiliary matrices of A 
      H1 = H.transpose() * W; 
      H2 = H1 * H; 
     } 

     // set the first value of lmbda 
     if (iter_ == 1) 
      lbmda = tau * H2.diagonal().maxCoeff(); 

     // form the system matrix A = H^t·W·H + lambda·I 
     A = H2 + lbmda * Idn; 

     // form the right hand side: H^t·W·dz 
     rhs = H1 * dz; 

     // Solve the increment: dx = solve(A, rhs); 
     solver.compute(A); 
     dx = solver.solve(rhs); 

     // calculate the objective function: Least squares function 
     a = 0.5 * dz * W * dz; //vector x matrix x vector -> vector of 1 element 
     f = a.coeffRef(0); 

     // calculate the gain ratio 
     b = 0.5 * dx * (lbmda * dx - rhs); //vector x matrix x vector -> vector of 1 element 
     rho = (f_prev - f)/b.coeffRef(0); 


    } 

    return 0; 
} 

的过程执行以下操作:

  • 声明稀疏矩阵的矩阵(​​3210)
  • 储备矩阵存储
  • 调用外部函数来填充H,dzW
  • 做矩阵乘法和将结果存储到中间矩阵中

也是稀疏的。

此功能在.h文件的唯一功能。当我独自一人编译静态库被编译成静态库.lib

,它完美地编译。

然而,当我使用的库项目从另一个项目中,我得到以下错误:

error: C2679: binary '=' : no operator found which takes a right-hand operand of type 'const Eigen::CwiseBinaryOp' (or there is no acceptable conversion) 
\eigen\src/Core/Matrix.h(206): could be 'Eigen::Matrix<_Scalar,_Rows,_Cols> &Eigen::Matrix<_Scalar,_Rows,_Cols>::operator =(const Eigen::Matrix<_Scalar,_Rows,_Cols> &)' 
with 
[ 
    _Scalar=double, 
    _Rows=-1, 
    _Cols=1 
] 
d:\proyectos\proyectos_i+d\ingrid\eigen\eigen_3_3_3\eigen\src/Core/Matrix.h(281): or  'Eigen::Matrix<_Scalar,_Rows,_Cols> &Eigen::Matrix<_Scalar,_Rows,_Cols>::operator =(Eigen::Matrix<_Scalar,_Rows,_Cols> &&)' 
with 
[ 
    _Scalar=double, 
    _Rows=-1, 
    _Cols=1 
] 
while trying to match the argument list '(Vec, const Eigen::CwiseBinaryOp)' 

此错误标志线:

H1 = H.transpose() * W; 

H2 = H1 * H; 

rhs = H1 * dz; 

b = 0.5 * dx * (lbmda * dx - rhs); 

a = 0.5 * dz * W * dz; 

我从这个理解,我不能存储在一个新的稀疏矩阵中稀疏矩阵乘法的结果。我不知道这个解决方案。

(我使用3.3.3本征)

回答

0

我看不出有什么线究竟会导致你的错误,但它看起来很像是通过计算ab造成的。不能将col-向量与另一个col向量相乘而不转置,例如

b = 0.5 * dx.transpose() * (lbmda * dx - rhs); 

然而,这其实是一个点的产品,所以你应该只写

double b = 0.5 * dx.dot(lbmda * dx - rhs); 
+0

你是对的,但编译器在此之前抛出了另一个错误。 –

+0

@SantiPeñate-Vera请提供一个MCVE。即,提供应该编译的内容(除了你看到的错误外),但删除所有不必要的东西。在你的代码片段中没有'Idn',也没有声明'solver'。 – chtz

+0

我试图在“清洁”项目上重现问题 –

0

的问题是,我写的所有功能,在.H。 通过将该函数的主体放在.cpp中,一切正常。

这个.h和.cpp的双字节是让我对C++最感兴趣的东西。

无论如何,供将来参考。