To use NR in VC++.Net


Takamasa
05-11-2008, 11:00 PM
Hello,
I would try to compile NR in VC++.Net, but is that possible to do that?

Please let me know.
Many thanks,
Takamasa

davekw7x
05-14-2008, 11:19 AM
...is that possible to do that?

Yes.

1. What version of Visual Studio/Visual C++ are you using? 2008? 2005? What?

2. What version of Numerical Recipes are you using? Version 2? Version 3?

3. Do you usually compile and execute from within the Integrated Development Environment or from a command line?

4. What is your level of experience with compiling and using command-line programs with Microsoft compilers (or others)?

Regards,

Dave

Takamasa
05-19-2008, 03:14 AM
> |--------------------------------------------------------------------------|
> | |
> | 1. What version of Visual Studio/Visual C++ are you using? 2008? 2005? |

2008
> | 2. What version of Numerical Recipes are you using? Version 2? Version |
> | 3?

We are about to get 2 or 3. So what you will recommand with
VC++ 2008?
|
> | |
> | 3. Do you usually compile and execute from within the Integrated Development Environment or from a command line?

Yes.

> | |
> | 4. What is your level of experience with compiling and using |
> | command-line programs with Microsoft compilers (or others)? |

I have already experienced for compiled and used command-line programs with Microsoft compilers.

Takamasa

davekw7x
05-19-2008, 10:52 AM
> We are about to get 2 or 3. So what you will recommand with VC++ 2008?

In my personal opinion, there is no contest: NR3. See Footnote. Of course, the only appropriate general answer would be, "It depends." The point made by the authors is that the purpose of the "package" (the text and software) is educational. NR3 has more stuff and later stuff. The use of C++ templates and other "improved" programming techniques makes it a lot more appealing to me personally. (But maybe that's just me.)


I have already experienced for compiled and used command-line programs with Microsoft compilers.

Then here's a way to get started:

Install the nr3 stuff at some convenient location. On my Windows XP system I put it at H:\nr3

With this, all of the source code is at H:\nr3\code

Open a Visual Studio 2008 command prompt Window (from the visual studio->tools directory that was created by your installation).

Navigate to H:\nr3 and create a new subdirectory, say, test

Paste the following into your text editor and save as xtrapzd.cpp.

//
// Test program for NR3 Trapzd object
//
// Works with recent versions of Microsoft Visual C++
// (Not version 6 --- the template stuff is hosed.)
//
// Works with GNU g++ 3.4.x and 4.x.x (maybe older
// stuff, but I haven't tested).
//
// Works with Borland bcc32 version 5.82 (not 5.5.1)
//
// davekw7x
// May, 2008
//
#include "../code/nr3.h"
#include "../code/quadrature.h"
//
// Some compilers define Pi/2 as M_PI_2 in <cmath>
// Some do not. So...
//
#ifndef M_PI_2
# define M_PI_2 1.57079632679489661923
#endif

//
// function to test integration routines
// func(x) = x^2*(x^2-2)*sin(x)
//
Doub func(const Doub & x)
{
return (x*x*x*x-2.0*x*x)*sin(x);
}

//
// Analytic anti-derivative of test function
// (After collecting terms.)
//
Doub fint(const Doub & x)
{
return (4.0*x*x*x - 28.0*x)*sin(x) - (x*x*x*x - 14.0*x*x + 28.0)*cos(x);
}

int main()
{

// Limits of integration
Doub a = 0.0;
Doub b = M_PI_2;

double value;
double actual = fint(b) - fint(a);
double err;

//
// Define a pointer to function to use with the
// Trapzd constructor.
//
// Some people prefer functors. Chacun Ã* son goût!
//
Doub (*fp)(const Doub &) = func;
Trapzd<Doub (*) (const Doub &)> myTrapzd(fp, a, b);

cout << endl << "Integral of the function with 2^(n-1) points"
<< endl << endl
<< setw(6) << "n" << setw(24) << "approx. integral"
<< setw(17) << "absolute error" << endl << endl;


//
// Get values for increasing number of points
//
for (int i = 1; i <= 15; i++)
{
Doub s = myTrapzd.next();
cout << setw(6) << i
<< fixed << setw(20) << s;
err = s - actual;
cout << scientific << setw(20) << err << endl;
}
cout << endl;

//
// Get the best NR3 value for trapezoidal rule integration
//
value = qtrap(fp, a, b);
err = value - actual;
cout << "From qtrap, value = " << fixed << value
<< ", error = " << scientific << err << endl;

//
// Get the best NR3 value for Simpson's rule integration
//
value = qsimp(fp, a, b);
err = value - actual;
cout << "From qsimp, value = " << fixed << value
<< ", error = " << scientific << err << endl << endl;

cout << "Actual value of integral is ";
cout << setprecision(15) << fixed << actual << "..." << endl << endl;

return 0;
}



Execute the following from the command line:

cl xtrapzd.cpp /EHsc


There should be no compiler errors or warnings (at least there weren't for my version of cl.exe).

When I execute test I see

Integral of the function with 2^(n-1) points

n approx. integral absolute error

1 0.905773 1.384932e+000
2 -0.020945 4.582139e-001
3 -0.361461 1.176975e-001
4 -0.449584 2.957508e-002
5 -0.471756 7.402520e-003
6 -0.477308 1.851167e-003
7 -0.478696 4.628250e-004
8 -0.479043 1.157083e-004
9 -0.479130 2.892722e-005
10 -0.479152 7.231812e-006
11 -0.479157 1.807954e-006
12 -0.479158 4.519884e-007
13 -0.479159 1.129971e-007
14 -0.479159 2.824938e-008
15 -0.479159 7.062228e-009

From qtrap, value = -0.479159, error = 3.986034e-012
From qsimp, value = -0.479159, error = 6.902812e-013

Actual value of integral is -0.479158810107194...


Summary: For simple programs (NR3 stuff not included from more than one source file) that is all that is required. Just include the proper nr3 "xxxx.h" files.

I made a shortcut to the "Visual Studio Command Prompt" window and set its properties to start in "H:\nr3\vs9_examples," a directory where I put programs to test the NR3 routines.

For more interesting projects, just set up a Makefile for use with nmake.

Note that for my own use, I usually tweak the interface files (separating declarations from implementations) so that I can create projects a little differently than the way the NR3 folks set them up. It's a relatively minor effort, "fixing" them as I go.

For starters, just do things the "NR3" way.


Regards,

Dave

Footnote on a personal level: I am not a professional research scientist or mathematician. I have no professional connection with the authors or the publishers (of Numerical Recipes or anything else). My opinion is just that: an opinion. Free advice freely given and worth exactly what you want it to be worth.

Truth be told: I rarely create Windows programs; it's almost always command-line stuff. On my Windows platforms I almost always use the cygwin port of GNU compilers rather than Visual Studio. A fair amount of my code gets ported to embedded systems (not the NR3 stuff, however), and in my experience, the GNU compilers are more consistent for different platforms. So, my "opinions" given as "recommendations" to users of Visual Studio may not be worth as much as you pay for them after all.