bool isSymbolicSparse(const SXMatrix& ex) { for(int k=0; k<ex.size(); ++k) // loop over non-zero elements if(!ex.at(k)->isSymbolic()) // if an element is not symbolic return false; return true; }
void substituteInPlace(const SXMatrix &v, SXMatrix &vdef, std::vector<SXMatrix>& ex, bool reverse){ casadi_assert_message(isSymbolic(v),"the variable is not symbolic"); casadi_assert_message(v.sparsity() == vdef.sparsity(),"the sparsity patterns of the expression and its defining expression do not match"); if(v.empty()) return; // quick return if nothing to replace // Function inputs std::vector<SXMatrix> f_in; if(!reverse) f_in.push_back(v); // Function outputs std::vector<SXMatrix> f_out; f_out.push_back(vdef); f_out.insert(f_out.end(),ex.begin(),ex.end()); // Write the mapping function SXFunction f(f_in,f_out); f.init(); // Get references to the internal data structures const vector<SXAlgEl>& algorithm = f.algorithm(); vector<SX> work(f.getWorkSize()); // Iterator to the binary operations vector<SX>::const_iterator b_it=f->operations_.begin(); // Iterator to stack of constants vector<SX>::const_iterator c_it = f->constants_.begin(); // Iterator to free variables vector<SX>::const_iterator p_it = f->free_vars_.begin(); // Evaluate the algorithm for(vector<SXAlgEl>::const_iterator it=algorithm.begin(); it<algorithm.end(); ++it){ switch(it->op){ case OP_INPUT: // reverse is false, substitute out work[it->res] = vdef.at(it->arg.i[1]); break; case OP_OUTPUT: if(it->res==0){ vdef.at(it->arg.i[1]) = work[it->arg.i[0]]; if(reverse){ // Use the new variable henceforth, substitute in work[it->arg.i[0]] = v.at(it->arg.i[1]); } } else { // Auxillary output ex[it->res-1].at(it->arg.i[1]) = work[it->arg.i[0]]; } break; case OP_CONST: work[it->res] = *c_it++; break; case OP_PARAMETER: work[it->res] = *p_it++; break; default: { switch(it->op){ CASADI_MATH_FUN_BUILTIN(work[it->arg.i[0]],work[it->arg.i[1]],work[it->res]) } // Avoid creating duplicates const int depth = 2; // NOTE: a higher depth could possibly give more savings work[it->res].assignIfDuplicate(*b_it++,depth); } } } }
void simplify(SXMatrix &ex){ // simplify all non-zero elements for(int el=0; el<ex.size(); ++el) simplify(ex.at(el)); }