383c0f9b393840624eaeb8922785ef056b9eb9c6
[libdai.git] / matlab / dai_potstrength.cpp
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 /*=================================================================*
23 * *
24 * This is a MEX-file for MATLAB. *
25 * *
26 * N = dai_potstrength(psi,i,j); *
27 * *
28 *=================================================================*/
29
30
31 #include <iostream>
32 #include "mex.h"
33 #include "matlab.h"
34 #include "../factor.h"
35
36
37 using namespace std;
38
39
40 /* Input Arguments */
41
42 #define PSI_IN prhs[0]
43 #define I_IN prhs[1]
44 #define J_IN prhs[2]
45 #define NR_IN 3
46
47
48 /* Output Arguments */
49
50 #define N_OUT plhs[0]
51 #define NR_OUT 1
52
53
54 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
55 {
56 long ilabel, jlabel;
57
58 // Check for proper number of arguments
59 if ((nrhs != NR_IN) || (nlhs != NR_OUT)) {
60 mexErrMsgTxt("Usage: N = dai_potstrength(psi,i,j);\n\n"
61 "\n"
62 "INPUT: psi = structure with a Member field and a P field, like a CPTAB.\n"
63 " i = label of a variable in psi.\n"
64 " j = label of another variable in psi.\n"
65 "\n"
66 "OUTPUT: N = strength of psi in direction i->j.\n");
67 }
68
69 // Get input parameters
70 Factor psi = mx2Factor(PSI_IN);
71 ilabel = (long)*mxGetPr(I_IN);
72 jlabel = (long)*mxGetPr(J_IN);
73
74 // Find variable in psi with label ilabel
75 Var i;
76 for( VarSet::const_iterator n = psi.vars().begin(); n != psi.vars().end(); n++ )
77 if( n->label() == ilabel ) {
78 i = *n;
79 break;
80 }
81 assert( i.label() == ilabel );
82
83 // Find variable in psi with label jlabel
84 Var j;
85 for( VarSet::const_iterator n = psi.vars().begin(); n != psi.vars().end(); n++ )
86 if( n->label() == jlabel ) {
87 j = *n;
88 break;
89 }
90 assert( j.label() == jlabel );
91
92 // Calculate N(psi,i,j);
93 double N = psi.strength( i, j );
94
95 // Hand over result to MATLAB
96 N_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
97 *(mxGetPr(N_OUT)) = N;
98
99
100 return;
101 }