Initialize a NRVec or NRMat


eyc
05-11-2007, 05:29 PM
Hi. I am a beginner in NR. I want to initialize a NRVec in the way I used to initialize a double array, is this possible?

To be clear, I used to initialize a double array in the following way:

double Q[5]={3.2, 8.2, 5.5, 2.1, 3.0}

I tried to initialize a NRVec in the following way but failed:

Vec_I_DP Q({3.2, 8.2, 5.5, 2.1, 3.0},5)

Instead of first initialize a double array then initialize NRVec to that array, what can I do?

Kevin Dolan
05-12-2007, 08:46 AM
With the basic NRVec class, there really isn't anything else you can do.

But note that the only disadvantage to just intializing the vector with an array which you have initialized statically, is that you have to copy the data. And this is inevitable anyway, because the array inside the vector is dynamically allocated. It therefore must be initialized dynamically.

The only way around it would be to modify the NRVec class so that it can reference an array allocated by other means. It could then reference a static array too. Then you could avoid the copying. Such a thing is pretty easy to implement. Just add a flag to the class' data which stores whether the vector owns its data or not, so that the destructor can check the flag and only deallocate the data if it owns it. Then add constructors and/or functions to make the vector reference other arrays.

For something like this, it probably isn't worth the trouble, though.


Kevin

eyc
05-13-2007, 01:34 AM
Thank you very much. However, would you please explain in detail about the second point you mentioned?

"Such a thing is pretty easy to implement. Just add a flag to the class' data which stores whether the vector owns its data or not, so that the destructor can check the flag and only deallocate the data if it owns it. Then add constructors and/or functions to make the vector reference other arrays."

I am trying to use NR to do 2-dim interpolation. I have a very large table, so I hope there is as few copying as possible. :(

Kevin Dolan
05-13-2007, 04:38 PM
If you like, you can try my VecMat software, available here:

http://kdolan1973.mine.nu/vecmat/software.htm

It implements vector and matrix classes which are compatible with NR, and which have this feature, as well as many others. You can also refer to the code to see how to do it yourself. As I said before, that particular feature can easily be implemented by making minor modifications to the NRVec and NRMat classes provided in the NR software.

Basically you need to add a flag to the data part of the class that says whether it owns the data or not. The constructors which allocate data for the class would set this flag to true. The constructors which make the class reference an already existing array, would set it to false. You would need to add the latter constructors. They would set the pointer equal to the pointer to the already existing array, rather than allocating their own data. Finally, the destructor would check the value of the flag, and only deallocate the data if the flag is true.

If you need any further details, feel free to send me an email.

Kevin

davekw7x
04-17-2008, 02:44 PM
If you like, you can try my VecMat software, available here:...
Page is apparently no longer available.

But for people with questions similar to the Original Poster (and who don't need a separate library for other purposes):


#include <nrtypes.h>

void printVecDP(const Vec_DP & v);
void printMatDP(const Mat_DP & m);

int main()
{
double v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
int siz = sizeof(v)/sizeof(v[0]);
Vec_I_DP myVecDP(v, siz);

// Define in row-major order
double m[3*2] = {1, 2, 3, 4, 5, 6};
Mat_I_DP myMatDP(m, 3, 2);

printVecDP(myVecDP);
printMatDP(myMatDP);

return 0;
}

void printVecDP(const Vec_DP & v)
{
cout << "In PrintVecDP: "
<< "Number of elements = " << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << " v[" << i << "] = " << v[i] << endl;
}
cout << endl;
}

void printMatDP(const Mat_DP & m)
{
cout << "In PrintMatDP: "
<< "Number of rows = " << m.nrows()
<< " Number of cols = " << m.ncols()<< endl;
for (int i = 0; i < m.nrows(); i++) {
cout << " ";
for (int j = 0; j < m.ncols(); j++) {
cout << "m[" << i << "][" << j << "] = " << m[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}


Output

In PrintVecDP: Number of elements = 5
v[0] = 3.2
v[1] = 8.2
v[2] = 5.5
v[3] = 2.1
v[4] = 3

In PrintMatDP: Number of rows = 3 Number of cols = 2
m[0][0] = 1 m[0][1] = 2
m[1][0] = 3 m[1][1] = 4
m[2][0] = 5 m[2][1] = 6


Regards,

Dave

kutta
11-26-2008, 05:35 AM
Page is apparently no longer available.

But for people with questions similar to the Original Poster (and who don't need a separate library for other purposes):


#include <nrtypes.h>

void printVecDP(const Vec_DP & v);
void printMatDP(const Mat_DP & m);

int main()
{
double v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
int siz = sizeof(v)/sizeof(v[0]);
Vec_I_DP myVecDP(v, siz);

// Define in row-major order
double m[3*2] = {1, 2, 3, 4, 5, 6};
Mat_I_DP myMatDP(m, 3, 2);

printVecDP(myVecDP);
printMatDP(myMatDP);

return 0;
}

void printVecDP(const Vec_DP & v)
{
cout << "In PrintVecDP: "
<< "Number of elements = " << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << " v[" << i << "] = " << v[i] << endl;
}
cout << endl;
}

void printMatDP(const Mat_DP & m)
{
cout << "In PrintMatDP: "
<< "Number of rows = " << m.nrows()
<< " Number of cols = " << m.ncols()<< endl;
for (int i = 0; i < m.nrows(); i++) {
cout << " ";
for (int j = 0; j < m.ncols(); j++) {
cout << "m[" << i << "][" << j << "] = " << m[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}


Output

In PrintVecDP: Number of elements = 5
v[0] = 3.2
v[1] = 8.2
v[2] = 5.5
v[3] = 2.1
v[4] = 3

In PrintMatDP: Number of rows = 3 Number of cols = 2
m[0][0] = 1 m[0][1] = 2
m[1][0] = 3 m[1][1] = 4
m[2][0] = 5 m[2][1] = 6


Regards,

Dave

Dear Comrade
Though the above codes are useful,but i could not compile successfully for the simple reason my details of nrtypes.h may be outdated from the status of NR3's.
So please get me the details of the nrtypes.h that are valid for NR3 For ur clarity i have given here both nrtypes.h (obsolete)as well as the tried codes of your program to get VectDoub,MatDoub.If any changes (minor only) to be done only pl highlight the same.The application program of urs is neverthless a very useful and universally usable through out the NR wheneverr essentiated.Hence hats off !!!
Thanking You
As
Kutta(C.R.Muthukumar)


PS: this is the one I already requested elsewhere for the universally making the matrix (For Nr3-Vec and Mat)with initialisation fundamentally.Thanks



#ifndef _NR_TYPES_H_
#define _NR_TYPES_H_

#include <complex>
#include <fstream>
#include "nrutil.h"
using namespace std;

typedef double DP;

// Vector Types

typedef const NRVec<bool> Vec_I_BOOL;
typedef NRVec<bool> Vec_BOOL, Vec_O_BOOL, Vec_IO_BOOL;

typedef const NRVec<char> Vec_I_CHR;
typedef NRVec<char> Vec_CHR, Vec_O_CHR, Vec_IO_CHR;

typedef const NRVec<unsigned char> Vec_I_UCHR;
typedef NRVec<unsigned char> Vec_UCHR, Vec_O_UCHR, Vec_IO_UCHR;

typedef const NRVec<int> Vec_I_INT;
typedef NRVec<int> Vec_INT, Vec_O_INT, Vec_IO_INT;

typedef const NRVec<unsigned int> Vec_I_UINT;
typedef NRVec<unsigned int> Vec_UINT, Vec_O_UINT, Vec_IO_UINT;

typedef const NRVec<long> Vec_I_LNG;
typedef NRVec<long> Vec_LNG, Vec_O_LNG, Vec_IO_LNG;

typedef const NRVec<unsigned long> Vec_I_ULNG;
typedef NRVec<unsigned long> Vec_ULNG, Vec_O_ULNG, Vec_IO_ULNG;

typedef const NRVec<float> Vec_I_SP;
typedef NRVec<float> Vec_SP, Vec_O_SP, Vec_IO_SP;

typedef const NRVec<DP> Vec_I_DP;
typedef NRVec<DP> Vec_DP, Vec_O_DP, Vec_IO_DP;

typedef const NRVec<complex<float> > Vec_I_CPLX_SP;
typedef NRVec<complex<float> > Vec_CPLX_SP, Vec_O_CPLX_SP, Vec_IO_CPLX_SP;

typedef const NRVec<complex<DP> > Vec_I_CPLX_DP;
typedef NRVec<complex<DP> > Vec_CPLX_DP, Vec_O_CPLX_DP, Vec_IO_CPLX_DP;

// Matrix Types

typedef const NRMat<bool> Mat_I_BOOL;
typedef NRMat<bool> Mat_BOOL, Mat_O_BOOL, Mat_IO_BOOL;

typedef const NRMat<char> Mat_I_CHR;
typedef NRMat<char> Mat_CHR, Mat_O_CHR, Mat_IO_CHR;

typedef const NRMat<unsigned char> Mat_I_UCHR;
typedef NRMat<unsigned char> Mat_UCHR, Mat_O_UCHR, Mat_IO_UCHR;

typedef const NRMat<int> Mat_I_INT;
typedef NRMat<int> Mat_INT, Mat_O_INT, Mat_IO_INT;

typedef const NRMat<unsigned int> Mat_I_UINT;
typedef NRMat<unsigned int> Mat_UINT, Mat_O_UINT, Mat_IO_UINT;

typedef const NRMat<long> Mat_I_LNG;
typedef NRMat<long> Mat_LNG, Mat_O_LNG, Mat_IO_LNG;

typedef const NRVec<unsigned long> Mat_I_ULNG;
typedef NRMat<unsigned long> Mat_ULNG, Mat_O_ULNG, Mat_IO_ULNG;

typedef const NRMat<float> Mat_I_SP;
typedef NRMat<float> Mat_SP, Mat_O_SP, Mat_IO_SP;

typedef const NRMat<DP> Mat_I_DP;
typedef NRMat<DP> Mat_DP, Mat_O_DP, Mat_IO_DP;

typedef const NRMat<complex<float> > Mat_I_CPLX_SP;
typedef NRMat<complex<float> > Mat_CPLX_SP, Mat_O_CPLX_SP, Mat_IO_CPLX_SP;

typedef const NRMat<complex<DP> > Mat_I_CPLX_DP;
typedef NRMat<complex<DP> > Mat_CPLX_DP, Mat_O_CPLX_DP, Mat_IO_CPLX_DP;

// 3D Matrix Types

typedef const NRMat3d<DP> Mat3D_I_DP;
typedef NRMat3d<DP> Mat3D_DP, Mat3D_O_DP, Mat3D_IO_DP;

// Miscellaneous Types

typedef NRVec<unsigned long *> Vec_ULNG_p;
typedef NRVec<NRMat<DP> *> Vec_Mat_DP_p;
typedef NRVec<fstream *> Vec_FSTREAM_p;

#endif /* _NR_TYPES_H_ */

#include "nrtypes.h"
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <iomanip.h>
#include <fstream>
using namespace std;



int main()
{
double v;
double v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
double m[] = {1, 2, 3, 4, 5, 6};

int siz = sizeof(v)/sizeof(v[0]);
Vec_I_DP myVecDP(v, siz);

// Define in row-major order
double m[3*2] = {1, 2, 3, 4, 5, 6};
Mat_I_DP myMatDP(m, 3, 2);
void printVecDP(const Vec_DP & v);
void printMatDP(const Mat_DP & m);
printVecDP(myVecDP);
printMatDP(myMatDP);

return 0;
}

void printVecDP(const Vec_DP & v)
{
cout << "In PrintVecDP: "
<< "Number of elements = " << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << " v[" << i << "] = " << v[i] << endl;
}
cout << endl;
}

void printMatDP(const Mat_DP & m)
{
cout << "In PrintMatDP: "
<< "Number of rows = " << m.nrows()
<< " Number of cols = " << m.ncols()<< endl;
for (int i = 0; i < m.nrows(); i++) {
cout << " ";
for (int j = 0; j < m.ncols(); j++) {
cout << "m[" << i << "][" << j << "] = " << m[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}

davekw7x
11-26-2008, 04:09 PM
...nrtypes.h may be outdated from the status of NR3's.
This is the "Obsolete Editions" part of the forum and my example is valid for Numerical Recipes C++ version 2.

Version 3 doesn't have a separate "nrtypes.h" file. Among other things, all of the vector and matrix class definitions and implementations are in "nr3.h"


#include "../nr3.h"

void printVecDoub(VecDoub_I & v);
void printMatDoub(MatDoub_I & m);

int main()
{
Doub v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
int siz = sizeof(v)/sizeof(v[0]);
VecDoub myVecDoub(siz, v);

// Define in row-major order
Doub m[3*2] = {1, 2, 3, 4, 5, 6};
MatDoub myMatDoub(3, 2, m);

printVecDoub(myVecDoub);
printMatDoub(myMatDoub);

return 0;
}

void printVecDoub(VecDoub_I & v)
{
cout << "In PrintVecDoub: "
<< "Number of elements = " << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << " v[" << i << "] = " << v[i] << endl;
}
cout << endl;
}

void printMatDoub(MatDoub_I & m)
{
cout << "In PrintMatDoub: "
<< "Number of rows = " << m.nrows()
<< ", Number of cols = " << m.ncols()<< endl;
for (int i = 0; i < m.nrows(); i++) {
cout << " ";
for (int j = 0; j < m.ncols(); j++) {
cout << "m[" << i << "][" << j << "] = " << m[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}


Output:

In PrintVecDoub: Number of elements = 5
v[0] = 3.2
v[1] = 8.2
v[2] = 5.5
v[3] = 2.1
v[4] = 3

In PrintMatDoub: Number of rows = 3, Number of cols = 2
m[0][0] = 1 m[0][1] = 2
m[1][0] = 3 m[1][1] = 4
m[2][0] = 5 m[2][1] = 6


Regards,

Dave