我的程序简短,旨在测试我的PCA回归类。
#include <iostream>
#include <vector>
#include "data.generator.h"
#include "reg.class.h"
int main(int argc, char* argv[]) {
std::cout << "Flag A" << std::endl;
basicGenerator bg;
std::cout << "Sample size: " << bg.get_sampleSize() << std::endl;
bg.makeData();
std::cout << "Flag B" << std::endl;
std::vector<std::vector<double> > x;
std::vector<double> y;
bg.getDataForRegression(x,y);
unsigned int imax = y.size();
for (unsigned int i = 0 ; i < imax ; i++) {
std::cout << i << "\t" << y[i] << "\t" << x[i][0] << "\t" << x[i][1] << std::endl;
}
std::cout << "Flag C" << std::endl;
regressionPCA rpca(x,y);
return 0;
}
这是它产生的输出的尾端:
89 0.416356 0.565407 -0.114605
90 -0.258883 -0.585597 -0.183372
91 0.833355 0.914344 0.81171
92 -0.0338814 -0.00264442 -0.118973
93 1.13764 0.41599 1.33175
94 -1.86323 -1.90867 -1.35118
95 0.907604 1.14917 0.621669
96 2.1166 1.06194 1.1703
97 0.159543 0.14446 -0.665135
98 -0.508617 -0.370597 -0.703225
99 2.69086 2.75267 1.40633
Flag C
Ted@Ted-acer-i7w7 ~/New.System/tests
$
如您所见,最后一个输出来自我在堆栈上创建regressionPCA实例之前立即设置的标志。
这是该类的声明。
#ifndef REG_CLASS_H
#define REG_CLASS_H
#include <iosfwd>
#include <vector>
//#include <boost/smart_ptr/shared_array.hpp>
#include <gsl/gsl_linalg.h>
class regressionPCA {
private:
unsigned int nrows, ncols;
// boost::shared_array<double> B, Y, U, V, S;
regressionPCA(void) {}; // makes default construction impossible
public:
regressionPCA(const std::vector<std::vector<double> >&, const std::vector<double>&);
void Reset(const std::vector<std::vector<double> >&, const std::vector<double>&);
inline void setNrows(unsigned int v) { nrows = v;};
inline void setNcols(unsigned int v) { ncols = v;};
inline unsigned int getNrows(void) const { return nrows; };
inline unsigned int getNcols(void) const { return ncols; };
};
#endif
请注意,当我包含boost的shared_array时,程序不会仅仅在没有产生输出的情况下死亡,而是使用核心转储非常不合理地死亡。我不明白为什么。
这是regressionPCA类的实现:
#include "reg.class.h"
#include <iostream>
#include <algorithm>
#include <gsl/gsl_linalg.h>
regressionPCA::regressionPCA(const std::vector<std::vector<double> >&data,
const std::vector<double>& Ydata) {
std::cout << "Flag ALPHA" << std::endl;
Reset(data,Ydata);
}
void regressionPCA::Reset(const std::vector<std::vector<double> >&data,
const std::vector<double>& Ydata) {
unsigned int r(0),c(0),n(0);
std::cout << "r: " << r << "\tc: " << c << "\tn: " << n << std::endl;
c = data.size();
std::cout << "r: " << r << "\tc: " << c << "\tn: " << n << std::endl;
r = data[0].size();
std::cout << "r: " << r << "\tc: " << c << "\tn: " << n << std::endl;
setNrows(r);
setNcols(c);
n = r * c;
std::cout << "r: " << r << "\tc: " << c << "\tn: " << n << std::endl;
double* B = new double[c];
double* Y = new double[r];
double* U = new double[n];
double* V = new double[c*c];
double* S = new double[c];
/* boost::shared_array<double> B(new double[c]),
boost::shared_array<double> B(new double[c]),
Y(new double[r]),
U(new double[n]),
V(new double[c*c]),
S(new double[c]);
B = Btmp;
Y = Ytmp;
U = Utmp;
V = Vtmp;
S = Stmp;*/
double *bptr = U.get();
std::cout << "Flag1" << std::endl;
std::vector<std::vector<double> >::const_iterator it = data.begin(), end = data.end();
while (it != end) {
bptr = std::copy(it->begin(), it->end(),bptr);
++it;
}
std::cout << "Flag2" << std::endl;
bptr = Y.get();
std::copy(Ydata.begin(),Ydata.end(),bptr);
gsl_matrix_view Um = gsl_matrix_view_array(U.get(), getNrows(), getNcols());
gsl_matrix_view Ym = gsl_vector_view_array(Y.get(), getNrows());
gsl_matrix_view Bm = gsl_vector_view_array(B.get(), getNcols());
gsl_matrix_view Sm = gsl_vector_view_array(S.get(), getNcols());
gsl_matrix_view Vm = gsl_matrix_view_array(U.get(), getNcols(), getNcols());
std::cout << "Flag3" << std::endl;
gsl_linalg_SV_decomp_jacobi(Um,Vm,Sm);
std::cout << "Flag4" << std::endl;
gsl_linalg_SV_solve(Um,Vm,Sm,Ym,Bm);
std::cout << std::endl << std::endl << "Sv = " << gsl_vector_get(Sm,0) << "\t" << gsl_vector_get(Sm,1) << std::endl;
std::cout << std::endl << std::endl << "V = " << std::endl;
std::cout << "\t" << gsl_matrix_get(Vm,0,0) << "\t" << gsl_matrix_get(Vm,0,1) << std::endl;
std::cout << "\t" << gsl_matrix_get(Vm,1,0) << "\t" << gsl_matrix_get(Vm,1,1) << std::endl;
std::cout << std::endl << std::endl << "Beta = " << gsl_vector_get(Bm,0) << "\t" << gsl_vector_get(Bm,1) << std::endl;
}
注意,我知道执行路径没有进入构造函数,因为行“std :: cout&lt;&lt;”标记ALPHA“&lt;&lt; std :: endl;”不产生输出。
所以,有几个谜。
1)为什么仅包含boost的shared_array导致核心转储? 2)当我使用裸阵列指针时(是的,我知道内存泄漏,但这是我现在遇到的最少麻烦,而且我会避免让我的共享阵列对我起作用),为什么执行不能进入我的构造函数?
任何见解都将不胜感激。
顺便说一句:如果你注意到我对GSL采取了一些愚蠢的行为,我很高兴知道这件事,即使我的问题似乎与GSL无关。