// USAGE // [bestset,cond,cut,vol] = hkgrow_mex(A,set,t,eps,debugflag) // Note that targetvol is currently ignored void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { if (nrhs != 5) { mexErrMsgIdAndTxt("hkgrow_mex:notEnoughArguments", "hkgrow_mex needs six arguments not %i", nrhs); } debugflag = (int)mxGetScalar(prhs[4]); DEBUGPRINT(("hkgrow_mex: preprocessing start: \n")); const mxArray* mat = prhs[0]; const mxArray* set = prhs[1]; mxAssert(mxIsSparse(mat), "Input matrix is not sparse"); mxAssert(mxGetM(mat) == mxGetN(mat), "Input matrix not square"); mxArray* cond = mxCreateDoubleMatrix(1,1,mxREAL); mxArray* cut = mxCreateDoubleMatrix(1,1,mxREAL); mxArray* vol = mxCreateDoubleMatrix(1,1,mxREAL); if (nlhs > 1) { plhs[1] = cond; } if (nlhs > 2) { plhs[2] = cut; } if (nlhs > 3) { plhs[3] = vol; } mxAssert(nlhs <= 4, "Too many output arguments"); double eps = pow(10,-3); double t = 15.; if (nrhs >= 4) { t = mxGetScalar(prhs[2]); eps = mxGetScalar(prhs[3]); } sparserow r; r.m = mxGetM(mat); r.n = mxGetN(mat); r.ai = mxGetJc(mat); r.aj = mxGetIr(mat); r.a = mxGetPr(mat); std::vector< mwIndex > seeds; copy_array_to_index_vector( set, seeds ); DEBUGPRINT(("hkgrow_mex: preprocessing end: \n")); hkgrow(&r, seeds, t, eps, mxGetPr(cond), mxGetPr(cut), mxGetPr(vol) ); DEBUGPRINT(("hkgrow_mex: call to hkgrow() done\n")); if (nlhs > 0) { mxArray* cassign = mxCreateDoubleMatrix(seeds.size(),1,mxREAL); plhs[0] = cassign; double *ci = mxGetPr(cassign); for (size_t i=0; i<seeds.size(); ++i) { ci[i] = (double)(seeds[i] + 1); } } }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ int i; int nr, ns; double x1, x2; int flag; Vertexlist vtx; QuadraturePoints SP; double* vtxh; /*Get Input data */ if(nrhs != 4){ mexErrMsgTxt("Requires four input arguments"); } flag=(int)(mxGetScalar(prhs[0])); init_vtxlist(&vtx, mxGetM(prhs[3]), mxGetN(prhs[3])); vtxh=mxGetPr(prhs[3]); for(i=0; i<vtx.s1*vtx.s2; i++){ vtx.vtxlist[i%vtx.s1][i/vtx.s1]=vtxh[i]; } if(flag==0){ nr=(int)(mxGetScalar(prhs[1])); ns=(int)(mxGetScalar(prhs[2])); /*Get Quadrature rule */ SP=squad1d(vtx, nr, ns,0); } if(flag==3){ nr=(int)(mxGetScalar(prhs[1])); ns=(int)(mxGetScalar(prhs[2])); /*Get Quadrature rule */ SP=squad1d(vtx, nr, ns,1); } /* Create Output Arguments */ if(nlhs != 2){ mexErrMsgTxt("Requires two output argument\n"); } plhs[0] = (mxArray *) mxCreateDoubleMatrix(SP.n,SP.d,mxREAL); /*vector of the quadrature points*/ plhs[1] = (mxArray *) mxCreateDoubleMatrix(SP.n,1,mxREAL); /*vector of the quadrature weights*/ mxAssert(plhs[0] != NULL, "Out of memory"); mxAssert(plhs[1] != NULL, "Out of memory"); /* Copy the output */ { int i,j; double* r = mxGetPr(plhs[0]); for (j=0; j<SP.d; j++) { memcpy(&r[j*SP.n], SP.t[j], SP.n*sizeof(double)); } } memcpy(mxGetPr(plhs[1]), SP.wt, SP.n*sizeof(double)); free_qp(SP); return; }
/* ************************************************************ PROCEDURE mexFunction - Entry for Matlab ************************************************************ */ void mexFunction(const int nlhs, mxArray *plhs[], const int nrhs, const mxArray *prhs[]) { int i,j,nblk,k,nk,qDim; double *y; const double *d, *mu, *blkstartPr; int *blkstart; /* ------------------------------------------------------------ Check for proper number of arguments ------------------------------------------------------------ */ mxAssert(nrhs >= NPARIN, "qblkmul requires more input arguments."); mxAssert(nlhs <= NPAROUT, "qblkmul generates 1 output argument."); /* ------------------------------------------------------------ Get inputs d, mu, blkstart ------------------------------------------------------------ */ d = mxGetPr(D_IN); qDim = mxGetM(D_IN) * mxGetN(D_IN); nblk = mxGetM(MU_IN) * mxGetN(MU_IN); mu = mxGetPr(MU_IN); blkstartPr = mxGetPr(BLKSTART_IN); mxAssert(nblk == mxGetM(BLKSTART_IN) * mxGetN(BLKSTART_IN) - 1, "blkstart size mismatch."); /* ------------------------------------------------------------ Allocate int working array blkstart(nblk+1). ------------------------------------------------------------ */ blkstart = (int *) mxCalloc(nblk + 1, sizeof(int)); /* ------------------------------------------------------------ Convert Fortran double to C int ------------------------------------------------------------ */ for(i = 0; i <= nblk; i++){ j = blkstartPr[i]; /* double to int */ blkstart[i] = --j; } /* ------------------------------------------------------------ Let d point to Lorentz norm-bound ------------------------------------------------------------ */ if(qDim != blkstart[nblk] - blkstart[0]){ if(qDim == nblk + blkstart[nblk] - blkstart[0]){ d += nblk; /* Point to Lorentz norm-bound */ } else { mxAssert(qDim >= blkstart[nblk], "d size mismatch."); d += blkstart[0]; /* Point to Lorentz norm-bound */ } } qDim = blkstart[nblk] - blkstart[0]; /* ------------------------------------------------------------ Allocate output y(qDim) ------------------------------------------------------------ */ Y_OUT = mxCreateDoubleMatrix(qDim, 1, mxREAL); y = mxGetPr(Y_OUT); /* ------------------------------------------------------------ LORENTZ: yk = mu(k) * d[k] ------------------------------------------------------------ */ for(k = 0; k < nblk; k++){ nk = blkstart[k+1] - blkstart[k]; scalarmul(y,mu[k],d,nk); y += nk; d += nk; } }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { void *frame; int flen, len, r; void *code; uint32_T (*t)(void*); uint32_T res; const int dims[] = {1,1}; mxAssert(nrhs == 2, "bad call: use runx86(code,frame)"); len = (mxGetM(prhs[0]) * mxGetN(prhs[0])); /* code length */ code = mxGetData(prhs[0]); /* code address */ flen = (mxGetM(prhs[1]) * mxGetN(prhs[1])); /* frame length */ frame = mxGetData(prhs[1]); /* frame address */ t = (uint32_T (*)(void*))code; res = t(frame); /* into hyperspace */ mxAssert(nlhs<=1, "bad call: use rc = runx86(code,frame)"); plhs[0] = mxCreateNumericArray(2, dims, mxUINT32_CLASS, mxREAL); *(uint32_T*)mxGetData(plhs[0]) = res; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { const mwSize* dims; mwSize dims_[4]; char *CtC_err, *Ctd_err; mxAssert(nlhs==1,"Expects one output argument.\n"); mxAssert(nrhs==3,"Expects three input arguments.\n"); mxAssert(mxIsDouble(prhs[0]), "CtC must be a double array.\n"); CtC_err = "CtC must be num_bases*(num_bases+1)/2 x (patch_M/2 + 1) x patch_N.\n"; CtC_all = prhs[0]; dims = mxGetDimensions(CtC_all); num_bases = inv_tri(dims[0]); patch_M = 2*(dims[1]-1); if (mxGetNumberOfDimensions(CtC_all)==3){ patch_N = dims[2]; } else { patch_N = 1; } CtC_allr = mxGetPr(CtC_all); if (mxIsComplex(CtC_all)){ CtC_alli = mxGetPi(CtC_all); } mxAssert(mxIsDouble(prhs[1]), "Ctd must be a double array.\n"); Ctd_err = "Ctd must be num_bases x num_channels x (patch_M/2 + 1) x patch_N.\n"; Ctd_all = prhs[1]; dims = mxGetDimensions(Ctd_all); mxAssert(dims[0] == num_bases, Ctd_err); num_channels = dims[1]; mxAssert(dims[2] == patch_M/2+1, Ctd_err); if (mxGetNumberOfDimensions(Ctd_all)==4){ mxAssert(dims[3] == patch_N, Ctd_err); } else { mxAssert(1 == patch_N, Ctd_err); } Ctd_allr = mxGetPr(Ctd_all); if (mxIsComplex(Ctd_all)){ Ctd_alli = mxGetPi(Ctd_all); } mexPrintf("patch_M %d num_bases %d\n", patch_M, num_bases); mxAssert(mxIsDouble(prhs[2]), "lambda must be a double array.\n"); mxAssert(mxGetM(prhs[2])==num_bases && mxGetN(prhs[2])==1,"lambda must be num_bases x 1.\n"); lambda = mxGetPr(prhs[2]); dims_[0] = patch_M; dims_[1] = patch_N; dims_[2] = num_channels; dims_[3] = num_bases; plhs[0] = mxCreateNumericArray(4,dims_,mxDOUBLE_CLASS,mxCOMPLEX); A_freq_real = mxGetPr(plhs[0]); A_freq_imag = mxGetPi(plhs[0]); basis_solve(); }
inline DataT& operator()(size_t i, size_t j, size_t k) const { mxAssert(i < getSize<0>(), "ArrayAdapter::operator(): index for dimension 1 out of bounds."); mxAssert(i < getSize<1>(), "ArrayAdapter::operator(): index for dimension 2 out of bounds."); mxAssert(i < getSize<2>(), "ArrayAdapter::operator(): index for dimension 3 out of bounds."); return static_cast<const concrete_adaptor_t*>(this)-> mat_[i + getSize<0>() * (j + k * getSize<1>())]; }
/* ************************************************************ PROCEDURE sptriujcT - Compute starting positions for storing nonzeros of rows triu(X(i,:),1). Note: X is nxn block stored as subvector in a long vector. INPUT xir - integer subscript array of length xjc1. xjc0,xjc1 - xjc0 points to n x n sparse matrix X in xir. first - subscript of X(0,0); vec(X) is stored as subvector in xir. n - order of X. OUTPUT triujc - length n-1 integer array; triujc[i] is starting index for storing triu(X(i+1,:),1), i=0:n-2. It starts at triujc[0] = nnz(tril(X)). RETURNS nnz(X). Note that nnz(X) = triujc[n-2] + nnz(triu(X(n-1,:),1)) <= xjc1 - xjc0. ************************************************************ */ int sptriujcT(int *triujc, const int *xir, const int xjc0, const int xjc1, const int first,const int n) { int i,j,inz,jnz,jfirst,jlast; /* ------------------------------------------------------------ Observe that triu(X(n,:))=[] and hence only use triujc[0:n-2]. ------------------------------------------------------------ */ mxAssert(n > 0,""); memset(triujc, 0, (n-1) * sizeof(int)); jnz = 0; /* jnz = nnz(tril(X)) */ jlast = 0; /* index right after last activated column */ for(inz = xjc0; inz < xjc1; inz++){ /* ------------------------------------------------------------ Translate vec(x)-subscript into (i,j) subscript ------------------------------------------------------------ */ if((i = xir[inz]) >= jlast){ /* move to new z-column */ j = (i-first) / n; /* col j = floor( (i-first)/n ) */ if(j >= n) break; /* finished all columns of X */ jfirst = first + n * j; jlast = jfirst + n; } mxAssert(i >= jfirst,""); i -= jfirst; /* ------------------------------------------------------------ x_{i,j} with i < j --> count as nonzero in row i of triu(x) ------------------------------------------------------------ */ if(i < j) /* Upper triangular nonzero in row i */ ++triujc[i]; /* 0 <= i <= j-1 <= n-2 */ /* ------------------------------------------------------------ x_{i,j} with i >= j --> count as nonzero in tril(x). ------------------------------------------------------------ */ else{ ++jnz; } } /* ------------------------------------------------------------ Now jnz = nnz(tril(X)) and triujc[i] = nnz(triu(X(i+1,:),1)). We will now let triujc point to 1st nonzero position for storing jth row of triu(X,1) in row-wise format, as follows: [jnz, jnz + triujc[0], .., jnz + triujc[n-2]]. ------------------------------------------------------------ */ for(i = 0; i < n-1; i++){ j = triujc[i]; /* nnz(triu(X(i,:),1)) */ triujc[i] = jnz; /* position to store triu(X(i,:),1) */ jnz += j; /* point to next available position */ } /* ------------------------------------------------------------ Now jnz = triujc[n-1, NEW] + triujc[n-1,OLD] = nnz(X). ------------------------------------------------------------ */ mxAssert(jnz == inz - xjc0,""); return jnz; }
/* ************************************************************ PROCEDURE bwsolve -- Solve y from L'*y = b, where L is lower-triangular. INPUT Ljc, Lir, Lpr - sparse lower triangular matrix xsuper - starting column in L for each (dense) supernode. nsuper - number of super nodes UPDATED y - full xsuper[nsuper]-vector, yOUTPUT = L' \ yINPUT. WORKING ARRAY fwork - length max(collen[i] - superlen[i]) <= m-1, where collen[i] := L.jc[xsuper[i]+1]-L.jc[xsuper[i]] and superlen[i] := xsuper[i+1]-xsuper[i]. ************************************************************ */ void bwsolve(double *y, const mwIndex *Ljc, const mwIndex *Lir, const double *Lpr, const mwIndex *xsuper, const mwIndex nsuper, double *fwork) { mwIndex jsup,i,j,inz,k,jnnz; double yj; /* ------------------------------------------------------------ For each supernode jsup: ------------------------------------------------------------ */ j = xsuper[nsuper]; /* column after current snode (j=m)*/ for(jsup = nsuper; jsup > 0; jsup--){ i = j; mxAssert(j == xsuper[jsup],""); inz = Ljc[--j]; inz++; /* jump over diagonal entry */ if(j <= xsuper[jsup-1]){ /* ------------------------------------------------------------ If supernode is singleton j, then simply y[j] -= L(j+1:m,j)'*y(j+1:m) ------------------------------------------------------------ */ if(inz < Ljc[i]){ yj = Lpr[inz] * y[Lir[inz]]; for(++inz; inz < Ljc[i]; inz++) yj += Lpr[inz] * y[Lir[inz]]; y[j] -= yj; } } else{ /* ------------------------------------------------------------ For a "real" supernode: Let fwork = sparse(y(i:m)), then let y[j] -= L(i:m,j)'*fwork for all j in supernode ------------------------------------------------------------ */ for(jnnz = 0; inz < Ljc[i]; inz++) fwork[jnnz++] = y[Lir[inz]]; if(jnnz > 0) while(i > xsuper[jsup-1]){ yj = realdot(Lpr+Ljc[i]-jnnz, fwork, jnnz); mxAssert(i>0,""); y[--i] -= yj; } k = 1; do{ /* ------------------------------------------------------------ It remains to do a dense bwsolve on nodes j-1:-1:xsuper[jsup] The equation L(:,j)'*yNEW = yOLD(j), yields y(j) -= L(j+(1:k),j)'*y(j+(1:k)), k=1:i-xsuper[jsup]-1. ------------------------------------------------------------ */ mxAssert(j>0,""); --j; y[j] -= realdot(Lpr+Ljc[j]+1, y+j+1, k++); } while(j > xsuper[jsup-1]); } } }
/* MX_OVERRIDDEN */ void mx::FileStream::Flush(void) { mxAssert(IsOpen()); if (EOF == fflush(m_hFileDescriptor)) { // We cannot reach eof during write (check it). mxAssert(!feof(m_hFileDescriptor)); // File I/O error other than EOF. mxThrow(GenericIOException(ferror(m_hFileDescriptor))); } }
void fxLoadScript(txLinker* linker, txString path, txLinkerScript** link, FILE** fileAddress) { txLinkerScript* script = NULL; FILE* aFile = NULL; Atom atom; txByte version[4]; script = fxNewLinkerChunkClear(linker, sizeof(txLinkerScript)); script->path = fxNewLinkerString(linker, path, c_strlen(path)); aFile = fopen(path, "rb"); mxThrowElse(aFile); *fileAddress = aFile; mxThrowElse(fread(&atom, sizeof(atom), 1, aFile) == 1); atom.atomSize = ntohl(atom.atomSize); atom.atomType = ntohl(atom.atomType); mxAssert(atom.atomType == XS_ATOM_BINARY); mxThrowElse(fread(&atom, sizeof(atom), 1, aFile) == 1); atom.atomSize = ntohl(atom.atomSize); atom.atomType = ntohl(atom.atomType); mxAssert(atom.atomType == XS_ATOM_VERSION); mxThrowElse(fread(version, sizeof(version), 1, aFile) == 1); mxAssert(version[0] == XS_MAJOR_VERSION); mxAssert(version[1] == XS_MINOR_VERSION); mxAssert(version[2] == XS_PATCH_VERSION); mxAssert(version[3] != 1); mxThrowElse(fread(&atom, sizeof(atom), 1, aFile) == 1); atom.atomSize = ntohl(atom.atomSize); atom.atomType = ntohl(atom.atomType); mxAssert(atom.atomType == XS_ATOM_SYMBOLS); script->symbolsSize = atom.atomSize - sizeof(atom); script->symbolsBuffer = fxNewLinkerChunk(linker, script->symbolsSize); mxThrowElse(fread(script->symbolsBuffer, script->symbolsSize, 1, aFile) == 1); mxThrowElse(fread(&atom, sizeof(atom), 1, aFile) == 1); atom.atomSize = ntohl(atom.atomSize); atom.atomType = ntohl(atom.atomType); mxAssert(atom.atomType == XS_ATOM_CODE); script->codeSize = atom.atomSize - sizeof(atom); script->codeBuffer = fxNewLinkerChunk(linker, script->codeSize); mxThrowElse(fread(script->codeBuffer, script->codeSize, 1, aFile) == 1); if (version[3] == -1) { mxThrowElse(fread(&atom, sizeof(atom), 1, aFile) == 1); atom.atomSize = ntohl(atom.atomSize); atom.atomType = ntohl(atom.atomType); mxAssert(atom.atomType == XS_ATOM_HOSTS); script->hostsSize = atom.atomSize - sizeof(atom); script->hostsBuffer = fxNewLinkerChunk(linker, script->hostsSize); mxThrowElse(fread(script->hostsBuffer, script->hostsSize, 1, aFile) == 1); } fclose(aFile); *fileAddress = NULL; *link = script; }
void copy_array_to_index_vector(const mxArray* v, std::vector<mwIndex>& vec) { mxAssert(mxIsDouble(v), "array type is not double"); size_t n = mxGetNumberOfElements(v); double *p = mxGetPr(v); vec.resize(n); for (size_t i=0; i<n; ++i) { double elem = p[i]; mxAssert(elem >= 1, "Only positive integer elements allowed"); vec[i] = (mwIndex)elem - 1; } }
/* ************************************************************ PROCEDURE mexFunction - Entry for Matlab [delta,h,alpha] = iswnbr(vSQR,thetaSQR) Computes proximity "delta" w.r.t cregion C(theta). The projection of v onto C(theta) is (1-alpha)*max(h,v). ************************************************************ */ void mexFunction(const int nlhs, mxArray *plhs[], const int nrhs, const mxArray *prhs[]) { mxArray *myplhs[NPAROUT]; double *w,*pdelta,*ph,*palpha, *fwork; double thetaSQR; int i,n; /* ------------------------------------------------------------ Check for proper number of arguments ------------------------------------------------------------ */ mxAssert(nrhs >= NPARIN, "iswnbr requires more input arguments"); mxAssert(nlhs <= NPAROUT, "iswnbr produces less output arguments"); /* ------------------------------------------------------------ Get input vector w:=vSQR and cregion parameter thetaSQR ------------------------------------------------------------ */ w = mxGetPr(VSQR_IN); n = mxGetM(VSQR_IN) * mxGetN(VSQR_IN); thetaSQR = mxGetScalar(THETASQR_IN); /* ------------------------------------------------------------ Allocate output DELTA, H, ALPHA. ------------------------------------------------------------ */ DELTA_OUT = mxCreateDoubleMatrix(1, 1, mxREAL); pdelta = mxGetPr(DELTA_OUT); H_OUT = mxCreateDoubleMatrix(1, 1, mxREAL); ph = mxGetPr(H_OUT); ALPHA_OUT = mxCreateDoubleMatrix(1, 1, mxREAL); palpha = mxGetPr(ALPHA_OUT); /* ------------------------------------------------------------ Allocate working array fwork(n) ------------------------------------------------------------ */ fwork = (double *) mxCalloc(n,sizeof(double)); /* ------------------------------------------------------------ The actual job is done here:. ------------------------------------------------------------ */ *pdelta = getdelta(ph,palpha, w,thetaSQR,n, fwork); /* ------------------------------------------------------------ RELEASE WORKING ARRAY. ------------------------------------------------------------ */ mxFree(fwork); /* ------------------------------------------------------------ Copy requested output parameters (at least 1), release others. ------------------------------------------------------------ */ i = MAX(nlhs, 1); memcpy(plhs,myplhs, i * sizeof(mxArray *)); for(; i < NPAROUT; i++) mxDestroyArray(myplhs[i]); }
void add_ml_var(MLVars *ml_vars, mxArray *array, const char *vname) { _MLVar *node = mxMalloc(sizeof(_MLVar)); char *varName = mxMalloc(sizeof(char) * (strlen(vname)+1)); mxAssert(ml_vars, "NULL ML Variable list"); strcpy(varName, vname); /* Make the new node */ node->_array = array; node->_vname = varName; node->_next = NULL; DBG2(msg("add_new_ml_var: %s, node: %d\n",varName,node)); /* If the list is empty, this is the `base' */ if (ml_vars->_base == NULL) ml_vars->_base = node; /* If the list is not empty, link this to the end */ if (ml_vars->_end != NULL) ml_vars->_end->_next = node; /* Make this the end node */ ml_vars->_end = node; /* Do bookkeeping */ ml_vars->_length++; ml_vars->_current = node; }
char opt_getDoubleVectorFromOpt (const mxArray *opt, POptionSettings optSet, const char *name, size_t m, size_t n, double *dpointer) { size_t l; mxArray *field; mxAssert(m==1 || n==1,"getDoubleVectorFromOpt: m!=1 && n!=1"); field=mxGetField(opt,(mwIndex)0,name); if ((field==NULL) || (mxIsEmpty(field))) { if (optSet->warnMiss) mexPrintf("Option '%s' fehlt\n",name); return (char)1; } if (!mxIsDouble(field)) { if (optSet->warnType) mexPrintf("Option '%s' hat falschen Typ\n",name); return (char)2; } if ((mxGetM(field)!=(size_t)m) || (mxGetN(field)!=(size_t)n)) { if (optSet->warnSize) mexPrintf("Option '%s' hat falsche Größe\n"); return (char)3; } if (m>n) l=m; else l=n; memcpy(dpointer,mxGetPr(field),l*sizeof(double)); return (char)0; }
mxArray * next_ml_var(MLVars *ml_vars) { mxAssert(ml_vars, "NULL ML Variable list"); ml_vars->_current = ml_vars->_current->_next; return (ml_vars->_current) ? ml_vars->_current->_array : NULL; }
/* ************************************************************ PROCEDURE fwsolve -- Solve ynew from L*y = yold, where L is lower-triangular. INPUT L - sparse lower triangular matrix xsuper - starting column in L for each (dense) supernode. nsuper - number of super nodes UPDATED y - full vector, on input y = rhs, on output y = L\rhs. WORK fwork - length max(collen[i] - superlen[i]) <= m-1, where collen[i] := L.jc[xsuper[i]+1]-L.jc[xsuper[i]] and superlen[i] := xsuper[i+1]-xsuper[i]. ************************************************************ */ void fwsolve(double *y, const int *Ljc, const int *Lir, const double *Lpr, const int *xsuper, const int nsuper, double *fwork) { int jsup,i,j,inz,jnnz; double yi,yj; /* ------------------------------------------------------------ For each supernode jsup: ------------------------------------------------------------ */ j = xsuper[0]; /* 1st col of current snode (j=0)*/ inz = Ljc[0]; /* 1st nonzero in L (inz = 0) */ for(jsup = 1; jsup <= nsuper; jsup++){ /* ------------------------------------------------------------ The first equation, 1*y=b(j), yields y(j) = b(j). ------------------------------------------------------------ */ mxAssert(inz == Ljc[j],""); yj = y[j++]; ++inz; /* jump over diagonal entry */ if(j >= xsuper[jsup]) /* ------------------------------------------------------------ If supernode is singleton, then simply set y(j+1:m)-=yj*L(j+1:m,j) ------------------------------------------------------------ */ for(; inz < Ljc[j]; inz++) y[Lir[inz]] -= yj * Lpr[inz]; else{ /* ------------------------------------------------------------ Supernode contains multiple subnodes: Remember (i,yi) = 1st subnode, then perform dense forward solve within current supernode. ------------------------------------------------------------ */ i = j; yi = yj; do{ subscalarmul(y+j, yj, Lpr+inz, xsuper[jsup] - j); inz = Ljc[j]; yj = y[j++]; ++inz; /* jump over diagonal entry */ } while(j < xsuper[jsup]); jnnz = Ljc[j] - inz; /* ------------------------------------------------------------ jnnz = number of later entries that are influenced by this supernode. Compute the update in the array fwork(jnnz) ------------------------------------------------------------ */ if(jnnz > 0){ scalarmul(fwork, yj, Lpr+inz,jnnz); while(i < j){ addscalarmul(fwork,yi,Lpr+Ljc[i]-jnnz,jnnz); yi = y[i++]; } /* ------------------------------------------------------------ Update y with fwork at the specified sparse locations ------------------------------------------------------------ */ for(i = 0; i < jnnz; i++) y[Lir[inz++]] -= fwork[i]; } } } }
int opt_getIntFromOpt (const mxArray *opt, POptionSettings optSet, const char *name, int defaultValue) { mxArray *field; mxClassID classID; void *data; field=mxGetField(opt,(mwIndex)0,name); if ((field==NULL) || (mxIsEmpty(field))) { if (optSet->warnMiss) mexPrintf("Option '%s' fehlt, nehme default %i\n",name,defaultValue); return defaultValue; } classID=mxGetClassID(field); switch (classID) { case mxDOUBLE_CLASS: case mxINT8_CLASS: case mxUINT8_CLASS: case mxINT16_CLASS: case mxUINT16_CLASS: case mxINT32_CLASS: case mxUINT32_CLASS: case mxINT64_CLASS: case mxUINT64_CLASS: break; default: if (optSet->warnType) mexPrintf("Option '%s' hat falschen Typ, nehme default %i\n", name,defaultValue); return defaultValue; } if ((mxGetNumberOfDimensions(field)!=(mwSize)2) || (mxGetM(field)!=(size_t)1) || (mxGetN(field)!=(size_t)1)) { if (optSet->warnSize) mexPrintf("Option '%s' hat falsche Größe, nehme default %i\n", name,defaultValue); return defaultValue; } data=mxGetData(field); switch (classID) { case mxDOUBLE_CLASS: return (int)*((double*) data);break; case mxINT8_CLASS: return (int)*((int8_t*) data);break; case mxUINT8_CLASS: return (int)*((uint8_t*) data);break; case mxINT16_CLASS: return (int)*((int16_t*) data);break; case mxUINT16_CLASS: return (int)*((uint16_t*)data);break; case mxINT32_CLASS: return (int)*((int32_t*) data);break; case mxUINT32_CLASS: return (int)*((uint32_t*)data);break; case mxINT64_CLASS: return (int)*((int64_t*) data);break; case mxUINT64_CLASS: return (int)*((uint64_t*)data);break; default: return defaultValue; } mxAssert(0,"getIntFromOpt: should not happen"); return 0; }
void* operator new[](size_t size) { void *ptr = NULL; // mexWarnMsgTxt("Overloaded new[] operator"); ptr = mxMalloc(size); mxAssert(ptr!=NULL,"memory allocation error"); mexMakeMemoryPersistent(ptr); return ptr; }
char * get_mxarray_name(_MLVar *ml_vars) { mxAssert(ml_vars, "NULL ML Variable list"); if (ml_vars != NULL) return ml_vars->_vname; else return 0; }
/* ************************************************************ PROCEDURE mexFunction - Entry for Matlab y = vecsym(x,K) Computes "symmetrization of x: Yk = (Xk+Xk')/2 ************************************************************ */ void mexFunction(const int nlhs, mxArray *plhs[], const int nrhs, const mxArray *prhs[]) { mxArray *output_array[1], *Xk; coneK cK; int k, nk, nksqr, lqDim,lenfull; const double *x; double *y; /* ------------------------------------------------------------ Check for proper number of arguments ------------------------------------------------------------ */ mxAssert(nrhs >= 2, "vecsym requires 2 input arguments."); mxAssert(nlhs <= 1, "vecsym generates 1 output argument."); /* ------------------------------------------------------------ Disassemble cone K structure ------------------------------------------------------------ */ conepars(K_IN, &cK); /* ------------------------------------------------------------ Compute some statistics based on cone K structure ------------------------------------------------------------ */ lqDim = cK.lpN + cK.qDim; lenfull = lqDim + cK.rDim + cK.hDim; /* ------------------------------------------------------------ Get input vector x ------------------------------------------------------------ */ mxAssert(!mxIsSparse(X_IN), "x must be full."); mxAssert(mxGetM(X_IN) * mxGetN(X_IN) == lenfull, "Parameter `x' size mismatch."); x = mxGetPr(X_IN); /* ------------------------------------------------------------ Allocate output vector y, and make it vecsym(x) ------------------------------------------------------------ */ Y_OUT = mxCreateDoubleMatrix(lenfull, 1, mxREAL); y = mxGetPr(Y_OUT); memcpy(y,x,lqDim * sizeof(double)); x += lqDim; y += lqDim; vecsymPSD(y, x,cK.rsdpN,cK.sdpN,cK.sdpNL); }
/* ************************************************************ PROCEDURE mexFunction - Entry for Matlab ************************************************************ */ void mexFunction(const int nlhs, mxArray *plhs[], const int nrhs, const mxArray *prhs[]) { int i,k, nk; double *y; const double *d,*rdetd,*x; coneK cK; /* ------------------------------------------------------------ Check for proper number of arguments ------------------------------------------------------------ */ mxAssert(nrhs >= NPARIN, "qscaleK requires more input arguments."); mxAssert(nlhs <= NPAROUT, "qscaleK generates 1 output argument."); /* ------------------------------------------------------------ Disassemble cone K structure ------------------------------------------------------------ */ conepars(K_IN, &cK); /* ------------------------------------------------------------ Get scale data: (d,rdetd) and input x. ------------------------------------------------------------ */ mxAssert(mxGetM(D_IN) * mxGetN(D_IN) >= cK.lpN + cK.qDim, "d size mismatch"); d = mxGetPr(D_IN) + cK.lpN; /* skip LP part */ mxAssert(mxGetM(RDETD_IN) * mxGetN(RDETD_IN) == cK.lorN, "rdetx size mismatch"); rdetd = mxGetPr(RDETD_IN); mxAssert(mxGetM(X_IN) * mxGetN(X_IN) == cK.qDim, "x size mismatch"); x = mxGetPr(X_IN); /* ------------------------------------------------------------ Allocate output Y ------------------------------------------------------------ */ Y_OUT = mxCreateDoubleMatrix(cK.qDim, 1, mxREAL); y = mxGetPr(Y_OUT); /* ------------------------------------------------------------ The actual job is done here: y=D(d)x, Lorentz part. ------------------------------------------------------------ */ for(k = 0; k < cK.lorN; k++){ /* LORENTZ */ nk = cK.lorNL[k]; qlmul(y, d,x,rdetd[k],nk); y += nk; x += nk; d += nk; } }
/* ************************************************************** */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { char *g; /* M char array data reformatted for C */ int oN, iN, gN; /* lengths of M char array data */ int f; int* fp; /* flag bits */ int idx; short *pr; /* for M mxChar type */ mwSize dims[2]; /* check inputs */ mxAssert(nrhs == 2 || nrhs == 3, "bad call: use gem(i,g) or gem(i,g,f)"); mxAssert(nlhs<=1, "bad call: use o = gem(i,g)"); /* grab user input text */ mxAssert(mxIsChar(prhs[0]), "bad call 1rst arg: requires char"); iN = mxGetM(prhs[0])*mxGetN(prhs[0]); mxAssert(iN<DATALIM, "bad call 1rst arg: input is too long"); pr = (short*)mxGetData(prhs[0]); for (idx=0; idx<iN; idx++) input[idx] = (unsigned char)*(pr+idx)&0xFF; input[iN] = 0; /* null terminate */ i = input; /* the input */ /* grab user grammar */ mxAssert(mxIsChar(prhs[1]), "bad call 2nd arg: requires char"); gN = mxGetM(prhs[1])*mxGetN(prhs[1]); mxAssert(gN<DATALIM, "bad call 2nd arg: input is too long"); pr = (short*)mxGetData(prhs[1]); for (idx=0; idx<gN; idx++) code[idx] = (unsigned char)*(pr+idx)&0xFF; code[gN] = 0; /* null terminate */ g = code; /* the code */ /* grab flags */ if (nrhs == 3) { mxAssert(mxIsUint32(prhs[2]), "bad call 3rd arg: requires int32"); fp = (int*)mxGetData(prhs[2]); /* flag bits */ f = *fp; } else { f = 0; /* no flags */ } pregem(i, iN, g, gN, f); /* set up globals */ gem(); /* translate */ /* push output */ oN = o-output+1; /* number of output chars */ o = output; dims[0] = 1; dims[1] = oN; plhs[0] = mxCreateCharArray(2, (const mwSize*)&dims); pr = (short*)mxGetData(plhs[0]); /* 16 bits per char */ for (idx=0; idx<oN; idx++) *(pr+idx) = (*o++)&0xFF; }
/* ************************************************************ PROCEDURE bwprodform - Solves (PROD_j L(pj,betaj))' * yNEW = yOLD. INPUT p - nonzeros of sparse m x n matrix P. Has xsuper(j+1) nonzeros in column j. xsuper - xsuper(j+1) is number of nonzeros in p(:,j). perm - lists pivot order for columns where ordered(j)==1. ordered - ordered[j]==1 iff p(:,j) and beta(L:,j) have been reordered; the original row numbers are in perm(:,j). n - number of columns in p, beta. UPDATED y - length m vector. On input, the rhs. On output the solution to (PROD_j L(pj,betaj))' * yNEW = yOLD. ************************************************************ */ void bwprodform(double *y, const int *xsuper, const int *perm, const double *p, const double *beta, const int *betajc, const char *ordered, const int n, int pnnz, int permnnz) { int k,nk, mk; /* ------------------------------------------------------------ Backward solve L(pk,betak) * yNEXT = yPREV for k=n-1:-1:0. ------------------------------------------------------------ */ for(k = n-1; k >= 0; k--){ mk = xsuper[k+1]; nk = betajc[k+1] - betajc[k]; pnnz -= mk; if(ordered[k]){ permnnz -= mk; bwipr1o(y, perm+permnnz, p+pnnz, beta+betajc[k], mk, nk); } else bwipr1(y, p+pnnz, beta+betajc[k], mk, nk); } mxAssert(pnnz == 0,""); mxAssert(permnnz == 0 || permnnz == 1,""); }
/* ************************************************************ PROCEDURE bwprodform - Solves (PROD_j L(pj,betaj))' * yNEW = yOLD. INPUT p - nonzeros of sparse m x n matrix P. Has xsuper(j+1) nonzeros in column j. xsuper - xsuper(j+1) is number of nonzeros in p(:,j). perm - lists pivot order for columns where ordered(j)==1. ordered - ordered[j]==1 iff p(:,j) and beta(L:,j) have been reordered; the original row numbers are in perm(:,j). n - number of columns in p, beta. UPDATED y - length m vector. On input, the rhs. On output the solution to (PROD_j L(pj,betaj))' * yNEW = yOLD. ************************************************************ */ void bwprodform(double *y, const mwIndex *xsuper, const mwIndex *perm, const double *p, const double *beta, const mwIndex *betajc, const char *ordered, const mwIndex n, mwIndex pnnz, mwIndex permnnz) { mwIndex k,nk, mk; /* ------------------------------------------------------------ Backward solve L(pk,betak) * yNEXT = yPREV for k=n-1:-1:0. ------------------------------------------------------------ */ for(k = n; k > 0; k--){ mk = xsuper[k]; nk = betajc[k] - betajc[k-1]; pnnz -= mk; if(ordered[k-1]){ permnnz -= mk; bwipr1o(y, perm+permnnz, p+pnnz, beta+betajc[k-1], mk, nk); } else bwipr1(y, p+pnnz, beta+betajc[k-1], mk, nk); } mxAssert(pnnz == 0,""); mxAssert(permnnz == 0 || permnnz == 1,""); }
/* ************************************************************ PROCEDURE mexFunction - Entry for Matlab ************************************************************ */ void mexFunction(const int nlhs, mxArray *plhs[], const int nrhs, const mxArray *prhs[]) { int i,k, nk; double *y; const double *x,*qdetx; coneK cK; /* ------------------------------------------------------------ Check for proper number of arguments ------------------------------------------------------------ */ mxAssert(nrhs >= NPARIN, "qinvsqrt requires more input arguments."); mxAssert(nlhs <= NPAROUT, "qinvsqrt generates less output arguments."); /* ------------------------------------------------------------ Disassemble cone K structure ------------------------------------------------------------ */ conepars(K_IN, &cK); /* ------------------------------------------------------------ Get inputs x, qdetx ------------------------------------------------------------ */ mxAssert(mxGetM(X_IN) * mxGetN(X_IN) >= cK.lpN + cK.qDim, "x size mismatch"); x = mxGetPr(X_IN) + cK.lpN; /* skip LP part */ mxAssert(mxGetM(QDETX_IN) * mxGetN(QDETX_IN) == cK.lorN, "qdetx size mismatch"); qdetx = mxGetPr(QDETX_IN); /* ------------------------------------------------------------ Allocate output y ------------------------------------------------------------ */ Y_OUT = mxCreateDoubleMatrix(cK.qDim, 1, mxREAL); y = mxGetPr(Y_OUT); /* ------------------------------------------------------------ The actual job is done here: y = w^{-1/2}, Lorentz part. ------------------------------------------------------------ */ for(k = 0; k < cK.lorN; k++){ /* LORENTZ */ nk = cK.lorNL[k]; powminhalf(y, x,qdetx[k],nk); y += nk; x += nk; } }
double* mat2fort( const mxArray *X, int ldz, int ndz ) { int i,j,m,n,incz,cmplxflag; double *Z,*xr,*xi,*zp; Z = (double *) mxCalloc(2*ldz*ndz, sizeof(double)); xr = mxGetPr(X); xi = mxGetPi(X); mxAssert(mxGetM(X)<INT_MAX,"Matrix is too large for 32-bit FORTRAN"); mxAssert(mxGetN(X)<INT_MAX,"Matrix is too large for 32-bit FORTRAN"); m = (int)mxGetM(X); n = (int)mxGetN(X); zp = Z; incz = 2*(ldz-m); cmplxflag = (xi != NULL); for (j = 0; j < n; j++) { if (cmplxflag) { for (i = 0; i < m; i++) { *zp++ = *xr++; *zp++ = *xi++; } } else { for (i = 0; i < m; i++) { *zp++ = *xr++; zp++; } } zp += incz; } return(Z); }
/* MX_OVERRIDDEN */ mx::Size mx::FileStream::PrintfV( const Char * const sFormat, va_list pArguments) { mxAssert(IsOpen()); mxAssert(sFormat != NULL); int iCharsWritten; if ((iCharsWritten = #ifndef MXCPP_UNICODE ::vfprintf #else ::vfwprintf #endif (m_hFileDescriptor, sFormat, pArguments)) < 0) { // We cannot reach eof during write (check it). mxAssert(!feof(m_hFileDescriptor)); // File I/O error other than EOF. mxThrow(GenericIOException(ferror(m_hFileDescriptor))); } return iCharsWritten; }
/* ************************************************************ PROCEDURE qxqt - computes tril(Qb * X * Qb') Here, Qb = Q_1*Q_2*..*Q_{m-1}, where each Q_i is a Householder reflection. (Qb is from a Qb * R decomposition.) INPUT beta - length m vector c - m x m matrix, lower triangular gives Householder reflections m - order UPDATED x - m x m. On output, Xnew = Qb * X * Qb' This means: start with order 2 reflection, up to order m reflection. WORK fwork - length m working vector. ************************************************************ */ void qxqt(double *x, const double *beta, const double *c, const mwIndex m, double *fwork) { mwIndex k, inz; mxAssert(m>1,""); inz = SQR(m) - (m+2); /* ------------------------------------------------------------ For each k, c[inz] = c(k,k), the top of the lower-right block, x[k] is start of k-th row in k. ------------------------------------------------------------ */ for(k = m-1; k > 0; k--, inz -= m+1) { elqxq(x + k-1, -beta[k-1], c + inz, k-1, m, fwork); } /*elqxq(x , -beta[0], c , 0, m, fwork);*/ }
/* This function should free the nodes, but not the things they point to. */ void clear_ml_vars(MLVars *ml_vars) { _MLVar *next = NULL; _MLVar *node = NULL; mxAssert(ml_vars, "NULL ML Variable list"); node = ml_vars->_base; while (node) { next = node->_next; mxFree(node); node = next; } mxFree(ml_vars); }
DataType computeTentativeValue(int i, int j, std::map<Coordinate, DataType> & accepted) { const DataType inf = std::numeric_limits<DataType>::max(); typename std::map<Coordinate, DataType>::const_iterator iter; // Fetch (i-1,j) (returns infinity if not accepted) iter = accepted.find(Coordinate(i-1,j)); DataType im = (iter == accepted.end() ? inf : (*iter).second); // Fetch (i+1,j) iter = accepted.find(Coordinate(i+1,j)); DataType ip = (iter == accepted.end() ? inf : (*iter).second); // Fetch (i,j-1) iter = accepted.find(Coordinate(i,j-1)); DataType jm = (iter == accepted.end() ? inf : (*iter).second); // Fetch (i,j+1) iter = accepted.find(Coordinate(i,j+1)); DataType jp = (iter == accepted.end() ? inf : (*iter).second); // Get the minimum along each dimension DataType f1 = std::min(im, ip); DataType f2 = std::min(jm, jp); // Sort the values in ascending order if (f1 > f2) std::swap(f1,f2); mxAssert(f1 != inf, "Invalid tentative point - no accepted neighbors"); // Case 1: We have accepted values only along one dimension if (f2 == inf) return f1 + 1; // Case 2: We have accepted values along both dimensions DataType b = f1 + f2; //DataType root = b*b - 2*(f1*f1 + f2*f2 - 1); DataType a = f1 - f2; DataType root = 2 - a * a; if (root < 0) root = 0; return (b + std::sqrt(root))*0.5; }