Merged tests/*, matlab/*, utils/* from SVN head...
[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 <dai/matlab/matlab.h>
34 #include <dai/factor.h>
35
36
37 using namespace std;
38 using namespace dai;
39
40
41 /* Input Arguments */
42
43 #define PSI_IN prhs[0]
44 #define I_IN prhs[1]
45 #define J_IN prhs[2]
46 #define NR_IN 3
47
48
49 /* Output Arguments */
50
51 #define N_OUT plhs[0]
52 #define NR_OUT 1
53
54
55 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
56 {
57 long ilabel, jlabel;
58
59 // Check for proper number of arguments
60 if ((nrhs != NR_IN) || (nlhs != NR_OUT)) {
61 mexErrMsgTxt("Usage: N = dai_potstrength(psi,i,j);\n\n"
62 "\n"
63 "INPUT: psi = structure with a Member field and a P field, like a CPTAB.\n"
64 " i = label of a variable in psi.\n"
65 " j = label of another variable in psi.\n"
66 "\n"
67 "OUTPUT: N = strength of psi in direction i->j.\n");
68 }
69
70 // Get input parameters
71 Factor psi = mx2Factor(PSI_IN);
72 ilabel = (long)*mxGetPr(I_IN);
73 jlabel = (long)*mxGetPr(J_IN);
74
75 // Find variable in psi with label ilabel
76 Var i;
77 for( VarSet::const_iterator n = psi.vars().begin(); n != psi.vars().end(); n++ )
78 if( n->label() == ilabel ) {
79 i = *n;
80 break;
81 }
82 assert( i.label() == ilabel );
83
84 // Find variable in psi with label jlabel
85 Var j;
86 for( VarSet::const_iterator n = psi.vars().begin(); n != psi.vars().end(); n++ )
87 if( n->label() == jlabel ) {
88 j = *n;
89 break;
90 }
91 assert( j.label() == jlabel );
92
93 // Calculate N(psi,i,j);
94 double N = psi.strength( i, j );
95
96 // Hand over result to MATLAB
97 N_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
98 *(mxGetPr(N_OUT)) = N;
99
100 return;
101 }