1

Firstly, I would like to mention that I am a complete beginner when it comes to coding, let alone C++, so bear with me, as I need complete guidance. My task is to implement the Lanczos algorithm for the case of a 1-D anharmonic oscillator in C++, with reference to the paper linked Analytical Lanczos method.

The paper offers a step by step guide for the implementation of the algorithm: Step by step guide here

with the initial trial function being: Psi_1 = (1 + x^2) * (exp(-x^2 - 1/4 * x^4).

The paper also contains code in MATHEMATICA for this particular case. Mathematica code

and thus, here is my attempt, which is greatly unfinished, however, I wanted to ensure I was going along the correct path with regards to the programming logic. There are still plentiful errors etc. (Also excuse the lack of fundamentals here, I am only a beginner. Thank you very much.)

int main() {

    //Grid parameters.
    const int Rmin = 1, Rmax = 31, nx = 300;//Grid length and stepsize.

    double dx = (Rmax- Rmin) / nx; //Delta x.

    double a, b;

    std::vector<double> x, psi_1;

    for (int j = 1; j < 64; ++j) { //Corresponds to each succesive Lanczos Vector.

    
        for (int i = Rmin; i < nx + 1; i++) { //Defining the Hamiltonian on the grid. 

            x[i] = (nx / 2) + i;

            psi_1[i] = (1 + pow(x[i] * dx, 2)) * exp(pow(-x[i] * dx, 2) - (1 / 4 * pow(x[i] * dx, 4 )) //Trial wavefunction.

            H[i] = ((PSI[j][i + 1] - 2 * PSI[j][i] + PSI[j][i - 1]) / pow(dx, 2)) + PSI[j][i] * 1/2 * pow(x[i] * dx, 2) + PSI[j][i] * 2 * pow(x[i] * dx, 4) + PSI[j][i] * 1/2 * pow(x[i], 6); //Hamiltonian. ****   

            //First Lanczos step.

            PSI[1][i] = psi_1[i]
        }

    //Normalisation of the wavefunction (b).

    double b[j] = 0.0; 

    for (int i = Rmin; i < nx + 1; i++) { 

        PSI[1][i] = psi_1[i];

        b[j] += abs(pow(PSI[j][i], 2));
    }

    b[j] = b[j] * dx;

    for (int i = Rmin; i < nx + 1; i++) {

        PSI[j] = PSI[j] / sqrt(b[j]);
    }
    //Expectation values (a). Main diagonal of the Hamiltonian matrix.

    double a[j] = 0.0;

    for (int i = Rmin; i < nx + 1; i++) {

        a[j] += PSI[j] * H[i] * PSI[j] * dx
    }
    
    //Recursive expression.

    PSI[j] = H[i] * PSI[j-1] - PSI[j-1] * a[j-1] - PSI[j-2] * b[j-1]

    //Lanczos Matrix.

    LanczosMatrix[R][C] = 
    for (int R = 1; R < 64; R++) {
        row[R] = 
    }
}  

I have yet to finish the code, but some experienced guidance would be greatly appreciated! (also, the code has to be cleaned up greatly, but this was an attempt to get the general idea down first.)

463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126
Cia
  • 11
  • 1
  • Please focus your question on one specific issue. "There are still plentiful errors etc." what are those errors? If you need help with fixing a compiler error you should include the compiler error message in the question – 463035818_is_not_a_number May 06 '21 at 10:53
  • tip: when you need a comment to explain the meaning of a variable you should reconsider your naming. Eg `double dx = (Rmax- Rmin) / nx; //Delta x.` looks like it wants to be `double Delta_x = (Rmax -Rmin)/nx;`, notice how now the comment is not needed – 463035818_is_not_a_number May 06 '21 at 10:55
  • similar for blocks of code. `//Normalisation of the wavefunction (b). ....` could be just one line `normalize_wavefunction( ...);`. Though thats all about style. What the actual issue is, is unclear – 463035818_is_not_a_number May 06 '21 at 10:57
  • 1
    I am bit confused, what is the question? – Alessandro Teruzzi May 06 '21 at 11:03

0 Answers0