void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* Variables */ int i,n, s, f, e, n1, n2, s1, s2, nInstances,nNodes, nEdges, nNodeFeatures, nEdgeFeatures, *nStates, maxState, *nodeMap, *edgeMap, *edgeEnds, dims[3]; double *w, *Xnode, *Xedge, *nodePot, *edgePot; /* Input */ w = mxGetPr(prhs[0]); Xnode = mxGetPr(prhs[1]); Xedge = mxGetPr(prhs[2]); nodeMap = (int*)mxGetPr(prhs[3]); edgeMap = (int*)mxGetPr(prhs[4]); nStates = (int*)mxGetPr(prhs[5]); edgeEnds = (int*)mxGetPr(prhs[6]); i = (int)mxGetScalar(prhs[7]); i--; if (!mxIsClass(prhs[3],"int32")||!mxIsClass(prhs[4],"int32")||!mxIsClass(prhs[5],"int32")||!mxIsClass(prhs[6],"int32")||!mxIsClass(prhs[7],"int32")) mexErrMsgTxt("edgeEnds, nStates, nodeMap, edgeMap, i must be int32"); /* Compute Sizes */ nNodes = mxGetDimensions(prhs[3])[0]; nEdges = mxGetDimensions(prhs[6])[0]; nInstances = mxGetDimensions(prhs[1])[0]; nNodeFeatures = mxGetDimensions(prhs[1])[1]; nEdgeFeatures = mxGetDimensions(prhs[2])[1]; maxState = getMaxState(nStates, nNodes); /*printf("%d,%d,%d,%d,%d\n", nNodes, nEdges, nNodeFeatures, nEdgeFeatures, maxState);*/ /*printf("%d,%d\n",nInstances,i);*/ /* Make output */ plhs[0] = mxCreateDoubleMatrix(nNodes,maxState,mxREAL); nodePot = mxGetPr(plhs[0]); dims[0] = maxState; dims[1] = maxState; dims[2] = nEdges; plhs[1] = mxCreateNumericArray(3, dims, mxDOUBLE_CLASS, mxREAL); edgePot = mxGetPr(plhs[1]); for(n = 0; n < nNodes; n++) { for(s = 0; s < nStates[n]; s++) { for(f = 0; f < nNodeFeatures; f++) { if(nodeMap[n + nNodes*(s + maxState*f)] > 0) { nodePot[n+nNodes*s] += w[nodeMap[n+nNodes*(s+maxState*f)]-1]*Xnode[i + nInstances*(f + nNodeFeatures*n)]; } } nodePot[n+nNodes*s] = exp(nodePot[n+nNodes*s]); } } for(e = 0; e < nEdges; e++) { n1 = edgeEnds[e]-1; n2 = edgeEnds[e+nEdges]-1; for (s1 = 0; s1 < nStates[n1]; s1++) { for (s2 = 0; s2 < nStates[n2]; s2++) { for (f = 0; f < nEdgeFeatures; f++) { if (edgeMap[s1 + maxState*(s2 + maxState*(e + nEdges*f))] > 0) { edgePot[s1+maxState*(s2+maxState*e)] += w[edgeMap[s1+maxState*(s2+maxState*(e+nEdges*f))]-1]*Xedge[i + nInstances*(f+nEdgeFeatures*e)]; } } edgePot[s1+maxState*(s2+maxState*e)] = exp(edgePot[s1+maxState*(s2+maxState*e)]); } } } }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* Variables */ int n, s, f, n1, n2, s1, s2, i,e, nInstances, nNodes, nNodeFeatures, nEdges, maxState, nEdgeFeatures, *edgeEnds, *nStates, *nodeMap, *edgeMap, *Y; double obs, *g, *nodeBel, *edgeBel, *Xnode, *Xedge; /* Input */ g = mxGetPr(prhs[0]); i = (int)mxGetScalar(prhs[1])-1; nodeBel = mxGetPr(prhs[2]); edgeBel = mxGetPr(prhs[3]); edgeEnds = (int*)mxGetPr(prhs[4]); nStates = (int*)mxGetPr(prhs[5]); nodeMap = (int*)mxGetPr(prhs[6]); edgeMap = (int*)mxGetPr(prhs[7]); Xnode = mxGetPr(prhs[8]); Xedge = mxGetPr(prhs[9]); Y = (int*)mxGetPr(prhs[10]); if (!mxIsClass(prhs[1],"int32")||!mxIsClass(prhs[4],"int32")||!mxIsClass(prhs[5],"int32")||!mxIsClass(prhs[6],"int32")||!mxIsClass(prhs[7],"int32")||!mxIsClass(prhs[10],"int32")) mexErrMsgTxt("edgeEnds, nStates, nodeMap, edgeMap, i, Y must be int32"); /* Compute Sizes */ nInstances = mxGetDimensions(prhs[10])[0]; nNodeFeatures = mxGetDimensions(prhs[8])[1]; nNodes = mxGetDimensions(prhs[2])[0]; nEdgeFeatures = mxGetDimensions(prhs[9])[1]; nEdges = mxGetDimensions(prhs[4])[0]; maxState = getMaxState(nStates, nNodes); for(n = 0; n < nNodes; n++) { for(s = 0; s < nStates[n]; s++) { for(f = 0; f < nNodeFeatures; f++) { if(nodeMap[n + nNodes*(s + maxState*f)] > 0) { if(s == Y[i + nInstances*n]-1) { obs = 1; } else { obs = 0; } g[nodeMap[n + nNodes*(s + maxState*f)]-1] += Xnode[i + nInstances*(f + nNodeFeatures*n)]*(nodeBel[n + nNodes*s] - obs); } } } } for(e = 0; e < nEdges; e++) { n1 = edgeEnds[e]-1; n2 = edgeEnds[e+nEdges]-1; for (s1 = 0; s1 < nStates[n1]; s1++) { for (s2 = 0; s2 < nStates[n2]; s2++) { for (f = 0; f < nEdgeFeatures; f++) { if (edgeMap[s1 + maxState*(s2 + maxState*(e + nEdges*f))] > 0) { if (s1 == Y[i + nInstances*n1]-1 && s2 == Y[i + nInstances*n2]-1) { obs = 1; } else { obs = 0; } g[edgeMap[s1 + maxState*(s2 + maxState*(e + nEdges*f))]-1] += Xedge[i + nInstances*(f + nEdgeFeatures*e)]*(edgeBel[s1 + maxState*(s2 + maxState*e)] - obs); } } } } } }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* Variables */ int n, n1, n2, p, e, s, s1, s2, nNodeFeatures, nEdgeFeatures, maxState, nNodes, nEdges, *edgeEnds, *nStates, tieNodes,tieEdges, ising; double temp, *gw, *gv, *X, *Xedge, *y, observed, expected, *nodeBel, *edgeBel; /* Input */ gw = mxGetPr(prhs[0]); gv = mxGetPr(prhs[1]); X = mxGetPr(prhs[2]); Xedge = mxGetPr(prhs[3]); y = mxGetPr(prhs[4]); nodeBel = mxGetPr(prhs[5]); edgeBel = mxGetPr(prhs[6]); nStates = mxGetPr(prhs[7]); tieNodes = mxGetScalar(prhs[8]); tieEdges = mxGetScalar(prhs[9]); ising = mxGetScalar(prhs[10]); edgeEnds = mxGetPr(prhs[11]); /* Compute Sizes */ nNodeFeatures = mxGetDimensions(prhs[2])[1]; nNodes = mxGetDimensions(prhs[2])[2]; nEdgeFeatures = mxGetDimensions(prhs[3])[1]; nEdges = mxGetDimensions(prhs[11])[0]; maxState = getMaxState(nStates,nNodes); decrementEdgeEnds(edgeEnds,nEdges); for(n = 0; n < nNodes; n++) y[n]--; /* Update gw */ for(n = 0; n < nNodes; n++) { for(s = 0; s < nStates[n]-1; s++) { if (s == y[n]) { observed = 1; } else { observed = 0; } expected = nodeBel[n + nNodes*s]; for(p = 0; p < nNodeFeatures; p++) { if (tieNodes) { gw[p + nNodeFeatures*s] -= (observed - expected)*X[p+nNodeFeatures*n]; } else { gw[p + nNodeFeatures*(s + (maxState-1)*n)] -= (observed - expected)*X[p+nNodeFeatures*n]; } } } } /* Update gv */ for(e = 0; e < nEdges; e++) { n1 = edgeEnds[e]; n2 = edgeEnds[e+nEdges]; if (ising == 2) { for(s = 0; s < nStates[n1] && s < nStates[n2]; s++) { if (y[n1] == s && y[n2] == s) { observed = 1; } else { observed = 0; } expected = edgeBel[s + maxState*(s + maxState*e)]; for (p = 0; p < nEdgeFeatures; p++) { if (tieEdges) { gv[p+nEdgeFeatures*s] -= (observed-expected)*Xedge[p + nEdgeFeatures*e]; } else { gv[p+nEdgeFeatures*(s+maxState*e)] -= (observed-expected)*Xedge[p + nEdgeFeatures*e]; } } } } else if (ising) { if (y[n1] == y[n2]) { observed = 1; } else { observed = 0; } expected = 0; for (s = 0; s < maxState; s++) { expected += edgeBel[s + maxState*(s + maxState*e)]; } for (p = 0; p < nEdgeFeatures; p++) { if (tieEdges) { gv[p] -= (observed-expected)*Xedge[p + nEdgeFeatures*e]; } else { gv[p+nEdgeFeatures*e] -= (observed-expected)*Xedge[p + nEdgeFeatures*e]; } } } else /* (~ising) */ { for (s1 = 0; s1 < nStates[n1]; s1++) { for (s2 = 0; s2 < nStates[n2]; s2++) { if (s1 == nStates[n1]-1 && s2 == nStates[n2]-1) { continue; } if (s1 == y[n1] && s2 == y[n2]) { observed = 1; } else { observed = 0; } expected = edgeBel[s1 + maxState*(s2 + maxState*e)]; s = s1+s2*maxState; for(p = 0; p < nEdgeFeatures; p++) { if (tieEdges) { gv[p + nEdgeFeatures*s] -= (observed-expected)*Xedge[p + nEdgeFeatures*e]; } else { gv[p + nEdgeFeatures*(s + (maxState*maxState-1)*e)] -= (observed-expected)*Xedge[p + nEdgeFeatures*e]; } } } } } } }