示例#1
0
void EchoIt(MLINK stdlink,MLINK ml,bool b) {
  bool inCompound = false;
  int i;
  double d;
  char * s = 0;
  char * t = 0;
  long m,n;
  switch(MLGetType(stdlink)) {
  case MLTKINT;
     MLGetInteger(stdlink,&i);
     MLPutInteger(ml,i);
     break;
  case MLTKSYMB;
     MLGetSymbol(stdlink,&s);
     t = new char[strlen(s)+4];
     strcpy(t,s);
     strcat(t,"MXS");
     MLPutSymbol(ml,t);
     delete [] t;
     MLDisownSymbol(stdlink,s);
     break;
  case MLTKSTR;
     MLGetString(stdlink,&s);
     MLPutString(ml,s);
     MLDisownString(stdlink,s);
     break;
  case MLTKINT;
     MLGetInteger(stdlink,&i);
     MLPutInteger(ml,i);
     break;
  case MLTKFUNC;
     MLGetFunction(stdlink,&s,m);
     strcpy(t,s);
     strcat(t,"MXS");
     if(strcmp(s,"CompoundExpression")==0 && b) {
       inCompound = true;
       MLPutFunction(ml,t,2*m);
     } else {
       MLPutFunction(ml,t,m);
     };
     delete [] t;
     for(long n=1;n<=m;++n) {
       if(inCompound) {
         MLPutFunction(ml,"Print",4L); 
         MLPutString(ml,"Function:");
         MLPutString(ml,s);
         MLPutString(ml," ");
         MLPutInteger(ml,s_number);
         ++s_number;
       };
       EchoIt(stdlink,ml,b);
     };
     inCompound = false;
     break;
  default:
     DBG();
     break;
  };
};
示例#2
0
文件: qmma.c 项目: kevinarpe/kx
int mlput(K x,K y){
  int fstype=y->t,fsint=y->i,i=0,funcerr=0;
  K z;
  char b[2]={0,0};
  mlint64 j={0,0}; 
  //printf("%s:%d,%d\n","mlput",x->t,x->n);
  switch(x->t){
  case -KB: 
  case -KG: 
  case -KC:b[0]=x->g;R MLPutString(ml_lp,b); 
  case -KH:R MLPutInteger16(ml_lp,x->h);
  case -KI:R MLPutInteger32(ml_lp,x->i);
  case -KJ:*(J*)&j=x->j;R MLPutInteger64(ml_lp,j);
  case -KE:R MLPutReal32(ml_lp,x->e); 
  case -KF:R MLPutReal64(ml_lp,x->f); 
  case -KS:R MLPutSymbol(ml_lp,x->s);

  case KB: 
  case KG: 
  case KC:R MLPutByteString(ml_lp,kG(x),x->n);
  case KH:R MLPutInteger16List(ml_lp,kH(x),x->n);
  case KI:R MLPutInteger32List(ml_lp,kI(x),x->n);
  case KJ:R MLPutInteger64List(ml_lp,(mlint64*)kJ(x),x->n);
  case KE:R MLPutReal32List(ml_lp,kE(x),x->n); 
  case KF:R MLPutReal64List(ml_lp,kF(x),x->n);

  case KS:if(!MLPutFunction(ml_lp,"List",x->n)){
      R 0;
    }else{
      for(i=0;i<x->n;i++)if(!MLPutSymbol(ml_lp,kS(x)[i]))R 0;
    }
    break;
  case 0:
    if(0==x->n){
      R MLPutFunction(ml_lp, "List",0);
    }else if((3==x->n)&&(fstype==kK(x)[0]->t)){
      z=kK(x)[2];
      if(!MLPutFunction(ml_lp,kK(x)[1]->s,z->n)){R 0;}else{
	switch(z->t){
	case 0:for(i=0;i<z->n;i++)if(!mlput(kK(z)[i],y))R 0;break;
	case KH:for(i=0;i<z->n;i++)if(!MLPutInteger16(ml_lp,kH(z)[i]))R 0;break;
	case KI:for(i=0;i<z->n;i++)if(!MLPutInteger32(ml_lp,kI(z)[i]))R 0;break;
	case KJ:for(i=0;i<z->n;i++){*(J*)&j=kJ(z)[i];if(!MLPutInteger64(ml_lp,j))R 0;}break;
	case KE:for(i=0;i<z->n;i++)if(!MLPutReal32(ml_lp,kE(z)[i]))R 0;break;
	case KF:for(i=0;i<z->n;i++)if(!MLPutReal64(ml_lp,kF(z)[i]))R 0;break;
	case KS:for(i=0;i<z->n;i++)if(!MLPutSymbol(ml_lp,kS(z)[i]))R 0;break;
	case KC:for(i=0;i<z->n;i++){b[0]=kC(z)[i];if(!MLPutString(ml_lp,b))R 0;}break;
	default:break;
	}
      }
    }else{
      if(!MLPutFunction(ml_lp,"List",x->n)){R 0;}else{for(i=0;i<x->n;i++)if(!mlput(kK(x)[i],y)){MLPutSymbol(ml_lp,"ParaErr");funcerr=1;}if(funcerr)R 0;}
    }
    break; 
  default:
    R 0;
  }
  R 1;
}
void MLReturnError(const char *fname, const char *msgtag, const char *str1){
    MLPutFunction(stdlink,"CompoundExpression",2);
        MLPutFunction(stdlink,"Message",3);
                MLPutFunction(stdlink,"MessageName",2);
                    MLPutSymbol(stdlink,fname);
                    MLPutString(stdlink,msgtag);
                MLPutString(stdlink,str1);
        MLPutSymbol(stdlink,"$Failed");
}
示例#4
0
void MmaSink::put(const Field & x) {
#ifdef DEBUG_MMASINK
  GBStream << "sink:number " << this << '\n';
#endif

  /* New version by MAURICIO that uses strings */
  MLPutFunction(d_mlink, "ToExpression", 1);
  MLPutString(d_mlink, x.str().c_str());

  /* COMMENTED BY MAURICIO, NOV 09
  const long long & num = x.numerator().internal();
  const long long & den = x.denominator().internal();

  if(den==1L) {
    MLPutInteger(d_mlink,num);
  } else {
    MLPutFunction(d_mlink,"Rational",2L);
    MLPutInteger(d_mlink,num);
    MLPutInteger(d_mlink,den);
  };
  */
  
#ifdef DEBUG_MMASINK
  checkforerror();
#endif
  ++d_count;
};
示例#5
0
文件: Pythonika.c 项目: pc/pythonika
void mat_process_iterable_object(PyObject *obj, char *error_msg)
{
        int length;
        PyObject *iterator, *item;
        
        length = PyObject_Length(obj);

        MLPutFunction(stdlink, "List", 1);
        MLPutFunction(stdlink, "Sequence", length);
        
        iterator = PyObject_GetIter(obj);
        
        if(iterator==NULL) {
            MLPutString(stdlink, error_msg);
            return;
        }
        
        while(item = PyIter_Next(iterator)) {
            python_to_mathematica_object(item);
            Py_DECREF(item);
        }
        
        Py_DECREF(iterator);

        return;
}
示例#6
0
文件: Pythonika.c 项目: pc/pythonika
void process_std(PyObject *source)
{
    PyObject *std_data;
    char *std_str;
#ifdef PYTHON25
    Py_ssize_t length;
#else
    int length;
#endif

    DEBUG("process_std: getting data")
	std_data = PyObject_GetAttrString(source, "data");
    DEBUG("process_std: getting string from data")
	PyString_AsStringAndSize(std_data, &std_str, &length);
	
	if(length>0) {
        DEBUG("process_std: showing string")
        MLPutString(stdlink, std_str);
        Py_DECREF(std_data);
        
        PyObject_SetAttrString(source, "data", PyString_FromString(""));
        
        return;
    }
    MLPutSymbol(stdlink, "Null");
}
示例#7
0
文件: Pythonika.c 项目: pc/pythonika
void PyString(char *name, int *value, long len)
{
    long idx;
    char *buffer;
    PyObject *obj;
    
    
    buffer = (char *)malloc(len);
    if(!buffer) {
        MLPutString(stdlink, "Pythonika: Can't allocate memory for string.");
        return;
    }

    for(idx=0; idx<len; idx++)
        buffer[idx] = (char) value[idx];


    obj = PyString_FromStringAndSize(buffer, len);
    free(buffer);

    if(list_level>=0 && List[list_level]) {        
        PyList_Append(List[list_level], obj);
        Py_DECREF(obj);
        MLPutSymbol(stdlink, "Null");    
        return;
    }

    PyObject_SetAttrString( interpreter_state->main_module, name, obj);
    Py_DECREF(obj);
        
    MLPutSymbol(stdlink, "Null");
}
示例#8
0
文件: cddmlio.c 项目: hnxiao/mcdd
void dd_MLWriteError(dd_PolyhedraPtr poly)
{
  MLPutFunction(stdlink,"List",3);
  MLPutFunction(stdlink,"List",0);
  MLPutFunction(stdlink,"List",1);
  MLPutString(stdlink,"Error occured: code");
  MLPutFunction(stdlink,"List",1);
  MLPutInteger(stdlink,poly->child->Error);
}
示例#9
0
void MmaSink::put(const stringGB & x) {
#ifdef DEBUG_MMASINK
  GBStream << "sink:string " << this << x.value().chars() << '\n';
#endif
  MLPutString(d_mlink,(const char *)x.value().chars());
#ifdef DEBUG_MMASINK
  checkforerror();
#endif
  ++d_count;
};
示例#10
0
void MmaSink::put(const Variable& x) {
#ifdef DEBUG_MMASINK
  GBStream << "sink:variable " << this << x.cstring() << '\n';
#endif
  MLPutFunction(d_mlink,"ToExpression",1L);
  MLPutString(d_mlink,x.cstring());
#ifdef DEBUG_MMASINK
  checkforerror();
#endif
  ++d_count;
};
示例#11
0
文件: put.c 项目: szhorvat/mEngine
//put a complex array
void engputc(const char* VarName,
		     const int* mmaDim, int Depth,
		     const double* Re, int ReLen,
		     const double* Im, int ImLen)
{
	mxArray* MxVar = NULL;		//the variable to be put
	bool SUCCESS = true;		//success flag
	int i;						// generic iterator

	mwSize matlabDim[Depth];	// dimensions array in MATLAB format

	for (i=0; i < Depth; ++i)
		matlabDim[i] = (mwSize) mmaDim[i];


	if (NULL == Eng)	//if not opened yet, open it
	{
		msg("eng::noMLB");	//message 
		SUCCESS = false;
		goto epilog;
	}
	
	//create mxArray 
	MxVar = mxCreateNumericArray(Depth, matlabDim, mxDOUBLE_CLASS, mxCOMPLEX);
	if (NULL == MxVar)
	{
		msg("engPut::ercrt");
		SUCCESS = false;
		goto epilog;
	}
	//and populate
	memcpy((void *)(mxGetPr(MxVar)), (void *)Re, ReLen * sizeof(double));
	memcpy((void *)(mxGetPi(MxVar)), (void *)Im, ImLen * sizeof(double));

	//put
	if(engPutVariable(Eng, VarName, MxVar))	//not successful
	{
		msg("engPut::erput");
		SUCCESS = false;
		goto epilog;
	}
	
epilog:
	if (MxVar != NULL)
		mxDestroyArray(MxVar);

	if(SUCCESS)
		MLPutString(stdlink, VarName);
	else
		MLPutSymbol(stdlink, "$Failed");
}
示例#12
0
文件: Pythonika.c 项目: pc/pythonika
void PyOpenList(char *name)
{
    PyObject *list;
    
    if(list_level+1==MAX_LIST_LEVELS) {
        MLPutString(stdlink, MAX_LIST_LEVELS_MSG);
        return;
    }

    list = PyList_New(0);

    // If a list exists, embed this list inside
    if(list_level>=0 && List[list_level])
        PyList_Append(List[list_level], list);
        
    // Otherwise just add a new one using the given name
    else
        PyObject_SetAttrString(interpreter_state->main_module, name, list);

    List[++list_level] = list;

    Py_DECREF(list);
    MLPutSymbol(stdlink, "Null");    
}
示例#13
0
文件: cddmlio.c 项目: hnxiao/mcdd
void dd_MLWriteAmatrix(dd_Amatrix A, long rowmax, long colmax)
{
  long i,j;
  double a;
  char *str=NULL;

  if (A==NULL){
    rowmax=0; colmax=0;
  }
  MLPutFunction(stdlink,"List",rowmax);
  for (i=0; i < rowmax; i++) {
    MLPutFunction(stdlink,"List",colmax);
    for (j=0; j < colmax; j++) {
#if defined GMPRATIONAL
      str=dd_MLGetStrForNumber(A[i][j]);
      MLPutString(stdlink, str);
      if (str!=NULL) free(str);
#else
      a=dd_get_d(A[i][j]);
      MLPutDouble(stdlink, a);
#endif
    }
  }
}
示例#14
0
PetscErrorCode  PetscViewerMathematicaPutCSRMatrix(PetscViewer viewer, int m, int n, int *i, int *j, PetscReal *a)
{
    PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
    MLINK                   link   = vmath->link; /* The link to Mathematica */
    const char              *symbol;
    char                    *name;
    PetscBool               match;
    PetscErrorCode          ierr;

    PetscFunctionBegin;
    /* Determine the object name */
    if (!vmath->objName) name = "mat";
    else                 name = (char*) vmath->objName;

    /* Make sure Mathematica recognizes sparse matrices */
    MLPutFunction(link, "EvaluatePacket", 1);
    MLPutFunction(link, "Needs", 1);
    MLPutString(link, "LinearAlgebra`CSRMatrix`");
    MLEndPacket(link);
    /* Skip packets until ReturnPacket */
    ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);
    CHKERRQ(ierr);
    /* Skip ReturnPacket */
    MLNewPacket(link);

    /* Send the CSRMatrix object */
    MLPutFunction(link, "EvaluatePacket", 1);
    MLPutFunction(link, "Set", 2);
    MLPutSymbol(link, name);
    MLPutFunction(link, "CSRMatrix", 5);
    MLPutInteger(link, m);
    MLPutInteger(link, n);
    MLPutFunction(link, "Plus", 2);
    MLPutIntegerList(link, i, m+1);
    MLPutInteger(link, 1);
    MLPutFunction(link, "Plus", 2);
    MLPutIntegerList(link, j, i[m]);
    MLPutInteger(link, 1);
    MLPutRealList(link, a, i[m]);
    MLEndPacket(link);
    /* Skip packets until ReturnPacket */
    ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);
    CHKERRQ(ierr);
    /* Skip ReturnPacket */
    MLNewPacket(link);

    /* Check that matrix is valid */
    MLPutFunction(link, "EvaluatePacket", 1);
    MLPutFunction(link, "ValidQ", 1);
    MLPutSymbol(link, name);
    MLEndPacket(link);
    ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);
    CHKERRQ(ierr);
    MLGetSymbol(link, &symbol);
    ierr = PetscStrcmp("True", (char*) symbol, &match);
    CHKERRQ(ierr);
    if (!match) {
        MLDisownSymbol(link, symbol);
        SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Invalid CSR matrix in Mathematica");
    }
    MLDisownSymbol(link, symbol);
    /* Skip ReturnPacket */
    MLNewPacket(link);
    PetscFunctionReturn(0);
}
示例#15
0
文件: Pythonika.c 项目: pc/pythonika
void python_to_mathematica_object(PyObject *obj)
{
    if(PyBool_Check(obj)) {
        if(obj==Py_True)
            MLPutSymbol(stdlink, "True");
        else
            MLPutSymbol(stdlink, "False");
        return;
    }
    if(PyInt_Check(obj)) {
        MLPutLongInteger(stdlink, PyInt_AsLong(obj));
        return;
    }
    if(PyLong_Check(obj)) {
#ifdef PYTHON25
        Py_ssize_t length;
#else
        int length;
#endif
        char *str, *mat_expr;
        PyObject *long_as_str;
        
        long_as_str = PyObject_CallMethod(obj, "__str__", NULL);
        PyString_AsStringAndSize(long_as_str, &str, &length);
        
        MLPutFunction(stdlink, "ToExpression", 1);
        MLPutString(stdlink, str);

        Py_DECREF(long_as_str);
        return;
    }
    if(obj==Py_None) {
        MLPutSymbol(stdlink, "Null");
        return;
    }
    if(PyFloat_Check(obj)) {
        MLPutDouble(stdlink, (double)PyFloat_AsDouble(obj));
        return;
    }
    if(PyComplex_Check(obj)) {
        MLPutFunction(stdlink, "Complex", 2);
        MLPutDouble(stdlink, (double)PyComplex_RealAsDouble(obj));
        MLPutDouble(stdlink, (double)PyComplex_ImagAsDouble(obj));
        return;
    }
    if(PyString_Check(obj)) {
        char *str;
#ifdef PYTHON25
        Py_ssize_t length;
#else
        int length;
#endif
        
        PyString_AsStringAndSize(obj, &str, &length);
        
        MLPutByteString(stdlink, (unsigned char *)str, length);
        return;
    }

    if(PyUnicode_Check(obj)) {
        MLPutUnicodeString(stdlink,
            PyUnicode_AsUnicode(obj),
            PyUnicode_GetSize(obj) );
        return;
    }
    if(PyTuple_Check(obj)) {
        
        mat_process_iterable_object(obj, "Can't get iterator for 'tuple'");

        return;
    }
    if(PyList_Check(obj)) {
    
        mat_process_iterable_object(obj, "Can't get iterator for 'list'");

        return;
    }
#ifndef PYTHON23
    if(PyObject_TypeCheck(obj, &PySet_Type)) {
    
        mat_process_iterable_object(obj, "Can't get iterator for 'set'");
    
        return;
    }
#endif
    if(PyDict_Check(obj)) {
        PyObject *items;
        
        items = PyDict_Items(obj);
        python_to_mathematica_object(items);
        Py_DECREF(items);

        return;
    }
    
    // This should ideally print info, like type of the object, that
    // can't be converted.
    MLPutString(stdlink, "Object type can't be converted!");
}
示例#16
0
//put a complex array
void engputc(const char* VarName,
		     const int* Dim, int Depth,
		     const double* Re, int ReLen,
		     const double* Im, int ImLen)
{
    mwSize newDim[Depth];
    int i;
    for(i = 0; i < Depth; ++i)
    {
        newDim[i] = (mwSize)Dim[i];
    }
	mxArray* MxVar = NULL;		//the variable to be put
	bool SUCCESS = true;		//success flag

	if (NULL == Eng)	//if not opened yet, open it
	{
		msg("eng::noMLB");	//message 
		SUCCESS = false;
		goto epilog;
	}
	
	//create mxArray 
	MxVar = mxCreateNumericArray(Depth, newDim, mxDOUBLE_CLASS, mxCOMPLEX);
	if (NULL == MxVar)
	{
		msg("engPut::ercrt");
		SUCCESS = false;
		goto epilog;
	}
	
	unsigned char *start_of_pr;
    size_t bytes_to_copy;
	start_of_pr = (unsigned char *)mxGetData(MxVar);
    bytes_to_copy = ReLen * mxGetElementSize(MxVar);
    memcpy(start_of_pr,Re,bytes_to_copy);

	start_of_pr = (unsigned char *)mxGetImagData(MxVar);
    bytes_to_copy = ImLen * mxGetElementSize(MxVar);
    memcpy(start_of_pr,Im,bytes_to_copy);
	
	
	//and populate
    // memcpy((void *)(mxGetPr(MxVar)), (void *)Re, ReLen * sizeof(double));
    // memcpy((void *)(mxGetPi(MxVar)), (void *)Im, ImLen * sizeof(double));

	//put
	if(engPutVariable(Eng, VarName, MxVar))	//not successful
	{
		msg("engPut::erput");
		SUCCESS = false;
		goto epilog;
	}
	
epilog:
	if (MxVar != NULL)
		mxDestroyArray(MxVar);

	if(SUCCESS)
		MLPutString(stdlink, VarName);
	else
		MLPutSymbol(stdlink, "$Failed");
}
示例#17
0
// put a real array to the MatLab workspace
void engputr(const char* VarName,
		     const int* Dim, int Depth,
		     const double* Val, int ValLen)
{
    // mwSize *newDim;
    // newDim = (mwSize*)Dim;
    // mwSize newDim[]={1,3};
    mwSize newDim[Depth];
    int i;
    for(i = 0; i < Depth; ++i)
    {
        newDim[i] = (mwSize)Dim[i];
    }
    mwSize newDepth;
    newDepth = (mwSize)Depth;
	mxArray* MxVar = NULL;		//the variable to be put
	bool SUCCESS = true;		//success flag
	if (NULL == Eng)	//if not opened yet
	{
		msg("eng::noMLB");	//message 
		SUCCESS = false;
		goto epilog;
	}
	
	//create mxArray 
    // MxVar = mxCreateNumericArray(Depth, Dim, mxDOUBLE_CLASS, mxREAL);
    // char stuff[100] = "B;a ;alsj asldjasdf ";
    // stuff[1] = "T";
    // MLPutInteger(stdlink, newDepth);
	MxVar = mxCreateNumericArray(newDepth, newDim, mxDOUBLE_CLASS, mxREAL);
	if (NULL == MxVar)
	{
		msg("engPut::ercrt");
		SUCCESS = false;
		goto epilog;
	}
	//and populate
	
	unsigned char *start_of_pr;
    start_of_pr = (unsigned char *)mxGetData(MxVar);
    size_t bytes_to_copy;
    bytes_to_copy = ValLen * mxGetElementSize(MxVar);
    memcpy(start_of_pr,Val,bytes_to_copy);
	
    // memcpy((void *)(mxGetPr(MxVar)), (void *)Val, ValLen * sizeof(double));
	
	//put
	if(engPutVariable(Eng, VarName, MxVar))	//not successful
	{
		msg("engPut::erput");
		SUCCESS = false;
		goto epilog;
	}
	
epilog:
	if (MxVar != NULL)
		mxDestroyArray(MxVar);

	if(SUCCESS)
		MLPutString(stdlink, VarName);
	else
		MLPutSymbol(stdlink, "$Failed");
}
示例#18
0
文件: get.cpp 项目: Jornason/MATLink
// Takes a MATLAB variable and writes in in Mathematica form to link
void toMma(const mxArray *var, MLINK link) {

    // the following may occur when retrieving empty struct fields
    // it showsup as [] in MATLAB so we return {}
    // note that non-existent variables are caught and handled in eng_get()
    if (var == NULL) {
        MLPutFunction(link, "List", 0);
        return;
    }

    // get size information
    mwSize depth = mxGetNumberOfDimensions(var);
    const mwSize *mbDims = mxGetDimensions(var);

    // handle zero-size arrays
    if (mxIsEmpty(var)) {
        if (mxIsChar(var))
            MLPutString(link, "");
        else
            MLPutFunction(link, "List", 0);
        return;
    }

    // translate dimension information to Mathematica order
    std::vector<int> mmDimsVec(depth);
    std::reverse_copy(mbDims, mbDims + depth, mmDimsVec.begin());
    int *mmDims = &mmDimsVec[0];

    int len = mxGetNumberOfElements(var);

    // numerical (sparse or dense)
    if (mxIsNumeric(var)) {
        mxClassID classid = mxGetClassID(var);

        // verify that var is of a supported class
        switch (classid) {
        case mxDOUBLE_CLASS:
        case mxSINGLE_CLASS:
        case mxINT32_CLASS:
        case mxINT16_CLASS:
        case mxUINT16_CLASS:
        case mxINT8_CLASS:
        case mxUINT8_CLASS:
            break;
        default:
            putUnknown(var, link);
            return;
        }

        if (mxIsSparse(var)) {
            // Note: I realised that sparse arrays can only hold double precision numerical types
            // in MATLAB R2013a.  I will leave the below implementation for single precision & integer
            // types in case future versions of MATLAB will add support for them.

            int ncols = mxGetN(var); // number of columns

            mwIndex *jc = mxGetJc(var);
            mwIndex *ir = mxGetIr(var);

            int nnz = jc[ncols]; // number of nonzeros

            MLPutFunction(link, CONTEXT "matSparseArray", 4);
            mlpPutIntegerList(link, jc, ncols + 1);
            mlpPutIntegerList(link, ir, nnz);

            // if complex, put as im*I + re
            if (mxIsComplex(var)) {
                MLPutFunction(link, "Plus", 2);
                MLPutFunction(link, "Times", 2);
                MLPutSymbol(link, "I");
                switch (classid) {
                 case mxDOUBLE_CLASS:
                    MLPutReal64List(link, mxGetPi(var), nnz); break;
                 case mxSINGLE_CLASS:
                    MLPutReal32List(link, (float *) mxGetImagData(var), nnz); break;
                 case mxINT16_CLASS:
                    MLPutInteger16List(link, (short *) mxGetImagData(var), nnz); break;
                 case mxINT32_CLASS:
                    MLPutInteger32List(link, (int *) mxGetImagData(var), nnz); break;
                 default:
                    assert(false); // should never reach here
                }
            }

            switch (classid) {
             case mxDOUBLE_CLASS:
                MLPutReal64List(link, mxGetPr(var), nnz); break;
             case mxSINGLE_CLASS:
                MLPutReal32List(link, (float *) mxGetData(var), nnz); break;
             case mxINT16_CLASS:
                MLPutInteger16List(link, (short *) mxGetData(var), nnz); break;
             case mxINT32_CLASS:
                MLPutInteger32List(link, (int *) mxGetData(var), nnz); break;
             default:
                assert(false); // should never reach here
            }

            MLPutInteger32List(link, mmDims, depth);
        }
        else // not sparse
        {
            MLPutFunction(link, CONTEXT "matArray", 2);

            // if complex, put as im*I + re
            if (mxIsComplex(var)) {
                MLPutFunction(link, "Plus", 2);
                MLPutFunction(link, "Times", 2);
                MLPutSymbol(link, "I");
                switch (classid) {
                 case mxDOUBLE_CLASS:
                    MLPutReal64Array(link, mxGetPi(var), mmDims, NULL, depth); break;
                 case mxSINGLE_CLASS:
                    MLPutReal32Array(link, (float *) mxGetImagData(var), mmDims, NULL, depth); break;
                 case mxINT32_CLASS:
                    MLPutInteger32Array(link, (int *) mxGetImagData(var), mmDims, NULL, depth); break;
                 case mxINT16_CLASS:
                    MLPutInteger16Array(link, (short *) mxGetImagData(var), mmDims, NULL, depth); break;
                 case mxUINT16_CLASS:
                  {
                    int *arr = new int[len];
                    unsigned short *mbData = (unsigned short *) mxGetImagData(var);
                    std::copy(mbData, mbData + len, arr);
                    MLPutInteger32Array(link, arr, mmDims, NULL, depth);
                    delete [] arr;
                    break;
                  }
                 case mxINT8_CLASS:
                  {
                    short *arr = new short[len];
                    char *mbData = (char *) mxGetImagData(var);
                    std::copy(mbData, mbData + len, arr);
                    MLPutInteger16Array(link, arr, mmDims, NULL, depth);
                    delete [] arr;
                    break;
                  }
                 case mxUINT8_CLASS:
                  {
                    short *arr = new short[len];
                    unsigned char *mbData = (unsigned char *) mxGetImagData(var);
                    std::copy(mbData, mbData + len, arr);
                    MLPutInteger16Array(link, arr, mmDims, NULL, depth);
                    delete [] arr;
                    break;
                  }
                 default:
                    assert(false); // should never reach here
                }
            }

            switch (classid) {
            case mxDOUBLE_CLASS:
                MLPutReal64Array(link, mxGetPr(var), mmDims, NULL, depth); break;
            case mxSINGLE_CLASS:
                MLPutReal32Array(link, (float *) mxGetData(var), mmDims, NULL, depth); break;
            case mxINT32_CLASS:
                MLPutInteger32Array(link, (int *) mxGetData(var), mmDims, NULL, depth); break;
            case mxINT16_CLASS:
                MLPutInteger16Array(link, (short *) mxGetData(var), mmDims, NULL, depth); break;
            case mxUINT16_CLASS:
             {
                int *arr = new int[len];
                unsigned short *mbData = (unsigned short *) mxGetData(var);
                std::copy(mbData, mbData + len, arr);
                MLPutInteger32Array(link, arr, mmDims, NULL, depth);
                delete [] arr;
                break;
             }
            case mxINT8_CLASS:
             {
                short *arr = new short[len];
                char *mbData = (char *) mxGetData(var);
                std::copy(mbData, mbData + len, arr);
                MLPutInteger16Array(link, arr, mmDims, NULL, depth);
                delete [] arr;
                break;
             }
            case mxUINT8_CLASS:
             {
                short *arr = new short[len];
                unsigned char *mbData = (unsigned char *) mxGetData(var);
                std::copy(mbData, mbData + len, arr);
                MLPutInteger16Array(link, arr, mmDims, NULL, depth);
                delete [] arr;
                break;
             }
            default:
                assert(false); // should never reach here
            }

            MLPutInteger32List(link, mmDims, depth);
        }
    }
    // logical (sparse or dense)
    else if (mxIsLogical(var))
        if (mxIsSparse(var)) {
            int ncols = mxGetN(var); // number of columns

            mwIndex *jc = mxGetJc(var);
            mwIndex *ir = mxGetIr(var);
            mxLogical *logicals = mxGetLogicals(var);

            int nnz = jc[ncols]; // number of nonzeros

            MLPutFunction(link, CONTEXT "matSparseLogical", 4);
            mlpPutIntegerList(link, jc, ncols + 1);
            mlpPutIntegerList(link, ir, nnz);

            short *integers = new short[nnz];
            std::copy(logicals, logicals+nnz, integers);

            MLPutInteger16List(link, integers, nnz);

            MLPutInteger32List(link, mmDims, depth);

            delete [] integers;
        }
        else // not sparse
        {
            mxLogical *logicals = mxGetLogicals(var);

            short *integers = new short[len];
            std::copy(logicals, logicals+len, integers);

            MLPutFunction(link, CONTEXT "matLogical", 2);
            MLPutInteger16Array(link, integers, mmDims, NULL, depth);
            MLPutInteger32List(link, mmDims, depth);

            delete [] integers;
        }
    // char array
    else if (mxIsChar(var)) {
        assert(sizeof(mxChar) == sizeof(unsigned short));
        // 1 by N char arrays (row vectors) are sent as a string
        if (depth == 2 && mbDims[0] == 1) {
            const mxChar *str = mxGetChars(var);
            MLPutFunction(link, CONTEXT "matString", 1);
            MLPutUTF16String(link, reinterpret_cast<const unsigned short *>(str), len); // cast may be required on other platforms: (mxChar *) str
        }
        // general char arrays are sent as an array of characters
        else {
            MLPutFunction(link, CONTEXT "matCharArray", 2);
            const mxChar *str = mxGetChars(var);
            MLPutFunction(link, "List", len);
            for (int i=0; i < len; ++i)
                MLPutUTF16String(link, reinterpret_cast<const unsigned short *>(str + i), 1);
            MLPutInteger32List(link, mmDims, depth);
        }
    }
    // struct
    else if (mxIsStruct(var)) {
        int nfields = mxGetNumberOfFields(var);
        MLPutFunction(link, CONTEXT "matStruct", 2);
        MLPutFunction(link, "List", len);
        for (int j=0; j < len; ++j) {
            MLPutFunction(link, "List", nfields);
            for (int i=0; i < nfields; ++i) {
                const char *fieldname;

                fieldname = mxGetFieldNameByNumber(var, i);
                MLPutFunction(link, "Rule", 2);
                MLPutString(link, fieldname);
                toMma(mxGetFieldByNumber(var, j, i), link);
            }
        }
        MLPutInteger32List(link, mmDims, depth);
    }
    // cell
    else if (mxIsCell(var)) {
        MLPutFunction(link, CONTEXT "matCell", 2);
        MLPutFunction(link, "List", len);
        for (int i=0; i < len; ++i)
            toMma(mxGetCell(var, i), link);
        MLPutInteger32List(link, mmDims, depth);
    }
    // unknown or failure; TODO distinguish between unknown and failure
    else
    {
        putUnknown(var, link);
    }
}
示例#19
0
文件: get.cpp 项目: Jornason/MATLink
void putUnknown(const mxArray *var, MLINK link) {
    const char *classname = mxGetClassName(var);
    MLPutFunction(link, CONTEXT "matUnknown", 1);
    MLPutString(link, classname);
}
示例#20
0
void sload(const char *fn, int *SZ, long SZlen) {

	uint16_t numberChannels;
	size_t k=0;
	size_t numberSamples;
	double samplerate;
	double *t;
	char *str = NULL;
#ifdef _WIN32
	long int sz[2];
#else
	size_t sz[2];
#endif
	biosig_data_type *data;
#ifdef __LIBBIOSIG2_H__
	size_t rowcol[2];
#endif

	HDRTYPE *hdr = constructHDR(0,0);

if (VERBOSE_LEVEL > 5)
	fprintf(stdout,"=== start sload ===\n");

/* contains [experiment,series,sweep,trace] numbers for selecting data. */

	while ((k < SZlen) && (k < 5)) {
#ifdef __LIBBIOSIG2_H__
		biosig_set_segment_selection(hdr, k+1, SZ[k]);
#else
		hdr->AS.SegSel[k] = (uint32_t)SZ[k];
#endif
		k++;
	}

	// ********* open file and read header ************
	hdr = sopen(fn, "r", hdr);
	if (serror2(hdr)) {
		destructHDR(hdr);
		fprintf(stdout,"Cannot open file <%s>\n", fn);
		return;
	}

#ifdef __LIBBIOSIG2_H__
	numberChannels = biosig_get_number_of_channels(hdr);
	numberSamples = biosig_get_number_of_samples(hdr);
	samplerate = biosig_get_samplerate(hdr);
	biosig_reset_flag(hdr, BIOSIG_FLAG_ROW_BASED_CHANNELS);
#else
	numberChannels = hdr->NS;
	numberSamples = hdr->NRec * hdr->SPR
	samplerate = hdr->SampleRate;
	hdr->FLAG.ROW_BASED_CHANNELS = 0;
#endif

if (VERBOSE_LEVEL > 5)
	fprintf(stdout,"open filename <%s>NoOfChans=%i\n", fn, numberChannels);

	// ********** read data ********************
	sread(NULL, 0, numberSamples, hdr);
	if (serror2(hdr)) {
		destructHDR(hdr);
		fprintf(stdout,"Error reading data from file <%s>\n", fn);
		return;
	}

#ifdef __LIBBIOSIG2_H__
	biosig_get_datablock(hdr, &data, &rowcol[0], &rowcol[1]);
	sz[0] = rowcol[1];
	sz[1] = rowcol[0];
#else
	sz[0] = hdr->data.size[1];
	sz[1] = hdr->data.size[0];
	data  = hdr->data.block;
#endif

	MLPutFunction(stdlink, "List", 3);
	// write data matrix
	MLPutRealArray(stdlink, data, sz, NULL, 2);

	// generate and write time axis
	t = (double*)malloc(numberSamples * sizeof(double));
	for (k=0; k < numberSamples;) {
		t[k] = (++k)/samplerate;
	}
	MLPutRealList(stdlink, t, numberSamples);
	free(t);

	// generate and write header information in JSON format
	asprintf_hdr2json(&str, hdr);
	MLPutString(stdlink, str);
	free(str);

if (VERBOSE_LEVEL > 5) {
	for (k=0; k<numberChannels; k++)
		fprintf(stdout,"%f ",data[k]);
		fprintf(stdout,"\n\nopen filename <%s>@%p sz=[%i,%i]\n", fn, data, sz[1],sz[0]);
	}

	// *********** close file *********************
	sclose(hdr);
	destructHDR(hdr);
	return;
}