bcc49057474057b716aa9d0b99bc171c59d8abfa
[libdai.git] / src / factor.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2010 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <dai/factor.h>
13
14
15 namespace dai {
16
17
18 using namespace std;
19
20
21 Factor createFactorIsing( const Var &n, Real h ) {
22 DAI_ASSERT( n.states() == 2 );
23 Real buf[2];
24 buf[0] = std::exp(-h);
25 buf[1] = std::exp(h);
26 return Factor(n, &buf[0]);
27 }
28
29
30 Factor createFactorIsing( const Var &n1, const Var &n2, Real J ) {
31 DAI_ASSERT( n1.states() == 2 );
32 DAI_ASSERT( n2.states() == 2 );
33 DAI_ASSERT( n1 != n2 );
34 Real buf[4];
35 buf[0] = (buf[3] = std::exp(J));
36 buf[1] = (buf[2] = std::exp(-J));
37 return Factor( VarSet(n1, n2), &buf[0] );
38 }
39
40
41 Factor createFactorExpGauss( const VarSet &ns, Real beta ) {
42 Factor fac( ns );
43 for( size_t t = 0; t < fac.nrStates(); t++ )
44 fac.set( t, std::exp(rnd_stdnormal() * beta) );
45 return fac;
46 }
47
48
49 Factor createFactorPotts( const Var &n1, const Var &n2, Real J ) {
50 Factor fac( VarSet( n1, n2 ), 1.0 );
51 DAI_ASSERT( n1.states() == n2.states() );
52 for( size_t s = 0; s < n1.states(); s++ )
53 fac.set( s * (n1.states() + 1), std::exp(J) );
54 return fac;
55 }
56
57
58 Factor createFactorDelta( const Var &v, size_t state ) {
59 Factor fac( v, 0.0 );
60 DAI_ASSERT( state < v.states() );
61 fac.set( state, 1.0 );
62 return fac;
63 }
64
65
66 } // end of namespace dai