NR3 CD : How to use?


smp
12-04-2011, 10:50 PM
Hi,

I have NR3 CD. How to use it? That CD contains one of the folders named as "codes". This folder contains all .h files.

Another thing I don't know C++. But I know C and I use gcc compiler.

So how to use recipes in NR3?

Regards,
smp

davekw7x
12-05-2011, 03:33 PM
HiHi.

...That CD contains one of the folders named as "codes".
Really? Mine has the stuff in a folder called "code".

Anyhow...

...So how to use...
Here's what I did on my Linux workstation (currently using gcc/g++ version 4.1.2):


I created a directory "nr3" under my home directory.
I copied masterdependency.txt from the CD to my nr3 directory. (There may be other files that you want, but this is OK for starters.) You can use masterdependency.txt to find out what other NR3 files must be included for given applications.
I created a directory named "code" under my nr3 directory and copied the contents of nf3/code on the CD to my nr3/code directory.


Now, I create projects under my nr3 directory. For example to test the nr3 function "julday()," in code/calendar.h I created a directory nr3/xjulday and used my favorite text editor to create the following C++ file:


//
// xjulday.cpp
//
// A C++ language program to test julday and caldat in calendar.h
//
// davekw7x
//
#include "../code/nr3.h"
#include "../code/calendar.h"

int main()
{
int month, day, year;
cout << "Enter mm dd yyyy: ";
while (cin >> month >> day >> year) {
int julDay = julday(month, day, year);
cout << "For " << month
<< "/" << day
<< "/" << year
<< ", the Julian Day is " << julDay
<< endl;

int calmonth, calday, calyear;
caldat(julDay, calmonth, calday, calyear);
cout << "For julDay = " << julDay <<", caldat yields "
<< calmonth << "/" << calday << "/" << calyear << endl;

cout << endl << "Enter mm dd yyyy: ";
}
cout << endl << "Goodbye for now." << endl;
return 0;
}


I compiled it with

g++ -Wall -W xjulday.cpp -o xjulday

A run looks like this:

Enter mm dd yyyy: 12 5 2011
For 12/5/2011, the Julian Day is 2455901
For julDay = 2455901, caldat yields 12/5/2011

Enter mm dd yyyy: quit

Goodbye for now.


don't know C++. But I know C
Hmmm...
That is going to be a problem for the vast majority of the nr3 applications. Most of them make use of C++-style classes (they use them as C++-style structs, and they are a lot different from C-style structs). See Footnote.


Regards,

Dave

Footnote:
Functions like julday() and caldat() can be called from a program written in C, since they do not use C++-specific constructs, and C++ compilers cheerfully accept most C language programs.

Here's a test program. The language in main() is standard C, but note (this is important): I gave the file a name ending with ".cpp" not ".c" and I compiled with g++, not gcc:

/*
* testc.cpp <---NOTE: Save it as ".cpp", not ".c"
*
* C Language file to test julday() and caldat() in calendar.h
*
* davekw7x
*/
#include "../code/nr3.h"
#include "../code/calendar.h"

int main()
{
int month, day, year;
int calmonth, calday, calyear;
int julDay;
char inbuf[BUFSIZ];
printf("Enter mm dd yyyy: ");
while (fgets(inbuf, sizeof(inbuf), stdin)) {
if (sscanf(inbuf, "%d %d %d", &month, &day, &year) != 3) {
break;
}
julDay = julday(month, day, year);
printf("For %d/%d/%d, the julday() returns %d\n",
month, day, year, julDay);
caldat(julDay, calmonth, calday, calyear);
printf("For julDay = %d, caldat yields %d/%d/%d\n",
julDay, calmonth, calday, calyear);
printf("\nEnter mm dd yyyy: ");
}
return 0;
}


Compiled with
g++ -Wall -W testc.cpp -o testc

Output is same as before.

Now, you can test the bessjy function in code/nr3.h with something like

/*
xbessjy.cpp

davekw7x
*/
#include "../code/nr3.h"
#include "../code/bessel.h"

/* Driver for routine bessjy */

int main()
{
/*
The Bessjy struct is defined in code/bessel.h
Since it's C++, the struct definition is automatically
a typedef.
*/
Bessjy bessjy;
int n;
int i;

for (n = 1; n <= 3; n++) {
for (i = 0; i < 5; i++ ) {
double x = i+1;
double z = bessjy.jn(n,x);
printf("Bessjy.jn(%d, %.1f) = %+f\n",n, x, z);
}
printf("\n");
}

return 0;
}
The C++-style struct bessjy has "methods" (functions) defined in bessel.h that can be called with the notation that I showed above. This may be a way to "get your feet wet" in mild C++ before taking the full plunge.

But here's the bottom line:

You can use C-style I/O and other standard C library functions, but you can't write a "pure" C language program to use the more "interesting" C++ classes (or C++-style structs).


Post-bottom-line comment:
So, for example, there is no way (no way) to use quadrature functions (qsimp or qtrap) to perform numerical integration without using C++, and it may be hard for a C programmer to divine the calling protocol by inspecting the C++ code in quadrature.h. Unfortunately, no "Examples" book has been published by the NR3 authors to accompany NR3.

Numerous examples have been posted on this forum, by myself and other volunteers, in response to specific requests by people learning C++ (or maybe by people who already know C++ but haven't figured out how to use some of the NR3 class objects and methods).

smp
12-08-2011, 10:16 PM
Hi Dave,

(1)
Hi.
Quote:
Originally Posted by smp
...That CD contains one of the folders named as "codes".
Really? Mine has the stuff in a folder called "code".

My NR3 CD contains these 9 things:


code
index_by_file.htm
index_by_section.htm
legacy
museum
gfx
index_by_ident.htm
index.htm
license.htm


Yes. It is "code" folder and not "codes".

(2)

I copied masterdependency.txt from the CD to my nr3 directory. (There may be other files that you want, but this is OK for starters.) You can use masterdependency.txt to find out what other NR3 files must be included for given applications.

Where is "masterdependency.txt" file in CD?

(3) Which function in NR3 does surface fitting?

(4) Is there any document in NR3 CD which tells which function (routine) does what?


Thanks & Regards,
smp

davekw7x
12-09-2011, 08:02 AM
...Where is "masterdependency.txt" file

Actually I misspoke. It's not on the CD.

You can go here: http://www.nr.com/dependencies/ to see what files must be included to use a particular code file.

Or, you can download the dependencies as a text file so that you don't have to access the web every time you want to create a new project: http://www.nr.com/dependencies/masterdependency.txt.
Is there any document in NR3 CD which tells which function (routine) does what?

Try this:
Open the index.htm file from the CD in your browser. Click on the Browse by book section number and section title link.

Which function in NR3 does...Browse the Table of Contents of the book (or, maybe, the Index of Code Files by Book Section link on the CD index.htm file) to find a topic that seems to be related to your requirements.




Regards,

Dave