Chap 7: sec 7.7: Monte Carlo integration


amirvahid
03-03-2010, 07:51 AM
Dear all,

I tried to run the example in p. 401 of the NR book but it gives some errors. Here is my source code:

#include "C:\Program Files\Numerical Recipes\NR_C302\code\nr3.h"
#include "C:\Program Files\Numerical Recipes\NR_C302\code/mcintegrate.h"
#include "C:\Program Files\Numerical Recipes\NR_C302\code/ran.h"
//
// Driver for routine Mcintegration
// Extracted by Amir Vahid from sec. 7.7
//

int main()
{
VecDoub xlo(3), xhi(3);
xlo[0] = 1.; xhi[0] = 4.;
xlo[1] = -3.; xhi[1] = 4.;
xlo[2] = -1.; xhi[2] = 1.;
MCintegrate mymc(xlo,xhi,torusfuncs,torusregion,NULL,10201);
mymc.step(1000000);
mymc.calcanswers();
return 0;
}

VecDoub torusfuncs(const VecDoub &x) {
//Return the integrands in equation (7.7.5), with . rho=1.
Doub den = 1.;
VecDoub f(4);
f[0] = den;
for (Int i=1;i<4;i++) f[i] = x[i-1]*den;
return f;
}

Bool torusregion(const VecDoub &x) {
//Return the inequality (7.7.3).
return SQR(x[2])+SQR(sqrt(SQR(x[0])+SQR(x[1]))-3.) <= 1.;
}

Here are the listed errors.

1>------ Rebuild All started: Project: McIntegrate, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'McIntegrate', configuration 'Debug|Win32'
1>Compiling...
1>McIntegrate.cpp
1>c:\program files\numerical recipes\nr_c302\code\mcintegrate.h(10) : error C2146: syntax error : missing ';' before identifier 'ran'
1>c:\program files\numerical recipes\nr_c302\code\mcintegrate.h(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\numerical recipes\nr_c302\code\mcintegrate.h(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\numerical recipes\nr_c302\code\mcintegrate.h(24) : error C2614: 'MCintegrate' : illegal member initialization: 'ran' is not a base or member
1>c:\program files\numerical recipes\nr_c302\code\mcintegrate.h(39) : error C2065: 'ran' : undeclared identifier
1>c:\program files\numerical recipes\nr_c302\code\mcintegrate.h(39) : error C2228: left of '.doub' must have class/struct/union
1> type is ''unknown-type''
1>c:\my projects\mcintegrate\mcintegrate\mcintegrate.cpp(2 1) : error C2084: function 'VecDoub torusfuncs(const VecDoub &)' already has a body
1> c:\program files\numerical recipes\nr_c302\code\mcintegrate.h(59) : see previous definition of 'torusfuncs'
1>c:\my projects\mcintegrate\mcintegrate\mcintegrate.cpp(3 0) : error C2084: function 'Bool torusregion(const VecDoub &)' already has a body
1> c:\program files\numerical recipes\nr_c302\code\mcintegrate.h(67) : see previous definition of 'torusregion'
1>Build log was saved at "file://c:\My Projects\McIntegrate\McIntegrate\Debug\BuildLog.ht m"
1>McIntegrate - 8 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


I wonder if you could help me fix this problem. Thanks

MPD78
03-03-2010, 08:44 AM
Have you tried fixing the first bug?

1>c:\program files\numerical recipes\nr_c302\code\mcintegrate.h(10) : error C2146: syntax error : missing ';' before identifier 'ran'


Simply states that you are missing a semicolon.

Fix that simple bug and then try to compile.

Let us know if that works.

Thanks
Matt

davekw7x
03-03-2010, 08:46 AM
...it gives some errors...

1. You must include ran.h before you include mcintegrate.h See the master dependencies list (http://www.nr.com/dependencies/masterdependency.txt)

2. Since the functions torusfuncs() and torusregion() are implemented in mcintegrate.h you should not put them in your main program file.

Regards,

Dave

amirvahid
03-03-2010, 09:03 AM
Have you tried fixing the first bug?



Simply states that you are missing a semicolon.

Fix that simple bug and then try to compile.

Let us know if that works.

Thanks
Matt

Matt, thanks but I think this was not the issue.

Thanks Dave for your help. The code is running now. The above-mentioned function are actually implemented in the "mcintegrate.h" header file. I appreciate it! Here is the working code:

##include "C:\Program Files\Numerical Recipes\NR_C302\code\nr3.h"
#include "C:\Program Files\Numerical Recipes\NR_C302\code/ran.h"
#include "C:\Program Files\Numerical Recipes\NR_C302\code/mcintegrate.h"

//
// Driver for routine Mcintegration
// Extracted by Amir Vahid from Sec. 7.7.
//

int main()
{
VecDoub xlo(3), xhi(3);
xlo[0] = 1.; xhi[0] = 4.;
xlo[1] = -3.; xhi[1] = 4.;
xlo[2] = -1.; xhi[2] = 1.;
MCintegrate mymc(xlo,xhi,torusfuncs,torusregion,NULL,10201);
mymc.step(1000000);
mymc.calcanswers();
for (Int i=1;i<4;i++){
cout << mymc.ff[i] << endl;
}
return 0;
}


}

MPD78
03-03-2010, 09:06 AM
...include ran.h before you include mcintegrate.h

Ahh, yes. I overlooked that. I have done that before. :)

Glad it all works correctly for you now.

Thanks
Matt

amirvahid
03-03-2010, 01:02 PM
Dear All,
The code that I have posted is for a volume in a cube which its equation (or constraints) is known. But suppose that we have a volume with an unknown equation. A general method for calculating of its volume is putting the volume in a cube and assigning meshes to it. This method is not efficient , however. Do you think that there is a way of doing that with Monte Carlo integration? Is it mentioned in Chapter 7? Thanks