1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2006-2010 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
12 #include <dai/factor.h>
21 Factor
createFactorIsing( const Var
&n
, Real h
) {
22 DAI_ASSERT( n
.states() == 2 );
24 buf
[0] = std::exp(-h
);
26 return Factor(n
, &buf
[0]);
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
);
35 buf
[0] = (buf
[3] = std::exp(J
));
36 buf
[1] = (buf
[2] = std::exp(-J
));
37 return Factor( VarSet(n1
, n2
), &buf
[0] );
41 Factor
createFactorExpGauss( const VarSet
&ns
, Real beta
) {
43 for( size_t t
= 0; t
< fac
.nrStates(); t
++ )
44 fac
.set( t
, std::exp(rnd_stdnormal() * beta
) );
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
) );
58 Factor
createFactorDelta( const Var
&v
, size_t state
) {
60 DAI_ASSERT( state
< v
.states() );
61 fac
.set( state
, 1.0 );
66 Factor
createFactorDelta( const VarSet
& vs
, size_t state
) {
67 Factor
fac( vs
, 0.0 );
68 DAI_ASSERT( state
< vs
.nrStates() );
69 fac
.set( state
, 1.0 );
74 } // end of namespace dai