Reset/delete values of a vector or matrix defined with NR2


Madoro
02-25-2013, 09:53 AM
I'm using the numerical recipes definition of vectors and matrices (Vec_DP and Mat_DP, from version 2) into an MFC Application project in Visual C++ 2008.
I'm playing around with the definitions to see how they work (based on this example http://www.nr.com/forum/archive/index.php/t-1681.html), and I think I understand mostly, but in my program I would need a reset function to "clear" the content of the vectors and matrices used.

I've already tried delete(mat), delete[] (mat) (as shown here http://www.nr.com/forum/showthread.php?t=298&highlight=delete) , free(mat), mat.~NRMat(), but none of the previous worked. My initial solution was to make a loop and equal all the elements to zero, but it should be a more elegant solution, any ideas?

An example of my code:

#include "stdafx.h"
#include <math.h>
#include <cmath>
#include <stdio.h>
#include "nrtypes.h"
#include "nrutil.h"

void TestFunc(Vec_I_DP &data);
void printVecDP(Vec_I_DP & v);
void printMatDP (Mat_I_DP & m);

Mat_DP mat(5,6);
Mat_DP mat2;
Mat_DP (*kktest);

int main()
{
DP v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
Vec_DP vdp(v, 5);
Vec_DP vdp2;

vdp2 = vdp;
cout << " Here's vdp: " << endl;
printVecDP(vdp);
cout << " Here's vdp2:" << endl;
printVecDP(vdp2);

TestFunc(vdp2);
cout << " matrix:" << endl;
printMatDP(mat);
printMatDP(mat2);
cout << " matrix2:" << endl;
printMatDP(*kktest);

/* none of this is working */
//mat.~NRMat();
//~NRMat(mat);
//delete[](mat);
//free(mat);

for (int i=0; i<mat.nrows(); i++){
for (int j=0; j<mat.ncols(); j++){
mat[i][j] = 0;
}
}

cout << " matrix:" << endl;
printMatDP(mat);
printMatDP(mat2);
cout << " matrix2:" << endl;
printMatDP(*kktest);

return 0;
}

void TestFunc(Vec_I_DP &data)
{
int n, m, t=data.size();
Vec_DP rsp = data;
for (n=0; n<t; n++)
rsp[n] = data[n]*10.0;


for (n=0; n<t; n++){
for (m=0; m<6; m++){
mat[n][m] = rsp[n];
}
}
mat2=mat;
kktest = &mat;
}

void printVecDP (Vec_I_DP & v)
{
if (v.size() == 0) {
cout << " In printVecDP: The vector is empty." << endl;
}
else {
for (int i = 0; i < v.size(); i++) {
cout << " v[" << i << "] = " << v[i] << endl;
}
}
cout << endl;
}

void printMatDP (Mat_I_DP & m)
{
if ((m.nrows() == 0) | (m.ncols() ==0)) {
cout << " In printMatDP: The matrix is empty." << endl;
}
else {
for (int i = 0; i < m.nrows(); i++) {
for (int j = 0; j < m.ncols(); j++) {
cout << " m["<<i<<"]"<<"["<<j<<"] = " << m[i][j];
}
cout << endl;
}
}
cout << endl;
}

Thanks in advance.

davekw7x
02-26-2013, 02:28 PM
...a more elegant solution...


A Mat_DP is an NRMat<DP>


There is an overloaded assignment operator for NRMat objects (defined in nrutil_nr.h) that copies a value into all the elements of an NRMat.


So, since mat is a Mat_DP, try the following:


mat = 0.0; // Taa-daa!



Now, this has a more "elegant" appearance in the user's source code, but the same operations are performed in the assigment operator method as in your code: It uses nested loops to store a zero in each element of mat.


Regards,

Dave

Madoro
02-27-2013, 09:25 AM
oh, thanks davekw7x, now I feel silly... :o
I reached that solution yesterday, but I'm still courius about the delete or remove thing, why are not working with the NRMat definitions if they are present in the nrutil_nr.h?

Thank you very much.

davekw7x
02-27-2013, 03:42 PM
... delete or remove thing...

What delete or remove thing?


, why are not working with the NRMat definitions
Where did you see anything about "removing" NRMat objects?

When you declare an NRMat object (for example a Mat_DP) the class constructor allocates storage for the object's data fields. The class destructor de-allocates that storage automatically when that object goes out of scope. The delete [] operator in the destructor works on pointers to memory that were obtained from the operating system in the constructor or in certain other functions that were applied to that particular object.

Speaking in C++ generalities: It is hardly ever a good idea for the application program to call such a destructor explicitly. Why would you design a program that requires such a thing?

I will go so far to say that I can't think of a single good reason for an NR application program to call a Mat_DP destructor explicitly or to use the delete operator explicitly on any of the internal data structures of a Mat_DP object. I can think of about a million reasons not to do these things.

Just look at the source code in nrutil_nr.h. If there is something you don't understand, ask specific questions. Maybe someone can walk you through it.

Bottom line: Since you have the source, you can add (or subtract) functionality by changing the source. There are also ways to use external libraries for manipulating matrices (like multiplying matrices, for example) if you need more than is supplied with the NR distribution.



Regards,

Dave

Madoro
02-28-2013, 05:45 AM
Thank you very much, davekw7x, for patiently answering my newbie questions...:o

The reason I wanted to use delete is because I want to create an executable with visual c++ that can be run several times (some vectors and matrices are created, whose dimensions are inserted numbers by the user) but results from different executions shouldn?t interferr each other.

I was thinking that maybe the "=0.0 reset" was a begginer solution, and a more secure and clean way to reset the content of a vector was deleting the memory. But if you recommend me not to use it, I will continue with the initial idea, which is much simpler for me.

Regards,

Madoro

davekw7x
02-28-2013, 03:18 PM
The reason I wanted to use delete is because I want to create an executable with visual c++ that can be run several times (some vectors and matrices are created, whose dimensions are inserted numbers by the user) but results from different executions should't interfere each other.

Each time you launch a program like yours, it has its own program space and its own data space. There will not be any sharing or interference between the different processes.

That's not a NR thing or even a C++ thing. It's an operating system thing. (Current versions of Windows, Linux or any other operating system you are likely to be using.)


Regards,

Dave