what do I have to do to include recipes in a program?


fluo
02-14-2013, 07:28 AM
Hello,
I would like to try the example program "xcorrel.cpp" which calls the function "correl". I copied the main from the NR directory I have bought and I have included some header files (copied in the same directory of my main).
When I compile with the command g++ I find this errors:

#include <iostream>
#include <iomanip>
#include "nr3.h"
#include "fourier.h"
#include "correl.h"
using namespace std;

int main()
{
const int N=64;
int i,j;
DP cmp;
Vec_DP data1(N),data2(N),ans(N);

for (i=0;i<N;i++) {
if ((i > N/2-N/8-1) && (i < N/2+N/8-1))
data1[i]=1.0;
else
data1[i]=0.0;
data2[i]=data1[i];
}
NR::correl(data1,data2,ans);
// Calculate directly
cout << setw(3) << "n" << setw(15) << "CORREL";
cout << setw(19) << "direct calc." << endl;
cout << fixed << setprecision(6);
for (i=0;i<=16;i++) {
cmp=0.0;
for (j=0;j<N;j++)
cmp += data1[((i+j) % N)]*data2[j];
cout << setw(3) << i << setw(16) << ans[i];
cout << setw(16) << cmp << endl;
}
return 0;
}

esercizio.cpp: In function 'int main()':
esercizio.cpp:12: error: 'DP' was not declared in this scope
esercizio.cpp:12: error: expected `;' before 'cmp'
esercizio.cpp:13: error: 'Vec_DP' was not declared in this scope
esercizio.cpp:13: error: expected `;' before 'data1'
esercizio.cpp:17: error: 'data1' was not declared in this scope
esercizio.cpp:19: error: 'data1' was not declared in this scope
esercizio.cpp:20: error: 'data2' was not declared in this scope
esercizio.cpp:20: error: 'data1' was not declared in this scope
esercizio.cpp:22: error: 'NR' has not been declared
esercizio.cpp:22: error: 'data1' was not declared in this scope
esercizio.cpp:22: error: 'data2' was not declared in this scope
esercizio.cpp:22: error: 'ans' was not declared in this scope
esercizio.cpp:28: error: 'cmp' was not declared in this scope

Thank you for your attention,
Fluo

davekw7x
02-14-2013, 02:56 PM
...I would like to try the example program "xcorrel.cpp" ...

In NR3 it's Doub and VecDoub, not DP and Vec_DP.

Also, you don't use the NR:: namespace any more.

Note that nr3.h includes almost all of the C++ header files that you (probably) need, and it also does the namespace std thing.

Bottom line:


#include "nr3.h"
#include "fourier.h"
#include "correl.h"

int main()
{
const int N = 64;
int i, j;
Doub cmp;
VecDoub data1(N), data2(N), ans(N);

for (i = 0; i < N; i++) {
if ((i > N / 2 - N / 8 - 1) && (i < N / 2 + N / 8 - 1)) {
data1[i] = 1.0;
}
else {
data1[i] = 0.0;
}
data2[i] = data1[i];
}
correl(data1, data2, ans);

// Calculate directly
cout << setw(3) << "n" << setw(15) << "CORREL";
cout << setw(19) << "direct calc." << endl;
cout << fixed << setprecision(6);
for (i = 0; i <= 16; i++) {
cmp = 0.0;
for (j = 0; j < N; j++) {
cmp += data1[((i + j) % N)] * data2[j];
}
cout << setw(3) << i << setw(16) << ans[i];
cout << setw(16) << cmp << endl;
}
return 0;
}


Output:

n CORREL direct calc.
0 15.000000 15.000000
1 14.000000 14.000000
2 13.000000 13.000000
3 12.000000 12.000000
4 11.000000 11.000000
5 10.000000 10.000000
6 9.000000 9.000000
7 8.000000 8.000000
8 7.000000 7.000000
9 6.000000 6.000000
10 5.000000 5.000000
11 4.000000 4.000000
12 3.000000 3.000000
13 2.000000 2.000000
14 1.000000 1.000000
15 0.000000 0.000000
16 -0.000000 0.000000

fluo
02-16-2013, 11:12 AM
thank you!