CRSSparsity reshape(const CRSSparsity& a, int n, int m){ casadi_assert_message(a.numel() == n*m, "reshape: number of elements must remain the same." << endl << "Input argument has shape " << a.size1() << " x " << a.size2() << " = " << a.numel() << ", while you request a reshape to " << n << " x " << m << " = " << n*m ); // our strategy is: (col,rowind) -> (col,row) -> modulus calculus -> (col_new, row_new) -> sp_NZ std::vector<int> row = a.getRow(); const std::vector<int> &col = a.col(); std::vector<int> row_new(a.size()); std::vector<int> col_new(a.size()); // int i=0;int j=0; int z =0; for(int k=0; k<a.size(); ++k){ int i = row[k]; int j = col[k]; int z = j+i*a.size2(); row_new[k] = z/m; col_new[k] = z%m; } return sp_triplet(n,m,row_new,col_new); }
CRSSparsity sp_rowcol(std::vector<int> row, std::vector<int> col, int nrow, int ncol) { std::vector<int> rowind(nrow+1); std::vector<int> col_new(row.size()*col.size()); // resulting col: the entries of col are repeated row.size() times for (int i=0;i<row.size();i++) std::copy(col.begin(),col.end(),col_new.begin()+col.size()*i); // resulting rowind: first entry is always zero int cnt=0; int z=0; rowind[0]=0; int k=0; try { for (k=0; k < row.size(); k++) { // resulting rowind: fill up rowind entries with copies while (z<row[k]) rowind.at(++z)=cnt; // resulting rowind: add col.size() at each row[k] rowind.at(row[k]+1)=(cnt+=col.size()); } while (z<nrow) rowind.at(++z)=cnt; } catch (out_of_range& oor) { casadi_error( "sp_rowcol: out-of-range error." << endl << "The " << k << "th entry of row (" << row[k] << ") was bigger or equal to the specified total number of rows (" << nrow << ")" ); } return CRSSparsity(nrow, ncol, col_new, rowind); }
int main(int argc, char* argv[]) { col* c = col_new(); col* sol; col_random(c); //sol = col_g1search(300000, c); sol = col_gnsearch(300000, c); //sol = col_sq1search(300000, c); col_print_info(sol); col_print(sol); //col_print_fail(sol); return 0; }