CRSSparsity CSparseCholeskyInternal::getFactorizationSparsity() const {
   casadi_assert(S_);
   int n = AT_.n;
   int nzmax = S_->cp[n];
   std::vector< int > col(n+1);
   std::copy(S_->cp,S_->cp+n+1,col.begin());
   std::vector< int > rowind(nzmax);
   int *Li = &rowind.front();
   int *Lp = &col.front();
   const cs* C;
   C = S_->pinv ? cs_symperm (&AT_, S_->pinv, 1) : &AT_;
   std::vector< int > temp(2*n);
   int *c = & temp.front();
   int *s = c+n;
   for (int k = 0 ; k < n ; k++) c [k] = S_->cp [k] ;
   for (int k = 0 ; k < n ; k++) {       /* compute L(k,:) for L*L' = C */
     int top = cs_ereach (C, k, S_->parent, s, c) ;
     for ( ; top < n ; top++)    /* solve L(0:k-1,0:k-1) * x = C(:,k) */
       {
         int i = s [top] ;               /* s [top..n-1] is pattern of L(k,:) */
         int p = c [i]++ ;
         Li [p] = k ;                /* store L(k,i) in column i */
       }
     int p = c [k]++ ;
     Li [p] = k ;    
   }
   Lp [n] = S_->cp [n] ; 
   return trans(CRSSparsity(n, n, rowind, col));
 
 }
Esempio n. 2
0
CRSSparsity sp_rowcol(std::vector<int> row, std::vector<int> col, int nrow, int ncol) {
  std::vector<int> rowind(nrow+1);
  std::vector<int> col_new(row.size()*col.size());
  
  // resulting col: the entries of col are repeated row.size() times
  for (int i=0;i<row.size();i++)
    std::copy(col.begin(),col.end(),col_new.begin()+col.size()*i);

  // resulting rowind: first entry is always zero
  int cnt=0;
  int z=0;
  rowind[0]=0;
  int k=0;
  try {
    for (k=0; k < row.size(); k++) {
      // resulting rowind: fill up rowind entries with copies
      while (z<row[k])
        rowind.at(++z)=cnt;
        
      // resulting rowind: add col.size() at each row[k]
      rowind.at(row[k]+1)=(cnt+=col.size());
    }
    while (z<nrow)
      rowind.at(++z)=cnt;                 
  }
  catch (out_of_range& oor) {
    casadi_error(
      "sp_rowcol: out-of-range error." << endl <<
      "The " << k << "th entry of row (" << row[k] << ") was bigger or equal to the specified total number of rows (" << nrow << ")"
    );
  }
  return CRSSparsity(nrow, ncol, col_new, rowind);
}
Esempio n. 3
0
int CRSSparsity::getNZ(int i, int j){
  casadi_assert_message(i<size1() && j<size2(),"Indices out of bounds");

  if (i<0) i += size1();
  if (j<0) j += size2();
  
  // Quick return if matrix is dense
  if(numel()==size())
    return j+i*size2();
  
  // Quick return if we are adding an element to the end
  if(rowind(i)==size() || (rowind(i+1)==size() && col().back()<j)){
    vector<int>& colv = colRef();
    vector<int>& rowindv = rowindRef();
    colv.push_back(j);
    for(int ii=i; ii<size1(); ++ii){
      rowindv[ii+1]++;
    }
    return colv.size()-1;
  }

  // go to the place where the element should be
  int ind;
  for(ind=rowind(i); ind<rowind(i+1); ++ind){ // better: loop from the back to the front
    if(col(ind) == j){
      return ind; // element exists
    } else if(col(ind) > j)
      break;                // break at the place where the element should be added
  }
  
  // Make sure that there no other objects are affected
  makeUnique();
  
  // insert the element
  colRef().insert(colRef().begin()+ind,j);
  for(int row=i+1; row<size1()+1; ++row)
    rowindRef()[row]++;
  
  // Return the location of the new element
  return ind;
}
Esempio n. 4
0
CRSSparsity::CRSSparsity(int nrow, int ncol, bool dense){
  vector<int> col, rowind(nrow+1,0);
  if(dense){
    col.resize(nrow*ncol);
    rowind.resize(nrow+1);
    for(int i=0; i<nrow+1; ++i)
      rowind[i] = i*ncol;
    for(int i=0; i<nrow; ++i)
      for(int j=0; j<ncol; ++j)
        col[j+i*ncol] = j;
  }
  assignNode(new CRSSparsityInternal(nrow, ncol, col, rowind));
}
 DMatrix CSparseCholeskyInternal::getFactorization() const {
   casadi_assert(L_);
   cs *L = L_->L;
   int nz = L->nzmax;
   int m = L->m; // number of rows
   int n = L->n; // number of columns
   std::vector< int > rowind(m+1);
   std::copy(L->p,L->p+m+1,rowind.begin());
   std::vector< int > col(nz);
   std::copy(L->i,L->i+nz,col.begin());
   std::vector< double > data(nz);
   std::copy(L->x,L->x+nz,data.begin());
   return trans(DMatrix(CRSSparsity(m, n, col, rowind),data));
 }
Esempio n. 6
0
void svdFrmCSR(gk_csr_t *mat, int rank, std::vector<std::vector<double>>& uFac,
                std::vector<std::vector<double>>& iFac) {
 
  int u, item;
  int nnz = 0;
  //int nrows = mat->nrows;
  //int ncols = mat->ncols;
  for (int u = 0; u < mat->nrows; u++) {
    nnz += mat->rowptr[u+1] - mat->rowptr[u];
  }

  std::cout << "\nNNZ = " << nnz;

  arma::uvec rowind(nnz); //of size nnz
  arma::uvec colptr(mat->ncols + 1); //of size n_cols + 1
  arma::vec  values(nnz); //values in the matrix
  
  for (item = 0; item < mat->ncols; item++) {
    colptr[item] = mat->colptr[item];
    for (int jj = mat->colptr[item]; jj < mat->colptr[item+1]; jj++) {
      rowind[jj] = mat->colind[jj];
      values[jj] = mat->colval[jj];
    }
  }
  colptr[item] = mat->colptr[item];

  arma::sp_mat X(rowind, colptr, values, mat->nrows, mat->ncols);
  arma::mat U(mat->nrows, rank, arma::fill::zeros); 
  arma::mat V(mat->ncols, rank, arma::fill::zeros);
  arma::vec s(rank);
  
  //singular value decomposition
  arma::svds(U, s, V, X, rank);
  
  for (u = 0; u < mat->nrows; u++) {
    for (int j = 0; j < rank; j++) {
      uFac[u][j] = U(u,j);
    }
  }
  std::cout << "\nsize of U: " << size(U) << " V: " << size(V) << std::endl;
  
  for (item = 0; item < mat->ncols; item++) {
    for (int j = 0; j < rank; j++) {
      iFac[item][j] = V(item,j);
    }
  }
  
}
Esempio n. 7
0
SparseCCS* SparseCCS::generate_random_lower(int size, real XMin, real XMax, double fillPercent)
{
	int i;

	Vector<int> rowind(int(size * size * (fillPercent + 0.05)));
	int* colptr = new int[size + 1];

	int colp = 1;
	colptr[0] = 0;
	for (i = 0; i < size; i++)
	{
		rowind.add(i);
		for (int j = i + 1; j < size; j++)
		{
			double r = double (rand()) / (double (RAND_MAX) + 1.0);
			if (r >= 1.0 - fillPercent)
			{
				rowind.add(j);
			}
		}
		colptr[colp++] = rowind.size();
	}

	colp--;
	real* values = new real[colptr[colp]];
	int* realRowind = new int[colptr[colp]];
	int whichCol = 0;
	for (int i = 0; i < colptr[colp]; i++)
	{
		realRowind[i] = rowind[i];
		real X;
		do
		{
			real r = real (rand()) / (real (RAND_MAX) + 1.0);
			X = XMin + r * (XMax - XMin);
		}
		while (X == 0 || (colptr[whichCol] == i && X < 0));
		if (colptr[whichCol] == i)
		{
			whichCol++;
		}
		values[i] = X;
	}

	return new SparseCCS(size, size, values, realRowind, colptr);
}
Esempio n. 8
0
SparseCCS* SparseCCS::generate_random(int rows, int cols, real XMin, real XMax, double fillPercent)
{
	int i;

	Vector<int> rowind(int(rows * cols * (fillPercent + 0.05)));
	int* colptr = new int[cols + 1];

	int colp = 1;
	colptr[0] = 0;
	for (i = 0; i < cols; i++)
	{
		for (int j = 0; j < rows; j++)
		{
			double r = double (rand()) / (double (RAND_MAX) + 1.0);
			if (r >= 1.0 - fillPercent)
			{
				rowind.add(j);
			}
		}
		colptr[colp++] = rowind.size();
	}

	colp--;
	real* values = new real[colptr[colp]];
	int* realRowind = new int[colptr[colp]];
	for (int i = 0; i < colptr[colp]; i++)
	{
		realRowind[i] = rowind[i];
		real X;
		do
		{
			real r = real (rand()) / (real (RAND_MAX) + 1.0);
			X = XMin + r * (XMax - XMin);
		}
		while (X == 0);
		values[i] = X;
	}

	return new SparseCCS(rows, cols, values, realRowind, colptr);
}
Esempio n. 9
0
CRSSparsity sp_tril(int n) {
  if (n<0)
    throw CasadiException("sp_tril expects a positive integer as argument");

  int c=0;
  int t=0;
  std::vector< int >  	col((n*(n+1))/2,0);
  for (int i=0;i<(n*(n+1))/2;i++) {
    col[i]=t++;
    if (t>c) {
      t=0;
      c++;
    }
  }

  std::vector< int >  	rowind(n+1,0);
  c=0;
  for (int i=1;i<n+1;i++)
    rowind[i]=rowind[i-1]+1+(c++);

  return CRSSparsity(n,n,col,rowind);
}
Esempio n. 10
0
CRSSparsity sp_band(int n, int p) {
  casadi_assert_message(n>=0, "sp_band expects a positive integer as argument");
  casadi_assert_message((p<0? -p : p)<n, "sp_band: position of band schould be smaller then size argument");
  
  int nc = n-(p<0? -p : p);
  
  std::vector< int >  	col(nc);
  
  int offset = max(p,0);
  for (int i=0;i<nc;i++) {
    col[i]=i+offset;
  }
  
  std::vector< int >  	rowind(n+1);
  
  offset = min(p,0);
  for (int i=0;i<n+1;i++) {
    rowind[i]=max(min(i+offset,nc),0);
  }
  
  return CRSSparsity(n,n,col,rowind);
  
}
Esempio n. 11
0
int CRSSparsity::rowind(int row) const{
  return rowind().at(row);
}
Esempio n. 12
0
void SVMLightRunner::SVMLightModelToSVMConfiguration(
    MODEL *model, SVMConfiguration &config
) {
    LOG(
        config.log,
        LogLevel::DEBUG_LEVEL,
        __debug_prefix__ + ".SVMLightModelToSVMConfiguration() Started."
    );

    long i, j, k;
    SVECTOR *v;

    /* 0=linear, 1=poly, 2=rbf, 3=sigmoid, 4=custom -- same as GMUM.R! */
    config.kernel_type = static_cast<KernelType>(model->kernel_parm.kernel_type);
    // -d int      -> parameter d in polynomial kernel
    config.degree = model->kernel_parm.poly_degree;
    // -g float    -> parameter gamma in rbf kernel
    config.gamma = model->kernel_parm.rbf_gamma;
    // -s float    -> parameter s in sigmoid/poly kernel
    config.gamma = model->kernel_parm.coef_lin;
    // -r float    -> parameter c in sigmoid/poly kernel
    config.coef0 = model->kernel_parm.coef_const;
    // -u string   -> parameter of user defined kernel
    config.kernel_parm_custom = model->kernel_parm.custom;
    // highest feature index - no assignment to read-only data
    //config->data.n_cols = model->totwords;
    // number of training documents - no assignment to read-only data
    //config->target.n_rows = model->totdoc;
    // number of support vectors plus 1 (!)
    config.l = model->sv_num - 1;
    // Threshold b (has opposite sign than SVMClient::predict()
    // NOTE: see libraryReadModel()
    config.b = - model->b;
    config.nr_class = 2; // svmlight works only with 2 classes

    config.alpha_y = arma::zeros<arma::vec>(config.l);
    int dim = model->totwords;

    // Constructing alphas and SV:
    arma::uvec colptr(config.l+1); //this always has this dim
    colptr(0) = 0; //always

    int non_zero = 0;
    //get necessary statistics 
    for (i = 1; i < model->sv_num; i++) {
      for (v = model->supvec[i]->fvec; v; v=v->next) {
        for (j = 0; (v->words[j]).wnum; j++) {
            non_zero += 1;
        }
      }
    }
    arma::uvec rowind(non_zero);
    arma::vec values(non_zero);

    int current = 0;
    for (i = 1; i < model->sv_num; i++) {
      for (v = model->supvec[i]->fvec; v; v=v->next) {
        config.alpha_y(i - 1) = model->alpha[i]*v->factor;
        for (j = 0; (v->words[j]).wnum; j++) {
            k = (v->words[j]).wnum - 1;
            rowind[current] = k;
            values[current] = v->words[j].weight;
            current++;
        }
        colptr(i) = current;
      }
    }

    config.support_vectors = arma::sp_mat(rowind, colptr, values, dim, config.l); 
	config.w = (config.support_vectors * config.alpha_y);

    LOG(
        config.log,
        LogLevel::DEBUG_LEVEL,
        __debug_prefix__ + ".SVMLightModelToSVMConfiguration() Done."
    );

}