TemplateFunctions-How to initiate.


kutta
04-06-2009, 10:23 PM
Hello NR Comrades,
A function generated from a function template is called a templatefunction.Though the undermentioned program demonstrates the useof templates for implementing the lnsearch algorithm .,but not so here as it still requires some techincal finish to make it executable.
Advance thanks for the person who is able to render the same
Thanking You
As
C.R.Muthukumar(Kutta)
:)


#include "nr3.h"
#include <math.h>
#include <ctype.h>
#include "ran-7.1.cpp"
#define RAN
#define myRan
#define Insrch
using namespace std;

void lnsrch(VecDoub_I , const Doub , VecDoub_I , VecDoub_IO ,VecDoub_O , Doub , const Doub , Bool );
template <class T>
void lnsrch(VecDoub_I &xold, const Doub fold, VecDoub_I &g, VecDoub_IO &p,
VecDoub_O &x, Doub &f, const Doub stpmax, Bool &check, T &func) {
const Doub ALF=1.0e-4, TOLX=numeric_limits<Doub>::epsilon();
Doub a,alam,alam2=0.0,alamin,b,disc,f2=0.0;
Doub rhs1,rhs2,slope=0.0,sum=0.0,temp,test,tmplam;
Int i,n=xold.size();
check=false;
for (i=0;i<n;i++) sum += p[i]*p[i];
sum=sqrt(sum);
if (sum > stpmax)
for (i=0;i<n;i++)
p[i] *= stpmax/sum;
for (i=0;i<n;i++)
slope += g[i]*p[i];
if (slope >= 0.0) throw("Roundoff problem in lnsrch.");
test=0.0;
for (i=0;i<n;i++) {
temp=abs(p[i])/MAX(abs(xold[i]),1.0);
if (temp > test) test=temp;
}
alamin=TOLX/test;
alam=1.0;
for (;;) {
for (i=0;i<n;i++) x[i]=xold[i]+alam*p[i];
f=func(x);
if (alam < alamin) {
for (i=0;i<n;i++) x[i]=xold[i];
check=true;
return;
} else if (f <= fold+ALF*alam*slope) return;
else {
if (alam == 1.0)
tmplam = -slope/(2.0*(f-fold-slope));
else {
rhs1=f-fold-alam*slope;
rhs2=f2-fold-alam2*slope;
a=(rhs1/(alam*alam)-rhs2/(alam2*alam2))/(alam-alam2);
b=(-alam2*rhs1/(alam*alam)+alam*rhs2/(alam2*alam2))/(alam-alam2);
if (a == 0.0) tmplam = -slope/(2.0*b);
else {
disc=b*b-3.0*a*slope;
if (disc < 0.0) tmplam=0.5*alam;
else if (b <= 0.0) tmplam=(-b+sqrt(disc))/(3.0*a);
else tmplam=-slope/(b+sqrt(disc));
}
if (tmplam>0.5*alam)
tmplam=0.5*alam;
}
}
alam2=alam;
f2 = f;
alam=MAX(tmplam,0.1*alam);
}
}

int man()
{

typedef const NRvector<Doub> VecDoub_I;
typedef NRvector<Doub> VecDoub, VecDoub_O, VecDoub_IO;
typedef NRvector<Bool> VecBool, VecBool_O, VecBool_I;

Ran myran(12345678); // For debugging, use the same seed every time
//Ran myran(time(0)); // For exploration, use different seeds
Int n =3;

VecDoub fold(n);
Doub stpmax(n);
Doub f=3.4;
VecDoub_I xold(n);
VecDoub_I g(n);
VecDoub_O x(n);
VecDoub_IO p(n);
"Bool true";
Int i;

for (i = 0; i < n; i++) {
//xold[i] = myran.doub();
// fold[i]=myran.doub();
//g[i] = myran.doub();
// stpmax[i]= myran.doub();
x[i] = myran.doub();
p[i] = myran.doub();
i++;
}
//Int j =1;
//xold[j] ={1.2};
// fold[j]={3.4};
// g[j] = {5.6};
// stpmax[j]={2.4};
Insrch(1.2,3.4,5.6,2.4, x[i],f, p[i],true);

//Insrch(x[i],p[i]);

// while (fold[i]==3; stpmax[i]==3;f==3.4;)
//void lnsrch(VecDoub_I &xold, const Doub fold, VecDoub_I &g, VecDoub_IO &p,
//VecDoub_O &x, Doub &f, const Doub stpmax, Bool &check, T &func) {
//lnsrch(VecDoub_I , const Doub , VecDoub_I , VecDoub_IO ,VecDoub_O , Doub , const Doub , Bool );
//lnsrch(xold,fold,g,p,x,f,stpmax,check);
// cout << endl << "Final ";
return 0;

}

davekw7x
04-07-2009, 01:10 PM
...program demonstrates the useof templates for implementing the lnsearch algorithm
The lnsearch() function is used by several of the minimization routines in NR3. Illustrating its use in a stand-alone program doesn't make much sense to me, but if you want to do it, you will have to supply various input and output arguments and give meaningful values to the inputs.

The text has comments in the code that tell what the function parameters do. The following might be compilable, and even executable, but without meaningful input arguments it doesn't illustrate much:

//
// See nr3.h do find out what happens with "throw"
// with _USENRERRORCLASS_ defined, then
// see where I use "throw" in this program. Try
// uncommenting the following #define to see
// the effect.
//
//#define _USENRERRORCLASS_ 1

#include "../code/nr3.h"
#include "../code/qrdcmp.h"
#include "../code/ludcmp.h"
#include "../code/roots_multidim.h"


// lnsearch requires a function with a VecDoub argument
Doub myfunction(VecDoub_I x)
{
Doub retval = 0;

/* Evaluate the function, using the vector x and set retval */

return retval;
}

int main()
{

Int n = 3;

VecDoub xold(n); /* Vector with values of x that you give lnsearch */
Doub fold; /* Value of function(xold) */
VecDoub gradient(n); /* Gradient vector at xold */
VecDoub pdir(n); /* Direction vector for making a step */
VecDoub xnew(n); /* Output of lnsearch with new values of x */
Doub fnew; /* Value of function(xnew) */
Doub stpmax; /* Maximum step size to keep it from running away */
Bool check; /* Return value that tells you whether it "worked */
//
// Assign a value to stpmax.
//
// stpmax = whatever;
//
// Assign values to the elements of xold.
//
// for i = 0, 1, ..., n-1:
// xold[i] = whatever;
//
// Get the value of the function for xold
fold = myfunction(xold);
//
// Assign values to the elements of gradient (the gradient
// vector of the function at value xold). For your
// function, maybe you know an analytic function, or maybe
// use some numerical approximation for the value of gradient
//
// for i = 0, 1, ..., n-1:
// gradient[i] = whatever;
//
// Assign values to the vector pdir: The direction to go for
// the search.
//
//

#ifdef _USENRERRORCLASS_
try {
lnsrch(xold, fold, gradient, pdir, xnew, fnew, stpmax, check, myfunction);
}

catch(NRerror s) {
cerr << "Problem in main() in " __FILE__ << ": ";
NRcatch(s);
}
#else
try {
lnsrch(xold, fold, gradient, pdir, xnew, fnew, stpmax, check, myfunction);
}
catch(int i) {
cerr << "lnsrch threw exception (" << i << ")" << endl;
}
#endif
return 0;
}


it still requires some techincal finish to make it executable.

It requires more than some "finishing touches."

Notes:
1. All of the standard C++ library headers and the 'using namespace std;' statement are already in "nr3.h" I mean, it isn't "wrong" to include them here, but what's the point? Why not get familiar with what the given files and functions that the NR3 distribution supplies? Since we have no clues as to what is in your file "ran-7.1.cpp" there is no way that someone could apply some "finish" to your program to make it compilable. Why not stick with the NR3 headers that are given in the distribution? (I'm guessing that it would be "ran.h" in this case.)

2. What the heck do you expect the following to do?

#define RAN
#define myRan
#define Insrch


Well, I'll tell you: By making these definitions, the preprocessor replaces these identifiers by nothing (an empty string) wherever they are used the program. I can't see for the life of me why you would want to do that. The compiler certainly won't let you create an object of anything named "myRan," and I cover the "Insrch" in one of the following points. Since you don't use "RAN" anywhere, it doesn't actually hurt anything (rather than maybe creating confusion on the part of a reader), but it certainly doesn't help anything either.

3. The function prototype for lnsearch is not necessary if you include the NR3 header "roots_multidim.h" Furthermore, it is a violation of the copyright to post the implementation code for the function itself.

4. What do you expect to happen with "int man()"? Maybe you meant "int main()" ??? Without a main() function the compiler certainly won't be able to create an executable program.

5. The typedef statements that you have in your program are duplicates of some of the typedefs in nr3.h It is unnecessary and invalid to repeat them.

6. The following declares xold to be a constant vector but doesn't give it a value.

VecDoub_I xold(n);

How can you expect to give it a value at any point in your program, since you declared it to be a constant?

7. What do you expect the following to do:

"Bool true";

This a string literal, but you don't do anything with it, therefore the statement is totally useless.

8. You have defined Insrch to be 'nothing' then you have the following statement:

Insrch(1.2,3.4,5.6,2.4, x[i],f, p[i],true);


The preprocessor replaces "Insrch" with nothing, so the compiler sees

(1.2,3.4,5.6,2.4, x[i],f, p[i],true);

What do you think the compiler will do with this code? Well, assuming that corrections had been made so the compiler could compile the stuff up to this point, I will tell you: It creates code that evaluates the comma-separated expressions left to right. The result is equal to the value of the last item (true). The program doesn't do anything with this value, so the entire expression is totally useless. (Most compilers will optimize away any statements that can have no possible effect on the program's results, so the whole statement probably just goes away.)


Etc.


Regards,

Dave

kutta
04-08-2009, 11:16 AM
The lnsearch() function is used by several of the minimization routines in NR3. Illustrating its use in a stand-alone program doesn't make much sense to me, but if you want to do it, you will have to supply various input and output arguments and give meaningful values to the inputs.

The text has comments in the code that tell what the function parameters do. The following might be compilable, and even executable, but without meaningful input arguments it doesn't illustrate much:

//
// See nr3.h do find out what happens with "throw"
// with _USENRERRORCLASS_ defined, then
// see where I use "throw" in this program. Try
// uncommenting the following #define to see
// the effect.
//
//#define _USENRERRORCLASS_ 1

#include "../code/nr3.h"
#include "../code/qrdcmp.h"
#include "../code/ludcmp.h"
#include "../code/roots_multidim.h"


// lnsearch requires a function with a VecDoub argument
Doub myfunction(VecDoub_I x)
{
Doub retval = 0;

/* Evaluate the function, using the vector x and set retval */

return retval;
}

int main()
{

Int n = 3;

VecDoub xold(n); /* Vector with values of x that you give lnsearch */
Doub fold; /* Value of function(xold) */
VecDoub gradient(n); /* Gradient vector at xold */
VecDoub pdir(n); /* Direction vector for making a step */
VecDoub xnew(n); /* Output of lnsearch with new values of x */
Doub fnew; /* Value of function(xnew) */
Doub stpmax; /* Maximum step size to keep it from running away */
Bool check; /* Return value that tells you whether it "worked */
//
// Assign a value to stpmax.
//
// stpmax = whatever;
//
// Assign values to the elements of xold.
//
// for i = 0, 1, ..., n-1:
// xold[i] = whatever;
//
// Get the value of the function for xold
fold = myfunction(xold);
//
// Assign values to the elements of gradient (the gradient
// vector of the function at value xold). For your
// function, maybe you know an analytic function, or maybe
// use some numerical approximation for the value of gradient
//
// for i = 0, 1, ..., n-1:
// gradient[i] = whatever;
//
// Assign values to the vector pdir: The direction to go for
// the search.
//
//

#ifdef _USENRERRORCLASS_
try {
lnsrch(xold, fold, gradient, pdir, xnew, fnew, stpmax, check, myfunction);
}

catch(NRerror s) {
cerr << "Problem in main() in " __FILE__ << ": ";
NRcatch(s);
}
#else
try {
lnsrch(xold, fold, gradient, pdir, xnew, fnew, stpmax, check, myfunction);
}
catch(int i) {
cerr << "lnsrch threw exception (" << i << ")" << endl;
}
#endif
return 0;
}



It requires more than some "finishing touches."

Notes:
1. All of the standard C++ library headers and the 'using namespace std;' statement are already in "nr3.h" I mean, it isn't "wrong" to include them here, but what's the point? Why not get familiar with what the given files and functions that the NR3 distribution supplies? Since we have no clues as to what is in your file "ran-7.1.cpp" there is no way that someone could apply some "finish" to your program to make it compilable. Why not stick with the NR3 headers that are given in the distribution? (I'm guessing that it would be "ran.h" in this case.)

2. What the heck do you expect the following to do?

#define RAN
#define myRan
#define Insrch


Well, I'll tell you: By making these definitions, the preprocessor replaces these identifiers by nothing (an empty string) wherever they are used the program. I can't see for the life of me why you would want to do that. The compiler certainly won't let you create an object of anything named "myRan," and I cover the "Insrch" in one of the following points. Since you don't use "RAN" anywhere, it doesn't actually hurt anything (rather than maybe creating confusion on the part of a reader), but it certainly doesn't help anything either.

3. The function prototype for lnsearch is not necessary if you include the NR3 header "roots_multidim.h" Furthermore, it is a violation of the copyright to post the implementation code for the function itself.

4. What do you expect to happen with "int man()"? Maybe you meant "int main()" ??? Without a main() function the compiler certainly won't be able to create an executable program.

5. The typedef statements that you have in your program are duplicates of some of the typedefs in nr3.h It is unnecessary and invalid to repeat them.

6. The following declares xold to be a constant vector but doesn't give it a value.

VecDoub_I xold(n);

How can you expect to give it a value at any point in your program, since you declared it to be a constant?

7. What do you expect the following to do:

"Bool true";

This a string literal, but you don't do anything with it, therefore the statement is totally useless.

8. You have defined Insrch to be 'nothing' then you have the following statement:

Insrch(1.2,3.4,5.6,2.4, x[i],f, p[i],true);


The preprocessor replaces "Insrch" with nothing, so the compiler sees

(1.2,3.4,5.6,2.4, x[i],f, p[i],true);

What do you think the compiler will do with this code? Well, assuming that corrections had been made so the compiler could compile the stuff up to this point, I will tell you: It creates code that evaluates the comma-separated expressions left to right. The result is equal to the value of the last item (true). The program doesn't do anything with this value, so the entire expression is totally useless. (Most compilers will optimize away any statements that can have no possible effect on the program's results, so the whole statement probably just goes away.)


Etc.


Regards,

Dave

Well,many thanks.Your logical objections were really amazing but contextually doesn't help either of us owing to my simple request as it does not find a solu .

Pl have glimpse at the follwing codes for an application of Template function with multiple Parametrs.
All i wanted was a simple way of initialisation for the coded program to get some output, be it an algorithm or even simulated application under Template Functionsusage.When u see this coded program and its clear cut output u would agree that it hardly matters whether u get the output for an application or for any defined template function as above.Your program is able clear the snags but does not give any valid output as the one shown below.
Please make it a result fetching program....
Please try now to rise up to the occation on a comparitative basis between the two programs.
Thanking You
As
Kutta(C.R.Muthukumar)



//An application of TemplateFunction
//Function with Two generic Types
#include <iostream>
#include <string>
using namespace std;
template<class T1 ,class T2>
void display(T1 x,T2 y)
{
cout << x << " " << y << " " << "\n";
}
int main(){
display(1999,"EBG");
display(12.34,1234);
return 0;
}


/**The out of the above program :
{
1999 EBG
12.34 1234
}*/

:)

davekw7x
04-08-2009, 12:26 PM
...doesn't help either of us owing to my simple request as it does not find a solu .I'm sorry if my response didn't meet your needs. Your "simple request" was to give a fix for your program. I tried to emphasize that the program in your original post had so many invalid and illegal things that I don't see a simple fix. I gave an example of a program using the templated function of your example. My program can be compiled and executed, but gives no genuinely useful results since the function is not given any meaningful inputs.

I mentioned that the particular function of your example is used by some of the minimization routines in the NR distribution. Defining a particular set of input values for that particular function that give some expected output is your job, not mine. In other words, I don't have any vision of an application where I personally would call lnsearch() directly from my program. If you see a need for such things, then tell us what inputs you want to use and tell us the nature of the output that you would expect to see, given the description in the NR textbook.

...Please make it a result fetching program...

In order to get meaningful help in a programming project, I respectfully suggest the following general steps:

1. Write a program specification. The program specification describes inputs and outputs and describes the processing that the program must do to transform input data to outputs. If the program uses a function that is supplied in the NR distribution, then the specification for the function itself will be given in the text (including comments in the text that don't actually appear in the C++ source code files). If you are going to use a function, you must have (at least) a main function that calls it.

2. Give a set of input data that you want to test (or give a way of generating the test data). Tell us what you expect the program to produce as output. If you don't know what inputs to use, then how do you know what output to expect? How could someone help you see it? I mean, the process begins with a problem of your choosing. What is the problem you are trying to solve? If you give the input/output information, maybe someone can help you learn how to craft a program that tries satisfy your requirements.

Regards,

Dave

kutta
04-12-2009, 03:53 AM
I'm sorry if my response didn't meet your needs. Your "simple request" was to give a fix for your program. I tried to emphasize that the program in your original post had so many invalid and illegal things that I don't see a simple fix. I gave an example of a program using the templated function of your example. My program can be compiled and executed, but gives no genuinely useful results since the function is not given any meaningful inputs.

I mentioned that the particular function of your example is used by some of the minimization routines in the NR distribution. Defining a particular set of input values for that particular function that give some expected output is your job, not mine. In other words, I don't have any vision of an application where I personally would call lnsearch() directly from my program. If you see a need for such things, then tell us what inputs you want to use and tell us the nature of the output that you would expect to see, given the description in the NR textbook.



In order to get meaningful help in a programming project, I respectfully suggest the following general steps:

1. Write a program specification. The program specification describes inputs and outputs and describes the processing that the program must do to transform input data to outputs. If the program uses a function that is supplied in the NR distribution, then the specification for the function itself will be given in the text (including comments in the text that don't actually appear in the C++ source code files). If you are going to use a function, you must have (at least) a main function that calls it.

2. Give a set of input data that you want to test (or give a way of generating the test data). Tell us what you expect the program to produce as output. If you don't know what inputs to use, then how do you know what output to expect? How could someone help you see it? I mean, the process begins with a problem of your choosing. What is the problem you are trying to solve? If you give the input/output information, maybe someone can help you learn how to craft a program that tries satisfy your requirements.

Regards,

Dave

Many thanks and being rather perplexed ,an higher order program is enclosed which needs an expedition inorder to make it an executable one,inlieu of its initialisation technique.
Also please let the scalar variable be changed to the required to suit the same.(How this is normally done).
Thanking You
AS
C.R.Muthukumar(Kutta)

#include "nr3.h"
//#include "Ran"
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>

using namespace std;


void quadct(const Doub x, const Doub y, VecDoub_I &xx, VecDoub_I &yy, Doub &fa,Doub &fb, Doub &fc, Doub &fd)
{
Int k,na,nb,nc,nd,nn=xx.size();
Doub ff;
na=nb=nc=nd=0;
for (k=0;k<nn;k++) {
if (yy[k] == y && xx[k] == x) continue;
if (yy[k] > y)
xx[k] > x ? ++na : ++nb;
else
xx[k] > x ? ++nd : ++nc;
}
ff=1.0/nn;
fa=ff*na;
fb=ff*nb;
fc=ff*nc;
fd=ff*nd;
}
int main()
{

Ran myran(12345678);
Int n =6;
VecDoub_I xx(n),yy(n);

for (int i = 0; i < n; i++) {

"xx[i] = myran.doub()";
"yy[i] = myran.doub()";

int i;
cout << "xx[i]:" << " " << xx[i] << endl;
cout << "yy[i]:" << " " << yy[i] << endl;

"printf( VecDoub_I,VecDoub_I)";
cout << endl << "Initial ";

cout << endl << "Final ";
" printf( VecDoub_I , VecDoub_I )";
}

return 0;
}
struct Ran {
Ullong u,v,w;
Ran(Ullong j) : v(4101842887655102017LL), w(1) {
u = j ^ v; int64();
v = u; int64();
w = v; int64();
}
inline Ullong int64() {
u = u * 2862933555777941757LL + 7046029254386353087LL;
v ^= v >> 17; v ^= v << 31; v ^= v >> 8;
w = 4294957665U*(w & 0xffffffff) + (w >> 32);
Ullong x = u ^ (u << 21); x ^= x >> 35; x ^= x << 4;
return (x + v) ^ w;
}
inline Doub doub() { return 5.42101086242752217E-20 * int64(); }
inline Uint int32() { return (Uint)int64(); }
};
struct Ranq1 {
Ullong v;
Ranq1(Ullong j) : v(4101842887655102017LL) {
v ^= j;
v = int64();
}
inline Ullong int64() {
v ^= v >> 21; v ^= v << 35; v ^= v >> 4;
return v * 2685821657736338717LL;
}
inline Doub doub() { return 5.42101086242752217E-20 * int64(); }
inline Uint int32() { return (Uint)int64(); }
};
struct Ranq2 {
Ullong v,w;
Ranq2(Ullong j) : v(4101842887655102017LL), w(1) {
v ^= j;
w = int64();
v = int64();
}
inline Ullong int64() {
v ^= v >> 17; v ^= v << 31; v ^= v >> 8;
w = 4294957665U*(w & 0xffffffff) + (w >> 32);
return v ^ w;
}
inline Doub doub() { return 5.42101086242752217E-20 * int64(); }
inline Uint int32() { return (Uint)int64(); }
};
struct Ranhash {
inline Ullong int64(Ullong u) {
Ullong v = u * 3935559000370003845LL + 2691343689449507681LL;
v ^= v >> 21; v ^= v << 37; v ^= v >> 4;
v *= 4768777513237032717LL;
v ^= v << 20; v ^= v >> 41; v ^= v << 5;
return v;
}
inline Uint int32(Ullong u)
{ return (Uint)(int64(u) & 0xffffffff) ; }
inline Doub doub(Ullong u)
{ return 5.42101086242752217E-20 * int64(u); }
};
struct Ranbyte {
Int s[256],i,j,ss;
Uint v;
Ranbyte(Int u) {
v = 2244614371U ^ u;
for (i=0; i<256; i++) {s[i] = i;}
for (j=0, i=0; i<256; i++) {
ss = s[i];
j = (j + ss + (v >> 24)) & 0xff;
s[i] = s[j]; s[j] = ss;
v = (v << 24) | (v >> 8);
}
i = j = 0;
for (Int k=0; k<256; k++) int8();
}
inline unsigned char int8() {
i = (i+1) & 0xff;
ss = s[i];
j = (j+ss) & 0xff;
s[i] = s[j]; s[j] = ss;
return (unsigned char)(s[(s[i]+s[j]) & 0xff]);
}
Uint int32() {
v = 0;
for (int k=0; k<4; k++) {
i = (i+1) & 0xff;
ss = s[i];
j = (j+ss) & 0xff;
s[i] = s[j]; s[j] = ss;
v = (v << 8) | s[(s[i]+s[j]) & 0xff];
}
return v;
}
Doub doub() {
return 2.32830643653869629E-10 * ( int32() +
2.32830643653869629E-10 * int32() );
}
};
struct Ranfib {
Doub dtab[55], dd;
Int inext, inextp;
Ranfib(Ullong j) : inext(0), inextp(31) {
Ranq1 init(j);
for (int k=0; k<55; k++) dtab[k] = init.doub();
}
Doub doub() {
if (++inext == 55) inext = 0;
if (++inextp == 55) inextp = 0;
dd = dtab[inext] - dtab[inextp];
if (dd < 0) dd += 1.0;
return (dtab[inext] = dd);
}
inline unsigned long int32()
{ return (unsigned long)(doub() * 4294967295.0);}
};
struct Ranlim32 {
Uint u,v,w1,w2;
Ranlim32(Uint j) : v(2244614371U), w1(521288629U), w2(362436069U) {
u = j ^ v; int32();
v = u; int32();
}
inline Uint int32() {
u = u * 2891336453U + 1640531513U;
v ^= v >> 13; v ^= v << 17; v ^= v >> 5;
w1 = 33378 * (w1 & 0xffff) + (w1 >> 16);
w2 = 57225 * (w2 & 0xffff) + (w2 >> 16);
Uint x = u ^ (u << 9); x ^= x >> 17; x ^= x << 6;
Uint y = w1 ^ (w1 << 17); y ^= y >> 15; y ^= y << 5;
return (x + v) ^ (y + w2);
}
inline Doub doub() { return 2.32830643653869629E-10 * int32(); }
inline Doub truedoub() {
return 2.32830643653869629E-10 * ( int32() +
2.32830643653869629E-10 * int32() );
}
};



:)

davekw7x
04-12-2009, 09:56 AM
...to make it an executable
The only thing that I can see that your main() function is trying to do is to assign values to arrays xx and yy. Your program tries to print each value as it is assigned.

Here is a complete, correct, executable program (using NR headers) that assigns values to the vectors in one loop and prints them out in another.

I formatted the output according to my personal taste.


#include "../code/nr3.h"
#include "../code/ran.h"

int main()
{

Ran myran(12345678);
Int n = 6;
// By using VecDoub_I, you had declared xx and yy as const
VecDoub xx(n), yy(n);

//
// For this loop, we assign "random" values to xx and yy
//
for (Int i = 0; i < n; i++) {
// int i; // This would declare a brand new uninitialized variable(!)
xx[i] = myran.doub();
yy[i] = myran.doub();
}

cout << scientific; // I like scientific notation for floating point stuff

cout << "Initially:" << endl;
for (Int i = 0; i < n; i++) {
cout << " xx[" << i << "]:" << " " << xx[i]
<< ", yy[" << i << "]:" << " " << yy[i] << endl;
}

return 0;
}



Output after compiling with GNU g++ version 4.1.2:

Initially:
xx[0]: 6.223736e-01, yy[0]: 1.154017e-01
xx[1]: 7.750629e-01, yy[1]: 4.490045e-01
xx[2]: 4.455370e-01, yy[2]: 7.149345e-01
xx[3]: 5.976951e-01, yy[3]: 5.332354e-01
xx[4]: 3.758452e-02, yy[4]: 9.165017e-02
xx[5]: 8.664851e-01, yy[5]: 2.328559e-01



Also please let the scalar variable be changed to the required to suit the same... Other than the loop counters, the only scalar variable that I see is n. Make it anything you want it to be. If you want to get the value from the user, you could do something like the following:


.
.
.
Int n;
cout << "Enter a positive integer: ";
cin >> n;
if (!cin || (n <= 0)) {
cerr << "Invalid entry." << endl;
exit(EXIT_FAILURE);
}
VecDoub xx(n), yy(n);
.
.
.


Output from a few runs:

Enter a positive integer: 4
Initially:
xx[0]: 6.223736e-01, yy[0]: 1.154017e-01
xx[1]: 7.750629e-01, yy[1]: 4.490045e-01
xx[2]: 4.455370e-01, yy[2]: 7.149345e-01
xx[3]: 5.976951e-01, yy[3]: 5.332354e-01



Enter a positive integer: n
Invalid entry.



Enter a positive integer: 0
Invalid entry.



Regards,

Dave

kutta
04-14-2009, 10:37 AM
The only thing that I can see that your main() function is trying to do is to assign values to arrays xx and yy. Your program tries to print each value as it is assigned.

Here is a complete, correct, executable program (using NR headers) that assigns values to the vectors in one loop and prints them out in another.

I formatted the output according to my personal taste.


#include "../code/nr3.h"
#include "../code/ran.h"

int main()
{

Ran myran(12345678);
Int n = 6;
// By using VecDoub_I, you had declared xx and yy as const
VecDoub xx(n), yy(n);

//
// For this loop, we assign "random" values to xx and yy
//
for (Int i = 0; i < n; i++) {
// int i; // This would declare a brand new uninitialized variable(!)
xx[i] = myran.doub();
yy[i] = myran.doub();
}

cout << scientific; // I like scientific notation for floating point stuff

cout << "Initially:" << endl;
for (Int i = 0; i < n; i++) {
cout << " xx[" << i << "]:" << " " << xx[i]
<< ", yy[" << i << "]:" << " " << yy[i] << endl;
}

return 0;
}



Output after compiling with GNU g++ version 4.1.2:

Initially:
xx[0]: 6.223736e-01, yy[0]: 1.154017e-01
xx[1]: 7.750629e-01, yy[1]: 4.490045e-01
xx[2]: 4.455370e-01, yy[2]: 7.149345e-01
xx[3]: 5.976951e-01, yy[3]: 5.332354e-01
xx[4]: 3.758452e-02, yy[4]: 9.165017e-02
xx[5]: 8.664851e-01, yy[5]: 2.328559e-01


Other than the loop counters, the only scalar variable that I see is n. Make it anything you want it to be. If you want to get the value from the user, you could do something like the following:


.
.
.
Int n;
cout << "Enter a positive integer: ";
cin >> n;
if (!cin || (n <= 0)) {
cerr << "Invalid entry." << endl;
exit(EXIT_FAILURE);
}
VecDoub xx(n), yy(n);
.
.
.


Output from a few runs:

Enter a positive integer: 4
Initially:
xx[0]: 6.223736e-01, yy[0]: 1.154017e-01
xx[1]: 7.750629e-01, yy[1]: 4.490045e-01
xx[2]: 4.455370e-01, yy[2]: 7.149345e-01
xx[3]: 5.976951e-01, yy[3]: 5.332354e-01



Enter a positive integer: n
Invalid entry.



Enter a positive integer: 0
Invalid entry.



Regards,

Dave

Hello NR-Comrade
Thanks oncemore for the elegant method .On the same .please let me comprehend the scalar variables coding to initilialse & enable execution through this.
Thanking You
As
C.R.Muthukumar(Kutta)


#include "nr3.h"
#include "ran.h"
#include <iostream>

using namespace std;



class KsDist{

float x1,y1;

public:

// void CNT(void {count =0;});
void quadct(const Doub x, const Doub y, VecDoub_I &xx, VecDoub_I &yy, Doub &fa,Doub &fb, Doub &fc, Doub &fd);
void ks2d1s(VecDoub_I &x1, VecDoub_I &y1, void quadvl(const Doub, const Doub,Doub &, Doub &, Doub &, Doub &), Doub &d1, Doub &prob);
void ks2d2s(VecDoub_I &x1, VecDoub_I &y1, VecDoub_I &x2, VecDoub_I &y2, Doub &d,Doub &prob);

//friend void quadct(const Doub x, const Doub y, VecDoub_I &xx, VecDoub_I &yy, Doub &fa,Doub &fb, Doub &fc, Doub &fd);
//friend void ks2d1s(VecDoub_I &x1, VecDoub_I &y1, void quadvl(const Doub, const Doub,Doub &, Doub &, Doub &, Doub &), Doub &d1, Doub &prob);
//friend void ks2d2s(VecDoub_I &x1, VecDoub_I &y1, VecDoub_I &x2, VecDoub_I &y2, Doub &d,Doub &prob);


};

void KsDist::quadct(const Doub x, const Doub y, VecDoub_I &xx, VecDoub_I &yy, Doub &fa,Doub &fb, Doub &fc, Doub &fd)
{
Int k,na,nb,nc,nd,nn=xx.size();
Doub ff;
na=nb=nc=nd=0;
for (k=0;k<nn;k++) {
if (yy[k] == y && xx[k] == x) continue;
if (yy[k] > y)
xx[k] > x ? ++na : ++nb;
else
xx[k] > x ? ++nd : ++nc;
}
ff=1.0/nn;
fa=ff*na;
fb=ff*nb;
fc=ff*nc;
fd=ff*nd;
}
void KsDist::ks2d1s(VecDoub_I &x1, VecDoub_I &y1, void quadvl(const Doub, const Doub,Doub &, Doub &, Doub &, Doub &), Doub &d1, Doub &prob)
{
Int j,n1=x1.size();
Doub dum,dumm,fa,fb,fc,fd,ga,gb,gc,gd,r1,rr,sqen;
// KSdist ks;
d1=0.0;
for (j=0;j<n1;j++) {
quadct(x1[j],y1[j],x1,y1,fa,fb,fc,fd);
quadvl(x1[j],y1[j],ga,gb,gc,gd);
if (fa > ga) fa += 1.0/n1;
if (fb > gb) fb += 1.0/n1;
if (fc > gc) fc += 1.0/n1;
if (fd > gd) fd += 1.0/n1;
d1=MAX(d1,abs(fa-ga));
d1=MAX(d1,abs(fb-gb));
d1=MAX(d1,abs(fc-gc));
d1=MAX(d1,abs(fd-gd));
}
pearsn(x1,y1,r1,dum,dumm);
sqen=sqrt(Doub(n1));
rr=sqrt(1.0-r1*r1);
//prob=ks.qks(d1*sqen/(1.0+rr*(0.25-0.75/sqen)));
}
void KsDist::ks2d2s(VecDoub_I &x1, VecDoub_I &y1, VecDoub_I &x2, VecDoub_I &y2, Doub &d,Doub &prob)
{
Int j,n1=x1.size(),n2=x2.size();
Doub d1,d2,dum,dumm,fa,fb,fc,fd,ga,gb,gc,gd,r1,r2,rr,sq en;
//KSdist; ks;
d1=0.0;
for (j=0;j<n1;j++) {
quadct(x1[j],y1[j],x1,y1,fa,fb,fc,fd);
quadct(x1[j],y1[j],x2,y2,ga,gb,gc,gd);
if (fa > ga) fa += 1.0/n1;
if (fb > gb) fb += 1.0/n1;
if (fc > gc) fc += 1.0/n1;
if (fd > gd) fd += 1.0/n1;
d1=MAX(d1,abs(fa-ga));
d1=MAX(d1,abs(fb-gb));
d1=MAX(d1,abs(fc-gc));
d1=MAX(d1,abs(fd-gd));
}
d2=0.0;
for (j=0;j<n2;j++) {
quadct(x2[j],y2[j],x1,y1,fa,fb,fc,fd);
quadct(x2[j],y2[j],x2,y2,ga,gb,gc,gd);
if (ga > fa) ga += 1.0/n1;
if (gb > fb) gb += 1.0/n1;
if (gc > fc) gc += 1.0/n1;
if (gd > fd) gd += 1.0/n1;
d2=MAX(d2,abs(fa-ga));
d2=MAX(d2,abs(fb-gb));
d2=MAX(d2,abs(fc-gc));
d2=MAX(d2,abs(fd-gd));
}
d=0.5*(d1+d2);
sqen=sqrt(n1*n2/Doub(n1+n2));
pearsn(x1,y1,r1,dum,dumm);
pearsn(x2,y2,r2,dum,dumm);
rr=sqrt(1.0-0.5*(r1*r1+r2*r2));
// prob=ks.qks(d*sqen/(1.0+rr*(0.25-0.75/sqen)));
}
int main()
{

Ran myran(12345678);
int n;
cout << "Enter a positive integer: ";
cin >> n;
if (!cin || (n <= 0)) {
cerr << "Invalid entry." << endl;
exit(EXIT_FAILURE);
}
Doub d1,prob;
VecDoub x(n),y(n);
VecDoub xx(n), yy(n);
VecDoub_I x1(n),y1(n);
VecDoub x2(n),y2(n);

for (Int i = 0; i < n; i++) {
// int i; // This would declare a brand new uninitialized variable(!)
// x[i]= myran.doub();
// y[i]= myran.doub();
xx[i] = myran.doub();
yy[i] = myran.doub();
// x1[i]= myran.doub();
// y1[i]= myran.doub();
x2[i]= myran.doub();
y2[i]= myran.doub();

}

cout << scientific; // I like scientific notation for floating point stuff

cout << "Initially:" << endl;
for (Int i = 0; i < n; i++) {
// cout << " x[" << i << "]:" << " " << x[i]
// << ", y[" << i << "]:" << " " << y[i] << endl;
cout << " xx[" << i << "]:" << " " << xx[i]
<< ", yy[" << i << "]:" << " " << yy[i] << endl;
// cout << " x1[" << i << "]:" << " " << x1[i]
// << ", y1[" << i << "]:" << " " << y1[i] << endl;
//cout << " x2[" << i << "]:" << " " << x2[i]
// << ", y2[" << i << "]:" << " " << y2[i] << endl;

};
KsDist P;
// P.CNT();
cout << "\nobject P" << "\n";
P.ks2d1s( VecDoub,VecDoub);void quadvl( Doub,Doub,Doub,Doub,Doub,Doub),Doub,Doub);
cout << "\n";
return 0;
}

:)

davekw7x
04-14-2009, 01:27 PM
...please let me comprehend...

1. By posting code that was copied directly from the NR distribution, you have violated the copyright (again).

2. Since you have chosen to modify the code, it's not NR code. Furthermore, since you still haven't indicated what, exactly, you are trying to do, I don't have any motivation (or the stamina) to try to figure out what you want to accomplish. I simply can't figure out what I could do to help.

I mean, just post the main() or other functions that you are using to test the NR functions, and show the NR #include files that you used, but don't post the NR code itself. If you can get help in understanding how to use the NR distribution functions, then maybe you can try modifying them for whatever reasons you decide.

Finally, I hate to repeat myself, but the following is important. Really.

In order to get meaningful help in a programming project, I respectfully suggest the following general steps:

Write a program specification. The program specification describes inputs and outputs and describes the processing that the program must do to transform input data to outputs. If the program uses a function that is supplied in the NR distribution, then the specification for the function itself will be given in the text (including comments in the text that don't actually appear in the C++ source code files). If you are going to use a function, you must have (at least) a main function that calls it.


Give a set of input data that you want to test (or give a way of generating the test data). Tell us what you expect the program to produce as output. If you don't know what inputs to use, then how do you know what output to expect? How could someone help you see it? I mean, the process begins with a problem of your choosing. What is the problem you are trying to solve? If you give the input/output information, maybe someone can help you learn how to craft a program that tries satisfy your requirements.

I wouldn't want to hurt your feelings, and I would like to help, but, really, what can you expect to accomplish by throwing us a bunch of code that won't compile and asking for someone to make it work (and to, somehow, satisfy your unspecified requirements)?

I really, really don't know what you expect.

At least that's the way that I see it.

Regards,

Dave

davekw7x
04-15-2009, 01:11 PM
...
I really, really don't know what you expect.

In an attempt to keep from being totally negative, I hereby supply a test program for the function in your last post. I had really rather that you supply some narrative about what you want to test (maybe someone can help; maybe not), but here goes . . .

The scheme for generating the test points was taken from the program xks12d1s.cpp from the NR release 2 C++ distribution. (I hope I am not violating any copyright or licensing provisions.)

The idea is that the program can generate set of point from a jointly uniform distribution or can make it non-uniform by letting the user give a value for a "non-uniformity factor" from zero to one. If the "non-uniformity factor is zero, the coordinates are obtained from a uniform random number generator. If the value of the "non-uniformity" factor is one, the coordinates are obtained by squaring the outputs from the random number generator. In-between values can be entered to see the effects of "almost uniform" or less-and-less "almost uniform" distributions.

In each case, the Kolmogorov-Smirnov test uses the NR-Supplied function quadvl() to allow us to try to guess whether the points came from a particular jointly uniform distribution, as described in the text.




// Example for testing ks2d1s
//
// davekw7x
//
#include "../code/nr3.h"
#include "../code/gamma.h"
#include "../code/sort.h"
#include "../code/moment.h"
#include "../code/incgammabeta.h"
#include "../code/erf.h"
#include "../code/stattests.h"
#include "../code/quadvl.h"
#include "../code/ksdist.h"
#include "../code/kstests_2d.h"
#include "../code/ran.h"

int main()
{
Int num_points, num_trials;
Doub factor;

Int seed = 12345678;
//Int seed = time(0);
Ran ran(seed);

//
// Terminate the program if the user enters something non-numeric
// or if the value is less than three
//
cout << "Enter the number of points (Must be a greater than 2): ";
while ((cin >> num_points) && (num_points > 2)) {

// An "empty" loop that consumes the rest of the input line
while (cin.get() != '\n')
;

//
// Low values of the factor makes the point distribution
// be jointly uniform. As factor gets larger, the
// distribution gets "less and less" jointly uniform
//

// Terminate the loop and exit the program if the user
// makes a non-numeric entry, otherwise make sure an
// appropriate value is entered, then proceed.
//
cout << "Enter the non-linearity factor (0.0 to 1.0) : ";
while ((cin >> factor) && ((factor < 0) || (factor > 1.0))) {

// An "empty" loop that consumes the rest of the input line
while (cin.get() != '\n')
;

if (factor < 0.0) {
cout << "Factor can not be less than 0." << endl;
}
if (factor > 1.0) {
cout << "Factor can not be greater than 1." << endl;
}
cout << endl;
cout << "Enter the non-linearity factor (0.0 to 1.0) : ";
}
if (!cin) {
break;
}

// An "empty" loop that consumes the rest of the input line
while (cin.get() != '\n')
;

//
// Terminate the loop and exit the program if the user
// makes a non-numeric entry, otherwise make sure an
// appropriate value is entered, then proceed.
//
cout << "Enter the number of trials : ";
while ((cin >> num_trials) && (num_trials <= 0)) {

// An "empty" loop that consumes the rest of the input line
while (cin.get() != '\n')
;

cout << "Must be a positive integer."
<< endl << endl;
cout << "Enter the number of trials : ";
}
if (!cin) {
break;
}

cout << endl << endl
<< "The null hypothesis is that the distribution of the"
<< endl
<< "points (x,y) is jointly uniform for x in [-1,1] and"
<< endl
<< "and y in [-1,1]"
<< endl << endl
<< "If the probability number in the table is small, then"
<< endl
<< "we reject the null hypothesis, and we believe that the"
<< endl
<< "distribution is not jointly uniform in the given region."
<< endl << endl;

cout << " Probability that D is greater"
<< endl
<< "K-S D value than the K-S D value"
<< endl
<< "---------------------------------------------------"
<< endl;

VecDoub x(num_points), y(num_points);
for (Int i = 0; i < num_trials; i++) {
for (Int j = 0; j < num_points; j++) {

Doub rand_temp = ran.doub();

rand_temp = rand_temp * ((1.0 - factor) + rand_temp * factor);
x[j] = 2.0 * rand_temp - 1.0;

rand_temp = ran.doub();
rand_temp = rand_temp * ((1.0 - factor) + rand_temp * factor);
y[j] = 2.0 * rand_temp - 1.0;

}

Doub d, prob;
ks2d1s(x, y, quadvl, d, prob);
cout << fixed << setprecision(6) << setw(9) << d
<< scientific << setprecision(2) << setw(24) << prob
<< endl;
}
cout << endl << endl
<< "================================================== ======="
<< endl << endl;

cout << "Enter the number of points (Must be a greater than 2): ";
}
if (cin) {
cout << "Goodbye for now." << endl;
}
else {
cout << "Invalid entry. Program is ending." << endl;
}

return 0;
}


A run:

Enter the number of points (Must be a greater than 2): 100
Enter the non-linearity factor (0.0 to 1.0) : 0
Enter the number of trials : 10


The null hypothesis is that the distribution of the
points (x,y) is jointly uniform for x in [-1,1] and
and y in [-1,1]

If the probability number in the table is small, then
we reject the null hypothesis, and we believe that the
distribution is not jointly uniform in the given region.

Probability that D is greater
K-S D value than the K-S D value
---------------------------------------------------
0.114277 2.98e-01
0.142727 1.04e-01
0.083035 6.98e-01
0.071415 8.54e-01
0.097621 4.95e-01
0.064879 9.20e-01
0.101042 4.50e-01
0.139054 1.20e-01
0.110488 3.35e-01
0.150400 7.44e-02


================================================== =======

Enter the number of points (Must be a greater than 2): 100
Enter the non-linearity factor (0.0 to 1.0) : 1
Enter the number of trials : 10


The null hypothesis is that the distribution of the
points (x,y) is jointly uniform for x in [-1,1] and
and y in [-1,1]

If the probability number in the table is small, then
we reject the null hypothesis, and we believe that the
distribution is not jointly uniform in the given region.

Probability that D is greater
K-S D value than the K-S D value
---------------------------------------------------
0.385235 8.61e-10
0.399130 1.70e-10
0.376531 2.22e-09
0.414181 3.23e-11
0.430946 4.02e-12
0.330657 2.64e-07
0.329387 2.95e-07
0.395225 2.79e-10
0.319550 7.41e-07
0.326801 3.76e-07


================================================== =======

Enter the number of points (Must be a greater than 2): 0
Goodbye for now.



Regards,

Dave