0
votes

I'm trying to define a dynamic Matrix in C++ , using Eigen library . First I get the number of rows and columns as an input (nZ) then I define my Matrix in the following class but it doesn't let me to use (nZ) as the number of rows variable ! Do you have any idea what should I do?

GetParams Params ;

class Hamiltonian {

public:
Hamiltonian();
void SetHam0(double,double,int,double)
virtual ~Hamiltonian();
int nZ = Params.Get_nZ() ;

private:
Eigen::MatrixXd U_on = Eigen::MatrixXd.setZero(nZ,1) ;
Eigen::MatrixXd Ham0 = Eigen::MatrixXd.setZero(nZ,nZ) ;
Eigen::MatrixXd mstar = Eigen::MatrixXd.setZero(nZ,nZ) ;
Eigen::MatrixXd U_v = Eigen::MatrixXd.setZero(nZ,1) ;

};

1
nZ valuation and all setZero calls must be done in the constructor.Michael M.

1 Answers

2
votes

Valuate nZ and all matrices in the constructor :

class Hamiltonian {

public:
Hamiltonian();
virtual ~Hamiltonian();

private:
Eigen::MatrixXd U_on;
Eigen::MatrixXd Ham0;
Eigen::MatrixXd mstar;
Eigen::MatrixXd U_v;
int nZ;

};

Hamiltonian::Hamiltonian()
{
  GetParams Params;
  nZ = Params.Get_nZ();

  U_on = Eigen::MatrixXd.setZero(nZ,1) ;
  Ham0 = Eigen::MatrixXd.setZero(nZ,nZ) ;
  mstar = Eigen::MatrixXd.setZero(nZ,nZ) ;
  U_v = Eigen::MatrixXd.setZero(nZ,1) ;
}