Reputation: 87
I am working on a vector which is the final output of my code. What I am finding is that the vector size is different before and after I pass it to a function even though I am not adding any elements to it. I am passing the vector by reference. The function in question is NM_sim and I am not able debug why this is happening. Thanks for your time and help! I am tracking the size of the vector before and after passing it to the function NM_sim. After calling NM_sim the size of the vector changes. Here is part of my code:
state_type is described as std::vector
random_select(gene_ind, n_ka_temp, n_kd_temp, kavec_pert, kdvec_pert, kaval_pert, kdval_pert);
state_type param_pert;
param_pert.push_back(param[0]);
param_pert.push_back(param[1]);
param_pert.push_back(param[2]);
param_pert.insert(param_pert.end(),kaval_pert.begin(),kaval_pert.end());
param_pert.insert(param_pert.end(),kdval_pert.begin(),kdval_pert.end());
transform(param_pert.begin(),param_pert.end(),param_pert.begin(),powof10());
cout << "########## Value of param size is: " << param.size() << " ################" << endl;
MC_sim ( x_d, t_d, mean_xd, fex_nm, jex_nm, gene_ind, n_ka_temp, n_kd_temp, error_pert, param_pert);
for (int i = 0; i < param.size(); i++)cout << "########## Value of param from MC is: " << param[i] << " ################" << endl;
cout << "########## Value of param size is: " << param.size() << " ################" << endl;
cout << "The optimized value of error from MC calculation is: " << error_pert << endl;
NM_sim( x_d, t_d, mean_xd, fex_nm, jex_nm, gene_ind, n_ka_temp, n_kd_temp, error_pert, param_pert);
cout << "The optimized value of error from NM calculation is: " << error_pert << endl;
Inside NM_sim:
void NM_sim( const state_type &x_d, const state_type &t_d, const state_type &mean_xd, myFex_single &fex_nm, myJex_single &jex_nm, const int &gene_ind, const int nka, const int nkd, double &error_ode, state_type ¶m)
{
const int param_size = 3 + nka + nkd;
cout << "########## Value of error from MC is: " << error_ode << " ################" << endl;
cout << "########## Value of param size is: " << param.size() << " ################" << endl;
for (int i = 0; i < param.size(); i++)cout << "########## Value of param from MC is: " << param[i] << " ################" << endl;
....
}
The output I get is:
########## Value of param from MC is: 0.789519 ################
########## Value of param from MC is: -0.47315 ################
########## Value of param from MC is: -0.693194 ################
########## Value of param from MC is: 0.368322 ################
########## Value of param from MC is: 0.298118 ################
########## Value of param from MC is: 0.883191 ################
########## Value of param size is: 6 ################
The optimized value of error from MC calculation is: 0.000329494
########## Value of error from MC is: 0.000329494 ################
########## Value of param size is: 13 ################
########## Value of param from MC is: 0.789519 ################
########## Value of param from MC is: -0.47315 ################
########## Value of param from MC is: -0.693194 ################
########## Value of param from MC is: 0.368322 ################
########## Value of param from MC is: 0.298118 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0.883191 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0 ################
The vector size goes from 6 after MC_sim to 13 after I pass it to NM_sim. Any thoughts or comments on how to fix it are appreciated! Thanks!
Upvotes: 1
Views: 1147
Reputation: 495
STEP ONE: Eliminate all the code except for passing back and forth. Do you still have the problem? If not, then the problem is in your function code. If problem still exists, then at least you know it's the passing that is causing your problem.
Upvotes: 0
Reputation: 7249
Your comparing sizes of param_pert
and param
. These two are not the same vectors.
cout << "..." << param.size() << "..." << endl;
MC_sim ( x_d, t_d, mean_xd, fex_nm, jex_nm, gene_ind, n_ka_temp, n_kd_temp, error_pert, param_pert)
try:
cout << "########## Value of param size is: " << param_pert.size() << " ################" << endl;
Upvotes: 1