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); }
std::vector<int> getNZDense(const CRSSparsity &sp) { std::vector<int> ret(sp.size()); std::vector<int> row = sp.getRow(); const std::vector<int> &col = sp.col(); int s2 = sp.size2(); for(int k=0;k<sp.size();k++) { ret[k] = col[k]+row[k]*s2; } return ret; }
bool dependsOn(const SXMatrix& ex, const SXMatrix &arg){ if(ex.size()==0) return false; SXFunction temp(arg,ex); temp.init(); CRSSparsity Jsp = temp.jacSparsity(); return Jsp.size()!=0; }
Reshape::Reshape(const MX& x, CRSSparsity sp){ casadi_assert(x.size()==sp.size()); setDependencies(x); setSparsity(sp); }