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.
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.