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 vec(const CRSSparsity& a){ return reshape(trans(a),a.numel(),1); }