-1

I am using Spectra C++ library, which is built on top of the Eigen C++ library.

I will need to construct a correct SymGEigsSolver object based on the given the SelectionRule and GeigsMode parameters in a method. The definition of SymGEigsSolver is as follows:

template<typename Scalar, int SelectionRule, typename OpType, typename BOpType, int GEigsMode>
class Spectra::SymGEigsSolver< Scalar, SelectionRule, OpType, BOpType, GEigsMode >

However when I tried to compile the below function:

typedef SparseMatrix<double, Eigen::RowMajor> SpMat;
typedef Spectra::SparseSymMatProd<double, Eigen::Upper, Eigen::RowMajor> SpSymMatProd;
typedef Spectra::SparseCholesky<double, Eigen::Upper> SpCholesky;
typedef Spectra::SparseRegularInverse<double, Eigen::Upper, Eigen::RowMajor> SpRegularInverse;


template < typename Scalar,
    int SelectionRule,
    typename OpType,
    typename BOpType,
    int GEigsMode >
    SymGEigsSolver<Scalar, SelectionRule, OpType, BOpType, GEigsMode>& CreateSolverEngine
    (const SpMat& A, const SpMat&  B, int selectRule, int gMode,  int nMode, int ncv)
{
    SpSymMatProd op(A);

    if (gMode == Spectra::GEIGS_CHOLESKY)
    {
        SpCholesky  Bop(B);
        SymGEigsSolver<Scalar, SelectionRule, SpSymMatProd, SpCholesky, GEigsMode>
            ges1(op, Bop, nMode, ncv);
        return ges1;
    }
    if (gMode == Spectra::GEIGS_REGULAR_INVERSE)
    {
        SpRegularInverse Bop2(B);
        SymGEigsSolver<Scalar, SelectionRule, SpSymMatProd, SpRegularInverse, GEigsMode>
            ges(op, Bop2, nMode, ncv);
        return ges;
    }

    throw std::out_of_range("out of range for "+gMode);


}

I got compilation errors such as:

Error   C2664   'Spectra::SymGEigsSolver<Scalar,SelectionRule,OpType,BOpType,GEigsMode>::SymGEigsSolver(OpType *,BOpType *,int,int)'
: cannot convert parameter 1
from 'SpSymMatProd' to 'SpSymMatProd *' 

And

C2440   'return' : cannot convert from
'Spectra::SymGEigsSolver<Scalar,SelectionRule,OpType,BOpType,GEigsMode>'
to
'Spectra::SymGEigsSolver<Scalar,SelectionRule,OpType,BOpType,GEigsMode>&'   

And

C2664   Spectra::SymGEigsSolver<Scalar,SelectionRule,OpType,BOpType,GEigsMode>::SymGEigsSolver(OpType *,BOpType *,int,int)'
: cannot convert parameter 1 from 'SpSymMatProd' to 'SpSymMatProd *'
Mat
  • 202,337
  • 40
  • 393
  • 406
Graviton
  • 81,782
  • 146
  • 424
  • 602
  • The first and third error messages are pretty readable. The second one isn't, but for that you might need to make a [mcve]. – nwp Mar 13 '18 at 08:39
  • 2
    You are trying to return by reference a local variable, which will no longer exist after the function finishes. You are also passing as argument an object itself, where a pointer is expected as a function parameter. – Daniel Langr Mar 13 '18 at 08:45
  • @DanielLangr, from what I can tell there is this copy elision that will allow me to return a local variable. Anyway I've figured out the solution – Graviton Mar 13 '18 at 08:58
  • @Graviton Copy elision is involved only if you return _by value_, not _by reference_. – Daniel Langr Mar 13 '18 at 09:14

1 Answers1

0

I figure out the answer by myself-- I mistype & at the method name. This is the correct solution:

typedef SparseMatrix<double, Eigen::RowMajor> SpMat;
typedef Spectra::SparseSymMatProd<double, Eigen::Upper, Eigen::RowMajor> SpSymMatProd;
typedef Spectra::SparseCholesky<double, Eigen::Upper> SpCholesky;
typedef Spectra::SparseRegularInverse<double, Eigen::Upper, Eigen::RowMajor> SpRegularInverse;


template < typename Scalar,
    int SelectionRule,
    typename OpType,
    typename BOpType,
    int GEigsMode >
    SymGEigsSolver<Scalar, SelectionRule, OpType, BOpType, GEigsMode> CreateSolverEngine
    (const SpMat& A, const SpMat&  B, int selectRule, int gMode,  int nMode, int ncv)
{
    SpSymMatProd op(A);

    if (gMode == Spectra::GEIGS_CHOLESKY)
    {
        SpCholesky  Bop(B);
        SymGEigsSolver<Scalar, SelectionRule, SpSymMatProd, SpCholesky, GEigsMode>
            ges1(op, Bop, nMode, ncv);
        return ges1;
    }
    if (gMode == Spectra::GEIGS_REGULAR_INVERSE)
    {
        SpRegularInverse Bop2(B);
        SymGEigsSolver<Scalar, SelectionRule, SpSymMatProd, SpRegularInverse, GEigsMode>
            ges(op, Bop2, nMode, ncv);
        return ges;
    }

    throw std::out_of_range("out of range for "+gMode);


}
Graviton
  • 81,782
  • 146
  • 424
  • 602
  • 1
    I don't have Spectra installed, but I find it surprising that this compiles. How does your change solve the C2664-issue? (Wouldn't that require writing `&op`, etc -- which would give you a pointer to a local variable, however ...) Also, the return types of both `if`-branches are different. Maybe Spectra does some implicit type conversion at all these cases. – chtz Mar 13 '18 at 11:37