Example #1
0
  void SymbolicQr::generateBody(std::ostream &stream, const std::string& type,
                                        CodeGenerator& gen) const {
    casadi_warning("Code generation for SymbolicQR still experimental");

    // Data structures to hold A, Q and R
    stream << "  static int prepared = 0;" << endl;
    stream << "  static d A[" << input(LINSOL_A).size() << "];" << endl;
    stream << "  static d Q[" << Q_.size() << "];" << endl;
    stream << "  static d R[" << R_.size() << "];" << endl;

    // Check if the factorization is up-to-date
    stream << "  int i;" << endl;
    stream << "  for (i=0; prepared && i<" << input(LINSOL_A).size()
           << "; ++i) prepared=A[i]!=x0[i];" << endl;

    // Factorize if needed
    int fact_ind = gen.getDependency(fact_fcn_);
    stream << "  if (!prepared) {" << endl;
    stream << "    for (i=0; i<" << input(LINSOL_A).size() << "; ++i) A[i]=x0[i];" << endl;
    stream << "    f" << fact_ind << "(A, Q, R);" << endl;
    stream << "    prepared = 1;" << endl;
    stream << "  }" << endl;

    // Solve
    int solv_ind_N = gen.getDependency(solv_fcn_N_);
    int neq = input(LINSOL_B).size1();
    int nrhs = input(LINSOL_B).size2();
    stream << "  for (i=0; i<" << nrhs << "; ++i) {" << endl;
    stream << "    f" << solv_ind_N << "(Q, R, x1, r0);" << endl;
    stream << "    x1+=" << neq << "; r0+=" << neq << ";" << endl;
    stream << "  }" << endl;
  }
Example #2
0
void EvaluationMX::generateOperation(std::ostream &stream, const std::vector<std::string>& arg, const std::vector<std::string>& res, CodeGenerator& gen) const{
  
  // Get the index of the function
  int f = gen.getDependency(fcn_);
  stream << "  f" << f << "_buffered(";
  
  // Pass inputs to the function input buffers
  for(int i=0; i<arg.size(); ++i){
    // Pass argument to the function
    stream << arg.at(i) << ",";
    
    // Pass argument sparsity to the function
    if(dep(i).isNull()){
      stream << "0";
    } else {
      int sp_i = gen.getSparsity(dep(i).sparsity());
      stream << "s" << sp_i;
    }

    // Separate with a space to visualize argument grouping
    if(i+1<arg.size()+res.size()) stream << ", ";
  }

  // Separate arguments and results with two extra spaces
  stream << "  ";

  // Pass results to the function input buffers
  for(int i=0; i<res.size(); ++i){
    // Pass results buffer to the function
    stream << res.at(i) << ",";
    
    // Pass argument sparsity to the function
    int sp_i = gen.getSparsity(sparsity(i));
    stream << "s" << sp_i;

    // Separate with a space to visualize argument grouping
    if(i+1<res.size()) stream << ", ";
  }
  
  // Finalize the function call
  stream << ");" << endl;  
}