Example #1
0
 void Split::generate(CodeGenerator& g, const std::string& mem,
                      const std::vector<int>& arg, const std::vector<int>& res) const {
   int nx = nout();
   for (int i=0; i<nx; ++i) {
     int nz_first = offset_[i];
     int nz_last = offset_[i+1];
     int nz = nz_last-nz_first;
     if (res[i]>=0 && nz>0) { // if anything to assign
       if (nz==1) { // assign scalar
         g.body << "  " << g.workel(res[i]) << " = ";
         if (dep(0).nnz()==1) {
           // rhs is also scalar
           casadi_assert(nz_first==0);
           g.body << g.workel(arg[0]) << ";" << endl;
         } else {
           // rhs is an element in a vector
           g.body << g.work(arg[0], dep(0).nnz()) << "[" << nz_first << "];" << endl;
         }
       } else {
         // assign vector
         std::string r = g.work(arg[0], dep(0).nnz());
         if (nz_first!=0) r = r + "+" + g.to_string(nz_first);
         g.body << "  " << g.copy(r, nz, g.work(res[i], nnz(i))) << endl;
       }
     }
   }
 }
Example #2
0
 void HorzRepmat::generate(CodeGenerator& g, const std::string& mem,
                           const std::vector<int>& arg, const std::vector<int>& res) const {
   int nnz = dep(0).nnz();
   g.body << "  for (i=0;i<" << n_ << ";++i) {" << endl;
   g.body << "    " << g.copy(g.work(arg[0], dep(0).nnz()), nnz,
                              g.work(res[0], sparsity().nnz()) + "+ i*" + g.to_string(nnz))
          << endl
   << "  }" << endl;
 }
Example #3
0
  void Assertion::generate(CodeGenerator& g, const std::string& mem,
                           const std::vector<int>& arg, const std::vector<int>& res) const {
    // Generate assertion
    g.body << "  if (" << g.workel(arg[1]) << "!=1.) {" << endl
           << "    /* " << fail_message_ << " */" << endl
           << "    return 1;" << endl
           << "  }" << endl;

    // Copy if not inplace
    if (arg[0]!=res[0]) {
      g.body << "  " << g.copy(g.work(arg[0], nnz()), nnz(), g.work(res[0], nnz())) << endl;
    }
  }
Example #4
0
  void SetNonzerosSlice<Add>::
  generate(CodeGenerator& g, const std::string& mem,
           const std::vector<int>& arg, const std::vector<int>& res) const {
    // Copy first argument if not inplace
    if (arg[0]!=res[0]) {
      g.body << "  " << g.copy(g.work(arg[0], this->dep(0).nnz()), this->nnz(),
                               g.work(res[0], this->nnz())) << endl;
    }

    // Perform the operation inplace
    g.body << "  for (rr=" << g.work(res[0], this->nnz()) << "+" << s_.start << ", ss="
           << g.work(arg[1], this->dep(1).nnz()) << "; rr!="
           << g.work(res[0], this->nnz()) << "+" << s_.stop
           << "; rr+=" << s_.step << ")";
    g.body << " *rr " << (Add?"+=":"=") << " *ss++;" << endl;
  }
Example #5
0
  void SetNonzerosVector<Add>::
  generate(CodeGenerator& g, const std::string& mem,
           const std::vector<int>& arg, const std::vector<int>& res) const {
    // Copy first argument if not inplace
    if (arg[0]!=res[0]) {
      g.body << "  " << g.copy(g.work(arg[0], this->dep(0).nnz()), this->nnz(),
                               g.work(res[0], this->nnz())) << endl;
    }

    // Condegen the indices
    int ind = g.getConstant(this->nz_, true);

    // Perform the operation inplace
    g.body << "  for (cii=s" << ind << ", rr=" << g.work(res[0], this->nnz()) << ", "
           << "ss=" << g.work(arg[1], this->dep(1).nnz()) << "; cii!=s" << ind
           << "+" << this->nz_.size() << "; ++cii, ++ss)";
    g.body << " if (*cii>=0) rr[*cii] " << (Add?"+=":"=") << " *ss;" << endl;
  }
Example #6
0
void Reshape::generate(CodeGenerator& g, const std::string& mem,
                       const std::vector<int>& arg, const std::vector<int>& res) const {
    if (arg[0]==res[0]) return;
    g.body << "  " << g.copy(g.work(arg[0], nnz()), nnz(), g.work(res[0], nnz())) << endl;
}