New git HEAD version
[libdai.git] / src / factor.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <dai/factor.h>
10
11
12 namespace dai {
13
14
15 using namespace std;
16
17
18 Factor createFactorIsing( const Var &n, Real h ) {
19 DAI_ASSERT( n.states() == 2 );
20 Real buf[2];
21 buf[0] = std::exp(-h);
22 buf[1] = std::exp(h);
23 return Factor(n, &buf[0]);
24 }
25
26
27 Factor createFactorIsing( const Var &n1, const Var &n2, Real J ) {
28 DAI_ASSERT( n1.states() == 2 );
29 DAI_ASSERT( n2.states() == 2 );
30 DAI_ASSERT( n1 != n2 );
31 Real buf[4];
32 buf[0] = (buf[3] = std::exp(J));
33 buf[1] = (buf[2] = std::exp(-J));
34 return Factor( VarSet(n1, n2), &buf[0] );
35 }
36
37
38 Factor createFactorExpGauss( const VarSet &ns, Real beta ) {
39 Factor fac( ns );
40 for( size_t t = 0; t < fac.nrStates(); t++ )
41 fac.set( t, std::exp(rnd_stdnormal() * beta) );
42 return fac;
43 }
44
45
46 Factor createFactorPotts( const Var &n1, const Var &n2, Real J ) {
47 Factor fac( VarSet( n1, n2 ), 1.0 );
48 DAI_ASSERT( n1.states() == n2.states() );
49 for( size_t s = 0; s < n1.states(); s++ )
50 fac.set( s * (n1.states() + 1), std::exp(J) );
51 return fac;
52 }
53
54
55 Factor createFactorDelta( const Var &v, size_t state ) {
56 Factor fac( v, 0.0 );
57 DAI_ASSERT( state < v.states() );
58 fac.set( state, 1.0 );
59 return fac;
60 }
61
62
63 Factor createFactorDelta( const VarSet& vs, size_t state ) {
64 Factor fac( vs, 0.0 );
65 DAI_ASSERT( (BigInt)state < vs.nrStates() );
66 fac.set( state, 1.0 );
67 return fac;
68 }
69
70
71 } // end of namespace dai