Exemplo n.º 1
0
int CreateCellVariable(int iVar, matvar_t *matVariable, int * parent, int item_position)
{
  static const char *fieldNames[] = {"ce", "dims","entries"};
  int nbFields = 3;
  int K = 0;
  int prodDims = 0;
  int valueIndex = 0, type;
  int * cell_addr = NULL;
  int * cell_entry_addr = NULL;
  matvar_t ** allData = NULL;
  SciErr _SciErr;

  /* Returned mlist initialization */
  if (parent==NULL)
    {
      _SciErr = createMList(pvApiCtx, iVar, nbFields, &cell_addr); MATIO_ERROR;
    }
  else
    {
      _SciErr = createMListInList(pvApiCtx, iVar, parent, item_position, nbFields, &cell_addr); MATIO_ERROR;
    }
 
  /* FIRST LIST ENTRY: fieldnames */
  _SciErr = createMatrixOfStringInList(pvApiCtx, iVar, cell_addr, 1, 1, nbFields, (char **)fieldNames); MATIO_ERROR;
  
  /* SECOND LIST ENTRY: Dimensions (int32 type) */
  if(matVariable->rank==2) /* Two dimensions */
    {
      _SciErr = createMatrixOfInteger32InList(pvApiCtx, iVar, cell_addr, 2, 1, matVariable->rank, matVariable->dims); MATIO_ERROR;
    }
  else /* 3 or more dimensions -> Scilab HyperMatrix */
    {
      type = I_INT32;
      CreateHyperMatrixVariable(iVar, MATRIX_OF_VARIABLE_SIZE_INTEGER_DATATYPE, 
				&type, &matVariable->rank, matVariable->dims, matVariable->data,
				NULL, cell_addr, 2);
    }

  /* ALL OTHER ENTRIES: Fields data */
  prodDims = 1;
  for (K=0; K<matVariable->rank; K++)
    {
      prodDims *= matVariable->dims[K];
    }

  allData = (matvar_t**) (matVariable->data);

  if (prodDims == 1) /* Scalar cell */
    {
      /* Create list entry in the stack */
      if (!CreateMatlabVariable(iVar, allData[0], cell_addr, 3)) /* Could not Create Variable */
	{
	  sciprint("Do not know how to read a variable of class %d.\n", allData[0]->class_type);
	}
    }
  else
    {
      _SciErr = createListInList(pvApiCtx, iVar, cell_addr, 3, prodDims, &cell_entry_addr); MATIO_ERROR;

      for (valueIndex = 0; valueIndex < prodDims; valueIndex++)
        {
          /* Create list entry in the stack */
          if (!CreateMatlabVariable(iVar, allData[valueIndex], cell_entry_addr, valueIndex+1)) /* Could not Create Variable */
            {
              sciprint("Do not know how to read a variable of class %d.\n", allData[valueIndex]->class_type);
            }
        }
    }
  
  return TRUE;
}
Exemplo n.º 2
0
extern "C" int sci_sdpa_read_prob(char * fname)
{
  int nb_vars, nb_constr, i, j, k, Index;
  struct blockmatrix pC;
  double * pa = NULL;
  struct constraintmatrix * pconstraints = NULL;
  struct sparseblock * tmp_block = NULL;
  int printlevel = 0, ret = 0, res = 0;
  char ** filename = NULL;
  int * filename_address = NULL, filename_rows, filename_cols, * filename_length = NULL;
  int * printlevel_address = NULL, printlevel_rows, printlevel_cols;
  double * printlevel_value = NULL, * c_out = NULL, * b_out = NULL;
  int * c_out_address = NULL, * a_out_address = NULL;
  int * constraint_address = NULL;
  int constraint_block_rows, constraint_block_cols, constraint_nb_item;
  double * block_value = NULL;
  int minrhs = 1, maxrhs = 2;
  int minlhs = 4, maxlhs = 4;
  SciErr sciErr;

  CheckRhs(minrhs,maxrhs);
  CheckLhs(minlhs,maxlhs);

  ///////////////////////
  // Read the filename //
  ///////////////////////

  sciErr = getVarAddressFromPosition(pvApiCtx,FILENAME_IN,&filename_address); CSDP_ERROR;
  sciErr = getMatrixOfString(pvApiCtx,filename_address, &filename_rows, &filename_cols, NULL, NULL); CSDP_ERROR;
  if (filename_rows*filename_cols!=1)
    {
      Scierror(999,"%s: an unique string is requested for the filename\n",fname);
      return 0;
    }
  filename_length = (int *)MALLOC(filename_rows*filename_cols*sizeof(int));
  sciErr = getMatrixOfString(pvApiCtx,filename_address, &filename_rows, &filename_cols, filename_length, NULL); CSDP_ERROR;
  filename = (char **)MALLOC(filename_rows*filename_cols*sizeof(char *));
  for(i=0;i<filename_rows*filename_cols;i++)
    {
      filename[i] = (char *)MALLOC((filename_length[i]+1)*sizeof(char));
    }
  sciErr = getMatrixOfString(pvApiCtx,filename_address, &filename_rows, &filename_cols, filename_length, filename); CSDP_ERROR;

  /////////////////////
  // Read printlevel //
  /////////////////////

  if (Rhs==2)
    {
      sciErr = getVarAddressFromPosition(pvApiCtx,PRINTLEVEL_IN, &printlevel_address); CSDP_ERROR;
      sciErr = getMatrixOfDouble(pvApiCtx,printlevel_address, &printlevel_rows, &printlevel_cols, &printlevel_value); CSDP_ERROR;
      printlevel = (int)*printlevel_value;
    }
  else
    {
      printlevel = 0;
    }

  ///////////////////////////
  // Read the sdpa problem //
  ///////////////////////////

  ret =  read_prob(filename[0], &nb_vars, &nb_constr, &pC, &pa, &pconstraints, printlevel);

#ifdef DEBUG
  printf("DEBUG: read file %s, return code = %d nb_vars = %d nb_constr = %d\n", filename[0], ret, nb_vars, nb_constr);
#endif

  ///////////////
  // Process C //
  ///////////////

  sciErr = createList(pvApiCtx,C_OUT,pC.nblocks,&c_out_address); CSDP_ERROR;

#ifdef DEBUG
  printf("DEBUG: process c: %d blocks\n", pC.nblocks);
#endif

  for(i=0;i<pC.nblocks;i++)
    {
#ifdef DEBUG
      printf("DEBUG: processing block %d: blockssize = %d blockcat = %d\n", i+1, pC.blocks[i+1].blocksize, pC.blocks[i+1].blockcategory);
#endif
      if (pC.blocks[i+1].blockcategory==MATRIX)
	{
	  sciErr = allocMatrixOfDoubleInList(pvApiCtx, C_OUT, c_out_address, i+1, pC.blocks[i+1].blocksize, pC.blocks[i+1].blocksize, &c_out); CSDP_ERROR;
	  
	  for(j=0;j<pC.blocks[i+1].blocksize;j++)
	    {
	      for(k=0;k<pC.blocks[i+1].blocksize;k++)
		{
		  c_out[j + k*pC.blocks[i+1].blocksize] = pC.blocks[i+1].data.mat[ijtok(j+1,k+1,pC.blocks[i+1].blocksize)];
#ifdef DEBUG
		  printf("DEBUG: MATRIX - c_out_m[%d][%d] = %f - Index = %d\n",j,k,c_out[j + k*pC.blocks[i+1].blocksize], ijtok(j+1,k+1,pC.blocks[i+1].blocksize));
#endif
		}
	    }
	}
      else if (pC.blocks[i+1].blockcategory==DIAG)
	{
	  sciErr = allocMatrixOfDoubleInList(pvApiCtx, C_OUT, c_out_address, i+1, pC.blocks[i+1].blocksize, 1, &c_out); CSDP_ERROR;
	  
	  for(j=0;j<pC.blocks[i+1].blocksize;j++)
	    {
	      c_out[j] = pC.blocks[i+1].data.vec[j+1];
#ifdef DEBUG
	      printf("DEBUG: DIAG - c_out_d[%d] = %f\n",j,c_out[j]);
#endif
	    }	  
	}
      else
	{
	  Scierror(999,"%s: wrong blockcat type PACKEDMATRIX\n",fname);
	  return 0;
	}
    }

  ///////////////
  // Process A //
  ///////////////

#ifdef DEBUG
  printf("DEBUG: process a - nb_constr = %d\n",nb_constr);
#endif
  sciErr = createList(pvApiCtx,A_OUT,nb_constr,&a_out_address); CSDP_ERROR;

  for(i=0;i<nb_constr;i++)
    {
#ifdef DEBUG
      printf("DEBUG: processing constraint %d\n", i+1);
#endif
      // constraint_nb_item: nb blocks for constraint i+1
      Index = 0;
      tmp_block = pconstraints[i+1].blocks;
      while (tmp_block)
	{
	  tmp_block = tmp_block->next;
	  Index++;
	}
      constraint_nb_item = Index;

      sciErr = createListInList(pvApiCtx,A_OUT, a_out_address, i+1, constraint_nb_item, &constraint_address); CSDP_ERROR;

      Index = 0;
      tmp_block = pconstraints[i+1].blocks;
      for(j=0;j<constraint_nb_item;j++)
	{
#ifdef DEBUG
	  printf("DEBUG: processing block %d: nb_block = %d blocksize = %d numentries %d\n", j+1, constraint_nb_item, tmp_block->blocksize, tmp_block->numentries);
	  printf("DEBUG:                      blocknum = %d constraintnum = %d issparse = %d\n", tmp_block->blocknum, tmp_block->constraintnum, tmp_block->issparse);
#endif
	  constraint_block_rows = tmp_block->blocksize;
	  constraint_block_cols = tmp_block->blocksize;
#ifdef DEBUG
	  int ii;
	  for(ii=0;ii<tmp_block->numentries;ii++)
	    {
	      printf("entries[%d] = %f\n",ii+1,tmp_block->entries[ii+1]);
	    }
#endif
	  sciErr = allocMatrixOfDoubleInList(pvApiCtx, A_OUT, constraint_address, j+1, constraint_block_rows, constraint_block_cols, &block_value); CSDP_ERROR;
	  if (res)
	    {
	      Scierror(999,"%s: error while allocating a matrix A on the stack\n", fname);
	      return 0;
	    }
	  for(k=0;k<constraint_block_rows*constraint_block_cols;k++) block_value[k] = 0.0;

	  for(k=0;k<tmp_block->numentries;k++)
	    {
	      block_value[(tmp_block->iindices[k+1]-1) + (tmp_block->jindices[k+1]-1)*tmp_block->blocksize] = tmp_block->entries[k+1];
#ifdef DEBUG
	      printf("a_out[%d][%d] = %f\n",tmp_block->iindices[k+1],tmp_block->jindices[k+1],
			block_value[(tmp_block->iindices[k+1]-1)+(tmp_block->jindices[k+1]-1)*tmp_block->blocksize]);
#endif
	    }
	  tmp_block = tmp_block->next;
	}
    }

  ///////////////
  // Process B //
  ///////////////

#ifdef DEBUG
  printf("DEBUG: process B\n");
#endif
  sciErr = allocMatrixOfDouble(pvApiCtx, B_OUT, nb_constr, 1, &b_out); CSDP_ERROR;
  if (res)
    {
      Scierror(999,"%s: error while allocating a vector on the stack\n", fname);
      return 0;
    }
  for(i=0;i<nb_constr;i++) 
    {
      b_out[i] = pa[i+1];
#ifdef DEBUG
      printf("a[%d] = %f\n", i, b_out[i]);
#endif
    }

  ////////////////////
  // Process status //
  ////////////////////

#ifdef DEBUG
  printf("DEBUG: process status\n");
#endif
  sciErr = allocMatrixOfDouble(pvApiCtx, STATUS_OUT, 1, 1, &b_out); CSDP_ERROR;
  *b_out = (double)ret;
#ifdef DEBUG
  printf("DEBUG: status = %d,%d\n",*b_out,ret);
#endif

  LhsVar(1) = C_OUT;
  LhsVar(2) = A_OUT;
  LhsVar(3) = B_OUT;
  LhsVar(4) = STATUS_OUT;

  return 0;
}
Exemplo n.º 3
0
int CreateStructVariable(void *pvApiCtx, int iVar, matvar_t *matVariable, int * parent, int item_position)
{
    char **fieldNames = NULL;
    int nbFields = 0;
    int fieldIndex = 0;
    int K = 0;
    int prodDims = 0;
    int valueIndex = 0;
    matvar_t *fieldMatVar = NULL;
    matvar_t ** allData = NULL;
    int * cell_addr = NULL;
    int * cell_entry_addr = NULL;
    int type;
    SciErr sciErr;
    int *piDims = NULL;
    int i = 0;

    /* Fields of the struct */
    nbFields = 2; /* "st" "dims" */
    nbFields += Mat_VarGetNumberOfFields(matVariable);

    fieldNames = (char**) MALLOC(sizeof(char*) * nbFields);
    if (fieldNames == NULL)
    {
        Scierror(999, _("%s: No more memory.\n"), "CreateStructVariable");
        return FALSE;
    }

    fieldNames[0] = strdup("st");
    if (fieldNames[0] == NULL)
    {
        Scierror(999, _("%s: No more memory.\n"), "CreateStructVariable");
        return FALSE;
    }
    fieldNames[1] = strdup("dims");
    if (fieldNames[1] == NULL)
    {
        Scierror(999, _("%s: No more memory.\n"), "CreateStructVariable");
        return FALSE;
    }

    for (fieldIndex = 1; fieldIndex < nbFields - 1; fieldIndex++)
    {
        fieldMatVar = Mat_VarGetStructField(matVariable, &fieldIndex, MAT_BY_INDEX, 0);
        fieldNames[fieldIndex + 1] = strdup(fieldMatVar->name);
        if (fieldNames[fieldIndex + 1] == NULL)
        {
            Scierror(999, _("%s: No more memory.\n"), "CreateStructVariable");
            return FALSE;
        }
    }

    /* Returned mlist initialization */
    if (parent == NULL)
    {
        sciErr = createMList(pvApiCtx, iVar, nbFields, &cell_addr);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }
    }
    else
    {
        sciErr = createMListInList(pvApiCtx, iVar, parent, item_position, nbFields, &cell_addr);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }
    }

    /* FIRST LIST ENTRY: fieldnames */
    sciErr = createMatrixOfStringInList(pvApiCtx, iVar, cell_addr, 1, 1, nbFields, fieldNames);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    /* SECOND LIST ENTRY: Dimensions (int32 type) */
    if (nbFields == 2) /* Empty struct must have size 0x0 in Scilab */
    {
        matVariable->dims[0] = 0;
        matVariable->dims[1] = 0;
    }

    piDims = (int *) MALLOC(matVariable->rank * sizeof(int));
    for (i = 0 ; i < matVariable->rank ; ++i)
    {
        piDims[i] = (int)matVariable->dims[i];
    }

    if (matVariable->rank == 2) /* Two dimensions */
    {
        sciErr = createMatrixOfInteger32InList(pvApiCtx, iVar, cell_addr, 2, 1, matVariable->rank, piDims);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }
    }
    else /* 3 or more dimensions -> Scilab HyperMatrix */
    {
        type = I_INT32;
        CreateHyperMatrixVariable(pvApiCtx, iVar, MATRIX_OF_VARIABLE_SIZE_INTEGER_DATATYPE,
                                  &type, &matVariable->rank, piDims, (double*)matVariable->data,
                                  NULL, cell_addr, 2);
    }

    FREE(piDims);

    /* ALL OTHER ENTRIES: Fields data */
    prodDims = 1;
    for (K = 0; K < matVariable->rank; K++)
    {
        prodDims *= (int)matVariable->dims[K];
    }

    allData = (matvar_t**) (matVariable->data);

    if (prodDims == 1) /* Scalar struct */
    {
        for (fieldIndex = 0; fieldIndex < nbFields - 2; fieldIndex++)
        {
            /* Create list entry in the stack */
            if (!CreateMatlabVariable(pvApiCtx, iVar, allData[fieldIndex], cell_addr, fieldIndex + 3)) /* Could not Create Variable */
            {
                if (allData[fieldIndex]->class_type != 0) /* class is 0 for not initialized fields */
                {
                    sciprint("Do not know how to read a variable of class %d.\n", allData[fieldIndex]->class_type);
                }
            }
        }
    }
    else
    {
        for (fieldIndex = 0; fieldIndex < nbFields - 2; fieldIndex++)
        {
            sciErr = createListInList(pvApiCtx, iVar, cell_addr, fieldIndex + 3, prodDims, &cell_entry_addr);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 0;
            }

            for (valueIndex = 0; valueIndex < prodDims; valueIndex++)
            {
                /* Create list entry in the stack */
                if (!CreateMatlabVariable(pvApiCtx, iVar, allData[(fieldIndex) + (nbFields - 2)*valueIndex], cell_entry_addr, valueIndex + 1)) /* Could not Create Variable */
                {
                    if (allData[(fieldIndex) + (nbFields - 2)*valueIndex]->class_type != 0) /* class is 0 for not initialized fields */
                    {
                        sciprint("Do not know how to read a variable of class %d.\n", allData[(fieldIndex) + (nbFields - 2)*valueIndex]->class_type);
                    }
                }
            }
        }
    }

    freeArrayOfString(fieldNames, nbFields);

    return TRUE;
}