void store_idata(const char* fpath, const real_2d_array& data) { FILE* f = fopen(fpath, "w"); if (!f) throw "[store_data] Failed opening file!"; for (int row=0; row<data.rows(); ++row) { for (int col=0; col<data.cols()-1; ++col) { fprintf(f, "%i\t",(int)data[row][col]); } fprintf(f, "%i", (int)data[row][data.cols()-1]); //if (row != data.rows()-1) fprintf(f, "\n"); } fclose(f); }
// Opposite of slice --URI void insert(real_2d_array &matrix, int i, real_1d_array &output){ int j; int c = matrix.cols(); for(j=0; j<c; j++) matrix[i][j] = output[j]; }
void print_matrix(const real_2d_array& m, int row1, int row2, int col1, int col2) { row1 = min(row1, m.rows()); row2 = min(row2, m.rows()); col1 = min(col1, m.cols()); col2 = min(col2, m.cols()); for (int r=row1; r<row2; ++r) { for (int c=col1; c<col2; ++c) { cout<<m[r][c]<<"\t"; } cout<<endl; } }
CostCalculator_eigen::CostCalculator_eigen(const real_1d_array expectReturn, const real_2d_array &varMatrix, const real_1d_array &tradingCost, const real_1d_array ¤tWeight) { assert(expectReturn.length() == varMatrix.rows()); assert(varMatrix.rows() == varMatrix.cols()); assert(tradingCost.length() == varMatrix.rows()); assert(currentWeight.length() == varMatrix.rows()); variableNumber_ = expectReturn.length(); xReal_.resize(variableNumber_, 1); varMatrix_.resize(variableNumber_, variableNumber_); expectReturn_.resize(variableNumber_); tradingCost_.resize(variableNumber_); currentWeight_.resize(variableNumber_); for (int i = 0; i != variableNumber_; ++i) { expectReturn_(i) = expectReturn[i]; tradingCost_(i) = tradingCost[i]; currentWeight_(i) = currentWeight[i]; for (int j = 0; j != variableNumber_; ++j) varMatrix_(i, j) = varMatrix[i][j]; } }
// slices i^th row of matrix, into output void slice(real_2d_array &matrix, int i, real_1d_array &output){ int j; int c = matrix.cols(); for(j=0;j<c;j++) output[j] = matrix[i][j]; }