Esempio n. 1
0
dd_MatrixPtr FromEigen (const cref_matrix_t& input)
{
    if (dd_debug)
    {
        std::cout << "from eigen input " << input << std::endl;
    }
    dd_debug = false;
    dd_MatrixPtr M=NULL;
    dd_rowrange i;
    dd_colrange j;
    dd_rowrange m_input = (dd_rowrange)(input.rows());
    dd_colrange d_input = (dd_colrange)(7);
    dd_RepresentationType rep=dd_Generator;
    mytype value;
    dd_NumberType NT = dd_Real;;
    dd_init(value);

    M=dd_CreateMatrix(m_input, d_input);
    M->representation=rep;
    M->numbtype=NT;

    for (i = 1; i <= input.rows(); i++)
    {
        dd_set_d(value, 0);
        dd_set(M->matrix[i-1][0],value);
        for (j = 2; j <= d_input; j++)
        {
          dd_set_d(value, input(i-1,j-2));
          dd_set(M->matrix[i-1][j-1],value);
        }
    }
  dd_clear(value);
  return M;
}
Esempio n. 2
0
dd_LPPtr MB_get_LP_MatrixPtr(const mxArray * in)
{
	mxArray * tmpobj;
	dd_MatrixPtr A;
	int j;
	double * value;
	dd_ErrorType error=dd_NoError;
	dd_LPPtr lp;
	
	dd_set_global_constants(); /* First, this must be called once to use cddlib. */
	
	if (A=FT_get_H_MatrixPtr(in)) {
		/* set objective */
		if ((tmpobj = mxGetField(in, 0, "obj")) &&
		    (mxGetNumberOfDimensions(tmpobj) <= 2) && 
		    (mxGetM(tmpobj) == 1)) {
		    	value = mxGetPr(tmpobj);
  			A->objective = dd_LPmin;
  			dd_set_d(A->rowvec[0], 0.0); /* there is no constant term */
  			for (j = 1; j < A->colsize; j++)
  				dd_set_d(A->rowvec[j],value[j-1]);
  			lp=dd_Matrix2LP(A, &error);
  			dd_FreeMatrix(A);
			return lp;
  		}else{
  			mexErrMsgTxt("Error in the setting of LP objective.");
  		}
	}else{
		mexErrMsgTxt("Error in the setting of LP matrix.");
	}
	return 0;
}
Esempio n. 3
0
dd_MatrixPtr FT_get_H_MatrixPtr(const mxArray * in)
{
    mxArray * tmpa;	
    mxArray * tmpb;	
    mxArray * tmpl;	
    dd_MatrixPtr A;
    int eqsize, m, n, i, j;
    double * a;
    double * b;
    double * lin;
	
    if ((tmpa = mxGetField(in, 0, "A")) &&  
        (tmpb = mxGetField(in, 0, "B")) &&
        (mxGetNumberOfDimensions(tmpa) == 2) && /* A must be a matrix */
        (mxGetNumberOfDimensions(tmpb) == 2) && /* B must be a matrix */
        (mxGetM(tmpa) == mxGetM(tmpb)) && /* Dimension must be compatible */
        (mxGetN(tmpb) == 1)) { /* B must be a column vector */
        m = mxGetM(tmpa);
        n = mxGetN(tmpa) + 1; /* Create [b, -A] */
        a = mxGetPr(tmpa);
        b = mxGetPr(tmpb);		
        A = dd_CreateMatrix(m, n);
        for (i = 0; i < m; i++) {
            dd_set_d(A->matrix[i][0],b[i]); /* A[i,0] <- b[i] */
            for (j = 0; j < n - 1; j++) {
                dd_set_d(A->matrix[i][j + 1],- a[j * m + i]); /* A[i,j+1] <- a[i,j] */
            }
        }
        A->representation = dd_Inequality; 		
        A->numbtype = dd_Real;
        /* set lineality if present */
        if ((tmpl = mxGetField(in, 0, "lin")) &&
            (mxGetNumberOfDimensions(tmpl) <= 2) && 
            (mxGetM(tmpl) == 1)) {
            lin = mxGetPr(tmpl);
            eqsize = mxGetN(tmpl);
            for (i = 0; i < eqsize; i++) {
                if (lin[i] <= (double) m) 
                    set_addelem(A->linset,(long) lin[i]); /* linset uses 1-based */
                else
                    mexWarnMsgTxt("Error in the lineality vector ");
            }
        }
        return A;
    } else {
        return 0;
    }
}
Esempio n. 4
0
void dd_MLSetMatrixWithString(dd_rowrange m, dd_colrange d, char line[], dd_MatrixPtr M)
{
   dd_rowrange i=0;
   dd_colrange j=0;
   char *next,*copy;
   const char ct[]=", {}\n";  /* allows separators ","," ","{","}". */
   mytype value;
   double dval;

   dd_init(value);
   copy=(char *)malloc(strlen(line) + 4048); /* some extra space as buffer.  Somehow linux version needs this */
   strcpy(copy,line);
   next=strtok(copy,ct);
   i=0; j=0;
   do {
#if defined GMPRATIONAL
      dd_sread_rational_value(next, value);
#else
      dval=atof(next);
      dd_set_d(value,dval);
#endif
      dd_WriteNumber(stderr,value);
      dd_set(M->matrix[i][j],value);
      j++;
      if (j == d) {
         i++; j=0;
      }
  } while ((next=strtok(NULL,ct))!=NULL);
  free(copy);
  dd_clear(value);
  return;
}
Esempio n. 5
0
void allvertices(int m_input, int d_input, double *a_input)
/* output vertices and incidences */
{
  dd_PolyhedraPtr poly;
  dd_MatrixPtr A=NULL,G=NULL;
  dd_SetFamilyPtr GI=NULL;
  dd_rowrange i,m; 
  dd_colrange j,d;
  dd_ErrorType err;

  m=(dd_rowrange)m_input; d=(dd_colrange)d_input;
  A=dd_CreateMatrix(m,d);
  for (i=0; i<m; i++){
    for (j=0; j<d; j++) dd_set_d(A->matrix[i][j],a_input[i*d+j]);
  }
  A->representation=dd_Inequality;
  poly=dd_DDMatrix2Poly(A, &err);
    /* compute the second (generator) representation */
  if (err==dd_NoError) {
    G=dd_CopyGenerators(poly);
    GI=dd_CopyIncidence(poly);

    MLPutFunction(stdlink,"List",2);
    dd_MLWriteMatrix(G);
    dd_MLWriteSetFamily(GI);
  } else {
    dd_MLWriteError(poly);
  }

  dd_FreeMatrix(A);
  dd_FreeMatrix(G);
  dd_FreeSetFamily(GI);
}
Esempio n. 6
0
void allfacets(int n_input, int d_input, double *g_input)
/* output facets and incidences */
{
  dd_PolyhedraPtr poly;
  dd_MatrixPtr A=NULL,G=NULL;
  dd_SetFamilyPtr AI=NULL;
  dd_rowrange i,n; 
  dd_colrange j,d;
  dd_ErrorType err;

  n=(dd_rowrange)n_input; d=(dd_colrange)d_input;
  G=dd_CreateMatrix(n,d);
  for (i=0; i<n; i++){
    for (j=0; j<d; j++) dd_set_d(G->matrix[i][j],g_input[i*d+j]);
  }
  G->representation=dd_Generator;
  poly=dd_DDMatrix2Poly(G, &err);
    /* compute the second (inequality) representation */
  if (err==dd_NoError){
    A=dd_CopyInequalities(poly);
    AI=dd_CopyIncidence(poly);

    MLPutFunction(stdlink,"List",2);
    dd_MLWriteMatrix(A);
    dd_MLWriteSetFamily(AI);
  } else {
    dd_MLWriteError(poly);
  }

  dd_FreeMatrix(A);
  dd_FreeMatrix(G);
  dd_FreeSetFamily(AI);
}
Esempio n. 7
0
dd_MatrixPtr FT_get_V_MatrixPtr(const mxArray * in)
{
	mxArray * tmpv;	
	mxArray * tmpr;	
	dd_MatrixPtr V;
	int mr, m, n, i, j;
	double * v;
	double * r;
	
	if ((tmpv = mxGetField(in, 0, "V")) && 
	    (mxGetNumberOfDimensions(tmpv) <= 2)) {
	    	
		if ((tmpr = mxGetField(in, 0, "R")) && 
	    	    (mxGetNumberOfDimensions(tmpv) <= 2)) {
	    	    	mr = mxGetM(tmpr);
	    	    	r = mxGetPr(tmpr);
		} else mr = 0;
	    		    
		m = mxGetM(tmpv);
		n = mxGetN(tmpv) + 1;
		v = mxGetPr(tmpv);
		V = dd_CreateMatrix(m + mr, n);
		for (i = 0; i < m; i++) {
			dd_set_si(V->matrix[i][0],1);
			for (j = 0; j < n - 1; j++) {
				dd_set_d(V->matrix[i][j + 1], v[i + j * m]);
			}
		}
		for (i = m; i < m + mr; i++) {
			dd_set_si(V->matrix[i][0],0);
			for (j = 0; j < n - 1; j++) {
				dd_set_d(V->matrix[i][j + 1], r[(i - m) + j * mr]);
			}
		}		
		V->representation = dd_Generator;	
		V->numbtype = dd_Real;
		return V;
	}
	return 0;
}
Esempio n. 8
0
/*
 * Helper function to generate a matrix from init_vals
 */
static dd_MatrixPtr init_matrix(const double *init_vals, int rows,
                                int cols)
{
  dd_MatrixPtr m = dd_CreateMatrix(rows, cols);
  dd_rowrange i;
  dd_colrange j;

  /* Fill values */
  for (i = 0; i < m->rowsize; i++) {
    for (j = 0; j < m->colsize; j++) {
      dd_set_d(m->matrix[i][j], init_vals[m->colsize * i + j]);
    }
  }
  return m;
}
Esempio n. 9
0
void
find_interior_DS(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
  /* uses Dual Simplex method */
	/* We would like to find an iterior point
	   for a polyhedron in H representation 
	     A   x  <=  b.
	*/

	dd_ErrorType error=dd_NoError;
	dd_LPSolverType solver=dd_DualSimplex;
	dd_LPPtr lp, lp1;   /* pointer to LP data structure that is not visible by user. */
	dd_MatrixPtr A;
	int j;  
  
	dd_set_global_constants(); /* First, this must be called once to use cddlib. */


	/* Input an LP using the cdd library  */
	/* lp = MB_get_LP_MatrixPtr(prhs[0]); */

	if (A=FT_get_H_MatrixPtr(prhs[0])) {
		/* set objective */
		A->objective = dd_LPmin;
		for (j = 0; j < A->colsize; j++)
			dd_set_d(A->rowvec[j],0.0);
		lp=dd_Matrix2LP(A, &error);
  		dd_FreeMatrix(A);
	}else{
		mexErrMsgTxt("Error in the setting of LP matrix.");
	}

	
	lp1=dd_MakeLPforInteriorFinding(lp);
	dd_LPSolve(lp1,solver,&error);
	if (error!=dd_NoError) dd_WriteErrorMessages(stdout, error);
	    
	/* Take the solution. */
	plhs[0] = MB_set_LPsol_MatrixPtr(lp1);


	/* Free allocated spaces. */
	dd_FreeLPData(lp);
	dd_FreeLPData(lp1);
}
Esempio n. 10
0
/*
 * Create a dd_MatrixPtr, filled with values from init_vals.
 * length of init_vals must be rows*cols in row-major order
 *
 * This function adds a constraint: x > 0
 *
 * Caller must free returned matrix.
 */
static dd_MatrixPtr init_ineq_doubles(const double *init_vals,
                                      const size_t rows, const size_t cols,
                                      const float lower_bound,
                                      const float upper_bound)
{
  /* Only 3-column inputs supported */
  assert(cols == 3);

  dd_MatrixPtr m = dd_CreateMatrix(rows + 2, cols);
  dd_rowrange i;
  dd_colrange j;

  /* Fill values */
  for (i = 0; i < rows; i++) {
    for (j = 0; j < m->colsize; j++) {
      dd_set_d(m->matrix[i][j], init_vals[m->colsize * i + j]);
    }
  }

  /* Add a row constraining solution space x >= lower_bound */
  dd_set_d(m->matrix[rows][0], -lower_bound);   /* intercept */
  dd_set_d(m->matrix[rows][1], 1);              /* coef of x */
  dd_set_d(m->matrix[rows][2], 0);              /* coef of y */

  /* Add a row constraining solution space x <= upper_bound */
  dd_set_d(m->matrix[rows + 1][0], upper_bound);    /* intercept */
  dd_set_d(m->matrix[rows + 1][1], -1);             /* coef of x */
  dd_set_d(m->matrix[rows + 1][2], 0);              /* coef of y */

  /* Mark the representation as inequalities */
  m->representation = dd_Inequality;

  /* Input types = reals */
  m->numbtype = dd_Real;
  return m;
}