xtlqi.cpp problem

Hello. I'm trying to write a code, which would first generate a random matrix and then find its eigenfunctions and eigenvalues using routines tlqi and tred2. However, I'm fairy new to programming and I got stuck in the beginning. I first wanted to try the example from "Numerical recipes", xtlqi.cpp (here's a link https://github.com/burakbayramli/kod/blob/master/books/NumericalRecipes2/examples/xtqli.cpp ), but it doesn't compile. It returns the following errors
1
2
3
4
...\AppData\Local\Temp\ccP0pNqr.o	xtqli.cpp:(.text+0xec): undefined reference to `NR::tred2(NRMat<double>&, NRVec<double>&, NRVec<double>&)'
...\AppData\Local\Temp\ccP0pNqr.o	xtqli.cpp:(.text+0x10c): undefined reference to `NR::tqli(NRVec<double>&, NRVec<double>&, NRMat<double>&)'
...\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe	...\AppData\Local\Temp\ccP0pNqr.o: bad reloc address 0x0 in section `.pdata$_ZStanSt13_Ios_FmtflagsS_'
...\collect2.exe	[Error] ld returned 1 exit status 


Can anyone tell me what am I doing wrong?
it doesn't compile

It does compile. It doesn't link.

Each source file is compiled into object file. The object files are linked together to create the binary.

Your linker complains that code in the object file created from xtqli.cpp calls functions (tred2 and tqli) that are not implemented in any of the included object files.

You have to include the necessary parts to the build. (IDE's usually have "project" configuration.)
Thanks, I figured it out already though. My programming knowledge is really weak :/
I have another problem now. The problem I'm working on is to study eigenvalues of a perturbed harmonic oscillator using a few different methods. One of the things I have to do is to show how a certain eigenvalue converges when the matrix size increases. Here's my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <iomanip>
#include <fstream> 
#include <time.h>
#include <cmath>
#include "nr.h"
#include "tred2.cpp"
#include "tqli.cpp"
#include "pythag.cpp"
#include "nrutil_nr.h"
#include <array>

using namespace std;


int main(void)
{
ofstream output("konvergenca_LV2.txt"); 
streambuf* saved_buffer=cout.rdbuf(); 
cout.rdbuf(output.rdbuf());
cout.setf(ios::scientific,ios::floatfield); 
cout << scientific << setprecision(8);

double lambda=0.1;

const DP TINY=1.0e-6;

     for(lambda=0.1; lambda<1.0; lambda=lambda+0.1)
     {
	 int NN=0;
	 
     
    
     	cout <<"LV2, lambda = " + std::to_string(lambda) << endl;
    	
    	for (NN=4; NN<10; NN=NN+1)
    	{
    	int NP=NN;
        DP c_d[NN*NN];
		DP zero[NN*NN];

		for(int z4=0; z4<(NN*NN); z4=z4+1 )
		{c_d[z4]=0.0;
		zero[z4]=0.0;
		}
		int id=0;
        for(int z3=0; z3<(NN*NN); z3=z3+NP+1 )
        {c_d[z3]=0.5+id;
         id=id+1;
		}
        
		////////////////////////////////////////////////////////////////////////////
		
//	
//		 Mat_DP H0(c_d,NN,NN);
//   		 Mat_DP qij(zero,NN,NN);
//         for(int z1=0 ; z1<NN; z1++)
//        {for (int w1=0; w1<NN; w1++){
//
//        	float delta=0;
//        	if(fabs(w1-z1)==1){delta=1.0;}
//        	qij[z1][w1]=0.5*sqrt(z1+w1+1.0)*delta;
//        	
//		}
//		
//    }

    ////////////////////////////////////////////////////////////////////////////////
         Mat_DP H0(c_d,NN,NN);
   		 Mat_DP qij(zero,NN,NN);
 for(int z1=0;z1<NP;z1++)
        {for (int w1=0;w1<NP;w1++){

        	double c1=0;
        	double c2=0;
        	double c3=0;
        	double c4=0;
        	double c5=0;
        	
        	if(w1+4==z1){c1 = 0.25*sqrt((w1+4.0)*(w1+3.0)*(w1+2.0)*(w1+1.0));}
        	if(w1+2==z1){c2 = 0.5*(2.0*w1+3.0)*sqrt((w1+2.0)*(w1+1.0));}
        	if(w1==z1){c3 = 1.5*(pow(w1,2.0)+w1+0.5);}
        	if(w1-2==z1){c4 = w1*sqrt(1.0/(4.0*w1*(w1-1.0)))*(2.0*pow(w1,2.0)-3*w1+1);}
        	if(w1-4==z1){c5 = w1*sqrt(1.0/(16.0*w1*(w1-1.0)*(w1-2.0)*(w1-3.0)))*(1.0*pow(w1,3.0)-6.0*pow(w1,2.0)+11.0*w1-6.0);}
        	
        	qij[z1][w1] = c1+c2+c3+c4+c5;
        	
		}
    }

     /////////////////////////////////////////////////////////
     Mat_DP H(zero,NN,NN);
     for(int i1=0; i1<NN; i1++)
     {for (int j1=0; j1<NN; j1++)
     {H[i1][j1] = (1.0*H0[i1][j1] + 1.0*lambda*qij[i1][j1]); 
     
     	 }
	
     }
     /////////////////////////////////////////////////////////
     
     
//     for (int z4=0; z4<NP; z4++)
//	{for (int z5=0; z5<NP; z5++)
//	{
//	cout << fixed << setprecision(5);
//		cout << H[z4][z5] << "    ";
//		}
//     cout<<" "<<endl;
// }
// cout<<" "<<endl;
// cout<<" "<<endl;

	 Vec_DP d(NP),e(NP),f(NP);
         Mat_DP a(c_d,NP,NP),c(c_d,NP,NP);
	 NR::tred2(a,d,e);
         NR::tqli(d,e,a);
	cout << NP << "      "<< setw(8) <<d[1] << endl;
	 
}
cout<< " "<<endl;
}
return 0;
}


I should be getting different numbers for the chosen eigenvalue for different NN-s. But it always returns the same number and this is obviously not right. I think the matrix H looks correct, but the eigenvalues don't, they should exponentially converge with increasing NN. Did I declare something wrong? This is the first time I'm using something from Numerical recipes and I'm totally lost :(

EDIT: I found an error. I forgot to copy the values of matrix H into matrices a and c. I added
1
2
3
4
for (int z4=0; z4<NP; z4++)
     {for (int h4=0; h4<NP; h4++)
     {a[z4][h4]=H[z4][h4];
     c[z4][h4]=H[z4][h4];

Eigenvalues are now different for every N, but they are all over the place, they don't converge. Any ideas?
Last edited on
Topic archived. No new replies allowed.