Пример #1
0
  MX GetNonzeros::getGetNonzeros(const Sparsity& sp, const std::vector<int>& nz) const {
    // Get all the nonzeros
    vector<int> nz_all = getAll();

    // Eliminate recursive calls
    vector<int> nz_new(nz);
    for (vector<int>::iterator i=nz_new.begin(); i!=nz_new.end(); ++i) {
      if (*i>=0) *i = nz_all[*i];
    }
    return dep()->getGetNonzeros(sp, nz_new);
  }
Пример #2
0
  MX Concat::getGetNonzeros(const Sparsity& sp, const std::vector<int>& nz) const {
    // Get the first nonnegative nz
    int nz_test = -1;
    for (vector<int>::const_iterator i=nz.begin(); i!=nz.end(); ++i) {
      if (*i>=0) {
        nz_test = *i;
        break;
      }
    }

    // Quick return if none
    if (nz_test<0) return MX::zeros(sp);

    // Find out to which dependency it might depend
    int begin=0, end=0;
    int i;
    for (i=0; i<ndep(); ++i) {
      begin = end;
      end += dep(i).size();
      if (nz_test < end) break;
    }

    // Check if any nz refer to a different nonzero
    for (vector<int>::const_iterator j=nz.begin(); j!=nz.end(); ++j) {
      if (*j>=0 && (*j < begin || *j >= end)) {

        // Fallback to the base class
        return MXNode::getGetNonzeros(sp, nz);
      }
    }

    // All nz refer to the same dependency, update the nonzero indices
    if (begin==0) {
      return dep(i)->getGetNonzeros(sp, nz);
    } else {
      vector<int> nz_new(nz);
      for (vector<int>::iterator j=nz_new.begin(); j!=nz_new.end(); ++j) {
        if (*j>=0) *j -= begin;
      }
      return dep(i)->getGetNonzeros(sp, nz_new);
    }
  }