1 /* This file is part of libDAI - http://www.libdai.org/
2 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
9 #include <iostream>
10 #include "mex.h"
11 #include <dai/matlab/matlab.h>
12 #include <dai/factor.h>
15 using namespace std;
16 using namespace dai;
19 /* Input Arguments */
21 #define PSI_IN prhs[0]
22 #define I_IN prhs[1]
23 #define J_IN prhs[2]
24 #define NR_IN 3
27 /* Output Arguments */
29 #define N_OUT plhs[0]
30 #define NR_OUT 1
33 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) {
34 size_t ilabel, jlabel;
36 // Check for proper number of arguments
37 if ((nrhs != NR_IN) || (nlhs != NR_OUT)) {
38 mexErrMsgTxt("Usage: N = dai_potstrength(psi,i,j);\n\n"
39 "\n"
40 "INPUT: psi = structure with a Member field and a P field, like a CPTAB.\n"
41 " i = label of a variable in psi.\n"
42 " j = label of another variable in psi.\n"
43 "\n"
44 "OUTPUT: N = strength of psi in direction i->j.\n");
45 }
47 // Get input parameters
48 Factor psi = mx2Factor(PSI_IN);
49 ilabel = (size_t)*mxGetPr(I_IN);
50 jlabel = (size_t)*mxGetPr(J_IN);
52 // Find variable in psi with label ilabel
53 Var i;
54 for( VarSet::const_iterator n = psi.vars().begin(); n != psi.vars().end(); n++ )
55 if( n->label() == ilabel ) {
56 i = *n;
57 break;
58 }
59 DAI_ASSERT( i.label() == ilabel );
61 // Find variable in psi with label jlabel
62 Var j;
63 for( VarSet::const_iterator n = psi.vars().begin(); n != psi.vars().end(); n++ )
64 if( n->label() == jlabel ) {
65 j = *n;
66 break;
67 }
68 DAI_ASSERT( j.label() == jlabel );
70 // Calculate N(psi,i,j);
71 double N = psi.strength( i, j );
73 // Hand over result to MATLAB
74 N_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
75 *(mxGetPr(N_OUT)) = N;
77 return;
78 }